Skip to content

Commit 16a1c3a

Browse files
committed
Refactoring - 9
1 parent 38f9897 commit 16a1c3a

File tree

1 file changed

+39
-36
lines changed
  • presentation/src/main/kotlin/io/github/jisungbin/gitmessengerbot/activity/setup

1 file changed

+39
-36
lines changed

presentation/src/main/kotlin/io/github/jisungbin/gitmessengerbot/activity/setup/SetupViewModel.kt

+39-36
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
package io.github.jisungbin.gitmessengerbot.activity.setup
1111

1212
import androidx.lifecycle.ViewModel
13+
import androidx.lifecycle.viewModelScope
1314
import dagger.hilt.android.lifecycle.HiltViewModel
1415
import io.github.jisungbin.gitmessengerbot.activity.setup.mvi.MviSetupSideEffect
1516
import io.github.jisungbin.gitmessengerbot.activity.setup.mvi.MviSetupState
16-
import io.github.jisungbin.gitmessengerbot.domain.github.doWhen
17+
import io.github.jisungbin.gitmessengerbot.common.extension.doWhen
18+
import io.github.jisungbin.gitmessengerbot.common.extension.toException
1719
import io.github.jisungbin.gitmessengerbot.domain.github.model.user.GithubData
1820
import io.github.jisungbin.gitmessengerbot.domain.github.usecase.GithubGetUserInfoUseCase
1921
import io.github.jisungbin.gitmessengerbot.domain.github.usecase.GithubRequestAouthTokenUseCase
2022
import javax.inject.Inject
21-
import kotlinx.coroutines.flow.collect
2223
import org.orbitmvi.orbit.ContainerHost
2324
import org.orbitmvi.orbit.syntax.simple.intent
2425
import org.orbitmvi.orbit.syntax.simple.postSideEffect
@@ -34,42 +35,44 @@ class SetupViewModel @Inject constructor(
3435
override val container = container<MviSetupState, MviSetupSideEffect>(MviSetupState())
3536

3637
fun login(requestCode: String) = intent {
37-
githubRequestAouthTokenUseCase(requestCode).collect { githubAouthResult ->
38-
githubAouthResult.doWhen(
39-
onSuccess = { githubAouth ->
40-
var githubData = GithubData(aouthToken = githubAouth.token)
41-
githubGetUserInfoUseCase(githubData.aouthToken).collect { userInfoResult ->
42-
userInfoResult.doWhen(
43-
onSuccess = { userInfo ->
44-
githubData = githubData.copy(
45-
userName = userInfo.userName,
46-
profileImageUrl = userInfo.profileImageUrl
47-
)
48-
postSideEffect(MviSetupSideEffect.SaveData(githubData))
49-
reduce {
50-
state.copy(
51-
loaded = true,
52-
exception = null,
53-
aouthToken = githubData.aouthToken,
54-
userName = githubData.userName,
55-
profileImageUrl = githubData.profileImageUrl
56-
)
57-
}
58-
},
59-
onFail = { exception ->
60-
reduce {
61-
state.copy(exception = exception)
62-
}
63-
}
38+
githubRequestAouthTokenUseCase(
39+
requestCode = requestCode,
40+
coroutineScope = viewModelScope
41+
).doWhen(
42+
onSuccess = { githubAouth ->
43+
var githubData = GithubData(aouthToken = githubAouth.token)
44+
githubGetUserInfoUseCase(
45+
aouthToken = githubData.aouthToken,
46+
coroutineScope = viewModelScope
47+
).doWhen(
48+
onSuccess = { userInfo ->
49+
githubData = githubData.copy(
50+
userName = userInfo.userName,
51+
profileImageUrl = userInfo.profileImageUrl
6452
)
53+
postSideEffect(MviSetupSideEffect.SaveData(githubData))
54+
reduce {
55+
state.copy(
56+
loaded = true,
57+
exception = null,
58+
aouthToken = githubData.aouthToken,
59+
userName = githubData.userName,
60+
profileImageUrl = githubData.profileImageUrl
61+
)
62+
}
63+
},
64+
onFailure = { throwable ->
65+
reduce {
66+
state.copy(exception = throwable.toException())
67+
}
6568
}
66-
},
67-
onFail = { exception ->
68-
reduce {
69-
state.copy(exception = exception)
70-
}
69+
)
70+
},
71+
onFailure = { throwable ->
72+
reduce {
73+
state.copy(exception = throwable.toException())
7174
}
72-
)
73-
}
75+
}
76+
)
7477
}
7578
}

0 commit comments

Comments
 (0)