-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEAT/#19] Week6 / 심화 과제 #22
Changes from all commits
a594083
428b628
5bb053d
cae165a
50eec15
36ce63c
6e886a8
ecb6e57
d6e0f7a
acf6872
574b07a
d060b9a
8049716
c0e9b3a
c91896a
1504823
541335a
c37c4b0
d6841fc
1eecf85
b2135ad
49569bc
fa11e83
d00240b
363da8f
149c5b6
ce865f7
d17c116
140599e
213e623
c8ce94b
fe951ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
package org.sopt.dosopttemplate.data.service | ||
|
||
import org.sopt.dosopttemplate.data.remote.response.ResponsePeopleListDto | ||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
import retrofit2.http.Query | ||
|
||
interface PeopleService { | ||
|
||
// 사람들 리스트 받아오기 | ||
@GET("users") | ||
fun PeopleListGet( | ||
suspend fun PeopleListGet( | ||
@Query("page") page: Int, | ||
@Query("per_page") perPage: Int, | ||
): Call<ResponsePeopleListDto> | ||
): ResponsePeopleListDto | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,16 @@ import android.view.MotionEvent | |
import android.view.View | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import org.sopt.dosopttemplate.R | ||
import org.sopt.dosopttemplate.data.User | ||
import org.sopt.dosopttemplate.databinding.ActivityLoginBinding | ||
import org.sopt.dosopttemplate.di.UserSharedPreferences | ||
import org.sopt.dosopttemplate.presentation.main.BnvActivity | ||
import org.sopt.dosopttemplate.util.BackPressedUtil | ||
import org.sopt.dosopttemplate.util.UiState | ||
import org.sopt.dosopttemplate.util.hideKeyboard | ||
import org.sopt.dosopttemplate.util.showShortSnackBar | ||
import org.sopt.dosopttemplate.util.showShortToast | ||
|
||
class LoginActivity : AppCompatActivity() { | ||
private lateinit var binding: ActivityLoginBinding | ||
|
@@ -23,6 +27,13 @@ class LoginActivity : AppCompatActivity() { | |
super.onCreate(savedInstanceState) | ||
setContentView(binding.root) | ||
|
||
autoLogin() | ||
userClickSignUpBtn() | ||
userCickLoginBtn() | ||
backPressed() | ||
} | ||
|
||
private fun autoLogin() { | ||
// 자동 로그인으로 저장된 유저 정보 | ||
val spUser = UserSharedPreferences.getUser(this) | ||
|
||
|
@@ -33,45 +44,60 @@ class LoginActivity : AppCompatActivity() { | |
// 새로운 Activity를 수행하고 현재 Activity를 스텍에서 제거 | ||
startActivity(intent) | ||
} | ||
} | ||
|
||
// 회원가입 하러 가기 | ||
// 회원가입 하러 가기 | ||
private fun userClickSignUpBtn() { | ||
binding.btnSignupSignup.setOnClickListener { | ||
val intent = Intent(this, SignUpActivity::class.java) | ||
startActivity(intent) | ||
} | ||
} | ||
|
||
// 로그인 하기 | ||
// 로그인 하기 | ||
private fun userCickLoginBtn() { | ||
binding.btnLoginLogin.setOnClickListener { | ||
// 자동 로그인이 적용되지 않고, 회원가입에서 넘어온 경우 | ||
val signUpUser = intent.getParcelableExtra<User>("signUpUser") | ||
|
||
val inputId = binding.etSignupId.text.toString() | ||
val inputPw = binding.etSignupPw.text.toString() | ||
|
||
loginViewModel.loginUser(inputId, inputPw, this) | ||
loginViewModel.loginUser(inputId, inputPw) | ||
|
||
loginViewModel.loginResult.observe( | ||
loginViewModel.getLoginInfo.observe( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 옵저버는 onClickListener보다는 바깥쪽에 위치하는 게 더 좋을 것 같아요~! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 생각해보니 그렇네염.,,,,,맞슴니다 |
||
this, | ||
) { loginSuccessful -> | ||
if (loginSuccessful) { | ||
// showShortToast("getString(R.string.login_success)") | ||
|
||
if (binding.cbLoginAutologin.isChecked) { | ||
signUpUser?.let { | ||
loginViewModel.saveUserForAutoLogin(this, it) | ||
) { uiState -> | ||
when (uiState) { | ||
is UiState.Success -> { | ||
if (binding.cbLoginAutologin.isChecked) { | ||
signUpUser?.let { | ||
loginViewModel.saveUserForAutoLogin(this, it) | ||
Comment on lines
+70
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 UiSate 써보고 싶었는데,, 다음에 할 때 참고해야게써요 최곱니다,,, |
||
} | ||
} | ||
|
||
val userId = uiState.data?.userId | ||
showShortToast("로그인 성공, 유저 아이디 : $userId") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그냥 슥 봐서는 저는 두번 토스트 뜨는 이유가 안보이네욥... 로그 찍어봐야할듯 ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ㅋㅋㅋㅋㅋ 제가 버튼을 두번 누르는 이슈였습니다 ..ㅎㅎ^^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kangyuri1114 제 코드 중에 util에 setOnSingleClickListener 라구 있거덩요? 그거 가져와서 대신 쓰면 더블 클릭에 대응하지 않게 된답니다~~! |
||
|
||
val intent = Intent(this, BnvActivity::class.java) | ||
intent.putExtra("signUpUser", signUpUser) | ||
startActivity(intent) | ||
finish() | ||
} | ||
|
||
is UiState.Failure -> { | ||
showShortSnackBar(binding.root, "로그인 실패 : ${uiState.errorMessage}") | ||
} | ||
|
||
val intent = Intent(this, BnvActivity::class.java) | ||
intent.putExtra("signUpUser", signUpUser) | ||
startActivity(intent) | ||
finish() | ||
} else { | ||
// showShortSnackBar(binding.root, getString(R.string.login_fail)) | ||
is UiState.Loading -> { | ||
showShortSnackBar(binding.root, getString(R.string.uistate_loading)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun backPressed() { | ||
val backPressedUtil = BackPressedUtil<ActivityLoginBinding>(this) | ||
backPressedUtil.BackButton() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,35 @@ | ||
package org.sopt.dosopttemplate.presentation.auth | ||
|
||
import android.content.Context | ||
import android.widget.Toast | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.launch | ||
import org.sopt.dosopttemplate.data.User | ||
import org.sopt.dosopttemplate.data.remote.ServicePool.authService | ||
import org.sopt.dosopttemplate.data.remote.ServicePool | ||
import org.sopt.dosopttemplate.data.remote.request.RequestLoginDto | ||
import org.sopt.dosopttemplate.data.remote.response.ResponseLoginDto | ||
import org.sopt.dosopttemplate.di.UserSharedPreferences | ||
import retrofit2.Call | ||
import retrofit2.Response | ||
import org.sopt.dosopttemplate.util.UiState | ||
|
||
class LoginViewModel : ViewModel() { | ||
private val _loginResult = MutableLiveData<Boolean>() | ||
val loginResult: LiveData<Boolean> get() = _loginResult | ||
|
||
fun loginUser(inputId: String, inputPw: String, context: Context) { | ||
// _loginResult.value = | ||
// signUpUser != null && signUpUser.userId == inputId && signUpUser.userPw == inputPw | ||
|
||
authService.login(RequestLoginDto(inputId, inputPw)) | ||
.enqueue(object : retrofit2.Callback<ResponseLoginDto> { | ||
override fun onResponse( | ||
call: Call<ResponseLoginDto>, | ||
response: Response<ResponseLoginDto>, | ||
) { | ||
if (response.isSuccessful) { | ||
val data: ResponseLoginDto = | ||
response.body() ?: ResponseLoginDto(-1, "null", "null") | ||
val userId = data.id | ||
|
||
Toast.makeText( | ||
context, | ||
"로그인이 성공하였고 유저의 ID는 $userId 입니둥", | ||
Toast.LENGTH_SHORT, | ||
).show() | ||
_loginResult.value = true | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<ResponseLoginDto>, t: Throwable) { | ||
Toast.makeText( | ||
context, | ||
"ㅜ ㅜ 서버 에러 발생 ㅜ ㅜ", | ||
Toast.LENGTH_SHORT, | ||
).show() | ||
|
||
_loginResult.value = false | ||
} | ||
}) | ||
// 로그인 response에서 받아온 유저 정보 | ||
private val _getLoginInfo = MutableLiveData<UiState<User>>() | ||
val getLoginInfo: LiveData<UiState<User>> get() = _getLoginInfo | ||
Comment on lines
+18
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 변수명이 get으로 시작하면 너무 함수명같지 않나요..? ㅎㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저 이제 변수명 정하는거 오류났어요 |
||
|
||
fun loginUser(inputId: String, inputPw: String) = viewModelScope.launch { | ||
_getLoginInfo.value = UiState.Loading | ||
|
||
runCatching { | ||
ServicePool.authService.login( | ||
RequestLoginDto(inputId, inputPw), | ||
) | ||
}.onSuccess { | ||
_getLoginInfo.value = UiState.Success(User(it.id.toString(), it.username, it.nickname)) | ||
}.onFailure { | ||
_getLoginInfo.value = UiState.Failure(it.message.toString()) | ||
} | ||
} | ||
|
||
fun saveUserForAutoLogin(context: Context, signUpUser: User) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,8 @@ import android.os.Bundle | |
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import org.sopt.dosopttemplate.R | ||
import org.sopt.dosopttemplate.data.User | ||
import org.sopt.dosopttemplate.databinding.ActivitySignupBinding | ||
import org.sopt.dosopttemplate.util.UiState | ||
import org.sopt.dosopttemplate.util.showShortSnackBar | ||
import org.sopt.dosopttemplate.util.showShortToast | ||
|
||
|
@@ -19,29 +19,62 @@ class SignUpActivity : AppCompatActivity() { | |
super.onCreate(savedInstanceState) | ||
setContentView(binding.root) | ||
|
||
binding.lifecycleOwner = this | ||
binding.authViewModel = signUpViewModel | ||
|
||
observeValid() | ||
clickSignUpBtn() | ||
} | ||
|
||
private fun observeValid() { | ||
signUpViewModel.idFlag.observe(this) { idFlag -> | ||
binding.telSignupId.error = if (idFlag) null else getString(R.string.id_layout_title) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오홍 이런식으로 if문 쓰는방법도 좋네요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 조장이 고쳐줬어염 ㅎㅅㅎ |
||
btnEnable() | ||
} | ||
signUpViewModel.pwFlag.observe(this) { pwFlag -> | ||
binding.telSignupPw.error = if (pwFlag) null else getString(R.string.pw_layout_title) | ||
btnEnable() | ||
} | ||
signUpViewModel.nicknameFlag.observe(this) { nicknameFlag -> | ||
binding.telSignupNickname.error = | ||
if (nicknameFlag) null else getString(R.string.nickname_layout_title) | ||
btnEnable() | ||
} | ||
} | ||
|
||
private fun btnEnable() { | ||
signUpViewModel.signUpBtnFlag() | ||
binding.btnSignupSignup.isEnabled = signUpViewModel.signUpBtnFlag.value == true | ||
if (binding.btnSignupSignup.isEnabled) { | ||
signUpViewModel.onUserTextSizeChanged(40) | ||
} else { | ||
signUpViewModel.onUserTextSizeChanged(10) | ||
} | ||
} | ||
|
||
private fun clickSignUpBtn() { | ||
binding.btnSignupSignup.setOnClickListener { | ||
val signUpUserId = binding.etSignupId.text.toString() | ||
val signUpUserPw = binding.etSignupPw.text.toString() | ||
val signUpUserNickname = binding.etSignupNickname.text.toString() | ||
// val signUpUserAge = binding.etSignupAge.text.toString() | ||
|
||
val signUpUser = User(signUpUserId, signUpUserPw, signUpUserNickname) | ||
|
||
signUpViewModel.signUpUser(signUpUser, this) | ||
|
||
signUpViewModel.signUpResult.observe(this) { signUpSuccessful -> | ||
if (signUpSuccessful) { | ||
// 화면 전환 | ||
showShortToast(getString(R.string.signup_success)) | ||
|
||
val intent = Intent(this, LoginActivity::class.java) | ||
intent.putExtra("signUpUser", signUpUser) | ||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | ||
startActivity(intent) | ||
} else { | ||
showShortSnackBar(binding.root, getString(R.string.signup_fail)) | ||
signUpViewModel.signUpResult.observe(this) { uiState -> | ||
when (uiState) { | ||
is UiState.Success -> { | ||
showShortToast(getString(R.string.signup_success)) | ||
val intent = Intent(this, LoginActivity::class.java) | ||
startActivity(intent) | ||
finish() | ||
} | ||
|
||
is UiState.Failure -> { | ||
showShortSnackBar(binding.root, "회원가입 실패 : ${uiState.errorMessage}") | ||
} | ||
|
||
is UiState.Loading -> { | ||
showShortSnackBar(binding.root, getString(R.string.uistate_loading)) | ||
} | ||
} | ||
} | ||
binding.btnSignupSignup.setOnClickListener { | ||
signUpViewModel.signUpUserApi() | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요 부분의 Call<>도 빼주셔도 될듯함다! 그 전에 Call이 여기서 어떤 역할을 했었고, 왜 이제 안 감싸주어도 작동이 되는지 공부해보면 더 좋을 듯 합니다 ~~