generated from ajou4095/template-android
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Splash Login Event 연결 #5
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
.../kotlin/ac/dnd/bookkeeping/android/domain/usecase/authentication/IsLoginSucceedUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ac.dnd.bookkeeping.android.domain.usecase.authentication | ||
|
||
import ac.dnd.bookkeeping.android.domain.repository.AuthenticationRepository | ||
import kotlinx.coroutines.delay | ||
import javax.inject.Inject | ||
|
||
class IsLoginSucceedUseCase @Inject constructor( | ||
private val authenticationRepository: AuthenticationRepository | ||
) { | ||
suspend operator fun invoke(): Result<Unit> { | ||
// TODO: Implement this | ||
delay(1000L) | ||
return Result.success(Unit) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...ion/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ac.dnd.bookkeeping.android.presentation.ui.main.splash | ||
|
||
import ac.dnd.bookkeeping.android.domain.model.error.ServerException | ||
|
||
sealed interface SplashEvent { | ||
sealed interface Login : SplashEvent { | ||
data object Success : Login | ||
data class Failure(val exception: ServerException) : Login | ||
data class Error(val exception: Throwable) : Login | ||
} | ||
} |
108 changes: 79 additions & 29 deletions
108
...on/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,107 @@ | ||
package ac.dnd.bookkeeping.android.presentation.ui.main.splash | ||
|
||
import ac.dnd.bookkeeping.android.presentation.R | ||
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.eventObserve | ||
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.repeatOnStarted | ||
import ac.dnd.bookkeeping.android.presentation.ui.main.ApplicationState | ||
import ac.dnd.bookkeeping.android.presentation.ui.main.home.HomeConstant | ||
import ac.dnd.bookkeeping.android.presentation.ui.main.login.LoginConstant | ||
import android.os.Bundle | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
|
||
@Composable | ||
fun SplashScreen( | ||
appState: ApplicationState, | ||
// TODO : savedStateHandle 사용 | ||
arguments: Bundle, | ||
viewModel: SplashViewModel = hiltViewModel() | ||
) { | ||
val scope = rememberCoroutineScope() | ||
val entryPoint = arguments.getString(SplashConstant.ROUTE_ARGUMENT_ENTRY_POINT).orEmpty() | ||
LaunchedEffect(Unit) { | ||
scope.launch { | ||
delay(1000L) | ||
when (entryPoint) { | ||
SplashConstant.ROUTE_ARGUMENT_ENTRY_POINT_MAIN -> { | ||
appState.navController.navigate(LoginConstant.ROUTE) { | ||
popUpTo(SplashConstant.ROUTE) { | ||
inclusive = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
val state by viewModel.state.collectAsStateWithLifecycle() | ||
|
||
Box( | ||
Observer( | ||
appState = appState, | ||
viewModel = viewModel | ||
) | ||
|
||
Column( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.background(color = Color.Black), | ||
contentAlignment = Alignment.Center | ||
.fillMaxSize(), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
verticalArrangement = Arrangement.Center | ||
) { | ||
Image( | ||
modifier = Modifier.size(100.dp), | ||
painter = painterResource(id = R.drawable.ic_launcher), | ||
contentDescription = "" | ||
) | ||
Text( | ||
text = "Splash Screen", | ||
fontSize = 20.sp, | ||
color = Color.White | ||
color = Color.Black | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun Observer( | ||
appState: ApplicationState, | ||
viewModel: SplashViewModel | ||
) { | ||
val lifecycleOwner = LocalLifecycleOwner.current | ||
|
||
fun navigateToLogin() { | ||
appState.navController.navigate(LoginConstant.ROUTE) { | ||
popUpTo(SplashConstant.ROUTE) { | ||
inclusive = true | ||
} | ||
} | ||
} | ||
|
||
fun navigateToHome() { | ||
appState.navController.navigate(HomeConstant.ROUTE) { | ||
popUpTo(SplashConstant.ROUTE) { | ||
inclusive = true | ||
} | ||
} | ||
} | ||
|
||
fun login(event: SplashEvent.Login) { | ||
when (event) { | ||
SplashEvent.Login.Success -> { | ||
navigateToHome() | ||
} | ||
|
||
is SplashEvent.Login.Error -> { | ||
navigateToLogin() | ||
} | ||
|
||
is SplashEvent.Login.Failure -> { | ||
// TODO : Dialog Screen | ||
} | ||
} | ||
} | ||
LaunchedEffect(Unit) { | ||
lifecycleOwner.repeatOnStarted(CoroutineExceptionHandler(appState.coroutineExceptionHandler)) { | ||
viewModel.event.eventObserve { event -> | ||
when (event) { | ||
is SplashEvent.Login -> login(event) | ||
} | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ion/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ac.dnd.bookkeeping.android.presentation.ui.main.splash | ||
|
||
sealed interface SplashState { | ||
data object Init : SplashState | ||
} |
46 changes: 44 additions & 2 deletions
46
...src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,53 @@ | ||
package ac.dnd.bookkeeping.android.presentation.ui.main.splash | ||
|
||
import ac.dnd.bookkeeping.android.domain.model.error.ServerException | ||
import ac.dnd.bookkeeping.android.domain.usecase.authentication.IsLoginSucceedUseCase | ||
import ac.dnd.bookkeeping.android.presentation.common.base.BaseViewModel | ||
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.EventFlow | ||
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.MutableEventFlow | ||
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.asEventFlow | ||
import androidx.lifecycle.SavedStateHandle | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class SplashViewModel @Inject constructor( | ||
private val savedStateHandle: SavedStateHandle | ||
) : BaseViewModel() | ||
private val savedStateHandle: SavedStateHandle, | ||
private val isLoginSucceedUseCase: IsLoginSucceedUseCase | ||
) : BaseViewModel() { | ||
|
||
private val _state: MutableStateFlow<SplashState> = MutableStateFlow(SplashState.Init) | ||
val state: StateFlow<SplashState> = _state.asStateFlow() | ||
|
||
private val _event: MutableEventFlow<SplashEvent> = MutableEventFlow() | ||
val event: EventFlow<SplashEvent> = _event.asEventFlow() | ||
|
||
val entryPoint: String by lazy { | ||
savedStateHandle.get<String>(SplashConstant.ROUTE_ARGUMENT_ENTRY_POINT).orEmpty() | ||
} | ||
|
||
init { | ||
launch { | ||
login() | ||
} | ||
} | ||
|
||
private suspend fun login() { | ||
isLoginSucceedUseCase().onSuccess { | ||
_event.emit(SplashEvent.Login.Success) | ||
}.onFailure { exception -> | ||
when (exception) { | ||
is ServerException -> { | ||
_event.emit(SplashEvent.Login.Failure(exception)) | ||
} | ||
|
||
else -> { | ||
_event.emit(SplashEvent.Login.Error(exception)) | ||
} | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙀👍