Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ import com.example.hangsha_android.ui.view.onboarding.OnboardingViewModel
import com.example.hangsha_android.ui.view.onboarding.OnboardingWelcomeScreen
import com.example.hangsha_android.ui.view.signup.SignUpScreen
import com.example.hangsha_android.ui.view.signup.SignUpViewModel
import com.example.hangsha_android.ui.view.splash.SplashNavigationTarget
import com.example.hangsha_android.ui.view.splash.SplashScreen
import com.example.hangsha_android.ui.view.splash.SplashViewModel
import com.example.hangsha_android.ui.view.timetable.TimetableScreen
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
Expand All @@ -70,6 +73,7 @@ import com.kakao.sdk.common.model.ClientErrorCause
import com.kakao.sdk.user.UserApiClient

sealed class HangshaDestinations(val route: String) {
data object Splash : HangshaDestinations("splash")
data object Login : HangshaDestinations("login")
data object CredentialLogin : HangshaDestinations("credential_login")
data object SignUp : HangshaDestinations("sign_up")
Expand Down Expand Up @@ -116,16 +120,44 @@ fun HangshaNavHost(
) {
NavHost(
navController = navController,
startDestination = HangshaDestinations.Login.route,
startDestination = HangshaDestinations.Splash.route,
modifier = Modifier.padding(innerPadding)
) {
splashGraph(navController = navController)
loginGraph(navController = navController)
signUpGraph(navController = navController)
onboardingGraph(navController = navController)
mainGraph(navController = navController)
}
}

fun NavGraphBuilder.splashGraph(navController: NavHostController) {
composable(HangshaDestinations.Splash.route) {
val splashViewModel: SplashViewModel = hiltViewModel()
val splashUiState by splashViewModel.uiState.collectAsState()

LaunchedEffect(splashUiState.navigationTarget) {
when (splashUiState.navigationTarget) {
SplashNavigationTarget.Calendar -> {
navController.navigate(HangshaDestinations.Main.route) {
popUpTo(HangshaDestinations.Splash.route) { inclusive = true }
launchSingleTop = true
}
}
SplashNavigationTarget.Login -> {
navController.navigate(HangshaDestinations.Login.route) {
popUpTo(HangshaDestinations.Splash.route) { inclusive = true }
launchSingleTop = true
}
}
null -> Unit
}
}

SplashScreen()
}
}

fun NavGraphBuilder.loginGraph(navController: NavHostController) {
composable(HangshaDestinations.Login.route) {
val loginViewModel: LoginViewModel = hiltViewModel()
Expand Down Expand Up @@ -180,10 +212,6 @@ fun NavGraphBuilder.loginGraph(navController: NavHostController) {
}
}

LaunchedEffect(Unit) {
loginViewModel.tryAutoLogin()
}

LaunchedEffect(loginUiState.isLoginSuccessful) {
if (!loginUiState.isLoginSuccessful) {
return@LaunchedEffect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.example.hangsha_android.ui.view.login
data class LoginUiState(
val username: String = "",
val password: String = "",
val isAutoLoginLoading: Boolean = false,
val isCredentialLoginLoading: Boolean = false,
val isGoogleLoginLoading: Boolean = false,
val isKakaoLoginLoading: Boolean = false,
Expand All @@ -13,8 +12,7 @@ data class LoginUiState(
val loginMessage: String? = null
) {
val isAnyLoginLoading: Boolean
get() = isAutoLoginLoading ||
isCredentialLoginLoading ||
get() = isCredentialLoginLoading ||
isGoogleLoginLoading ||
isKakaoLoginLoading ||
isNaverLoginLoading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class LoginViewModel @Inject constructor(

private val _uiState = MutableStateFlow(LoginUiState())
val uiState: StateFlow<LoginUiState> = _uiState.asStateFlow()
private var hasAttemptedAutoLogin = false
private var isGuestModeRequested = false

fun onUsernameChanged(username: String) {
_uiState.update {
Expand All @@ -53,52 +51,6 @@ class LoginViewModel @Inject constructor(
}
}

fun tryAutoLogin() {
if (isGuestModeRequested) {
return
}
if (hasAttemptedAutoLogin) {
return
}
hasAttemptedAutoLogin = true

val refreshToken = authTokenStorage.getRefreshToken()
if (refreshToken.isNullOrBlank()) {
return
}

viewModelScope.launch {
_uiState.update {
it.copy(
isAutoLoginLoading = true,
loginMessage = null
)
}

val result = runCatching {
val response = authRepository.refresh(refreshToken)
if (!response.isSuccessful) {
throw HttpException(response)
}
if (isGuestModeRequested) {
return@launch
}
saveTokensFromResponse(response)
loadOrganizationNames()
loadExcludedKeywords()
}

result.fold(
onSuccess = {
onAuthSuccess("Logged in automatically.")
},
onFailure = { error ->
onAutoLoginFailure(error)
}
)
}
}

fun loginWithCredentials() {
val currentState = _uiState.value
val email = currentState.username.trim()
Expand Down Expand Up @@ -235,11 +187,9 @@ class LoginViewModel @Inject constructor(
}

fun continueAsGuest() {
isGuestModeRequested = true
authTokenStorage.clearTokens()
_uiState.update {
it.copy(
isAutoLoginLoading = false,
isCredentialLoginLoading = false,
isGoogleLoginLoading = false,
isKakaoLoginLoading = false,
Expand Down Expand Up @@ -384,7 +334,6 @@ class LoginViewModel @Inject constructor(
private fun onAuthSuccess(message: String) {
_uiState.update {
it.copy(
isAutoLoginLoading = false,
isCredentialLoginLoading = false,
isGoogleLoginLoading = false,
isKakaoLoginLoading = false,
Expand All @@ -398,7 +347,6 @@ class LoginViewModel @Inject constructor(
private fun onAuthFailure(message: String) {
_uiState.update {
it.copy(
isAutoLoginLoading = false,
isCredentialLoginLoading = false,
isGoogleLoginLoading = false,
isKakaoLoginLoading = false,
Expand Down Expand Up @@ -434,35 +382,4 @@ class LoginViewModel @Inject constructor(
onAuthFailure(message)
}

private fun onAutoLoginFailure(error: Throwable) {
when (error) {
is HttpException -> {
if (error.code() == 404) {
authTokenStorage.clearTokens()
_uiState.update {
it.copy(
isAutoLoginLoading = false,
isLoginSuccessful = false,
loginMessage = null
)
}
return
}

if (error.code() == 401) {
authTokenStorage.clearTokens()
_uiState.update {
it.copy(
isAutoLoginLoading = false,
isLoginSuccessful = false,
loginMessage = "Your session has expired. Please log in again."
)
}
return
}
}
}

onAuthFailure(error, "auto login")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.hangsha_android.ui.view.splash

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.hangsha_android.R

@Composable
fun SplashScreen() {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.White),
contentAlignment = Alignment.Center
) {
Column(
modifier = Modifier.offset(y = (-18).dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
painter = painterResource(id = R.mipmap.ic_launcher_foreground),
contentDescription = "\uD589\uC0E4 \uB85C\uACE0",
modifier = Modifier.size(86.dp),
contentScale = ContentScale.Fit
)
Spacer(modifier = Modifier.height(18.dp))
Text(
text = "\uC11C\uC6B8\uB300 \uD589\uC0AC\uB294 \uD589\uC0E4",
color = Color.Black,
fontSize = 15.sp,
fontWeight = FontWeight.SemiBold,
letterSpacing = 0.sp,
textAlign = TextAlign.Center
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.hangsha_android.ui.view.splash

data class SplashUiState(
val navigationTarget: SplashNavigationTarget? = null
)

enum class SplashNavigationTarget {
Calendar,
Login
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.example.hangsha_android.ui.view.splash

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.hangsha_android.data.local.AuthTokenStorage
import com.example.hangsha_android.data.network.model.LoginResponse
import com.example.hangsha_android.data.repository.AuthRepository
import com.example.hangsha_android.data.repository.ExcludedKeywordsRepository
import com.example.hangsha_android.data.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import retrofit2.HttpException
import retrofit2.Response

@HiltViewModel
class SplashViewModel @Inject constructor(
private val authRepository: AuthRepository,
private val authTokenStorage: AuthTokenStorage,
private val userRepository: UserRepository,
private val excludedKeywordsRepository: ExcludedKeywordsRepository
) : ViewModel() {

private val _uiState = MutableStateFlow(SplashUiState())
val uiState: StateFlow<SplashUiState> = _uiState.asStateFlow()

init {
verifyRefreshToken()
}

private fun verifyRefreshToken() {
val refreshToken = authTokenStorage.getRefreshToken()
if (refreshToken.isNullOrBlank()) {
navigateTo(SplashNavigationTarget.Login)
return
}

viewModelScope.launch {
val target = runCatching {
refreshSession(refreshToken)
}.fold(
onSuccess = { SplashNavigationTarget.Calendar },
onFailure = { error ->
if (error is HttpException && (error.code() == 401 || error.code() == 404)) {
authTokenStorage.clearTokens()
}
SplashNavigationTarget.Login
}
)

navigateTo(target)
}
}

private suspend fun refreshSession(refreshToken: String) {
val response = authRepository.refresh(refreshToken)
saveTokensFromResponse(response)
loadOrganizationNames()
loadExcludedKeywords()
}

private fun saveTokensFromResponse(response: Response<LoginResponse>) {
if (!response.isSuccessful) {
throw HttpException(response)
}

val authTokens = response.body()
val accessToken = authTokens?.accessToken
val refreshToken = authTokens?.refreshToken
if (accessToken.isNullOrBlank() || refreshToken.isNullOrBlank()) {
throw IllegalStateException("Authentication response did not include auth tokens.")
}

authTokenStorage.saveTokens(
accessToken = accessToken,
refreshToken = refreshToken
)
}

private suspend fun loadOrganizationNames() {
runCatching {
userRepository.ensureOrganizationNamesLoaded()
}
}

private suspend fun loadExcludedKeywords() {
runCatching {
excludedKeywordsRepository.refreshExcludedKeywords()
}
}

private fun navigateTo(target: SplashNavigationTarget) {
_uiState.update {
it.copy(navigationTarget = target)
}
}
}
Loading
Loading