Skip to content

Instantly share code, notes, and snippets.

View ibrahimsn98's full-sized avatar

İbrahim Süren ibrahimsn98

View GitHub Profile
@ibrahimsn98
ibrahimsn98 / BaseActivity.java
Last active August 19, 2018 15:38
android-mvvm-with-dagger-2
public abstract class BaseActivity extends DaggerAppCompatActivity {
@LayoutRes
protected abstract int layoutRes();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layoutRes());
ButterKnife.bind(this);
@ibrahimsn98
ibrahimsn98 / android-mvvm-with-dagger-2-build.gradle
Last active August 19, 2018 15:39
android-mvvm-with-dagger-2
def supportVersion = '27.1.1'
def retrofitVersion = '2.3.0'
def rxJavaVersion = '2.0.1'
def butterKnifeVersion = '8.8.1'
def daggerVersion = '2.13'
dependencies {
...
implementation "android.arch.lifecycle:extensions:1.1.1"
@ibrahimsn98
ibrahimsn98 / ViewModelFactory.java
Last active August 19, 2018 15:40
android-mvvm-with-dagger-2
@Singleton
public class ViewModelFactory implements ViewModelProvider.Factory {
private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators;
@Inject
public ViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
this.creators = creators;
}
@ibrahimsn98
ibrahimsn98 / ListFragment.java
Last active August 19, 2018 15:40
android-mvvm-with-dagger-2
public class ListFragment extends BaseFragment implements RepoSelectedListener {
@BindView(R.id.recyclerView) RecyclerView listView;
@BindView(R.id.tv_error) TextView errorTextView;
@BindView(R.id.loading_view) View loadingView;
@Inject ViewModelFactory viewModelFactory;
private ListViewModel viewModel;
@Override
@ibrahimsn98
ibrahimsn98 / RepoListAdapter.java
Last active August 19, 2018 15:41
android-mvvm-with-dagger-2
public class RepoListAdapter extends RecyclerView.Adapter<RepoListAdapter.RepoViewHolder>{
private RepoSelectedListener repoSelectedListener;
private final List<Repo> data = new ArrayList<>();
RepoListAdapter(ListViewModel viewModel, LifecycleOwner lifecycleOwner, RepoSelectedListener repoSelectedListener) {
this.repoSelectedListener = repoSelectedListener;
viewModel.getRepos().observe(lifecycleOwner, repos -> {
data.clear();
if (repos != null) {
@ibrahimsn98
ibrahimsn98 / ListViewModel.java
Last active August 19, 2018 15:41
android-mvvm-with-dagger-2
public class ListViewModel extends ViewModel {
private final RepoRepository repoRepository;
private CompositeDisposable disposable;
private final MutableLiveData<List<Repo>> repos = new MutableLiveData<>();
private final MutableLiveData<Boolean> repoLoadError = new MutableLiveData<>();
private final MutableLiveData<Boolean> loading = new MutableLiveData<>();
@Inject
@ibrahimsn98
ibrahimsn98 / RepoService.java
Last active August 19, 2018 15:41
android-mvvm-with-dagger-2
public interface RepoService {
@GET("orgs/Google/repos")
Single<List<Repo>> getRepositories();
@GET("repos/{owner}/{name}")
Single<Repo> getRepo(@Path("owner") String owner, @Path("name") String name);
}
@ibrahimsn98
ibrahimsn98 / ApplicationComponent.java
Last active August 19, 2018 15:42
android-mvvm-with-dagger-2
@Singleton
@Component(modules = {ContextModule.class, ApplicationModule.class, AndroidSupportInjectionModule.class, ActivityBindingModule.class})
public interface ApplicationComponent extends AndroidInjector<DaggerApplication> {
void inject(BaseApplication application);
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
@ibrahimsn98
ibrahimsn98 / ContextModule.java
Created August 19, 2018 15:42
android-mvvm-with-dagger-2
@Module
public abstract class ContextModule {
@Binds
abstract Context provideContext(Application application);
}
@ibrahimsn98
ibrahimsn98 / ApplicationModule.java
Created August 19, 2018 15:43
android-mvvm-with-dagger-2
@Singleton
@Module(includes = ViewModelModule.class)
public class ApplicationModule {
private static final String BASE_URL = "https://api.github.com/";
@Singleton
@Provides
static Retrofit provideRetrofit() {
return new Retrofit.Builder().baseUrl(BASE_URL)