Skip to content
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

1 signup #17

Merged
merged 3 commits into from
Mar 11, 2025
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
6 changes: 3 additions & 3 deletions core/di/src/main/java/com/photo/starsnap/di/NetworkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class NetworkModule {
authInterceptor: AuthInterceptor
): OkHttpClient {
return OkHttpClient().newBuilder().apply {
connectTimeout(3, TimeUnit.SECONDS)
readTimeout(3, TimeUnit.SECONDS)
writeTimeout(3, TimeUnit.SECONDS)
connectTimeout(10, TimeUnit.SECONDS)
readTimeout(10, TimeUnit.SECONDS)
writeTimeout(10, TimeUnit.SECONDS)
addInterceptor(loggerInterceptor)
addInterceptor(authInterceptor)
authenticator(authAuthenticator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -31,11 +31,11 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.photo.starsnap.designsystem.CustomColor.container
import com.photo.starsnap.designsystem.CustomColor.button
import com.photo.starsnap.designsystem.text.CustomTextStyle.hint1
import com.photo.starsnap.designsystem.text.CustomTextStyle.title2
import com.photo.starsnap.main.utils.EditTextType
import com.photo.starsnap.main.utils.getKeyboardType
import com.photo.starsnap.main.utils.maxLangth
import com.photo.starsnap.main.viewmodel.auth.SignupViewModel

@Composable
fun BaseEditText(
Expand Down Expand Up @@ -98,13 +98,11 @@ fun PasswordEditText(hint: String, password: (String) -> Unit) {
}

@Composable
fun VerifyCodeEditText(verifyCode: (String) -> Unit, state: Boolean) {
fun VerifyCodeEditText(viewModel: SignupViewModel) {
var code by remember { mutableStateOf("") }
LaunchedEffect(state) {
if (!state) {
code = ""
}
}

val timerUiState by viewModel.timerUiState.collectAsState()

Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally,
Expand All @@ -113,9 +111,9 @@ fun VerifyCodeEditText(verifyCode: (String) -> Unit, state: Boolean) {
value = code,
onValueChange = { input ->
// 최대 글자수 4개로 제한
if (input.length <= 4 && state) {
if (input.length <= 4 && timerUiState.isTimerRunning) {
code = input
verifyCode(input)
viewModel.verifyCode = input
}
},
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ fun VerifySignupScreen(viewModel: SignupViewModel, navController: NavController)

Spacer(Modifier.height(55.dp))

VerifyCodeEditText({ viewModel.verifyCode = it }, timerUiState.isTimerRunning)
// 인증번호 입력 칸?
VerifyCodeEditText(viewModel)

Spacer(Modifier.height(24.dp))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.photo.starsnap.main.viewmodel.auth

import android.os.CountDownTimer
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
Expand All @@ -25,8 +26,9 @@ class SignupViewModel @Inject constructor(

private var usernameCheckJob: Job? = null
private var emailCheckJob: Job? = null
private var resendTimer: Job? = null
private var timerJob: Job? = null
private var timer: CountDownTimer? = null
private var resendTimer: CountDownTimer? = null


private val _uiState = MutableStateFlow(SignupUiState())
val uiState: StateFlow<SignupUiState> get() = _uiState
Expand Down Expand Up @@ -85,11 +87,19 @@ class SignupViewModel @Inject constructor(
var verifyCode: String
get() = _uiState.value.verifyCode
set(value) {
if (value.isEmpty()) _uiState.value.copy(verifyCodeState = VerifyCodeState.DEFAULT)
_uiState.value = _uiState.value.copy(
verifyCode = value,
verifyCode = value, // `verifyCode`도 복사해야 할 수 있습니다.
verifyButtonState = value.length == 4
)
Log.d(TAG, value)

if (value.isEmpty()) {
_uiState.value = _uiState.value.copy(
verifyCodeState = VerifyCodeState.DEFAULT,
verifyButtonState = false // `verifyButtonState`도 초기화해야 할 수 있습니다.
)
}

Log.d(TAG, "${value.length} verifyButtonState: ${_uiState.value.verifyButtonState}")
}

Expand Down Expand Up @@ -205,18 +215,18 @@ class SignupViewModel @Inject constructor(
// 인증 번호 발송
fun sendEmail() = viewModelScope.launch {
_uiState.value = _uiState.value.copy(validCodeState = State.LOADING, verifyCode = "")
startTimer()
resendStarTimer()
timerStart()
timerResendStart()
runCatching {
authRepository.send(_uiState.value.email)
}.onSuccess {
_uiState.value = _uiState.value.copy(validCodeState = State.SUCCESS)
Log.d(TAG, it.toString())
}.onFailure { e ->
Log.d(TAG, e.message.toString())
_uiState.value = _uiState.value.copy(validCodeState = State.INTERNET_ERROR)
_timerUiState.value = _timerUiState.value.copy(isTimerRunning = false)
timerJob?.cancel()
_timerUiState.value =
_timerUiState.value.copy(isTimerRunning = false, timerValue = 0L, resendTime = 0L)
timer?.cancel()
resendTimer?.cancel()
}
}
Expand All @@ -238,7 +248,7 @@ class SignupViewModel @Inject constructor(
)

// 코드 인증 완료시 타이머 종료
timerJob?.cancel()
timer?.cancel()
resendTimer?.cancel()
_timerUiState.value =
_timerUiState.value.copy(timerValue = 0L, isTimerRunning = false, resendTime = 0L)
Expand All @@ -247,7 +257,8 @@ class SignupViewModel @Inject constructor(
_uiState.value =
_uiState.value.copy(
verifyCodeState = VerifyCodeState.ERROR,
verifyButtonState = false
verifyButtonState = false,
verifyCode = ""
)
} else {
_uiState.value =
Expand All @@ -263,7 +274,6 @@ class SignupViewModel @Inject constructor(
// 비밀번호 유효성 검사
private fun checkValidPassword(password: String, confirmPassword: String) {
val isPasswordValid = TextPattern.PASSWORD.toPattern().matcher(password).matches()
Log.d(TAG, "비밀번호: $password, 비밀번호 확인: $confirmPassword")

val passwordState = when {
password.isEmpty() -> PasswordState.DEFAULT
Expand All @@ -278,79 +288,52 @@ class SignupViewModel @Inject constructor(
)
}

// 타이머 시작(5분)
private fun startTimer() {
val duration = 300L // 초 단위로 수정 (5분)

timerJob?.cancel() // 기존 타이머 취소
timerJob = viewModelScope.launch {

val startTime = System.currentTimeMillis()
val endTime = startTime + duration * 1000

// 타이머 시작
_timerUiState.value =
_timerUiState.value.copy(
private fun timerStart() {
timer?.cancel()
timer = object : CountDownTimer(300_000L, 1000L) {
override fun onTick(millisUntilFinished: Long) {
_timerUiState.value = _timerUiState.value.copy(
isTimerRunning = true,
timerValue = duration
timerValue = millisUntilFinished / 1000
)

while (System.currentTimeMillis() < endTime) {
val remainingMillis = endTime - System.currentTimeMillis()
val remainingSeconds = (remainingMillis / 1000).coerceAtLeast(0)
Log.d(TAG, "일반: ${millisUntilFinished / 1000}")
}

// 상태 업데이트
_timerUiState.value = _timerUiState.value.copy(timerValue = remainingSeconds)
Log.d(
TAG,
"시간: ${_timerUiState.value.timerValue}, 활성화 상태: ${_timerUiState.value.isTimerRunning}"
override fun onFinish() {
// 타이머 종료 처리
_uiState.value = _uiState.value.copy(
verifyCodeState = VerifyCodeState.RESEND,
verifyButtonState = false
)

delay(1000L)
_timerUiState.value =
_timerUiState.value.copy(
timerValue = 0L,
isTimerRunning = false
) // 타이머 종료 시 0초로 설정
}

// 타이머 종료 처리
_uiState.value = _uiState.value.copy(
verifyCodeState = VerifyCodeState.RESEND,
verifyButtonState = false
)
_timerUiState.value =
_timerUiState.value.copy(timerValue = 0L, isTimerRunning = false) // 타이머 종료 시 0초로 설정
}
}.start()
}

private fun resendStarTimer() {
val resendTime = 60L // 초 단위로 수정 (5분)

resendTimer?.cancel() // 기존 타이머 취소
resendTimer = viewModelScope.launch {

val startTime = System.currentTimeMillis()
val endTime = startTime + resendTime * 1000

// 타이머 시작
_timerUiState.value =
_timerUiState.value.copy(resendTime = resendTime)

while (System.currentTimeMillis() < endTime) {
val remainingMillis = endTime - System.currentTimeMillis()
val remainingSeconds = (remainingMillis / 1000).coerceAtLeast(0)

// 상태 업데이트
_timerUiState.value = _timerUiState.value.copy(resendTime = remainingSeconds)
Log.d(
TAG,
"시간: ${_timerUiState.value.resendTime}"
private fun timerResendStart() {
resendTimer?.cancel()
resendTimer = object : CountDownTimer(60_000L, 1000L) {
override fun onTick(millisUntilFinished: Long) {
_timerUiState.value = _timerUiState.value.copy(
isTimerRunning = true,
resendTime = millisUntilFinished / 1000
)

delay(1000L)
Log.d(TAG, "재전송: ${millisUntilFinished / 1000}")
}

// 타이머 종료 처리
_timerUiState.value = _timerUiState.value.copy(
resendTime = 0L,
)
}
override fun onFinish() {
// 타이머 종료 처리
_timerUiState.value = _timerUiState.value.copy(
resendTime = 0L,
)
}
}.start()
}

fun resetVerifyCodeState() {
Expand All @@ -366,7 +349,7 @@ data class SignupUiState(
val username: String = "", // 닉네임
val password: String = "", // 비밀번호
val email: String = "", // 이메일
val verifyCode: String = "", // 인증번호
var verifyCode: String = "", // 인증번호
val token: String = "", // 회원가입 토큰

val usernameButtonState: Boolean = false,
Expand Down