-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject coroutine scopes and dispatchers instead of hardcoding. (#65)
- Loading branch information
1 parent
0ffc8e3
commit 41f045b
Showing
13 changed files
with
145 additions
and
74 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
31 changes: 31 additions & 0 deletions
31
v4/common/src/main/java/exchange/dydx/trading/common/di/CoroutineDispatchers.kt
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package exchange.dydx.trading.common.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import javax.inject.Qualifier | ||
|
||
object CoroutineDispatchers { | ||
|
||
@Qualifier annotation class Main | ||
|
||
@Qualifier annotation class IO | ||
|
||
@Qualifier annotation class Default | ||
} | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object CoroutineDispatchersModule { | ||
@Provides @CoroutineDispatchers.Main | ||
fun provideMain(): CoroutineDispatcher = Dispatchers.Main | ||
|
||
@Provides @CoroutineDispatchers.IO | ||
fun provideIO(): CoroutineDispatcher = Dispatchers.IO | ||
|
||
@Provides @CoroutineDispatchers.Default | ||
fun provideDefault(): CoroutineDispatcher = Dispatchers.Default | ||
} |
40 changes: 40 additions & 0 deletions
40
v4/common/src/main/java/exchange/dydx/trading/common/di/CoroutineScopes.kt
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package exchange.dydx.trading.common.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.ViewModelLifecycle | ||
import dagger.hilt.android.components.ViewModelComponent | ||
import dagger.hilt.android.scopes.ViewModelScoped | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.cancel | ||
import javax.inject.Qualifier | ||
import javax.inject.Singleton | ||
|
||
object CoroutineScopes { | ||
@Qualifier annotation class App | ||
|
||
@Qualifier annotation class ViewModel | ||
} | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object AppScopeModule { | ||
@Provides @CoroutineScopes.App @Singleton | ||
fun provideScope(): CoroutineScope = MainScope() | ||
} | ||
|
||
@Module | ||
@InstallIn(ViewModelComponent::class) | ||
object ViewModelScopeModule { | ||
@Provides @CoroutineScopes.ViewModel @ViewModelScoped | ||
fun provideScope(viewModelLifecycle: ViewModelLifecycle): CoroutineScope { | ||
val scope = MainScope() | ||
viewModelLifecycle.addOnClearedListener { | ||
scope.cancel() | ||
} | ||
return scope | ||
} | ||
} |
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
Oops, something went wrong.