Skip to content

Commit

Permalink
[mod] #18 signup repository 패턴적용
Browse files Browse the repository at this point in the history
  • Loading branch information
cacaocoffee committed Jun 7, 2024
1 parent 8339904 commit ad8381d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sopt.now.data.dto.request

import com.sopt.now.data.model.User
import com.sopt.now.domain.entity.request.AuthRequestModel
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -16,5 +17,14 @@ data class RequestSignUpDto(
val phone: String,
) {
fun toUserWithUserId(userid: String): User =
User(id = authenticationId, password = password, nickname = nickname, phoneNumber = phone, userId = userid)
User(
id = authenticationId,
password = password,
nickname = nickname,
phoneNumber = phone,
userId = userid
)

fun toAuthRequestModel(): AuthRequestModel =
AuthRequestModel(authenticationId, password, nickname, phone)
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/sopt/now/ui/signup/SignUpActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.sopt.now.util.UiState
import com.sopt.now.util.toast

class SignUpActivity : BindingActivity<ActivitySignUpBinding>(R.layout.activity_sign_up) {
private val viewModel by viewModels<SignUpViewModel>()
private val viewModel by viewModels<SignUpViewModel> { SignUpViewModelFactory() }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
44 changes: 23 additions & 21 deletions app/src/main/java/com/sopt/now/ui/signup/SignUpViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopt.now.data.api.ServicePool
import com.sopt.now.data.dto.request.RequestSignUpDto
import com.sopt.now.data.model.User
import com.sopt.now.domain.repository.AuthRepository
import com.sopt.now.util.UiState
import kotlinx.coroutines.launch
import org.json.JSONObject
import retrofit2.HttpException

class SignUpViewModel : ViewModel() {
private val authService by lazy { ServicePool.authService }

class SignUpViewModel(private val authRepository: AuthRepository) : ViewModel() {
private val _signUpState = MutableLiveData<UiState<User>>()
val signUpState = _signUpState

fun signUp(request: RequestSignUpDto) {
_signUpState.value = UiState.Loading
viewModelScope.launch {
runCatching {
authService.signUp(request)
}.onSuccess { response ->
if (response.isSuccessful)
_signUpState.value =
UiState.Success(request.toUserWithUserId(response.headers()["userid"].toString()))
else {
val errorMessage =
JSONObject(response.errorBody()?.string()).getString("message")
_signUpState.value = UiState.Error(errorMessage.toString())
}
}.onFailure { e ->
if (e is HttpException) {
_signUpState.value = UiState.Error(e.message())
} else {
_signUpState.value = UiState.Error(e.message.toString())
authRepository.signUp(request.toAuthRequestModel())
.onSuccess { response ->
if (response.isSuccessful)
_signUpState.value =
UiState.Success(request.toUserWithUserId(response.headers()[LOCATION].toString()))
else {
val errorMessage =
JSONObject(response.errorBody()?.string()).getString(MESSAGE)
_signUpState.value = UiState.Error(errorMessage.toString())
}
}.onFailure { e ->
if (e is HttpException) {
_signUpState.value = UiState.Error(e.message())
} else {
_signUpState.value = UiState.Error(e.message.toString())
}
}
}
}
}

companion object {
const val MESSAGE = "message"
const val LOCATION = "location"
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/sopt/now/ui/signup/SignUpViewModelFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sopt.now.ui.signup

import SignUpViewModel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.sopt.now.data.AuthRepositoryImpl
import com.sopt.now.data.api.ServicePool

class SignUpViewModelFactory : ViewModelProvider.Factory{
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(SignUpViewModel::class.java)) {
val repository =
AuthRepositoryImpl(ServicePool.authService)
return SignUpViewModel(repository) as T
} else {
throw IllegalArgumentException("Failed to create ViewModel: ${modelClass.name}")
}
}
}

0 comments on commit ad8381d

Please sign in to comment.