Last active
April 4, 2024 08:33
-
-
Save AliAzaz/9cbbef71bd0e9f377661ecaeca88fb81 to your computer and use it in GitHub Desktop.
Coroutine Dispatcher in a HILT injection Environment
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
class CoroutineDispatcherHelper : IDispatcher { | |
override fun dispatcherIO(): CoroutineDispatcher = Dispatchers.IO | |
override fun dispatcherMain(): CoroutineDispatcher = Dispatchers.Main | |
override fun dispatcherDefault(): CoroutineDispatcher = Dispatchers.Default | |
} |
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
@Module | |
@InstallIn(SingletonComponent::class) | |
object CoroutineDispatcherModule { | |
@Singleton | |
@Provides | |
fun provideCoroutineDispatcher(): CoroutineDispatcherHelper { | |
return CoroutineDispatcherHelper() | |
} | |
} |
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
interface IDispatcher { | |
fun dispatcherIO(): CoroutineDispatcher | |
fun dispatcherMain(): CoroutineDispatcher | |
fun dispatcherDefault(): CoroutineDispatcher | |
} |
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
fun <T> MutableLiveData<T>.launchItemInCoroutine(iDispatcher: CoroutineDispatcher, data: T) { | |
CoroutineScope(iDispatcher).launch { | |
postValue(data) | |
} | |
} | |
fun <T> SingleLiveEvent<T>.launchItemInCoroutine(iDispatcher: CoroutineDispatcher, data: T) { | |
CoroutineScope(iDispatcher).launch { | |
postValue(data) | |
} | |
} | |
fun <T> MutableSharedFlow<T>.launchItemInCoroutine(iDispatcher: CoroutineDispatcher, data: T) { | |
CoroutineScope(iDispatcher).launch { | |
emit(data) | |
} | |
} |
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
@HiltViewModel | |
class MainViewModel @Inject constructor( | |
private val verificationUseCase: VerificationUseCase, | |
private val dispatcherHelper: CoroutineDispatcherHelper, | |
) : ViewModel() { | |
fun verifyAndChangePassword(changePassword: ChangePassword) { | |
CoroutineScope(dispatcherHelper.dispatcherIO()).launch { | |
//Implementation here in coroutine environment | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should launch coroutines with
viewModelScope
if you want to cancel the coroutine when MainViewModel is cleared/destroyed.