Created
May 29, 2017 14:42
-
-
Save shahbazahmed1269/8dab6459ec264f764d807367410d4f13 to your computer and use it in GitHub Desktop.
Snippets for medium blog post "Getting started with android architecture components and MVVM"
This file contains 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 ApiResponse { | |
private List<Issue> issues; | |
private Throwable error; | |
public ApiResponse(List<Issue> issues) { | |
this.issues = issues; | |
this.error = null; | |
} | |
public ApiResponse(Throwable error) { | |
this.error = error; | |
this.issues = null; | |
} | |
// Getters... | |
} |
This file contains 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
allprojects { | |
repositories { | |
jcenter() | |
maven { url 'https://maven.google.com' } | |
} | |
} |
This file contains 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
compile 'com.squareup.retrofit2:retrofit:2.1.0' | |
compile 'com.squareup.retrofit2:converter-gson:2.1.0' | |
compile 'android.arch.lifecycle:runtime:1.0.0-alpha1' | |
compile 'android.arch.lifecycle:extensions:1.0.0-alpha1' | |
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-alpha1' |
This file contains 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 interface GithubApiService { | |
@GET("/repos/{owner}/{repo}/issues") | |
Call<List<Issue>> getIssues(@Path("owner") String owner, @Path("repo") String repo); | |
} |
This file contains 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 interface IssueRepository { | |
LiveData<ApiResponse> getIssues(String owner, String repo); | |
} |
This file contains 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 IssueRepositoryImpl implements IssueRepository { | |
public static final String BASE_URL = "https://api.github.com/"; | |
private GithubApiService mApiService; | |
public IssueRepositoryImpl() { | |
Retrofit retrofit = new Retrofit.Builder() | |
.addConverterFactory(GsonConverterFactory.create()) | |
.baseUrl(BASE_URL) | |
.build(); | |
mApiService = retrofit.create(GithubApiService.class); | |
} | |
public LiveData<ApiResponse> getIssues(String owner, String repo) { | |
final MutableLiveData<ApiResponse> liveData = new MutableLiveData<>(); | |
Call<List<Issue>> call = mApiService.getIssues(owner, repo); | |
call.enqueue(new Callback<List<Issue>>() { | |
@Override | |
public void onResponse(Call<List<Issue>> call, Response<List<Issue>> response) { | |
liveData.setValue(new ApiResponse(response.body())); | |
} | |
@Override | |
public void onFailure(Call<List<Issue>> call, Throwable t) { | |
liveData.setValue(new ApiResponse(t)); | |
} | |
}); | |
return liveData; | |
} | |
} |
This file contains 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 ListIssuesViewModel extends ViewModel { | |
private MediatorLiveData<ApiResponse> mApiResponse; | |
private IssueRepository mIssueRepository; | |
// No argument constructor | |
public ListIssuesViewModel() { | |
mApiResponse = new MediatorLiveData<>(); | |
mIssueRepository = new IssueRepositoryImpl(); | |
} | |
@NonNull | |
public LiveData<ApiResponse> getApiResponse() { | |
return mApiResponse; | |
} | |
public LiveData<ApiResponse> loadIssues(@NonNull String user, String repo) { | |
mApiResponse.addSource( | |
mIssueRepository.getIssues(user, repo), | |
apiResponse -> mApiResponse.setValue(apiResponse) | |
); | |
return mApiResponse; | |
} | |
} |
This file contains 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 MainActivity extends LifecycleActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mViewModel = ViewModelProviders.of(this).get(ListIssuesViewModel.class); | |
setupView(); | |
// Handle changes emitted by LiveData | |
mViewModel.getRes().observe(this, apiResponse -> { | |
if (apiResponse.getError() != null) { | |
handleError(apiResponse.getError()); | |
} else { | |
handleResponse(apiResponse.getIssues()); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment