Last active
January 17, 2020 09:59
-
-
Save JaldeepAsodariya/6bc706d8f4f08441e8065940619a16e6 to your computer and use it in GitHub Desktop.
MVVM implementation (JAVA)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
implementation 'androidx.appcompat:appcompat:1.1.0' | |
implementation 'androidx.core:core-ktx:1.1.0' | |
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | |
implementation 'androidx.legacy:legacy-support-v4:1.0.0' | |
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0' | |
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListFragment extends Fragment { | |
private SampleViewModel mViewModel; | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, | |
@Nullable Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.fragment_sample_list, container, false); | |
} | |
@Override | |
public void onActivityCreated(@Nullable Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
mViewModel = ViewModelProviders.of(this).get(SampleViewModel.class); | |
mViewModel.mResponseLiveData.observe(getViewLifecycleOwner(), new Observer<ModelResponse>() { | |
@Override | |
public void onChanged(ModelResponse response) { | |
if (response != null) { | |
progressbar.setVisibility(View.GONE); | |
// Update UI based on response | |
} | |
} | |
}); | |
fetchSampleList(); | |
} | |
private void fetchSampleList() { | |
progressbar.setVisibility(View.VISIBLE); | |
mViewModel.fetchData(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.lifecycle.MutableLiveData; | |
import androidx.lifecycle.ViewModel; | |
public class ListViewModel extends ViewModel { | |
public MutableLiveData<ModelResponse> mResponseLiveData = new MutableLiveData<>(); | |
public void fetchData() { | |
APISample objApiSample = APIRetroBuilder.getRetroBuilder(this, false).create(APISample.class); | |
Call<ModelResponse> callModelSampleRes = objApiSample.callSampleListApi(); | |
callModelSampleRes.enqueue(new Callback<ModelResponse>() { | |
@Override | |
public void onResponse(Call<ModelResponse> call, Response<ModelResponse> response) { | |
if (response.isSuccessful()) { | |
if (response.body() != null) { | |
mContentLiveData.setValue(response.body()); | |
} | |
} | |
} | |
@Override | |
public void onFailure(Call<ModelResponse> call, Throwable t) { | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment