This file contains hidden or 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
/* Copyright 2022 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 */ | |
public fun LifecycleOwner.addRepeatingJob( | |
state: Lifecycle.State, | |
coroutineContext: CoroutineContext = EmptyCoroutineContext, | |
block: suspend CoroutineScope.() -> Unit | |
): Job = lifecycleScope.launch(coroutineContext) { | |
repeatOnLifecycle(state, block) | |
} |
This file contains hidden or 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
class LocationActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// Create a new coroutine from the lifecycleScope | |
// since repeatOnLifecycle is a suspend function | |
lifecycleScope.launch { | |
// Suspend the coroutine until the lifecycle is DESTROYED. | |
// repeatOnLifecycle launches the block in a new coroutine every time the | |
// lifecycle is in the STARTED state (or above) and cancels it when it's STOPPED. |
This file contains hidden or 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
// androidTest/projectPath/TestCoroutinesDispatchersModule.kt file | |
@TestInstallIn( | |
components = [SingletonComponent::class], | |
replaces = [CoroutinesDispatchersModule::class] | |
) | |
@Module | |
object TestCoroutinesDispatchersModule { | |
@DefaultDispatcher |
This file contains hidden or 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
@Singleton | |
class MyRepository @Inject constructor( | |
@ApplicationScope private val externalScope: CoroutineScope | |
) { /* ... */ } |
This file contains hidden or 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
@Retention(AnnotationRetention.RUNTIME) | |
@Qualifier | |
annotation class ApplicationScope | |
@InstallIn(SingletonComponent::class) | |
@Module | |
object CoroutinesScopesModule { | |
@Singleton | |
@ApplicationScope |
This file contains hidden or 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
@InstallIn(SingletonComponent::class) | |
@Module | |
object CoroutinesScopesModule { | |
@Singleton | |
@Provides | |
fun providesCoroutineScope( | |
@DefaultDispatcher defaultDispatcher: CoroutineDispatcher | |
): CoroutineScope = CoroutineScope(SupervisorJob() + defaultDispatcher) | |
} |
This file contains hidden or 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
@InstallIn(SingletonComponent::class) | |
@Module | |
object CoroutinesDispatchersModule { | |
@DefaultDispatcher | |
@Provides | |
fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default | |
@IoDispatcher | |
@Provides |
This file contains hidden or 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
// CoroutinesQualifiers.kt file | |
@Retention(AnnotationRetention.RUNTIME) | |
@Qualifier | |
annotation class DefaultDispatcher | |
@Retention(AnnotationRetention.RUNTIME) | |
@Qualifier | |
annotation class IoDispatcher |
This file contains hidden or 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
@InstallIn(SingletonComponent::class) | |
@Module | |
object CoroutinesScopesModule { | |
@Singleton // Provide always the same instance | |
@Provides | |
fun providesCoroutineScope(): CoroutineScope { | |
// Run this code when providing an instance of CoroutineScope | |
return CoroutineScope(SupervisorJob() + Dispatchers.Default) | |
} |
This file contains hidden or 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
@Singleton // Scopes this type to the SingletonComponent | |
class MyRepository @Inject constructor( | |
private val externalScope: CoroutineScope | |
) { | |
/* ... */ | |
} |