generated from NOW-SOPT-ANDROID/now-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8339904
commit ad8381d
Showing
4 changed files
with
54 additions
and
23 deletions.
There are no files selected for viewing
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
44 changes: 23 additions & 21 deletions
44
app/src/main/java/com/sopt/now/ui/signup/SignUpViewModel.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,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
19
app/src/main/java/com/sopt/now/ui/signup/SignUpViewModelFactory.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,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}") | ||
} | ||
} | ||
} |