Skip to content

Commit

Permalink
Refactoring - 9
Browse files Browse the repository at this point in the history
  • Loading branch information
jisungbin committed Nov 8, 2021
1 parent 38f9897 commit 16a1c3a
Showing 1 changed file with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
package io.github.jisungbin.gitmessengerbot.activity.setup

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.jisungbin.gitmessengerbot.activity.setup.mvi.MviSetupSideEffect
import io.github.jisungbin.gitmessengerbot.activity.setup.mvi.MviSetupState
import io.github.jisungbin.gitmessengerbot.domain.github.doWhen
import io.github.jisungbin.gitmessengerbot.common.extension.doWhen
import io.github.jisungbin.gitmessengerbot.common.extension.toException
import io.github.jisungbin.gitmessengerbot.domain.github.model.user.GithubData
import io.github.jisungbin.gitmessengerbot.domain.github.usecase.GithubGetUserInfoUseCase
import io.github.jisungbin.gitmessengerbot.domain.github.usecase.GithubRequestAouthTokenUseCase
import javax.inject.Inject
import kotlinx.coroutines.flow.collect
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.syntax.simple.intent
import org.orbitmvi.orbit.syntax.simple.postSideEffect
Expand All @@ -34,42 +35,44 @@ class SetupViewModel @Inject constructor(
override val container = container<MviSetupState, MviSetupSideEffect>(MviSetupState())

fun login(requestCode: String) = intent {
githubRequestAouthTokenUseCase(requestCode).collect { githubAouthResult ->
githubAouthResult.doWhen(
onSuccess = { githubAouth ->
var githubData = GithubData(aouthToken = githubAouth.token)
githubGetUserInfoUseCase(githubData.aouthToken).collect { userInfoResult ->
userInfoResult.doWhen(
onSuccess = { userInfo ->
githubData = githubData.copy(
userName = userInfo.userName,
profileImageUrl = userInfo.profileImageUrl
)
postSideEffect(MviSetupSideEffect.SaveData(githubData))
reduce {
state.copy(
loaded = true,
exception = null,
aouthToken = githubData.aouthToken,
userName = githubData.userName,
profileImageUrl = githubData.profileImageUrl
)
}
},
onFail = { exception ->
reduce {
state.copy(exception = exception)
}
}
githubRequestAouthTokenUseCase(
requestCode = requestCode,
coroutineScope = viewModelScope
).doWhen(
onSuccess = { githubAouth ->
var githubData = GithubData(aouthToken = githubAouth.token)
githubGetUserInfoUseCase(
aouthToken = githubData.aouthToken,
coroutineScope = viewModelScope
).doWhen(
onSuccess = { userInfo ->
githubData = githubData.copy(
userName = userInfo.userName,
profileImageUrl = userInfo.profileImageUrl
)
postSideEffect(MviSetupSideEffect.SaveData(githubData))
reduce {
state.copy(
loaded = true,
exception = null,
aouthToken = githubData.aouthToken,
userName = githubData.userName,
profileImageUrl = githubData.profileImageUrl
)
}
},
onFailure = { throwable ->
reduce {
state.copy(exception = throwable.toException())
}
}
},
onFail = { exception ->
reduce {
state.copy(exception = exception)
}
)
},
onFailure = { throwable ->
reduce {
state.copy(exception = throwable.toException())
}
)
}
}
)
}
}

0 comments on commit 16a1c3a

Please sign in to comment.