Skip to content

Commit

Permalink
[feature/#1011] 회원인증 화면 side effect 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeyubin committed Jan 1, 2025
1 parent 6312831 commit 520010b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.flowWithLifecycle
import kotlinx.coroutines.launch
import org.sopt.official.R.drawable.ic_auth_memeber_error
import org.sopt.official.R.drawable.ic_auth_process
Expand All @@ -66,7 +72,9 @@ import org.sopt.official.feature.auth.component.CertificationSnackBar
import org.sopt.official.feature.auth.component.PhoneCertification

@Composable
fun CertificationScreen() {
internal fun CertificationRoute(
viewModel: CertificationViewModel = hiltViewModel()
) {
val snackBarHostState = remember { SnackbarHostState() }
val coroutineScope = rememberCoroutineScope()
val onShowSnackBar: () -> Unit = remember {
Expand All @@ -77,6 +85,19 @@ fun CertificationScreen() {
}
}

val lifecycleOwner = LocalLifecycleOwner.current

LaunchedEffect(viewModel.sideEffect, lifecycleOwner) {
viewModel.sideEffect.flowWithLifecycle(lifecycle = lifecycleOwner.lifecycle)
.collect { sideEffect ->
when (sideEffect) {
is CertificationSideEffect.ShowToast -> {
snackBarHostState.showSnackbar(sideEffect.message)
}
}
}
}

SnackbarHost(
hostState = snackBarHostState,
modifier = Modifier
Expand All @@ -87,6 +108,18 @@ fun CertificationScreen() {
}
)

CertificationScreen(
onPhoneNumberClick = {
onShowSnackBar()
viewModel.createPhoneNumber()
}
)
}

@Composable
private fun CertificationScreen(
onPhoneNumberClick: () -> Unit
) {
Column(
modifier = Modifier
.fillMaxSize()
Expand All @@ -97,7 +130,7 @@ fun CertificationScreen() {
TopBar()
Spacer(modifier = Modifier.height(44.dp))
PhoneCertification(
onPhoneNumberClick = onShowSnackBar,
onPhoneNumberClick = onPhoneNumberClick,
textColor = Gray80
)
Spacer(modifier = Modifier.height(10.dp))
Expand Down Expand Up @@ -205,6 +238,8 @@ internal enum class ErrorCase(val message: String) {
@Composable
private fun AuthCertificationPreview() {
SoptTheme {
CertificationScreen()
CertificationScreen(
onPhoneNumberClick = {}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.sopt.official.feature.auth.feature.certificate

sealed class CertificationSideEffect {
data class ShowToast(val message: String): CertificationSideEffect()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.sopt.official.feature.auth.feature.certificate

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import org.sopt.official.domain.auth.model.UserInformation
import org.sopt.official.domain.auth.repository.AuthRepository
import javax.inject.Inject
Expand All @@ -16,13 +21,22 @@ enum class CertificationType(val type: String) {
class CertificationViewModel @Inject constructor(
private val repository: AuthRepository
) : ViewModel() {
suspend fun createPhoneNumber() {
repository.getCertificationNumber(
UserInformation(
name = null,
phone = "01012345678",
type = CertificationType.REGISTER.type
)
).onSuccess { }.onFailure { }

private val _sideEffect = MutableSharedFlow<CertificationSideEffect>()
val sideEffect: SharedFlow<CertificationSideEffect> = _sideEffect.asSharedFlow()
fun createPhoneNumber() {
viewModelScope.launch {
repository.getCertificationNumber(
UserInformation(
name = null,
phone = "01012345678",
type = CertificationType.REGISTER.type
)
).onSuccess {
_sideEffect.emit(CertificationSideEffect.ShowToast("성공"))
}.onFailure {
_sideEffect.emit(CertificationSideEffect.ShowToast("실패"))
}
}
}
}

0 comments on commit 520010b

Please sign in to comment.