Skip to content

Commit

Permalink
[REFACTOR/#8] viewModel 연결안된거 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
codingmy committed Dec 29, 2023
1 parent f624a80 commit 0ed0212
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/org/sopt/dosopttemplate/data/dataclass/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import org.sopt.dosopttemplate.R

data class User(
val profileImage: Int= R.drawable.pr_image,
var nick: String="",
val self_description: String="",
var id: String="",
var pw: String="",
var mbti: String="",
var nick: String,
val self_description: String,
var id: String,
var pw: String,
var mbti: String,

)
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import retrofit2.http.Path
interface AuthService {

@POST("api/v1/members/sign-in")
fun postLogin(
suspend fun postLogin(
@Body request: RequestLoginDto,
): Response<ResponseLoginDto>

@POST("api/v1/members")
fun postSignUp(
suspend fun postSignUp(
@Body request: RequestSignUpDto,
): Response<Unit>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import org.sopt.dosopttemplate.R
import org.sopt.dosopttemplate.databinding.ActivitySignupBinding
Expand All @@ -14,7 +15,7 @@ import java.util.regex.Pattern

class SignUpActivity : AppCompatActivity() {
private lateinit var binding: ActivitySignupBinding
private lateinit var signUpViewModel: SignUpViewModel
private val signUpViewModel by viewModels<SignUpViewModel>()

//false 일 때 가입 불가
//true 일 때 가입 가능
Expand All @@ -37,21 +38,19 @@ class SignUpActivity : AppCompatActivity() {
btSignupButton.setOnClickListener {
//가입 조건 확인
if (checkCondition() && signUpIdAvailable && signUpPwAvailable) {
signUp()

trySignUp()

observeSignUpResult()
//토스트 띄우기
val intent = Intent(this@SignUpActivity, LoginActivity::class.java)

toast("회원가입 성공")
// 도전과제 꼭 할거라서 남겨놨습니다! /*
// //유저정보 -> 리스트로 구성
// val userInfoList = UserInfoToListString()
//
// sendUserInfo(intent, userInfoList)
// */
//액티비티 이동
startActivity(intent)

} else {
toast("모든 정보를 입력해야합니다.")
Expand Down Expand Up @@ -226,17 +225,24 @@ class SignUpActivity : AppCompatActivity() {
// return userInfoList
// }

fun signUp() = with(binding) {
val id = etSignupId.text.toString();
val pw = etSignupPw.text.toString()
val nickname = etSignupNickname.text.toString()
fun trySignUp() {
with(binding) {
val id = etSignupId.text.toString();
val pw = etSignupPw.text.toString()
val nickname = etSignupNickname.text.toString()
signUpViewModel.signUp(id, pw, nickname)
}
}

btSignupButton.setOnClickListener {
while (signUpViewModel.signUpSuccess.value == null) {
fun observeSignUpResult() {
signUpViewModel.signUpSuccess.observe(this) {
if (it) {
toast("회원가입 성공!!")
startActivity(intent)
} else {
toast("가입 실패")
}
toast(signUpViewModel.signUpSuccess.value!!)
}
}

}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sopt.dosopttemplate.presentation.signUp

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand All @@ -10,18 +11,20 @@ import org.sopt.dosopttemplate.module.ApiFactory
import org.sopt.dosopttemplate.module.AuthService

class SignUpViewModel : ViewModel() {
private val _signUpSuccess: MutableLiveData<String> = MutableLiveData()
val signUpSuccess: LiveData<String> = _signUpSuccess
private var _signUpSuccess: MutableLiveData<Boolean> = MutableLiveData()
var signUpSuccess: LiveData<Boolean> = _signUpSuccess

suspend fun signUp(id: String, pw: String, nickname: String) {
fun signUp(id: String, pw: String, nickname: String) {
viewModelScope.launch {
val signUpService = ApiFactory.create<AuthService>()
kotlin.runCatching {
signUpService.postSignUp(RequestSignUpDto(id, pw, nickname))
}.onSuccess {
_signUpSuccess.value = "회원가입 성공"
_signUpSuccess.value = true
Log.e("서버", _signUpSuccess.value.toString())
}.onFailure {
_signUpSuccess.value = "서버 에러 발생"
Log.e("서버", "가입 불가")

}
}
}
Expand Down

0 comments on commit 0ed0212

Please sign in to comment.