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).
  • Loading branch information
prashanDYDX committed Mar 14, 2024
1 parent 6a07df2 commit e87f1cd
Show file tree
Hide file tree
Showing 26 changed files with 241 additions and 292 deletions.
316 changes: 106 additions & 210 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,6 +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.EnvKey
import exchange.dydx.dydxstatemanager.clientState.DydxClientState
import exchange.dydx.dydxstatemanager.clientState.favorite.DydxFavoriteStore
import exchange.dydx.dydxstatemanager.clientState.favorite.DydxFavoriteStoreProtocol
Expand All @@ -36,6 +39,7 @@ 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
Expand Down Expand Up @@ -64,235 +68,127 @@ 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
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package exchange.dydx.trading.common

import android.app.Application
import android.content.Context
import android.util.Log
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -35,7 +36,7 @@ interface AppConfig {
}

data class AppConfigImpl(
override val appContext: Context?,
override val appContext: Application?,
override val appVersionName: String,
override val appVersionCode: String,
override val debug: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package exchange.dydx.trading.common.featureflags

import exchange.dydx.utilities.utils.SharedPreferencesStore
import javax.inject.Inject

enum class DydxFeatureFlag {
deployment_url,
force_mainnet,
}

class DydxFeatureFlags(
class DydxFeatureFlags @Inject constructor(
private val sharedPreferences: SharedPreferencesStore
) {
fun isFeatureEnabled(featureFlag: DydxFeatureFlag): Boolean {
Expand Down
Loading

0 comments on commit e87f1cd

Please sign in to comment.