Skip to content

Commit

Permalink
[mod] #93 코루틴 디스패처 외부 주입
Browse files Browse the repository at this point in the history
  • Loading branch information
l2zh committed Mar 1, 2024
1 parent 17ad059 commit d062e7b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
15 changes: 15 additions & 0 deletions app/src/main/java/sopt/motivoo/di/CoroutineModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sopt.motivoo.di

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

@Module
@InstallIn(ActivityComponent::class)
object CoroutineModule {
@Provides
fun provideMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
}
9 changes: 6 additions & 3 deletions app/src/main/java/sopt/motivoo/presentation/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
Expand All @@ -44,6 +44,9 @@ class MainActivity : AppCompatActivity() {
@Inject
lateinit var networkErrorListener: NetworkErrorListener

@Inject
lateinit var mainDispatcher: CoroutineDispatcher

override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.Theme_MOTIVOOAOS)
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -172,7 +175,7 @@ class MainActivity : AppCompatActivity() {

private fun setupTokenRefreshListener() {
authTokenRefreshListener.setOnTokenRefreshFailedCallback {
CoroutineScope(Dispatchers.Main).launch {
CoroutineScope(mainDispatcher).launch {
val navController: NavController = findNavController(R.id.fc_main)
val startDestinationId = navController.findStartDestination().id
val navOptions = NavOptions.Builder()
Expand All @@ -186,7 +189,7 @@ class MainActivity : AppCompatActivity() {

private fun setupApiCallFailed() {
networkErrorListener.setOnApiCallFailedCallback {
CoroutineScope(Dispatchers.Main).launch {
CoroutineScope(mainDispatcher).launch {
val navController: NavController = findNavController(R.id.fc_main)
val startDestinationId = navController.findStartDestination().id
val navOptions = NavOptions.Builder()
Expand Down

0 comments on commit d062e7b

Please sign in to comment.