Skip to content

Commit

Permalink
#10 [feat] : SignUpApi 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsuhyun committed Dec 4, 2024
1 parent 2289982 commit 2ef0449
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 36 deletions.
5 changes: 0 additions & 5 deletions app/src/main/java/org/sopt/and/presentation/main/NavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ import org.sopt.and.R
import org.sopt.and.presentation.home.HomeScreen
import org.sopt.and.presentation.home.HomeViewModel
import org.sopt.and.presentation.login.LoginScreen
import org.sopt.and.presentation.login.LoginViewModel
import org.sopt.and.presentation.mypage.MyScreen
import org.sopt.and.presentation.mypage.MyViewModel
import org.sopt.and.presentation.search.SearchScreen
import org.sopt.and.presentation.search.SearchViewModel
import org.sopt.and.presentation.signup.SignUpScreen
import org.sopt.and.presentation.signup.SignUpViewModel
import org.sopt.and.utils.toast

@Composable
Expand Down Expand Up @@ -64,7 +61,6 @@ fun NavGraph(
popUpTo(Routes.Login.screen) { inclusive = true }
}
},
viewModel = SignUpViewModel()
)
}
composable(Routes.Home.screen) {
Expand All @@ -77,7 +73,6 @@ fun NavGraph(
composable(Routes.My.screen) {
MyScreen(
navController = navController,
viewModel = MyViewModel()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import org.sopt.and.R
import org.sopt.and.domain.model.request.UserSignUpModel
import org.sopt.and.presentation.core.component.AuthTextField
import org.sopt.and.presentation.core.component.ErrorDialog
import org.sopt.and.presentation.core.component.WavveSignUpButton
import org.sopt.and.data.remote.model.request.UserSignUpRequestDto
import org.sopt.and.ui.theme.WavveTheme
import org.sopt.and.utils.noRippleClickable

Expand All @@ -61,7 +61,7 @@ import org.sopt.and.utils.noRippleClickable
fun SignUpScreen(
navController: NavController,
onSignUpSuccess: (String, String) -> Unit,
viewModel: SignUpViewModel = viewModel()
viewModel: SignUpViewModel = hiltViewModel()
) {
val focusManager = LocalFocusManager.current
val dispatcher = LocalOnBackPressedDispatcherOwner.current!!.onBackPressedDispatcher
Expand Down Expand Up @@ -243,7 +243,7 @@ fun SignUpScreen(
onSuccess = { userName, password, hobby ->
viewModel.postUserSignUp(
context = context,
body = UserSignUpRequestDto(
body = UserSignUpModel(
username = userName,
password = password,
hobby = hobby
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/org/sopt/and/presentation/signup/SignUpState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.sopt.and.presentation.signup

import org.sopt.and.domain.model.response.UserNumber

sealed class SignUpState {
data object Idle : SignUpState()
data object Loading : SignUpState()
data class Success(val result: UserNumber) : SignUpState()
data class Failure(val message: String) : SignUpState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,55 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import org.sopt.and.R
import org.sopt.and.di.ServicePool
import org.sopt.and.data.remote.model.request.UserSignUpRequestDto
import org.sopt.and.data.remote.model.response.UserSignUpResponseDto
import org.sopt.and.domain.model.request.UserSignUpModel
import org.sopt.and.domain.repository.UserRepository
import org.sopt.and.utils.AuthKey.PASSWORD_PATTERN
import org.sopt.and.utils.toast
import retrofit2.HttpException
import java.io.IOException
import javax.inject.Inject

class SignUpViewModel : ViewModel() {
private val userService by lazy { ServicePool.userService }
@HiltViewModel
class SignUpViewModel @Inject constructor(
private val userRepository: UserRepository
) : ViewModel() {
private val _signUpState = mutableStateOf<SignUpState>(SignUpState.Idle)
val signUpState: State<SignUpState> get() = _signUpState

private val _userState = mutableStateOf<UserSignUpResponseDto?>(null)
val userState: State<UserSignUpResponseDto?> get() = _userState

fun postUserSignUp(context: Context, body: UserSignUpRequestDto) {
fun postUserSignUp(context: Context, body: UserSignUpModel) {
_signUpState.value = SignUpState.Loading
viewModelScope.launch {
runCatching {
userService.postUserSignUp(body = body)
}.onSuccess { response ->
_userState.value = response.result
}.onFailure { error ->
when (error) {
is HttpException -> {
when (error.code()) {
400 -> context.toast(context.getString(R.string.fail_to_signup_maximum_length))
409 -> context.toast(context.getString(R.string.fail_to_signup_duplicate_name))
val result = userRepository.postUserSignUp(
userSignUpModel = body
)
_signUpState.value = result.fold(
onSuccess = { response ->
Log.d("SignUpSuccess", response.no.toString())
SignUpState.Success(response)
},
onFailure = { error ->
when (error) {
is HttpException -> {
when (error.code()) {
400 -> context.toast(context.getString(R.string.fail_to_signup_maximum_length))
409 -> context.toast(context.getString(R.string.fail_to_signup_duplicate_name))
}
}
}

is IOException -> {
context.toast(context.getString(R.string.fail_to_network))
}
is IOException -> {
context.toast(context.getString(R.string.fail_to_network))
}

else -> {
context.toast(context.getString(R.string.fail_to_signup))
else -> {
context.toast(context.getString(R.string.fail_to_signup))
}
}
SignUpState.Failure(error.message.orEmpty())
}
Log.e("postUserSignUpError", error.toString())
}
)
}
}

Expand Down

0 comments on commit 2ef0449

Please sign in to comment.