Skip to content

Commit

Permalink
Update AppModule.kt and used dependencies to follow Dagger Best Pract…
Browse files Browse the repository at this point in the history
…ices guide (in Notion). (#27)
  • Loading branch information
prashanDYDX authored Mar 14, 2024
1 parent cf3d37a commit a6adc3b
Show file tree
Hide file tree
Showing 26 changed files with 251 additions and 307 deletions.
324 changes: 109 additions & 215 deletions v4/app/src/main/java/exchange/dydx/trading/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package exchange.dydx.trading

import android.app.Application
import android.content.Context
import androidx.compose.material.SnackbarHostState
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -21,7 +23,7 @@ import exchange.dydx.abacus.utils.IOImplementations
import exchange.dydx.abacus.utils.Parser
import exchange.dydx.dydxstatemanager.AbacusStateManager
import exchange.dydx.dydxstatemanager.AbacusStateManagerProtocol
import exchange.dydx.dydxstatemanager.clientState.DydxClientState
import exchange.dydx.dydxstatemanager.EnvKey
import exchange.dydx.dydxstatemanager.clientState.favorite.DydxFavoriteStore
import exchange.dydx.dydxstatemanager.clientState.favorite.DydxFavoriteStoreProtocol
import exchange.dydx.dydxstatemanager.clientState.transfers.DydxTransferStateManager
Expand All @@ -36,15 +38,14 @@ import exchange.dydx.dydxstatemanager.protocolImplementations.AbacusThreadingImp
import exchange.dydx.dydxstatemanager.protocolImplementations.AbacusTimerImp
import exchange.dydx.dydxstatemanager.protocolImplementations.AbacusTrackingImp
import exchange.dydx.dydxstatemanager.protocolImplementations.AbacusWebSocketImp
import exchange.dydx.dydxstatemanager.protocolImplementations.LanguageKey
import exchange.dydx.platformui.components.PlatformDialog
import exchange.dydx.platformui.components.PlatformInfo
import exchange.dydx.platformui.designSystem.theme.StyleConfig
import exchange.dydx.platformui.designSystem.theme.ThemeConfig
import exchange.dydx.platformui.designSystem.theme.ThemeSettings
import exchange.dydx.trading.common.AppConfig
import exchange.dydx.trading.common.AppConfigImpl
import exchange.dydx.trading.common.featureflags.DydxFeatureFlags
import exchange.dydx.trading.common.logger.DydxLogger
import exchange.dydx.trading.common.theme.DydxTheme
import exchange.dydx.trading.common.theme.DydxThemeImpl
import exchange.dydx.trading.feature.shared.PreferenceKeys
Expand All @@ -54,245 +55,138 @@ import exchange.dydx.trading.integration.analytics.Tracking
import exchange.dydx.trading.integration.cosmos.CosmosV4ClientProtocol
import exchange.dydx.trading.integration.cosmos.CosmosV4ClientWebview
import exchange.dydx.trading.integration.cosmos.CosmosV4WebviewClientProtocol
import exchange.dydx.utilities.utils.CachedFileLoader
import exchange.dydx.utilities.utils.JsonUtils
import exchange.dydx.utilities.utils.SecureStore
import exchange.dydx.utilities.utils.SharedPreferencesStore
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.serialization.json.Json
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun provideThemeSettings(
@ApplicationContext appContext: Context,
sharedPreferencesStore: SharedPreferencesStore,
): ThemeSettings {
val preferenceStore = provideSharedPreferencesStore(appContext)
var theme = preferenceStore.read(PreferenceKeys.Theme)
if (theme.isNullOrEmpty()) {
theme = "dark"
interface AppModule {

companion object {
@Provides
@Singleton
fun provideThemeSettings(
@ApplicationContext appContext: Context,
preferenceStore: SharedPreferencesStore,
): ThemeSettings {
var theme = preferenceStore.read(PreferenceKeys.Theme)
if (theme.isNullOrEmpty()) {
theme = "dark"
}
val themeConfigValue =
ThemeConfig.createFromPreference(appContext, theme) ?: ThemeConfig.dark(appContext)
val themeConfig = MutableStateFlow<ThemeConfig?>(themeConfigValue)
val styleConfig =
MutableStateFlow<StyleConfig?>(JsonUtils.loadFromAssets(appContext, "dydxStyle.json"))
ThemeSettings.shared =
ThemeSettings(appContext, preferenceStore, themeConfig, styleConfig)
return ThemeSettings.shared
}
val themeConfigValue =
ThemeConfig.createFromPreference(appContext, theme) ?: ThemeConfig.dark(appContext)
val themeConfig = MutableStateFlow<ThemeConfig?>(themeConfigValue)
val styleConfig =
MutableStateFlow<StyleConfig?>(JsonUtils.loadFromAssets(appContext, "dydxStyle.json"))
ThemeSettings.shared =
ThemeSettings(appContext, sharedPreferencesStore, themeConfig, styleConfig)
return ThemeSettings.shared
}

@Provides
@Singleton
fun provideParser(): ParserProtocol =
Parser()

@Provides
@Singleton
fun provideSharedPreferencesStore(@ApplicationContext appContext: Context): SharedPreferencesStore =
SharedPreferencesStore(appContext)

@Provides
@Singleton
fun provideSecureStore(@ApplicationContext appContext: Context): SecureStore =
SecureStore(appContext)

@Provides
@Singleton
fun provideClientState(
sharedPreferencesStore: SharedPreferencesStore,
secureStore: SecureStore,
): DydxClientState =
DydxClientState(sharedPreferencesStore, secureStore)

@Provides
@Singleton
fun provideCosmosClient(client: CosmosV4WebviewClientProtocol): CosmosV4ClientProtocol =
client

@Provides
@Singleton
fun provideCosmosWebviewClient(@ApplicationContext appContext: Context): CosmosV4WebviewClientProtocol =
CosmosV4ClientWebview(appContext)

@Provides
@Singleton
fun provideRest(): RestProtocol =
AbacusRestImp()

@Provides
@Singleton
fun provideWebsocket(): WebSocketProtocol =
AbacusWebSocketImp()

@Provides
@Singleton
fun provideChain(cosmosClient: CosmosV4ClientProtocol): DYDXChainTransactionsProtocol =
AbacusChainImp(cosmosClient)

@Provides
@Singleton
fun provideTrackingProtocol(
tracker: Tracking,
): TrackingProtocol =
tracker

@Provides
@Singleton
fun provideTracking(
tracker: CompositeTracking,
): Tracking =
AbacusTrackingImp(tracker)
@Provides @Singleton
fun provideParser(): ParserProtocol = Parser()

@Provides
@Singleton
fun provideIOImplementation(
rest: RestProtocol?,
webSocket: WebSocketProtocol?,
chain: DYDXChainTransactionsProtocol?,
tracking: TrackingProtocol?,
threading: ThreadingProtocol?,
timer: TimerProtocol?,
fileSystem: FileSystemProtocol?,
): IOImplementations =
IOImplementations(rest, webSocket, chain, tracking, threading, timer, fileSystem)

@EnvKey @Provides
fun provideEnvKey(): String = PreferenceKeys.Env

@LanguageKey @Provides
fun provideLanguageKey(): String = PreferenceKeys.Language

@Provides
fun providePlatformInfo(): PlatformInfo =
PlatformInfo(
snackbarHostState = SnackbarHostState(),
infoType = MutableStateFlow(PlatformInfo.InfoType.Info),
)

@Provides
fun providePlatformDialog(
localizer: LocalizerProtocol,
): PlatformDialog =
PlatformDialog(
cancelTitle = localizer.localize("APP.GENERAL.CANCEL"),
confirmTitle = localizer.localize("APP.GENERAL.OK"),
)

@Provides
fun provideAppConfig(
application: Application,
): AppConfig = AppConfigImpl(
appContext = application,
appVersionName = BuildConfig.VERSION_NAME,
appVersionCode = BuildConfig.VERSION_CODE.toString(),
debug = BuildConfig.DEBUG,
activityClass = TradingActivity::class.java,
)

@Provides
@Singleton
fun provideCompositeTracker(): CompositeTracking =
CompositeTracker()
@Provides
@Singleton
fun provideJson(appConfig: AppConfig): Json = Json {
this.prettyPrint = appConfig.debug
this.ignoreUnknownKeys = true // !appConfig.debug
}

@Provides
@Singleton
fun provideThreading(): ThreadingProtocol =
AbacusThreadingImp()
@Provides
fun provideTheme(
application: Application,
appConfig: AppConfig,
): DydxTheme {
return DydxThemeImpl(application, appConfig, true)
}
}

@Provides
@Singleton
fun provideTimer(): TimerProtocol =
AbacusTimerImp()
@Binds fun bindCosmosClient(client: CosmosV4WebviewClientProtocol): CosmosV4ClientProtocol

@Provides
@Singleton
fun provideFileSystem(@ApplicationContext appContext: Context): FileSystemProtocol =
AbacusFileSystemImp(appContext)
@Binds fun bindRest(abacusRestImp: AbacusRestImp): RestProtocol

@Provides
@Singleton
fun provideIOImplementation(
rest: RestProtocol?,
webSocket: WebSocketProtocol?,
chain: DYDXChainTransactionsProtocol?,
tracking: TrackingProtocol?,
threading: ThreadingProtocol?,
timer: TimerProtocol?,
fileSystem: FileSystemProtocol?,
): IOImplementations =
IOImplementations(rest, webSocket, chain, tracking, threading, timer, fileSystem)
@Binds fun bindWebsocket(abacusWebSocketImp: AbacusWebSocketImp): WebSocketProtocol

@Provides
@Singleton
fun provideAbacusStateManager(
@ApplicationContext appContext: Context,
ioImplementations: IOImplementations,
parser: ParserProtocol,
walletStateManager: DydxWalletStateManagerProtocol,
transferStateManager: DydxTransferStateManagerProtocol,
cosmosClient: CosmosV4ClientProtocol,
preferencesStore: SharedPreferencesStore,
featureFlags: DydxFeatureFlags,
): AbacusStateManagerProtocol =
AbacusStateManager(
appContext,
ioImplementations,
parser,
walletStateManager,
transferStateManager,
cosmosClient,
preferencesStore,
PreferenceKeys.Env,
featureFlags,
)
@Binds fun bindChainProtocol(abacusChainImp: AbacusChainImp): DYDXChainTransactionsProtocol

@Provides
@Singleton
fun provideFeatureFlags(preferencesStore: SharedPreferencesStore): DydxFeatureFlags =
DydxFeatureFlags(preferencesStore)
@Binds fun bindTrackingProtocol(abacusTrackingImp: AbacusTrackingImp): TrackingProtocol

@Provides
@Singleton
fun provideLocalizer(
abacusLocalizerProtocol: AbacusLocalizerProtocol,
): LocalizerProtocol =
abacusLocalizerProtocol
@Binds fun bindTracking(abacusTrackingImp: AbacusTrackingImp): Tracking

@Provides
@Singleton
fun provideMutableLocalizer(
preferencesStore: SharedPreferencesStore,
ioImplementations: IOImplementations,
): AbacusLocalizerProtocol =
AbacusLocalizerImp(preferencesStore, PreferenceKeys.Language, ioImplementations)
@Binds fun bindThreading(abacusThreadingImp: AbacusThreadingImp): ThreadingProtocol

@Provides
@Singleton
fun provideWalletStateManager(clientState: DydxClientState): DydxWalletStateManagerProtocol =
DydxWalletStateManager(clientState)
@Binds fun bindTimer(abacusTimerImp: AbacusTimerImp): TimerProtocol

@Provides
@Singleton
fun provideTransferStateManager(clientState: DydxClientState): DydxTransferStateManagerProtocol =
DydxTransferStateManager(clientState)
@Binds fun bindFileSystem(abacusFileSystemImp: AbacusFileSystemImp): FileSystemProtocol

@Provides
@Singleton
fun provideUserFavoriteStore(clientState: DydxClientState): DydxFavoriteStoreProtocol =
DydxFavoriteStore(clientState)
@Binds fun bindAbacusStateManager(abacusStateManager: AbacusStateManager): AbacusStateManagerProtocol

@Provides
fun providePlatformInfo(): PlatformInfo =
PlatformInfo(
snackbarHostState = SnackbarHostState(),
infoType = MutableStateFlow(PlatformInfo.InfoType.Info),
)
@Binds fun bindLocalizer(abacusLocalizerProtocol: AbacusLocalizerProtocol): LocalizerProtocol

@Provides
fun providePlatformDialog(
localizer: LocalizerProtocol,
): PlatformDialog =
PlatformDialog(
cancelTitle = localizer.localize("APP.GENERAL.CANCEL"),
confirmTitle = localizer.localize("APP.GENERAL.OK"),
)
@Binds
fun bindLocalizerProtocol(abacusLocalizerImp: AbacusLocalizerImp): AbacusLocalizerProtocol

@Provides
@Singleton
fun provideCachedFileLoader(
@ApplicationContext appContext: Context,
): CachedFileLoader {
return CachedFileLoader(appContext)
}
@Binds
fun bindWalletStateManagerProtocol(walletStateManager: DydxWalletStateManager): DydxWalletStateManagerProtocol

@Provides
@Singleton
fun provideAppConfig(
@ApplicationContext appContext: Context,
): AppConfig = AppConfigImpl(
appContext = appContext,
appVersionName = BuildConfig.VERSION_NAME,
appVersionCode = BuildConfig.VERSION_CODE.toString(),
debug = BuildConfig.DEBUG,
activityClass = TradingActivity::class.java,
)
@Binds
fun bindTransferStateManagerProtocol(dydxTransferStateManager: DydxTransferStateManager): DydxTransferStateManagerProtocol

@Provides
@Singleton
fun provideLogger(): DydxLogger = DydxLogger()
@Binds
fun bindUserFavoriteStoreProtocol(dydxFavoriteStore: DydxFavoriteStore): DydxFavoriteStoreProtocol

@Provides
@Singleton
fun provideJson(appConfig: AppConfig): Json = Json {
this.prettyPrint = appConfig.debug
this.ignoreUnknownKeys = true // !appConfig.debug
}
@Binds
fun bindCompositeTracking(compositeTracker: CompositeTracker): CompositeTracking

@Provides
@Singleton
fun provideTheme(
@ApplicationContext appContext: Context,
appConfig: AppConfig,
): DydxTheme {
return DydxThemeImpl(appContext, appConfig, true)
}
@Binds fun bindCosmosV4WebviewClientProtocol(cosmosV4ClientWebview: CosmosV4ClientWebview): CosmosV4WebviewClientProtocol
}
Loading

0 comments on commit a6adc3b

Please sign in to comment.