1010package  io.github.jisungbin.gitmessengerbot.activity.setup 
1111
1212import  androidx.lifecycle.ViewModel 
13+ import  androidx.lifecycle.viewModelScope 
1314import  dagger.hilt.android.lifecycle.HiltViewModel 
1415import  io.github.jisungbin.gitmessengerbot.activity.setup.mvi.MviSetupSideEffect 
1516import  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 
1719import  io.github.jisungbin.gitmessengerbot.domain.github.model.user.GithubData 
1820import  io.github.jisungbin.gitmessengerbot.domain.github.usecase.GithubGetUserInfoUseCase 
1921import  io.github.jisungbin.gitmessengerbot.domain.github.usecase.GithubRequestAouthTokenUseCase 
2022import  javax.inject.Inject 
21- import  kotlinx.coroutines.flow.collect 
2223import  org.orbitmvi.orbit.ContainerHost 
2324import  org.orbitmvi.orbit.syntax.simple.intent 
2425import  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