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
02cbded
commit d635e6f
Showing
3 changed files
with
93 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.sopt.now.test.data | ||
|
||
data class BaseState( | ||
val isSuccess: Boolean, | ||
val message: String | ||
) |
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
59 changes: 59 additions & 0 deletions
59
app/src/main/java/com/sopt/now/test/presentation/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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.sopt.now.test.presentation | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import com.google.gson.Gson | ||
import com.sopt.now.test.data.ServicePool | ||
import com.sopt.now.test.data.BaseState | ||
import com.sopt.now.test.data.dto.request.RequestSignUpDto | ||
import com.sopt.now.test.data.dto.response.ResponseAuthDto | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
class SignUpViewModel : ViewModel() { | ||
private val authService by lazy { ServicePool.authService } | ||
val liveData = MutableLiveData<BaseState>() | ||
|
||
fun signUp(request: RequestSignUpDto) { | ||
authService.signUp(request).enqueue(object : Callback<ResponseAuthDto> { | ||
override fun onResponse( | ||
call: Call<ResponseAuthDto>, | ||
response: Response<ResponseAuthDto>, | ||
) { | ||
if (response.isSuccessful) { | ||
val data: ResponseAuthDto? = response.body() | ||
val userId = response.headers()["location"] | ||
liveData.value = BaseState( | ||
isSuccess = true, | ||
message = "회원가입 성공 유저의 ID는 $userId 입니다." | ||
) | ||
Log.d("SignUp", "data: $data, userId: $userId") | ||
} else { | ||
val error = response.errorBody()?.string() | ||
val gson = Gson() | ||
try { | ||
val errorResponse = gson.fromJson(error, ResponseAuthDto::class.java) | ||
liveData.value = BaseState( | ||
isSuccess = false, | ||
message = "회원가입 실패: ${errorResponse.message}" // 에러 메시지 사용 | ||
) | ||
} catch (e: Exception) { | ||
liveData.value = BaseState( | ||
isSuccess = false, | ||
message = "회원가입 실패: 에러 메시지 파싱 실패" | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<ResponseAuthDto>, t: Throwable) { | ||
liveData.value = BaseState( | ||
isSuccess = false, | ||
message = "서버 에러" | ||
) | ||
} | ||
}) | ||
} | ||
} |