-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edabdea
commit e72b3bd
Showing
11 changed files
with
184 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
app/src/main/java/io/catter2/di/CachedRetrofitCatApiDIModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
28 changes: 28 additions & 0 deletions
28
app/src/main/java/io/catter2/di/SharedPrefFavoritesRepoDIModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |