Skip to content

Commit

Permalink
Dagger2! Finally!
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanHasegawa committed Aug 1, 2017
1 parent 58634d7 commit 36967e5
Show file tree
Hide file tree
Showing 21 changed files with 189 additions and 120 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ dependencies {
picasso : '2.5.2',
retrofit : '2.3.0',
leonids : '1.3.1',
dagger2 : '2.11',

junit : '4.12',
mockito : '2.+',

espresso : '2.2.2',
dexmaker : '2.2.0',
]
annotationProcessor "com.google.dagger:dagger-compiler:$versions.dagger2"

compile "com.google.dagger:dagger:$versions.dagger2"
compile "com.android.support:appcompat-v7:$versions.supportLib"
compile "com.android.support:design:$versions.supportLib"
compile "com.squareup.picasso:picasso:$versions.picasso"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -13,6 +14,8 @@
import java.util.List;

import io.catter2.R;
import io.catter2.di.FavoritesRepoDIModule;
import io.catter2.di.UserDIComponent;
import io.catter2.espresso_utils.RecyclerViewItemCountAssertion;
import io.catter2.favorites.FavoritesRepository;
import io.catter2.favorites.GetFavoritesUseCase;
Expand All @@ -26,6 +29,12 @@ public class FavoritesActivityTest {
public ActivityTestRule<FavoritesActivity> activityRule =
new ActivityTestRule<>(FavoritesActivity.class, true, false /* Do not launch activity! */);

@BeforeClass
public static void beforeClass() {
// Warning: The AppDIComponent is still being created by the 'App' class!
UserDIComponent.initialize(mock(FavoritesRepoDIModule.class));
}

@Test
public void testThereAreThreeImages() {
final ArrayList<String> expectedUrls = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -16,6 +17,8 @@
import io.catter2.R;
import io.catter2.cat_api.FetchCatImagesUseCase;
import io.catter2.cat_api.TheCatAPI;
import io.catter2.di.FavoritesRepoDIModule;
import io.catter2.di.UserDIComponent;
import io.catter2.espresso_utils.RecyclerViewItemCountAssertion;
import io.catter2.favorites.AddFavoriteUseCase;
import io.catter2.favorites.FavoritesRepository;
Expand All @@ -34,6 +37,12 @@ public class ListActivityTest {
public ActivityTestRule<ListActivity> activityRule =
new ActivityTestRule<>(ListActivity.class, true, false /* Do not launch activity! */);

@BeforeClass
public static void beforeClass() {
// Warning: The AppDIComponent is still being created by the 'App' class!
UserDIComponent.initialize(mock(FavoritesRepoDIModule.class));
}

@Test
public void testThereAreThreeImages() {
final ArrayList<String> expectedUrls = new ArrayList<>();
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/io/catter2/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.catter2.di.AppDIComponent;
import io.catter2.di.AppDIModule;
import io.catter2.di.CachedRetrofitCatApiDIModule;
import io.catter2.di.RetrofitCatApiDIModule;

public class App extends Application {
@Override
Expand All @@ -21,5 +22,8 @@ public Context provideAppContext() {
}
};
AppDIComponent.initialize(appDIModule, new CachedRetrofitCatApiDIModule());

// Option if you don't want to cache the cat API responses.
// AppDIComponent.initialize(appDIModule, new RetrofitCatApiDIModule());
}
}
3 changes: 1 addition & 2 deletions app/src/main/java/io/catter2/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.widget.EditText;
import android.widget.TextView;

import io.catter2.di.AppDIComponent;
import io.catter2.di.SharedPrefFavoritesRepoDIModule;
import io.catter2.di.UserDIComponent;
import io.catter2.favorites_activity.FavoritesActivity;
Expand Down Expand Up @@ -70,7 +69,7 @@ private void attemptLogin() {
String token = uc.login(username, password);

if (token != null) {
UserDIComponent.initialize(new SharedPrefFavoritesRepoDIModule(AppDIComponent.get(), token));
UserDIComponent.initialize(new SharedPrefFavoritesRepoDIModule(token));
FavoritesActivity.launch(this);
} else {
errorTv.setVisibility(View.VISIBLE);
Expand Down
38 changes: 13 additions & 25 deletions app/src/main/java/io/catter2/di/AppDIComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import android.content.Context;

import javax.inject.Singleton;

import dagger.Component;
import io.catter2.cat_api.TheCatAPI;

public class AppDIComponent {
@Singleton
@Component(modules = {AppDIModule.class, TheCatAPIDIModule.class})
public abstract class AppDIComponent {
private static AppDIComponent instance;

public static AppDIComponent get() {
Expand All @@ -15,31 +20,14 @@ public static void initialize(AppDIModule appDIModule, TheCatAPIDIModule theCatA
if (AppDIComponent.get() != null) {
throw new RuntimeException("AppDIComponent already initialized.");
}
AppDIComponent.instance = new AppDIComponent(appDIModule, theCatAPIDIModule);
AppDIComponent.instance = DaggerAppDIComponent
.builder()
.appDIModule(appDIModule)
.theCatAPIDIModule(theCatAPIDIModule)
.build();
}

private AppDIModule appDIModule;
private TheCatAPIDIModule theCatAPIDIModule;

private Context appContext;
private TheCatAPI theCatAPI;
abstract public Context getAppContext();

public AppDIComponent(AppDIModule appDIModule, TheCatAPIDIModule theCatAPIDIModule) {
this.appDIModule = appDIModule;
this.theCatAPIDIModule = theCatAPIDIModule;
}

public Context getAppContext() {
if (appContext == null) {
appContext = appDIModule.provideAppContext();
}
return appContext;
}

public TheCatAPI getTheCatAPI() {
if (theCatAPI == null) {
theCatAPI = theCatAPIDIModule.provideTheCatAPI();
}
return theCatAPI;
}
abstract public TheCatAPI getTheCatAPI();
}
14 changes: 12 additions & 2 deletions app/src/main/java/io/catter2/di/AppDIModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

import android.content.Context;

public interface AppDIModule {
Context provideAppContext();
import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

@Module
public class AppDIModule {
@Provides
@Singleton
public Context provideAppContext() {
throw new EmptyModuleException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.catter2.cat_api.RetrofitTheCatAPI;
import io.catter2.cat_api.TheCatAPI;

public class CachedRetrofitCatApiDIModule implements TheCatAPIDIModule {
public class CachedRetrofitCatApiDIModule extends TheCatAPIDIModule {
@Override
public TheCatAPI provideTheCatAPI() {
return provideTheCatAPICached(provideTheCatAPIRetrofit());
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/io/catter2/di/EmptyModuleException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.catter2.di;

public class EmptyModuleException extends RuntimeException {
}
23 changes: 20 additions & 3 deletions app/src/main/java/io/catter2/di/FavoritesRepoDIModule.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
package io.catter2.di;

import android.content.Context;

import javax.inject.Named;

import dagger.Module;
import dagger.Provides;
import io.catter2.favorites.FavoritesRepository;

public interface FavoritesRepoDIModule {
AppDIComponent getAppDIComponent();
@Module
public class FavoritesRepoDIModule {
@Provides
@UserScope
public FavoritesRepository provideFavoritesRepository(
Context appContext, @Named("UserToken") String userToken) {
throw new EmptyModuleException();
}

FavoritesRepository provideFavoritesRepository();
@Provides
@Named("UserToken")
@UserScope
public String provideUserToken() {
throw new EmptyModuleException();
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/io/catter2/di/RetrofitCatApiDIModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.catter2.di;

import io.catter2.cat_api.TheCatAPI;

public class RetrofitCatApiDIModule extends CachedRetrofitCatApiDIModule {
@Override
public TheCatAPI provideTheCatAPI() {
return super.provideTheCatAPIRetrofit();
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package io.catter2.di;

import android.content.Context;

import io.catter2.favorites.FavoritesRepository;
import io.catter2.favorites.SharedPrefFavoritesRepository;

public class SharedPrefFavoritesRepoDIModule implements FavoritesRepoDIModule {
private AppDIComponent appDIComponent;
public class SharedPrefFavoritesRepoDIModule extends FavoritesRepoDIModule {
private String userToken;

public SharedPrefFavoritesRepoDIModule(AppDIComponent appDIComponent, String userToken) {
this.appDIComponent = appDIComponent;
public SharedPrefFavoritesRepoDIModule(String userToken) {
this.userToken = userToken;
}

@Override
public AppDIComponent getAppDIComponent() {
return this.appDIComponent;
public FavoritesRepository provideFavoritesRepository(Context appContext, String userToken) {
return new SharedPrefFavoritesRepository(appContext, userToken);
}

@Override
public FavoritesRepository provideFavoritesRepository() {
return new SharedPrefFavoritesRepository(appDIComponent.getAppContext(), provideUserToken());
}

public String provideUserToken() {
return this.userToken;
}
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/io/catter2/di/TheCatAPIDIModule.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package io.catter2.di;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import io.catter2.cat_api.TheCatAPI;

public interface TheCatAPIDIModule {
TheCatAPI provideTheCatAPI();
@Module
public class TheCatAPIDIModule {
@Provides
@Singleton
public TheCatAPI provideTheCatAPI() {
throw new EmptyModuleException();
}
}
32 changes: 12 additions & 20 deletions app/src/main/java/io/catter2/di/UserDIComponent.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.catter2.di;

import io.catter2.cat_api.TheCatAPI;
import dagger.Component;
import io.catter2.favorites.FavoritesRepository;

public class UserDIComponent {
@UserScope
@Component(modules = FavoritesRepoDIModule.class,
dependencies = AppDIComponent.class)
public abstract class UserDIComponent extends AppDIComponent {
private static UserDIComponent instance;

public static UserDIComponent get() {
Expand All @@ -14,30 +17,19 @@ public static void initialize(FavoritesRepoDIModule module) {
if (UserDIComponent.get() != null) {
throw new RuntimeException("UserDIComponent already initialized.");
}
UserDIComponent.instance = new UserDIComponent(module);
UserDIComponent.instance = DaggerUserDIComponent.builder()
.appDIComponent(AppDIComponent.get())
.favoritesRepoDIModule(module)
.build();
}

private FavoritesRepoDIModule module;
private FavoritesRepository favoritesRepository;

private UserDIComponent(FavoritesRepoDIModule module) {
this.module = module;
}

public TheCatAPI getTheCatAPIService() {
return this.module.getAppDIComponent().getTheCatAPI();
}

public FavoritesRepository getFavoritesRepository() {
if (favoritesRepository == null) {
favoritesRepository = module.provideFavoritesRepository();
}
return favoritesRepository;
}
public abstract FavoritesRepository getFavoritesRepository();

public void close() {
final FavoritesRepository favoritesRepository = getFavoritesRepository();
if (favoritesRepository != null) {
favoritesRepository.clearChangeListener();
}
UserDIComponent.instance = null;
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/io/catter2/di/UserScope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.catter2.di;

import javax.inject.Scope;

@Scope
public @interface UserScope {
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

import java.util.List;

import javax.inject.Inject;

import io.catter2.ImagesRvAdapter;
import io.catter2.list_activity.ListActivity;
import io.catter2.R;
import io.catter2.favorites.GetFavoritesUseCase;
import io.catter2.list_activity.ListActivity;

public class FavoritesActivity extends AppCompatActivity {

Expand All @@ -27,15 +29,12 @@ static public void launch(Context context) {
context.startActivity(intent);
}

@Inject
public GetFavoritesUseCase getFavoritesUseCase;

private RecyclerView recyclerView;
private ImagesRvAdapter rvAdapter;

private GetFavoritesUseCase getFavoritesUseCase;

public void injectGetFavoritesUserCase(GetFavoritesUseCase useCase) {
this.getFavoritesUseCase = useCase;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -57,7 +56,7 @@ public void onClick(View view) {
rvAdapter = new ImagesRvAdapter(null);
recyclerView.setAdapter(rvAdapter);

new FavoritesActivityDIComponent().inject(this);
FavoritesActivityDIComponent.initializeAndInject(this);
}

@Override
Expand Down
Loading

0 comments on commit 36967e5

Please sign in to comment.