-
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
37a79e0
commit 0b9804a
Showing
16 changed files
with
131 additions
and
226 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
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
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
11 changes: 0 additions & 11 deletions
11
app/src/main/java/io/catter2/di/CachedRetrofitCatApiDIModule.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
app/src/main/java/io/catter2/di/SharedPrefFavoritesRepoDIModule.kt
This file was deleted.
Oops, something went wrong.
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,52 +1,37 @@ | ||
package io.catter2.di | ||
|
||
import android.content.Context | ||
import dagger.Component | ||
import dagger.Module | ||
import dagger.Provides | ||
import com.github.salomonbrys.kodein.Kodein | ||
import com.github.salomonbrys.kodein.bind | ||
import com.github.salomonbrys.kodein.singleton | ||
import io.catter2.App | ||
import io.catter2.cat_api.CacheTheCatAPI | ||
import io.catter2.cat_api.RetrofitTheCatAPI | ||
import io.catter2.cat_api.TheCatAPI | ||
import javax.inject.Singleton | ||
|
||
|
||
@Module | ||
open class AppDIModule { | ||
@Provides | ||
@Singleton | ||
open fun provideAppContext(): Context { | ||
throw EmptyModuleException() | ||
} | ||
} | ||
|
||
|
||
@Module | ||
open class TheCatAPIDIModule { | ||
@Provides | ||
@Singleton | ||
open fun provideTheCatAPI(): TheCatAPI { | ||
throw EmptyModuleException() | ||
} | ||
} | ||
|
||
@Singleton | ||
@Component(modules = arrayOf(AppDIModule::class, TheCatAPIDIModule::class)) | ||
abstract class AppDIComponent { | ||
companion object { | ||
var instance: AppDIComponent? = null | ||
private set | ||
|
||
fun initialize(appDIModule: AppDIModule, theCatAPIDIModule: TheCatAPIDIModule) { | ||
if (AppDIComponent.instance != null) { | ||
throw RuntimeException("AppDIComponent already initialized.") | ||
import java.security.InvalidParameterException | ||
|
||
object AppKodein { | ||
lateinit var kodein: Kodein | ||
private set | ||
|
||
fun initialize(appContext: App, useCachedCatApi: Boolean) { | ||
try { | ||
kodein | ||
throw InvalidParameterException("AppKodein already initialized") | ||
} catch (e: UninitializedPropertyAccessException) { | ||
kodein = Kodein { | ||
bind<App>() with singleton { appContext } | ||
bind<Context>() with singleton { appContext } | ||
bind<TheCatAPI>() with singleton { | ||
val service = RetrofitTheCatAPI() | ||
when (useCachedCatApi) { | ||
true -> CacheTheCatAPI(service) | ||
else -> service | ||
} | ||
} | ||
} | ||
AppDIComponent.instance = DaggerAppDIComponent | ||
.builder() | ||
.appDIModule(appDIModule) | ||
.theCatAPIDIModule(theCatAPIDIModule) | ||
.build() | ||
} | ||
} | ||
|
||
abstract fun getAppContext(): Context | ||
|
||
abstract fun getTheCatAPI(): 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 |
---|---|---|
@@ -1,59 +1,36 @@ | ||
package io.catter2.di | ||
|
||
import android.content.Context | ||
import dagger.Component | ||
import dagger.Module | ||
import dagger.Provides | ||
import android.util.Log | ||
import com.github.salomonbrys.kodein.Kodein | ||
import com.github.salomonbrys.kodein.bind | ||
import com.github.salomonbrys.kodein.instance | ||
import com.github.salomonbrys.kodein.instanceOrNull | ||
import com.github.salomonbrys.kodein.provider | ||
import com.github.salomonbrys.kodein.singleton | ||
import com.github.salomonbrys.kodein.with | ||
import io.catter2.favorites.FavoritesRepository | ||
import javax.inject.Named | ||
import javax.inject.Scope | ||
import io.catter2.favorites.SharedPrefFavoritesRepository | ||
|
||
|
||
@Scope | ||
annotation class UserScope | ||
object UserKodein { | ||
var kodein: Kodein? = null | ||
private set | ||
|
||
|
||
@Module | ||
open class FavoritesRepoDIModule { | ||
@Provides | ||
@UserScope | ||
open fun provideFavoritesRepository( | ||
appContext: Context, @Named("UserToken") userToken: String): FavoritesRepository { | ||
throw EmptyModuleException() | ||
} | ||
|
||
@Provides | ||
@Named("UserToken") | ||
@UserScope | ||
open fun provideUserToken(): String { | ||
throw EmptyModuleException() | ||
} | ||
} | ||
|
||
|
||
@UserScope | ||
@Component(modules = arrayOf(FavoritesRepoDIModule::class), | ||
dependencies = arrayOf(AppDIComponent::class)) | ||
abstract class UserDIComponent : AppDIComponent() { | ||
companion object { | ||
var instance: UserDIComponent? = null | ||
private set | ||
|
||
fun initialize(module: FavoritesRepoDIModule) { | ||
if (UserDIComponent.instance != null) { | ||
throw RuntimeException("UserDIComponent already initialized.") | ||
fun initialize(userToken: String) { | ||
kodein?.run { throw RuntimeException("UserKodein already initialized.") } | ||
kodein = Kodein { | ||
extend(AppKodein.kodein) | ||
constant("UserToken") with userToken | ||
bind<FavoritesRepository>() with singleton { | ||
Log.i("USER", "Creating FavoritesRepository") | ||
SharedPrefFavoritesRepository(instance(), instance("UserToken")) | ||
} | ||
UserDIComponent.instance = DaggerUserDIComponent.builder() | ||
.appDIComponent(AppDIComponent.instance) | ||
.favoritesRepoDIModule(module) | ||
.build() | ||
} | ||
} | ||
|
||
abstract fun getFavoritesRepository(): FavoritesRepository? | ||
|
||
fun close() { | ||
getFavoritesRepository()?.clearChangeListener() | ||
UserDIComponent.instance = null | ||
fun clear() { | ||
kodein?.instanceOrNull<FavoritesRepository>()?.clearChangeListener() | ||
kodein = null | ||
} | ||
} | ||
|
Oops, something went wrong.