10
10
package io.github.jisungbin.gitmessengerbot.activity.setup
11
11
12
12
import androidx.lifecycle.ViewModel
13
+ import androidx.lifecycle.viewModelScope
13
14
import dagger.hilt.android.lifecycle.HiltViewModel
14
15
import io.github.jisungbin.gitmessengerbot.activity.setup.mvi.MviSetupSideEffect
15
16
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
17
19
import io.github.jisungbin.gitmessengerbot.domain.github.model.user.GithubData
18
20
import io.github.jisungbin.gitmessengerbot.domain.github.usecase.GithubGetUserInfoUseCase
19
21
import io.github.jisungbin.gitmessengerbot.domain.github.usecase.GithubRequestAouthTokenUseCase
20
22
import javax.inject.Inject
21
- import kotlinx.coroutines.flow.collect
22
23
import org.orbitmvi.orbit.ContainerHost
23
24
import org.orbitmvi.orbit.syntax.simple.intent
24
25
import org.orbitmvi.orbit.syntax.simple.postSideEffect
@@ -34,42 +35,44 @@ class SetupViewModel @Inject constructor(
34
35
override val container = container<MviSetupState , MviSetupSideEffect >(MviSetupState ())
35
36
36
37
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
64
52
)
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
+ }
65
68
}
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())
71
74
}
72
- )
73
- }
75
+ }
76
+ )
74
77
}
75
78
}
0 commit comments