Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".BaseApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.blink22.android.mvpandroid.activities;
package com.blink22.android.mvpandroid;

import android.content.Intent;
import android.os.Bundle;
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/com/blink22/android/mvpandroid/BaseApp.java
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
Expand Up @@ -29,7 +29,6 @@ public TodosAdapter(Context context, ArrayList<Todo> data, OnItemClickListener l
mContext = context;
}


@Override
public TodosVH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_todos, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.blink22.android.mvpandroid.apiInterfaces;
package com.blink22.android.mvpandroid.apiinterfaces;

import com.blink22.android.mvpandroid.models.Todo;

import java.util.ArrayList;


import io.reactivex.Observable;
import retrofit2.http.Body;
import retrofit2.http.GET;
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/com/blink22/android/mvpandroid/di/AppModule.java
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")

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?

String provideApiLink() {
return BuildConfig.BASE_URL;
}

@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 5 * 1024 * 1024;

Choose a reason for hiding this comment

The 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() {

Choose a reason for hiding this comment

The 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 @Named("cached") as well. Kindly let me know if I missed something.

OkHttpClient okHttpClient = new OkHttpClient();
return okHttpClient;
}

@Provides
@Singleton
@Named("rxjava2_call_adapter")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AFGhazy I believe @Named("rxjava2_call_adapter") is redundant and can be removed. What do you think?

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
Expand Up @@ -12,7 +12,6 @@ public enum NavigationItemEnum {
HOME(R.string.list_todos, android.R.drawable.ic_dialog_info, TodosActivity.class, 0),
ADD_NEW_NOTE(R.string.add_new_todo, android.R.drawable.ic_input_add, NewTodoActivity.class, 1);


private int labelResourceId;
private int iconResourceId;
private Class classToLaunch;
Expand Down Expand Up @@ -41,5 +40,3 @@ public int getId() {
return id;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

public class Todo{


@SerializedName("id")
private int id;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.blink22.android.mvpandroid.network;

import com.blink22.android.mvpandroid.BuildConfig;
import com.blink22.android.mvpandroid.apiInterfaces.TodosService;
import com.blink22.android.mvpandroid.apiinterfaces.TodosService;

import java.io.IOException;

Expand Down Expand Up @@ -39,8 +39,6 @@ public static NetworkManager getInstance() {

public TodosService getTodosService() {
if (mTodosService == null) {


mTodosService = new Retrofit
.Builder()
.baseUrl(BuildConfig.BASE_URL)
Expand All @@ -50,7 +48,6 @@ public TodosService getTodosService() {
.build()
.create(TodosService.class);
}

return mTodosService;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.blink22.android.mvpandroid.network;

import com.blink22.android.mvpandroid.apiInterfaces.TodosService;
import com.blink22.android.mvpandroid.apiinterfaces.TodosService;
import com.blink22.android.mvpandroid.models.Todo;


import java.util.ArrayList;

import io.reactivex.Observable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.support.v4.app.Fragment;

import com.blink22.android.mvpandroid.activities.BaseActivity;
import com.blink22.android.mvpandroid.BaseActivity;

/**
* Created by ahmedghazy on 7/26/18.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {

Expand All @@ -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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AFGhazy this should be handled by DI, add @Inject to the presenter do the necessary changes (add @ provide .. etc) and remove this line.

getLifecycle().addObserver(mPresenter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import android.widget.ProgressBar;
import android.widget.Toast;

import com.blink22.android.mvpandroid.activities.BaseActivity;
import com.blink22.android.mvpandroid.BaseApp;
import com.blink22.android.mvpandroid.BaseActivity;
import com.blink22.android.mvpandroid.adapters.TodosAdapter;
import com.blink22.android.mvpandroid.models.Todo;

Expand All @@ -34,4 +35,9 @@ public class TodosActivity extends BaseActivity{
protected Fragment createFragment() {
return TodosFragment.newInstance();
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import android.widget.ProgressBar;
import android.widget.Toast;

import com.blink22.android.mvpandroid.activities.BaseActivity;
import com.blink22.android.mvpandroid.BaseApp;
import com.blink22.android.mvpandroid.BaseActivity;
import com.blink22.android.mvpandroid.adapters.TodosAdapter;
import com.blink22.android.mvpandroid.models.Todo;

Expand All @@ -24,6 +25,8 @@
import com.blink22.android.mvpandroid.network.NetworkManager;
import com.blink22.android.mvpandroid.network.TodosSubscriber;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

Expand All @@ -32,6 +35,10 @@
*/

public class TodosFragment extends Fragment implements TodosContract.View {
@BindView(R.id.todos_list) RecyclerView mTodos;
@BindView(R.id.todos_progress) ProgressBar mProgressBar;
TodosContract.Presenter mTodosPresenter;
@Inject TodosSubscriber mTodosSubscriber;

public static TodosFragment newInstance() {

Expand All @@ -44,14 +51,13 @@ public static TodosFragment newInstance() {

private static final String TAG = TodosActivity.class.getSimpleName();

@BindView(R.id.todos_list) RecyclerView mTodos;
@BindView(R.id.todos_progress) ProgressBar mProgressBar;
TodosContract.Presenter mTodosPresenter;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setPresenter(TodosPresenter.getInstance(new TodosSubscriber(NetworkManager.getInstance().getTodosService()), this));

((BaseApp) getActivity().getApplication()).getNetworkComponent().inject(this);

setPresenter(TodosPresenter.getInstance(mTodosSubscriber, this));
getLifecycle().addObserver(mTodosPresenter);
}

Expand Down
Loading