Skip to content

Commit

Permalink
DI components and modules
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanHasegawa committed Jul 31, 2017
1 parent edabdea commit e72b3bd
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 39 deletions.
47 changes: 13 additions & 34 deletions app/src/main/java/io/catter2/App.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,25 @@
package io.catter2;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import io.catter2.cat_api.CacheTheCatAPI;
import io.catter2.cat_api.RetrofitTheCatAPI;
import io.catter2.cat_api.TheCatAPI;
import io.catter2.favorites.FavoritesRepository;
import io.catter2.favorites.SharedPrefFavoritesRepository;
import io.catter2.di.AppDIComponent;
import io.catter2.di.AppDIModule;
import io.catter2.di.CachedRetrofitCatApiDIModule;

public class App extends Application {
private static TheCatAPI theCatAPI;
private static FavoritesRepository favoritesRepository;

public static TheCatAPI getTheCatAPIService() {
return App.theCatAPI;
}

public static FavoritesRepository getFavoritesRepository() {
return App.favoritesRepository;
}


@Override
public void onCreate() {
super.onCreate();

TheCatAPI theCatAPIService = new RetrofitTheCatAPI();
TheCatAPI theCatAPICache = new CacheTheCatAPI(theCatAPIService);
App.theCatAPI = theCatAPICache;
}

public void initializeFavoritesRepository(String userToken) {
if (App.favoritesRepository != null) {
throw new RuntimeException("FavoritesRepository already initialized.");
}
App.favoritesRepository = new SharedPrefFavoritesRepository(this, userToken);
}

public void destroyFavoritesRepository() {
if (App.favoritesRepository != null) {
App.favoritesRepository.clearChangeListener();
App.favoritesRepository = null;
}
Log.d("App", "Initialized");
AppDIModule appDIModule = new AppDIModule() {
@Override
public Context provideAppContext() {
return App.this;
}
};
AppDIComponent.initialize(appDIModule, new CachedRetrofitCatApiDIModule());
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/io/catter2/FavoritesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.List;

import io.catter2.di.UserDIComponent;
import io.catter2.favorites.GetFavoritesUseCase;

public class FavoritesActivity extends AppCompatActivity {
Expand Down Expand Up @@ -50,7 +51,7 @@ public void onClick(View view) {
rvAdapter = new ImagesRvAdapter(null);
recyclerView.setAdapter(rvAdapter);

getFavoritesUseCase = new GetFavoritesUseCase(App.getFavoritesRepository());
getFavoritesUseCase = new GetFavoritesUseCase(UserDIComponent.get().getFavoritesRepository());
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/io/catter2/ListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;

import io.catter2.cat_api.FetchCatImagesUseCase;
import io.catter2.di.UserDIComponent;
import io.catter2.favorites.AddFavoriteUseCase;

public class ListActivity extends AppCompatActivity {
Expand Down Expand Up @@ -49,8 +50,8 @@ public void imageClicked(ImageView view, String url) {
});
recyclerView.setAdapter(adapter);

addFavoriteUseCase = new AddFavoriteUseCase(App.getFavoritesRepository());
fetchCatImagesUseCase = new FetchCatImagesUseCase(App.getTheCatAPIService());
addFavoriteUseCase = new AddFavoriteUseCase(UserDIComponent.get().getFavoritesRepository());
fetchCatImagesUseCase = new FetchCatImagesUseCase(UserDIComponent.get().getTheCatAPIService());

fetchCatImagesUseCase.getImagesUrls(new FetchCatImagesUseCase.Callback() {
@Override
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/io/catter2/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
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.login.LoginUseCase;

/**
Expand Down Expand Up @@ -51,7 +54,9 @@ public void onClick(View view) {

@Override
protected void onResume() {
((App) getApplication()).destroyFavoritesRepository();
if (UserDIComponent.get() != null) {
UserDIComponent.get().close();
}
super.onResume();
}

Expand All @@ -64,7 +69,7 @@ private void attemptLogin() {
String token = uc.login(username, password);

if (token != null) {
((App) getApplication()).initializeFavoritesRepository(token);
UserDIComponent.initialize(new SharedPrefFavoritesRepoDIModule(AppDIComponent.get(), token));
FavoritesActivity.launch(this);
} else {
errorTv.setVisibility(View.VISIBLE);
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/io/catter2/di/AppDIComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.catter2.di;

import android.content.Context;

import io.catter2.cat_api.TheCatAPI;

public class AppDIComponent {
private static AppDIComponent instance;

public static AppDIComponent get() {
return AppDIComponent.instance;
}

public static void initialize(AppDIModule appDIModule, TheCatAPIDIModule theCatAPIDIModule) {
if (AppDIComponent.get() != null) {
throw new RuntimeException("AppDIComponent already initialized.");
}
AppDIComponent.instance = new AppDIComponent(appDIModule, theCatAPIDIModule);
}

private AppDIModule appDIModule;
private TheCatAPIDIModule theCatAPIDIModule;

private Context appContext;
private TheCatAPI theCatAPI;

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;
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/io/catter2/di/AppDIModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.catter2.di;

import android.content.Context;

public interface AppDIModule {
Context provideAppContext();
}
20 changes: 20 additions & 0 deletions app/src/main/java/io/catter2/di/CachedRetrofitCatApiDIModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.catter2.di;

import io.catter2.cat_api.CacheTheCatAPI;
import io.catter2.cat_api.RetrofitTheCatAPI;
import io.catter2.cat_api.TheCatAPI;

public class CachedRetrofitCatApiDIModule implements TheCatAPIDIModule {
@Override
public TheCatAPI provideTheCatAPI() {
return provideTheCatAPICached(provideTheCatAPIRetrofit());
}

public TheCatAPI provideTheCatAPIRetrofit() {
return new RetrofitTheCatAPI();
}

public TheCatAPI provideTheCatAPICached(TheCatAPI catAPI) {
return new CacheTheCatAPI(catAPI);
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/io/catter2/di/FavoritesRepoDIModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.catter2.di;

import io.catter2.favorites.FavoritesRepository;

public interface FavoritesRepoDIModule {
AppDIComponent getAppDIComponent();

FavoritesRepository provideFavoritesRepository();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.catter2.di;

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

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

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

@Override
public AppDIComponent getAppDIComponent() {
return this.appDIComponent;
}

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

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

import io.catter2.cat_api.TheCatAPI;

public interface TheCatAPIDIModule {
TheCatAPI provideTheCatAPI();
}
43 changes: 43 additions & 0 deletions app/src/main/java/io/catter2/di/UserDIComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.catter2.di;

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

public class UserDIComponent {
private static UserDIComponent instance;

public static UserDIComponent get() {
return instance;
}

public static void initialize(FavoritesRepoDIModule module) {
if (UserDIComponent.get() != null) {
throw new RuntimeException("UserDIComponent already initialized.");
}
UserDIComponent.instance = new UserDIComponent(module);
}

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 void close() {
if (favoritesRepository != null) {
favoritesRepository.clearChangeListener();
}
}
}

0 comments on commit e72b3bd

Please sign in to comment.