Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rest of Dagger modules to follow best practices. #30

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package exchange.dydx.trading.common.navigation

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.navigation.NavController
Expand All @@ -17,8 +16,6 @@ import kotlinx.coroutines.flow.StateFlow
*/
interface DydxRouter {

var androidContext: Context?

enum class Presentation {
Push,
Modal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class TradingActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

viewModel.router.androidContext = this

Timber.tag(TAG).i("TradingActivity#onCreate")

CarteraSetup.run(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package exchange.dydx.trading.core

import android.content.Context
import android.app.Application
import android.content.Intent
import android.os.Bundle
import androidx.core.net.toUri
Expand All @@ -10,13 +10,15 @@ import androidx.navigation.NavDestination
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.navDeepLink
import dagger.hilt.android.scopes.ActivityRetainedScoped
import exchange.dydx.trading.common.AppConfig
import exchange.dydx.trading.common.navigation.DydxRouter
import exchange.dydx.trading.common.navigation.DydxRouter.Destination
import exchange.dydx.trading.common.navigation.MarketRoutes
import exchange.dydx.trading.integration.analytics.Tracking
import kotlinx.coroutines.flow.MutableStateFlow
import timber.log.Timber
import javax.inject.Inject

private const val TAG = "DydxRouterImpl"

Expand All @@ -27,10 +29,11 @@ private const val TAG = "DydxRouterImpl"
*
* So this is a very powerful class in terms of, bridging the dependency tree.
*/
class DydxRouterImpl(
override var androidContext: Context?,
private val appConfig: AppConfig,
@ActivityRetainedScoped
class DydxRouterImpl @Inject constructor(
private val application: Application,
private val tracker: Tracking,
appConfig: AppConfig,
) : DydxRouter {

private lateinit var navHostController: NavHostController
Expand Down Expand Up @@ -118,7 +121,7 @@ class DydxRouterImpl(
val routePath = routePath(route)
if (routePath.startsWith("http://") || routePath.startsWith("https://")) {
val intent = Intent(Intent.ACTION_VIEW, routePath.toUri())
androidContext?.startActivity(intent)
application.startActivity(intent)
} else {
pendingPresentation =
presentation // Let destinationChangedListener handle the presentation state change
Expand Down
24 changes: 4 additions & 20 deletions v4/core/src/main/java/exchange/dydx/trading/core/di/CoreModule.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
package exchange.dydx.trading.core.di

import android.content.Context
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityRetainedComponent
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.scopes.ActivityRetainedScoped
import exchange.dydx.trading.common.AppConfig
import exchange.dydx.trading.common.navigation.DydxRouter
import exchange.dydx.trading.core.DydxRouterImpl
import exchange.dydx.trading.integration.analytics.Tracking

@Module
@InstallIn(ActivityRetainedComponent::class)
object CoreModule {
interface CoreModule {

@Provides
@ActivityRetainedScoped
fun provideApplicationRouter(
@ApplicationContext androidContext: Context,
appConfig: AppConfig,
tracker: Tracking,
): DydxRouter {
return DydxRouterImpl(
androidContext = androidContext,
appConfig = appConfig,
tracker = tracker,
)
}
@Binds
fun bindDydxRouter(dydxRouterImpl: DydxRouterImpl): DydxRouter
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package exchange.dydx.trading.feature.market.di

import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityRetainedComponent
import dagger.hilt.android.scopes.ActivityRetainedScoped
import exchange.dydx.abacus.protocols.LocalizerProtocol
import exchange.dydx.dydxstatemanager.AbacusStateManagerProtocol
import exchange.dydx.dydxstatemanager.clientState.favorite.DydxFavoriteStoreProtocol
import exchange.dydx.trading.common.formatter.DydxFormatter
import exchange.dydx.trading.feature.market.marketinfo.components.tabs.DydxMarketAccountTabView
import exchange.dydx.trading.feature.market.marketinfo.components.tabs.DydxMarketStatsTabView
import exchange.dydx.trading.feature.market.marketinfo.components.tiles.DydxMarketTilesView
Expand All @@ -20,106 +17,79 @@ import exchange.dydx.trading.feature.market.marketlist.components.SortAction
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow

// @InstallIn(ViewModelComponent::class)
@Module
@InstallIn(ActivityRetainedComponent::class)
object MarketListModule {
interface MarketListModule {

@Provides
@ActivityRetainedScoped
fun provideFilterActionFlow(
@Binds fun bindFilterActionFlow(
mutableFlow: MutableStateFlow<FilterAction?>,
): Flow<FilterAction?> {
return mutableFlow
}

@Provides
@ActivityRetainedScoped
fun provideMutableFilterActionFlow(): MutableStateFlow<FilterAction?> {
return MutableStateFlow(null)
}
): Flow<FilterAction?>

@Provides
@ActivityRetainedScoped
fun provideSortActionFlow(
@Binds fun bindSortActionFlow(
mutableFlow: MutableStateFlow<SortAction?>,
): Flow<SortAction?> {
return mutableFlow
}
): Flow<SortAction?>

companion object {
@Provides
@ActivityRetainedScoped
fun provideMutableFilterActionFlow(): MutableStateFlow<FilterAction?> {
return MutableStateFlow(null)
}

@Provides
@ActivityRetainedScoped
fun provideMutableSortActionFlow(): MutableStateFlow<SortAction?> {
return MutableStateFlow(null)
@Provides
@ActivityRetainedScoped
fun provideMutableSortActionFlow(): MutableStateFlow<SortAction?> {
return MutableStateFlow(null)
}
}
}

@Module
@InstallIn(ActivityRetainedComponent::class)
object MarketIfoModule {
@Provides
@ActivityRetainedScoped
fun provideMutableMarketInfoStream(
abacusStateManager: AbacusStateManagerProtocol,
formatter: DydxFormatter,
localizer: LocalizerProtocol,
favoriteStore: DydxFavoriteStoreProtocol,
): MutableMarketInfoStreaming {
return MarketInfoStream(
abacusStateManager = abacusStateManager,
formatter = formatter,
localizer = localizer,
favoriteStore = favoriteStore,
)
}
interface MarketInfoModule {

@Binds
fun bindMutableMarketInfoStreaming(
marketInfoStream: MarketInfoStream,
): MutableMarketInfoStreaming

@Provides
@ActivityRetainedScoped
fun provideMarketInfoStream(
@Binds
fun bindMarketInfoStreaming(
mutableMarketInfoStream: MutableMarketInfoStreaming,
): MarketInfoStreaming {
return mutableMarketInfoStream
}
): MarketInfoStreaming

@Provides
@ActivityRetainedScoped
fun provideStatsTabSelection(
@Binds
fun bindStatsTabSelection(
mutableStatsTabFlow: MutableStateFlow<DydxMarketStatsTabView.Selection>,
): Flow<DydxMarketStatsTabView.Selection> {
return mutableStatsTabFlow
}
): Flow<DydxMarketStatsTabView.Selection>

@Provides
@ActivityRetainedScoped
fun provideMutableStatsTabSelection(): MutableStateFlow<DydxMarketStatsTabView.Selection> {
return MutableStateFlow(DydxMarketStatsTabView.Selection.Statistics)
}

@Provides
@ActivityRetainedScoped
fun provideAccountTabSelection(
@Binds
fun bindAccountTabSelection(
mutableAccountTabFlow: MutableStateFlow<DydxMarketAccountTabView.Selection>,
): Flow<DydxMarketAccountTabView.Selection> {
return mutableAccountTabFlow
}

@Provides
@ActivityRetainedScoped
fun provideMutableAccountTabSelection(): MutableStateFlow<DydxMarketAccountTabView.Selection> {
return MutableStateFlow(DydxMarketAccountTabView.Selection.Position)
}
): Flow<DydxMarketAccountTabView.Selection>

@Provides
@ActivityRetainedScoped
fun provideTileSelection(
@Binds
fun bindTileSelection(
mutableTileFlow: MutableStateFlow<DydxMarketTilesView.Tile>,
): Flow<DydxMarketTilesView.Tile> {
return mutableTileFlow
}
): Flow<DydxMarketTilesView.Tile>

companion object {
@Provides
@ActivityRetainedScoped
fun provideMutableStatsTabSelection(): MutableStateFlow<DydxMarketStatsTabView.Selection> {
return MutableStateFlow(DydxMarketStatsTabView.Selection.Statistics)
}

@Provides
@ActivityRetainedScoped
fun provideMutableAccountTabSelection(): MutableStateFlow<DydxMarketAccountTabView.Selection> {
return MutableStateFlow(DydxMarketAccountTabView.Selection.Position)
}

@Provides
@ActivityRetainedScoped
fun provideMutableTileSelection(): MutableStateFlow<DydxMarketTilesView.Tile> {
return MutableStateFlow(DydxMarketTilesView.Tile(DydxMarketTilesView.TileType.PRICE))
@Provides
@ActivityRetainedScoped
fun provideMutableTileSelection(): MutableStateFlow<DydxMarketTilesView.Tile> {
return MutableStateFlow(DydxMarketTilesView.Tile(DydxMarketTilesView.TileType.PRICE))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package exchange.dydx.trading.feature.market.marketinfo.streams

import dagger.hilt.android.scopes.ActivityRetainedScoped
import exchange.dydx.abacus.output.Asset
import exchange.dydx.abacus.output.PerpetualMarket
import exchange.dydx.abacus.output.PositionSide
Expand All @@ -20,6 +21,7 @@ import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.newSingleThreadContext
import javax.inject.Inject

interface MarketInfoStreaming {
val market: Flow<PerpetualMarket?>
Expand All @@ -32,7 +34,8 @@ interface MutableMarketInfoStreaming : MarketInfoStreaming {
fun update(marketId: String?)
}

class MarketInfoStream(
@ActivityRetainedScoped
class MarketInfoStream @Inject constructor(
val abacusStateManager: AbacusStateManagerProtocol,
val formatter: DydxFormatter,
val localizer: LocalizerProtocol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import exchange.dydx.trading.feature.newsalerts.alerts.alertprovider.providers.D
import exchange.dydx.trading.feature.newsalerts.alerts.alertprovider.providers.DydxTransferAlertsProvider
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import javax.inject.Inject

class DydxAlertsProvider(
class DydxAlertsProvider @Inject constructor(
val dydxSystemAlertsProvider: DydxSystemAlertsProvider,
val dydxTransferAlertsProvider: DydxTransferAlertsProvider,
val dydxFrontEndAlertsProvider: DydxFrontendAlertsProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import java.util.Date
import javax.inject.Inject

class DydxFrontendAlertsProvider(
class DydxFrontendAlertsProvider @Inject constructor(
private val abacusStateManger: AbacusStateManagerProtocol,
private val router: DydxRouter,
) : DydxCustomAlertsProviderProtocol {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package exchange.dydx.trading.feature.newsalerts.alerts.alertprovider.providers

import dagger.hilt.android.scopes.ActivityRetainedScoped
import exchange.dydx.abacus.protocols.LocalizerProtocol
import exchange.dydx.abacus.state.manager.ApiState
import exchange.dydx.abacus.state.manager.ApiStatus
Expand All @@ -16,8 +17,10 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import java.util.Date
import javax.inject.Inject

class DydxSystemAlertsProvider(
@ActivityRetainedScoped
class DydxSystemAlertsProvider @Inject constructor(
private val abacusStateManger: AbacusStateManagerProtocol,
private val router: DydxRouter,
private val localizer: LocalizerProtocol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import java.util.Date
import javax.inject.Inject

class DydxTransferAlertsProvider(
class DydxTransferAlertsProvider @Inject constructor(
private val abacusStateManger: AbacusStateManagerProtocol,
private val router: DydxRouter,
private val localizer: LocalizerProtocol,
Expand Down
Loading
Loading