From 09964c8939ecc9cf61717c4e37a57af9024e0726 Mon Sep 17 00:00:00 2001 From: Prashan Dharmasena Date: Fri, 15 Mar 2024 15:55:05 -0400 Subject: [PATCH] Update rest of Dagger modules to follow best practices. --- .../trading/common/navigation/DydxRouter.kt | 3 - .../exchange/dydx/trading/TradingActivity.kt | 2 - .../dydx/trading/core/DydxRouterImpl.kt | 13 +- .../dydx/trading/core/di/CoreModule.kt | 24 +-- .../trading/feature/market/di/MarketModule.kt | 138 +++++++----------- .../marketinfo/streams/MarketInfoStream.kt | 5 +- .../alertprovider/DydxAlertsProvider.kt | 3 +- .../providers/DydxFrontendAlertsProvider.kt | 3 +- .../providers/DydxSystemAlertsProvider.kt | 5 +- .../providers/DydxTransferAlertsProvider.kt | 3 +- .../feature/newsalerts/di/NewsAlertsModule.kt | 59 -------- .../feature/onboarding/di/OnboardingModule.kt | 22 +-- .../feature/portfolio/di/PortfolioModule.kt | 66 ++++----- .../feature/receipt/di/ReceiptModule.kt | 38 ++--- .../feature/receipt/streams/ReceiptStream.kt | 7 +- .../trading/feature/trade/di/TradeModule.kt | 66 ++++----- .../feature/trade/streams/TradeStream.kt | 5 +- .../feature/transfer/di/TransferModule.kt | 99 ++++++------- .../utils/DydxTransferInstanceStore.kt | 5 +- .../integration/cosmos/di/CosmosModule.kt | 26 ---- .../dydxstatemanager/di/StateManagerModule.kt | 15 -- .../dydx/integration/starkex/StarkexLib.kt | 36 +++-- .../dydx/integration/starkex/StarkexModule.kt | 28 ---- 23 files changed, 239 insertions(+), 432 deletions(-) delete mode 100644 v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/di/NewsAlertsModule.kt delete mode 100644 v4/integration/cosmos/src/main/java/exchange/dydx/trading/integration/cosmos/di/CosmosModule.kt delete mode 100644 v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/di/StateManagerModule.kt delete mode 100644 v4/integration/starkex/src/main/java/exchange/dydx/integration/starkex/StarkexModule.kt diff --git a/v4/common/src/main/java/exchange/dydx/trading/common/navigation/DydxRouter.kt b/v4/common/src/main/java/exchange/dydx/trading/common/navigation/DydxRouter.kt index dffb0ead..2ad0937e 100644 --- a/v4/common/src/main/java/exchange/dydx/trading/common/navigation/DydxRouter.kt +++ b/v4/common/src/main/java/exchange/dydx/trading/common/navigation/DydxRouter.kt @@ -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 @@ -17,8 +16,6 @@ import kotlinx.coroutines.flow.StateFlow */ interface DydxRouter { - var androidContext: Context? - enum class Presentation { Push, Modal, diff --git a/v4/core/src/main/java/exchange/dydx/trading/TradingActivity.kt b/v4/core/src/main/java/exchange/dydx/trading/TradingActivity.kt index 8fe0fece..cd45dbaa 100644 --- a/v4/core/src/main/java/exchange/dydx/trading/TradingActivity.kt +++ b/v4/core/src/main/java/exchange/dydx/trading/TradingActivity.kt @@ -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) diff --git a/v4/core/src/main/java/exchange/dydx/trading/core/DydxRouterImpl.kt b/v4/core/src/main/java/exchange/dydx/trading/core/DydxRouterImpl.kt index ab3981e3..5c6f2d0d 100644 --- a/v4/core/src/main/java/exchange/dydx/trading/core/DydxRouterImpl.kt +++ b/v4/core/src/main/java/exchange/dydx/trading/core/DydxRouterImpl.kt @@ -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 @@ -10,6 +10,7 @@ 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 @@ -17,6 +18,7 @@ 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" @@ -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 @@ -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 diff --git a/v4/core/src/main/java/exchange/dydx/trading/core/di/CoreModule.kt b/v4/core/src/main/java/exchange/dydx/trading/core/di/CoreModule.kt index 23272608..822c01a7 100644 --- a/v4/core/src/main/java/exchange/dydx/trading/core/di/CoreModule.kt +++ b/v4/core/src/main/java/exchange/dydx/trading/core/di/CoreModule.kt @@ -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 } diff --git a/v4/feature/market/src/main/java/exchange/dydx/trading/feature/market/di/MarketModule.kt b/v4/feature/market/src/main/java/exchange/dydx/trading/feature/market/di/MarketModule.kt index 3986be66..8dfe5d48 100644 --- a/v4/feature/market/src/main/java/exchange/dydx/trading/feature/market/di/MarketModule.kt +++ b/v4/feature/market/src/main/java/exchange/dydx/trading/feature/market/di/MarketModule.kt @@ -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 @@ -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, - ): Flow { - return mutableFlow - } - - @Provides - @ActivityRetainedScoped - fun provideMutableFilterActionFlow(): MutableStateFlow { - return MutableStateFlow(null) - } + ): Flow - @Provides - @ActivityRetainedScoped - fun provideSortActionFlow( + @Binds fun bindSortActionFlow( mutableFlow: MutableStateFlow, - ): Flow { - return mutableFlow - } + ): Flow + + companion object { + @Provides + @ActivityRetainedScoped + fun provideMutableFilterActionFlow(): MutableStateFlow { + return MutableStateFlow(null) + } - @Provides - @ActivityRetainedScoped - fun provideMutableSortActionFlow(): MutableStateFlow { - return MutableStateFlow(null) + @Provides + @ActivityRetainedScoped + fun provideMutableSortActionFlow(): MutableStateFlow { + 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, - ): Flow { - return mutableStatsTabFlow - } + ): Flow - @Provides - @ActivityRetainedScoped - fun provideMutableStatsTabSelection(): MutableStateFlow { - return MutableStateFlow(DydxMarketStatsTabView.Selection.Statistics) - } - - @Provides - @ActivityRetainedScoped - fun provideAccountTabSelection( + @Binds + fun bindAccountTabSelection( mutableAccountTabFlow: MutableStateFlow, - ): Flow { - return mutableAccountTabFlow - } - - @Provides - @ActivityRetainedScoped - fun provideMutableAccountTabSelection(): MutableStateFlow { - return MutableStateFlow(DydxMarketAccountTabView.Selection.Position) - } + ): Flow - @Provides - @ActivityRetainedScoped - fun provideTileSelection( + @Binds + fun bindTileSelection( mutableTileFlow: MutableStateFlow, - ): Flow { - return mutableTileFlow - } + ): Flow + + companion object { + @Provides + @ActivityRetainedScoped + fun provideMutableStatsTabSelection(): MutableStateFlow { + return MutableStateFlow(DydxMarketStatsTabView.Selection.Statistics) + } + + @Provides + @ActivityRetainedScoped + fun provideMutableAccountTabSelection(): MutableStateFlow { + return MutableStateFlow(DydxMarketAccountTabView.Selection.Position) + } - @Provides - @ActivityRetainedScoped - fun provideMutableTileSelection(): MutableStateFlow { - return MutableStateFlow(DydxMarketTilesView.Tile(DydxMarketTilesView.TileType.PRICE)) + @Provides + @ActivityRetainedScoped + fun provideMutableTileSelection(): MutableStateFlow { + return MutableStateFlow(DydxMarketTilesView.Tile(DydxMarketTilesView.TileType.PRICE)) + } } } diff --git a/v4/feature/market/src/main/java/exchange/dydx/trading/feature/market/marketinfo/streams/MarketInfoStream.kt b/v4/feature/market/src/main/java/exchange/dydx/trading/feature/market/marketinfo/streams/MarketInfoStream.kt index 8b6a9d55..713ccead 100644 --- a/v4/feature/market/src/main/java/exchange/dydx/trading/feature/market/marketinfo/streams/MarketInfoStream.kt +++ b/v4/feature/market/src/main/java/exchange/dydx/trading/feature/market/marketinfo/streams/MarketInfoStream.kt @@ -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 @@ -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 @@ -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, diff --git a/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/DydxAlertsProvider.kt b/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/DydxAlertsProvider.kt index 856bfe18..d6d5facd 100644 --- a/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/DydxAlertsProvider.kt +++ b/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/DydxAlertsProvider.kt @@ -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, diff --git a/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxFrontendAlertsProvider.kt b/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxFrontendAlertsProvider.kt index 51627c37..9d5619bc 100644 --- a/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxFrontendAlertsProvider.kt +++ b/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxFrontendAlertsProvider.kt @@ -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 { diff --git a/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxSystemAlertsProvider.kt b/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxSystemAlertsProvider.kt index 3cb16a91..2932581e 100644 --- a/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxSystemAlertsProvider.kt +++ b/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxSystemAlertsProvider.kt @@ -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 @@ -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, diff --git a/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxTransferAlertsProvider.kt b/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxTransferAlertsProvider.kt index 0e57602d..03483a66 100644 --- a/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxTransferAlertsProvider.kt +++ b/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/alerts/alertprovider/providers/DydxTransferAlertsProvider.kt @@ -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, diff --git a/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/di/NewsAlertsModule.kt b/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/di/NewsAlertsModule.kt deleted file mode 100644 index 045fee74..00000000 --- a/v4/feature/newsalerts/src/main/java/exchange/dydx/trading/feature/newsalerts/di/NewsAlertsModule.kt +++ /dev/null @@ -1,59 +0,0 @@ -package exchange.dydx.trading.feature.newsalerts.di - -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.trading.common.formatter.DydxFormatter -import exchange.dydx.trading.common.navigation.DydxRouter -import exchange.dydx.trading.feature.newsalerts.alerts.alertprovider.DydxAlertsProvider -import exchange.dydx.trading.feature.newsalerts.alerts.alertprovider.providers.DydxFrontendAlertsProvider -import exchange.dydx.trading.feature.newsalerts.alerts.alertprovider.providers.DydxSystemAlertsProvider -import exchange.dydx.trading.feature.newsalerts.alerts.alertprovider.providers.DydxTransferAlertsProvider - -@Module -@InstallIn(ActivityRetainedComponent::class) -object NewsAlertsModule { - @Provides - @ActivityRetainedScoped - fun provideDydxSystemAlertsProvider( - abacusStateManager: AbacusStateManagerProtocol, - router: DydxRouter, - localizer: LocalizerProtocol, - ): DydxSystemAlertsProvider { - return DydxSystemAlertsProvider(abacusStateManager, router, localizer) - } - - @Provides - @ActivityRetainedScoped - fun provideDydxFrontendAlertsProvider( - abacusStateManager: AbacusStateManagerProtocol, - router: DydxRouter, - ): DydxFrontendAlertsProvider { - return DydxFrontendAlertsProvider(abacusStateManager, router) - } - - @Provides - @ActivityRetainedScoped - fun provideDydxTransferAlertsProvider( - abacusStateManager: AbacusStateManagerProtocol, - router: DydxRouter, - localizer: LocalizerProtocol, - formatter: DydxFormatter, - ): DydxTransferAlertsProvider { - return DydxTransferAlertsProvider(abacusStateManager, router, localizer, formatter) - } - - @Provides - @ActivityRetainedScoped - fun provideDydxAlertsProvider( - dydxSystemAlertsProvider: DydxSystemAlertsProvider, - dydxTransferAlertsProvider: DydxTransferAlertsProvider, - dydxFrontEndAlertsProvider: DydxFrontendAlertsProvider, - ): DydxAlertsProvider { - return DydxAlertsProvider(dydxSystemAlertsProvider, dydxTransferAlertsProvider, dydxFrontEndAlertsProvider) - } -} diff --git a/v4/feature/onboarding/src/main/java/exchange/dydx/feature/onboarding/di/OnboardingModule.kt b/v4/feature/onboarding/src/main/java/exchange/dydx/feature/onboarding/di/OnboardingModule.kt index 5746dab2..9749a24c 100644 --- a/v4/feature/onboarding/src/main/java/exchange/dydx/feature/onboarding/di/OnboardingModule.kt +++ b/v4/feature/onboarding/src/main/java/exchange/dydx/feature/onboarding/di/OnboardingModule.kt @@ -1,5 +1,6 @@ package exchange.dydx.feature.onboarding.di +import dagger.Binds import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -11,19 +12,18 @@ import kotlinx.coroutines.flow.StateFlow @Module @InstallIn(ActivityRetainedComponent::class) -object OnboardingModule { +interface OnboardingModule { - @Provides - @ActivityRetainedScoped - fun provideWalletSignedStatusFlow( + @Binds + fun bindWalletSignedStatusFlow( flow: MutableStateFlow, - ): StateFlow { - return flow - } + ): StateFlow - @Provides - @ActivityRetainedScoped - fun provideMutableWalletSignedStatusFlow(): MutableStateFlow { - return MutableStateFlow(null) + companion object { + @Provides + @ActivityRetainedScoped + fun provideMutableWalletSignedStatusFlow(): MutableStateFlow { + return MutableStateFlow(null) + } } } diff --git a/v4/feature/portfolio/src/main/java/exchange/dydx/trading/feature/portfolio/di/PortfolioModule.kt b/v4/feature/portfolio/src/main/java/exchange/dydx/trading/feature/portfolio/di/PortfolioModule.kt index f8bc86ab..ccd2b935 100644 --- a/v4/feature/portfolio/src/main/java/exchange/dydx/trading/feature/portfolio/di/PortfolioModule.kt +++ b/v4/feature/portfolio/src/main/java/exchange/dydx/trading/feature/portfolio/di/PortfolioModule.kt @@ -1,5 +1,6 @@ package exchange.dydx.trading.feature.portfolio.di +import dagger.Binds import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -13,47 +14,40 @@ import kotlinx.coroutines.flow.MutableStateFlow @Module @InstallIn(ActivityRetainedComponent::class) -object PortfolioModule { +interface PortfolioModule { - @Provides - @ActivityRetainedScoped - fun provideDisplayContent( + @Binds + fun bindDisplayContent( mutableFlow: MutableStateFlow, - ): Flow { - return mutableFlow - } - - @Provides - @ActivityRetainedScoped - fun provideMutableDisplayContent(): MutableStateFlow { - return MutableStateFlow(DydxPortfolioView.DisplayContent.Overview) - } + ): Flow - @Provides - @ActivityRetainedScoped - fun provideSections( + @Binds + fun bindSections( mutableFlow: MutableStateFlow, - ): Flow { - return mutableFlow - } + ): Flow - @Provides - @ActivityRetainedScoped - fun providateMutableSections(): MutableStateFlow { - return MutableStateFlow(DydxPortfolioSectionsView.Selection.Positions) - } - - @Provides - @ActivityRetainedScoped - fun providePlacholderSections( + @Binds + fun bindPlacholderSections( mutableFlow: MutableStateFlow, - ): Flow { - return mutableFlow - } - - @Provides - @ActivityRetainedScoped - fun providateMutablePlaceholderSections(): MutableStateFlow { - return MutableStateFlow(DydxPortfolioPlaceholderView.Selection.Positions) + ): Flow + + companion object { + @Provides + @ActivityRetainedScoped + fun provideMutableDisplayContent(): MutableStateFlow { + return MutableStateFlow(DydxPortfolioView.DisplayContent.Overview) + } + + @Provides + @ActivityRetainedScoped + fun providateMutableSections(): MutableStateFlow { + return MutableStateFlow(DydxPortfolioSectionsView.Selection.Positions) + } + + @Provides + @ActivityRetainedScoped + fun providateMutablePlaceholderSections(): MutableStateFlow { + return MutableStateFlow(DydxPortfolioPlaceholderView.Selection.Positions) + } } } diff --git a/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/di/ReceiptModule.kt b/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/di/ReceiptModule.kt index eb16a7b5..73645dad 100644 --- a/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/di/ReceiptModule.kt +++ b/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/di/ReceiptModule.kt @@ -1,11 +1,11 @@ package exchange.dydx.trading.feature.receipt.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.dydxstatemanager.AbacusStateManagerProtocol import exchange.dydx.trading.feature.receipt.ReceiptType import exchange.dydx.trading.feature.receipt.streams.ReceiptStream import exchange.dydx.trading.feature.receipt.streams.ReceiptStreaming @@ -14,31 +14,23 @@ import kotlinx.coroutines.flow.MutableStateFlow @Module @InstallIn(ActivityRetainedComponent::class) -object ReceiptModule { +interface ReceiptModule { - @Provides - @ActivityRetainedScoped - fun provideReceiptTypeFlow( + @Binds + fun bindReceiptTypeFlow( mutableFlow: MutableStateFlow, - ): Flow { - return mutableFlow - } + ): Flow - @Provides - @ActivityRetainedScoped - fun provideMutableReceiptTypeFlow(): MutableStateFlow { - return MutableStateFlow(null) - } + @Binds + fun bindReceiptStream( + receiptStream: ReceiptStream, + ): ReceiptStreaming - @Provides - @ActivityRetainedScoped - fun providesReceiptStream( - abacusStateManager: AbacusStateManagerProtocol, - receiptTypeFlow: Flow<@JvmSuppressWildcards ReceiptType?>, - ): ReceiptStreaming { - return ReceiptStream( - abacusStateManager = abacusStateManager, - receiptTypeFlow = receiptTypeFlow, - ) + companion object { + @Provides + @ActivityRetainedScoped + fun provideMutableReceiptTypeFlow(): MutableStateFlow { + return MutableStateFlow(null) + } } } diff --git a/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/streams/ReceiptStream.kt b/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/streams/ReceiptStream.kt index 68c87ec5..2957933c 100644 --- a/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/streams/ReceiptStream.kt +++ b/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/streams/ReceiptStream.kt @@ -7,14 +7,15 @@ import exchange.dydx.trading.feature.receipt.TradeReceiptType import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged +import javax.inject.Inject interface ReceiptStreaming { val tradeSummaryFlow: Flow?> } -class ReceiptStream( - val abacusStateManager: AbacusStateManagerProtocol, - val receiptTypeFlow: Flow<@JvmSuppressWildcards ReceiptType?>, +class ReceiptStream @Inject constructor( + abacusStateManager: AbacusStateManagerProtocol, + receiptTypeFlow: Flow<@JvmSuppressWildcards ReceiptType?>, ) : ReceiptStreaming { override val tradeSummaryFlow: Flow?> = diff --git a/v4/feature/trade/src/main/java/exchange/dydx/trading/feature/trade/di/TradeModule.kt b/v4/feature/trade/src/main/java/exchange/dydx/trading/feature/trade/di/TradeModule.kt index 4028ceac..28114f68 100644 --- a/v4/feature/trade/src/main/java/exchange/dydx/trading/feature/trade/di/TradeModule.kt +++ b/v4/feature/trade/src/main/java/exchange/dydx/trading/feature/trade/di/TradeModule.kt @@ -1,11 +1,11 @@ package exchange.dydx.trading.feature.trade.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.dydxstatemanager.AbacusStateManagerProtocol import exchange.dydx.trading.feature.trade.streams.MutableTradeStreaming import exchange.dydx.trading.feature.trade.streams.TradeStream import exchange.dydx.trading.feature.trade.streams.TradeStreaming @@ -15,48 +15,38 @@ import kotlinx.coroutines.flow.MutableStateFlow @Module @InstallIn(ActivityRetainedComponent::class) -object TradeModule { - @Provides - @ActivityRetainedScoped - fun provideOrderbookToggleStateFlow( +interface TradeModule { + @Binds + fun bindOrderbookToggleStateFlow( mutableFlow: MutableStateFlow, - ): Flow { - return mutableFlow - } - - @Provides - @ActivityRetainedScoped - fun provideMutableOrderbookToggleStateFlow(): MutableStateFlow { - return MutableStateFlow(DydxTradeInputView.OrderbookToggleState.Open) - } + ): Flow - @Provides - @ActivityRetainedScoped - fun provideBottomSheetFlow( + @Binds + fun bindBottomSheetFlow( mutableFlow: MutableStateFlow, - ): Flow { - return mutableFlow - } + ): Flow - @Provides - @ActivityRetainedScoped - fun provideMutableBottomSheetStateFlow(): MutableStateFlow { - return MutableStateFlow(null) - } - - @Provides - @ActivityRetainedScoped - fun provideTradeStream( + @Binds + fun bindTradeStream( mutableStream: MutableTradeStreaming, - ): TradeStreaming { - return mutableStream - } + ): TradeStreaming + + @Binds + fun bindMutableTradeStream( + tradeStream: TradeStream, + ): MutableTradeStreaming + + companion object { + @Provides + @ActivityRetainedScoped + fun provideMutableOrderbookToggleStateFlow(): MutableStateFlow { + return MutableStateFlow(DydxTradeInputView.OrderbookToggleState.Open) + } - @Provides - @ActivityRetainedScoped - fun provideMutableTradeStream( - abacusStateManager: AbacusStateManagerProtocol, - ): MutableTradeStreaming { - return TradeStream(abacusStateManager) + @Provides + @ActivityRetainedScoped + fun provideMutableBottomSheetStateFlow(): MutableStateFlow { + return MutableStateFlow(null) + } } } diff --git a/v4/feature/trade/src/main/java/exchange/dydx/trading/feature/trade/streams/TradeStream.kt b/v4/feature/trade/src/main/java/exchange/dydx/trading/feature/trade/streams/TradeStream.kt index 82bf2644..16729366 100644 --- a/v4/feature/trade/src/main/java/exchange/dydx/trading/feature/trade/streams/TradeStream.kt +++ b/v4/feature/trade/src/main/java/exchange/dydx/trading/feature/trade/streams/TradeStream.kt @@ -1,5 +1,6 @@ package exchange.dydx.trading.feature.trade.streams +import dagger.hilt.android.scopes.ActivityRetainedScoped import exchange.dydx.abacus.output.SubaccountOrder import exchange.dydx.abacus.state.model.TradeInputField import exchange.dydx.dydxstatemanager.AbacusStateManagerProtocol @@ -12,6 +13,7 @@ import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.newSingleThreadContext +import javax.inject.Inject interface TradeStreaming { val submissionStatus: Flow @@ -23,7 +25,8 @@ interface MutableTradeStreaming : TradeStreaming { fun closePosition() } -class TradeStream( +@ActivityRetainedScoped +class TradeStream @Inject constructor( val abacusStateManager: AbacusStateManagerProtocol, ) : MutableTradeStreaming { diff --git a/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/di/TransferModule.kt b/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/di/TransferModule.kt index 16b981f5..b2481759 100644 --- a/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/di/TransferModule.kt +++ b/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/di/TransferModule.kt @@ -1,12 +1,11 @@ package exchange.dydx.trading.feature.transfer.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.ParserProtocol -import exchange.dydx.dydxstatemanager.AbacusStateManagerProtocol import exchange.dydx.trading.feature.shared.DydxScreenResult import exchange.dydx.trading.feature.transfer.DydxTransferError import exchange.dydx.trading.feature.transfer.DydxTransferSectionsView @@ -19,69 +18,55 @@ import kotlinx.coroutines.flow.StateFlow @Module @InstallIn(ActivityRetainedComponent::class) -class TransferModule { - @Provides - @ActivityRetainedScoped - fun provideSections( +interface TransferModule { + @Binds + fun bindSections( mutableFlow: MutableStateFlow, - ): Flow { - return mutableFlow - } - - @Provides - @ActivityRetainedScoped - fun providateMutableSections(): MutableStateFlow { - return MutableStateFlow(DydxTransferSectionsView.Selection.Deposit) - } + ): Flow - @Provides - @ActivityRetainedScoped - fun provideDydxTransferSearchParam( + @Binds + fun bindDydxTransferSearchParam( mutableFlow: MutableStateFlow, - ): Flow { - return mutableFlow - } + ): Flow - @Provides - @ActivityRetainedScoped - fun provideMutableDydxTransferSearchParam(): MutableStateFlow { - return MutableStateFlow(null) - } - - @Provides - @ActivityRetainedScoped - fun provideDydxTransferInstanceStoring( - abausStateManager: AbacusStateManagerProtocol, - parser: ParserProtocol, - ): DydxTransferInstanceStoring { - return DydxTransferInstanceStore(abausStateManager, parser) - } + @Binds + fun bindDydxTransferInstanceStoring( + dydxTransferInstanceStore: DydxTransferInstanceStore + ): DydxTransferInstanceStoring - @Provides - @ActivityRetainedScoped - fun provideError( + @Binds + fun bindError( mutableFlow: MutableStateFlow, - ): StateFlow { - return mutableFlow - } - - @Provides - @ActivityRetainedScoped - fun provideMutableError(): MutableStateFlow { - return MutableStateFlow(null) - } + ): StateFlow - @Provides - @ActivityRetainedScoped - fun provideScreenResult( + @Binds + fun bindScreenResult( mutableFlow: MutableStateFlow, - ): Flow { - return mutableFlow - } + ): Flow + + companion object { + @Provides + @ActivityRetainedScoped + fun provideMutableDydxTransferSearchParam(): MutableStateFlow { + return MutableStateFlow(null) + } + + @Provides + @ActivityRetainedScoped + fun providateMutableSections(): MutableStateFlow { + return MutableStateFlow(DydxTransferSectionsView.Selection.Deposit) + } + + @Provides + @ActivityRetainedScoped + fun provideMutableError(): MutableStateFlow { + return MutableStateFlow(null) + } - @Provides - @ActivityRetainedScoped - fun provideMutableScreenResult(): MutableStateFlow { - return MutableStateFlow(null) + @Provides + @ActivityRetainedScoped + fun provideMutableScreenResult(): MutableStateFlow { + return MutableStateFlow(null) + } } } diff --git a/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/utils/DydxTransferInstanceStore.kt b/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/utils/DydxTransferInstanceStore.kt index f47dac52..254beb8b 100644 --- a/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/utils/DydxTransferInstanceStore.kt +++ b/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/utils/DydxTransferInstanceStore.kt @@ -1,17 +1,20 @@ package exchange.dydx.trading.feature.transfer.utils +import dagger.hilt.android.scopes.ActivityRetainedScoped import exchange.dydx.abacus.output.input.TransferInput import exchange.dydx.abacus.output.input.TransferType import exchange.dydx.abacus.protocols.ParserProtocol import exchange.dydx.dydxstatemanager.AbacusStateManagerProtocol import exchange.dydx.dydxstatemanager.clientState.transfers.DydxTransferInstance import java.time.Instant +import javax.inject.Inject interface DydxTransferInstanceStoring { fun addTransferHash(hash: String, fromChainName: String?, toChainName: String?, transferInput: TransferInput) } -class DydxTransferInstanceStore( +@ActivityRetainedScoped +class DydxTransferInstanceStore @Inject constructor( private val abacusStateManager: AbacusStateManagerProtocol, private val parser: ParserProtocol, ) : DydxTransferInstanceStoring { diff --git a/v4/integration/cosmos/src/main/java/exchange/dydx/trading/integration/cosmos/di/CosmosModule.kt b/v4/integration/cosmos/src/main/java/exchange/dydx/trading/integration/cosmos/di/CosmosModule.kt deleted file mode 100644 index d68f3f69..00000000 --- a/v4/integration/cosmos/src/main/java/exchange/dydx/trading/integration/cosmos/di/CosmosModule.kt +++ /dev/null @@ -1,26 +0,0 @@ -package exchange.dydx.trading.integration.cosmos.di - -import dagger.Module -import dagger.hilt.InstallIn -import dagger.hilt.android.components.ActivityRetainedComponent -import exchange.dydx.trading.common.DydxException - -private const val TAG = "CosmosModule" - -@Module -@InstallIn(ActivityRetainedComponent::class) -object CosmosModule { - -// @ActivityRetainedScoped -// @Provides -// fun provideCosmosWebviewClient(@ApplicationContext context: Context, json: Json, starkex: StarkexLib): CosmosV4WebviewClientProtocol { -// return CosmosV4ClientWebview(context) -// } -// -// @Provides -// @Singleton -// fun provideCosmosClient(@ApplicationContext appContext: Context): CosmosV4ClientProtocol = -// CosmosV4ClientWebview(appContext) -} - -class CosmosException(cause: Throwable? = null, s: String) : DydxException(s, cause) diff --git a/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/di/StateManagerModule.kt b/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/di/StateManagerModule.kt deleted file mode 100644 index caa65a6c..00000000 --- a/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/di/StateManagerModule.kt +++ /dev/null @@ -1,15 +0,0 @@ -package exchange.dydx.dydxstatemanager.di - -import dagger.Module -import dagger.hilt.InstallIn -import dagger.hilt.android.components.ActivityRetainedComponent - -@Module -@InstallIn(ActivityRetainedComponent::class) -object StateManagerModule { -// @ActivityRetainedScoped -// @Provides -// fun provideAbacusAppStateMachine(): AppStateMachine { -// return AppStateMachine() -// } -} diff --git a/v4/integration/starkex/src/main/java/exchange/dydx/integration/starkex/StarkexLib.kt b/v4/integration/starkex/src/main/java/exchange/dydx/integration/starkex/StarkexLib.kt index 8c75832f..0d028c52 100644 --- a/v4/integration/starkex/src/main/java/exchange/dydx/integration/starkex/StarkexLib.kt +++ b/v4/integration/starkex/src/main/java/exchange/dydx/integration/starkex/StarkexLib.kt @@ -1,21 +1,25 @@ package exchange.dydx.integration.starkex -import android.content.Context +import android.app.Application +import dagger.hilt.android.scopes.ActivityRetainedScoped import exchange.dydx.integration.javascript.JavascriptApiImpl import exchange.dydx.integration.javascript.JavascriptRunnerV3 import kotlinx.coroutines.runBlocking import timber.log.Timber import java.io.IOException +import javax.inject.Inject -class StarkexLib( - context: Context, - filename: String = "starkex-lib.js", +private const val STARKEX_FILENAME: String = "starkex-lib.js" + +@ActivityRetainedScoped +class StarkexLib @Inject constructor( + application: Application, ) : JavascriptApiImpl( - context = context, - description = filename, - runner = JavascriptRunnerV3.runnerFromFile(context, filename) - ?: throw IOException("Fatal, unable to load runner from: $filename"), + context = application, + description = STARKEX_FILENAME, + runner = JavascriptRunnerV3.runnerFromFile(application, STARKEX_FILENAME) + ?: throw IOException("Fatal, unable to load runner from: $STARKEX_FILENAME"), ) { private val TAG = "StarkexLib" @@ -50,13 +54,15 @@ class StarkexLib( } } +private const val STARKEX_ETH_FILENAME: String = "starkex-eth.js" + // TODO move this into its own fine when it has some content, or delete if not used. -class StarkexEth( - context: Context, - filename: String = "starkex-eth.js", +@ActivityRetainedScoped +class StarkexEth @Inject constructor( + application: Application, ) : JavascriptApiImpl( - context = context, - description = filename, - runner = JavascriptRunnerV3.runnerFromFile(context, filename) - ?: throw IOException("Fatal, unable to load runner from: $filename"), + context = application, + description = STARKEX_ETH_FILENAME, + runner = JavascriptRunnerV3.runnerFromFile(application, STARKEX_ETH_FILENAME) + ?: throw IOException("Fatal, unable to load runner from: $STARKEX_ETH_FILENAME"), ) diff --git a/v4/integration/starkex/src/main/java/exchange/dydx/integration/starkex/StarkexModule.kt b/v4/integration/starkex/src/main/java/exchange/dydx/integration/starkex/StarkexModule.kt deleted file mode 100644 index 19d3c0d9..00000000 --- a/v4/integration/starkex/src/main/java/exchange/dydx/integration/starkex/StarkexModule.kt +++ /dev/null @@ -1,28 +0,0 @@ -package exchange.dydx.integration.starkex - -import android.content.Context -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 - -private const val TAG = "StarkexModule" - -@Module -@InstallIn(ActivityRetainedComponent::class) -object StarkexModule { - - @ActivityRetainedScoped - @Provides - fun provideStarkexLib(@ApplicationContext context: Context): StarkexLib { - return StarkexLib(context) - } - - @ActivityRetainedScoped - @Provides - fun provideStarkexEth(@ApplicationContext context: Context): StarkexEth { - return StarkexEth(context) - } -}