-
Notifications
You must be signed in to change notification settings - Fork 0
Add dagger2 support #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.blink22.android.mvpandroid; | ||
|
|
||
| import android.app.Application; | ||
|
|
||
| import com.blink22.android.mvpandroid.di.AppModule; | ||
| import com.blink22.android.mvpandroid.di.DaggerNetworkComponent; | ||
| import com.blink22.android.mvpandroid.di.NetworkComponent; | ||
| import com.blink22.android.mvpandroid.di.NetworkModule; | ||
|
|
||
| /** | ||
| * Created by ahmedghazy on 7/29/18. | ||
| */ | ||
|
|
||
| public class BaseApp extends Application { | ||
| NetworkComponent mNetworkComponent; | ||
|
|
||
| @Override | ||
| public void onCreate() { | ||
| super.onCreate(); | ||
|
|
||
| mNetworkComponent = DaggerNetworkComponent.builder() | ||
| .appModule(new AppModule(this)) | ||
| .networkModule(new NetworkModule()) | ||
| .build(); | ||
|
|
||
| } | ||
|
|
||
| public NetworkComponent getNetworkComponent() { | ||
| return mNetworkComponent; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.blink22.android.mvpandroid.di; | ||
|
|
||
| import android.app.Application; | ||
|
|
||
| import javax.inject.Singleton; | ||
|
|
||
| import dagger.Module; | ||
| import dagger.Provides; | ||
|
|
||
| /** | ||
| * Created by ahmedghazy on 7/29/18. | ||
| */ | ||
|
|
||
| @Module | ||
| public class AppModule { | ||
| Application mApplication; | ||
|
|
||
| public AppModule(Application application) { | ||
| mApplication = application; | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| public Application providesApplication() { | ||
| return mApplication; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.blink22.android.mvpandroid.di; | ||
|
|
||
| import com.blink22.android.mvpandroid.newTodo.NewTodoActivity; | ||
| import com.blink22.android.mvpandroid.newTodo.NewTodoFragment; | ||
| import com.blink22.android.mvpandroid.todos.TodosActivity; | ||
| import com.blink22.android.mvpandroid.todos.TodosFragment; | ||
|
|
||
| import javax.inject.Singleton; | ||
|
|
||
| import dagger.Component; | ||
|
|
||
| /** | ||
| * Created by ahmedghazy on 7/29/18. | ||
| */ | ||
|
|
||
| @Singleton | ||
| @Component(modules={AppModule.class, NetworkModule.class}) | ||
| public interface NetworkComponent { | ||
| void inject(TodosFragment todosFragment); | ||
| void inject(NewTodoFragment newTodoFragment); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package com.blink22.android.mvpandroid.di; | ||
|
|
||
| import android.app.Application; | ||
|
|
||
| import com.blink22.android.mvpandroid.BuildConfig; | ||
| import com.blink22.android.mvpandroid.apiinterfaces.TodosService; | ||
| import com.blink22.android.mvpandroid.network.TodosSubscriber; | ||
| import com.google.gson.Gson; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| import javax.inject.Named; | ||
| import javax.inject.Singleton; | ||
|
|
||
| import dagger.Module; | ||
| import dagger.Provides; | ||
| import okhttp3.Cache; | ||
| import okhttp3.OkHttpClient; | ||
| import retrofit2.CallAdapter; | ||
| import retrofit2.Retrofit; | ||
| import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; | ||
| import retrofit2.converter.gson.GsonConverterFactory; | ||
|
|
||
| /** | ||
| * Created by ahmedghazy on 7/29/18. | ||
| */ | ||
|
|
||
| @Module | ||
| public class NetworkModule { | ||
| @Provides | ||
| @Named("api_url") | ||
| String provideApiLink() { | ||
| return BuildConfig.BASE_URL; | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| Cache provideOkHttpCache(Application application) { | ||
| int cacheSize = 5 * 1024 * 1024; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AFGhazy extract this to a final constant |
||
| Cache cache = new Cache(application.getCacheDir(), cacheSize); | ||
| return cache; | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| @Named("cached") | ||
| OkHttpClient provideOkHttpClient(Cache cache) { | ||
| OkHttpClient okHttpClient = new OkHttpClient.Builder().cache(cache).build(); | ||
| return okHttpClient; | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| @Named("non_cached") | ||
| OkHttpClient provideOkHttpClientNonCached() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AFGhazy I believe this method is never used. If so we can remove it and remove |
||
| OkHttpClient okHttpClient = new OkHttpClient(); | ||
| return okHttpClient; | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| @Named("rxjava2_call_adapter") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AFGhazy I believe |
||
| CallAdapter.Factory provideCallAdapterFactory() { | ||
| return RxJava2CallAdapterFactory.create(); | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| Retrofit provideRetrofit(@Named("cached") OkHttpClient okHttpClient, | ||
| @Named("rxjava2_call_adapter") CallAdapter.Factory rxJava2CallAdapterFactory, | ||
| @Named("api_url") String url) { | ||
| return new Retrofit.Builder() | ||
| .addConverterFactory(GsonConverterFactory.create()) | ||
| .addCallAdapterFactory(rxJava2CallAdapterFactory) | ||
| .baseUrl(url) | ||
| .client(okHttpClient) | ||
| .build(); | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| TodosService provideTodosService(Retrofit retrofit) { | ||
| return retrofit.create(TodosService.class); | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| TodosSubscriber provideTodosSubscriber(TodosService todosService) { | ||
| return new TodosSubscriber(todosService); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,6 @@ | |
|
|
||
| public class Todo{ | ||
|
|
||
|
|
||
| @SerializedName("id") | ||
| private int id; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,10 +11,13 @@ | |
| import android.widget.Button; | ||
| import android.widget.TextView; | ||
|
|
||
| import com.blink22.android.mvpandroid.BaseApp; | ||
| import com.blink22.android.mvpandroid.R; | ||
| import com.blink22.android.mvpandroid.network.NetworkManager; | ||
| import com.blink22.android.mvpandroid.network.TodosSubscriber; | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import butterknife.BindView; | ||
| import butterknife.ButterKnife; | ||
|
|
||
|
|
@@ -33,6 +36,7 @@ public class NewTodoFragment extends Fragment implements NewTodoContract.View { | |
| @BindView(R.id.new_todo_submit) | ||
| Button mSubmit; | ||
| NewTodoContract.Presenter mPresenter; | ||
| @Inject TodosSubscriber mTodosSubscriber; | ||
|
|
||
| public static NewTodoFragment newInstance() { | ||
|
|
||
|
|
@@ -46,7 +50,10 @@ public static NewTodoFragment newInstance() { | |
| @Override | ||
| public void onCreate(@Nullable Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setPresenter(NewTodoPresenter.getInstance(new TodosSubscriber(NetworkManager.getInstance().getTodosService()), this)); | ||
|
|
||
| ((BaseApp) getActivity().getApplication()).getNetworkComponent().inject(this); | ||
|
|
||
| setPresenter(NewTodoPresenter.getInstance(mTodosSubscriber, this)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AFGhazy this should be handled by DI, add |
||
| getLifecycle().addObserver(mPresenter); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AFGhazy I believe
@Named("api_url")is redundant and can be removed. What do you think?