Skip to content

Instantly share code, notes, and snippets.

View shahbazahmed1269's full-sized avatar
:octocat:

Shahbaz Ahmed shahbazahmed1269

:octocat:
View GitHub Profile
@shahbazahmed1269
shahbazahmed1269 / IssueRepositoryImplSample.java
Created June 10, 2017 05:58
Part of my 2nd blog post on architecture components and MVVM
public class IssueRepositoryImpl implements IssueRepository {
@Inject
GithubApiService mApiService;
@Inject
public IssueRepositoryImpl() {
}
// Other code remains unchanged ...
}
@shahbazahmed1269
shahbazahmed1269 / AppComponent.java
Created June 10, 2017 05:55
Part of my 2nd blog post on architecture components and MVVM
@Component(modules = {AppModule.class})
public interface AppComponent {
void inject(MainActivity mainActivity);
}
@shahbazahmed1269
shahbazahmed1269 / AppModule.java
Created June 10, 2017 05:52
Part of my 2nd blog post on architecture components and MVVM
@Module
public class AppModule {
public static final String BASE_URL = "https://api.github.com/";
@Provides
@Singleton
GithubApiService provideGithubApiService() {
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
@shahbazahmed1269
shahbazahmed1269 / ListIssuesViewModelWithDI.java
Created June 10, 2017 05:45
Part of my 2nd blog post on architecture components and MVVM
public class ListIssuesViewModel extends ViewModel {
private IssueRepository mIssueRepository;
// Other member declarations
public ListIssuesViewModel(IssueRepository repository) {
mIssueRepository = repository;
// Other code
}
// Other code
@shahbazahmed1269
shahbazahmed1269 / ListIssuesViewModelnoDI.java
Created June 10, 2017 05:43
Part of my 2nd blog post on architecture components and MVVM
public class ListIssuesViewModel extends ViewModel {
private IssueRepository mIssueRepository;
// Other member declarations
public ListIssuesViewModel() {
mIssueRepository = new IssueRepositoryImpl();
// Other code
}
// Other code
public class ListIssuesViewModel extends ViewModel {
private MediatorLiveData<ApiResponse> mApiResponse;
private IssueRepository mIssueRepository;
// No argument constructor
public ListIssuesViewModel() {
mApiResponse = new MediatorLiveData<>();
mIssueRepository = new IssueRepositoryImpl();
}
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();
public interface IssueRepository {
LiveData<ApiResponse> getIssues(String owner, String repo);
}
public interface GithubApiService {
@GET("/repos/{owner}/{repo}/issues")
Call<List<Issue>> getIssues(@Path("owner") String owner, @Path("repo") String repo);
}
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
}
}