Skip to content
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] 7주차 필수 과제 #20

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.sopt.dosopttemplate.presentation.auth



import android.content.Context
import android.content.Intent
import android.os.Bundle
Expand All @@ -10,13 +9,15 @@ import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import org.sopt.dosopttemplate.databinding.ActivityLoginBinding
import org.sopt.dosopttemplate.di.UserSharedPreferences
import org.sopt.dosopttemplate.presentation.BnvActivity
import org.sopt.dosopttemplate.server.auth.LoginResp
import org.sopt.dosopttemplate.util.showShortSnackBar

class LoginActivity : AppCompatActivity() {

private lateinit var binding: ActivityLoginBinding
private var inputMethodManager: InputMethodManager? = null
private lateinit var loginViewModel: LoginViewModel
Expand All @@ -42,17 +43,32 @@ class LoginActivity : AppCompatActivity() {
val userId = binding.etSignupId.text.toString()
val userPw = binding.etSignupPw.text.toString()

loginViewModel.performLogin(
userId,
userPw,
{ loginResp -> handleLoginSuccess(loginResp) },
{ showShortSnackBar(binding.root, "로그인 실패") },
{ showShortSnackBar(binding.root, "네트워크 오류") }
)
loginViewModel.performLogin(userId, userPw)
}

inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?

observeLoginResult()
}

private fun observeLoginResult() {
lifecycleScope.launchWhenStarted {
loginViewModel.loginResult.collect { result ->
when (result) {
is Result.Success -> {
result.data.let { handleLoginSuccess(it) }
}

is Result.Error -> {
showShortSnackBar(binding.root, "에러 발생: ${result.exception.message}")
}

else -> {}
}
}
}
}

private fun handleLoginSuccess(loginResp: LoginResp) {
UserSharedPreferences.setLoggedIn(this, true)
UserSharedPreferences.setUserID(this, loginResp.id.toString())
Expand All @@ -62,15 +78,22 @@ class LoginActivity : AppCompatActivity() {
val toastMessage = "로그인에 성공했어요! USER의 ID는 $userId 입니둥."
Toast.makeText(this, toastMessage, Toast.LENGTH_SHORT).show()


val intent = Intent(this, BnvActivity::class.java)
intent.putExtra("ID", userId)
intent.putExtra("Nickname", loginResp.nickname)
startActivity(intent)
finish()
}

private fun handleEmptyLoginSuccess() {
val toastMessage = "로그인에 성공했어요!"
Toast.makeText(this, toastMessage, Toast.LENGTH_SHORT).show()
val intent = Intent(this, BnvActivity::class.java)
startActivity(intent)
finish()
}

fun hideKeyboard(v: View) {
inputMethodManager?.hideSoftInputFromWindow(v.windowToken, 0)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,31 @@ package org.sopt.dosopttemplate.presentation.auth

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.sopt.dosopttemplate.server.ServicePool
import org.sopt.dosopttemplate.server.auth.LoginReq
import org.sopt.dosopttemplate.server.auth.LoginResp


class LoginViewModel : ViewModel() {

/*
fun login(id: String, password: String) {
viewModelScope.launch {
try {
val response = authService.login(RequestLoginDto(id, password))
if (response.isSuccessful) {
loginResult.value = response.body()
loginSuccess.value = true
} else {
loginSuccess.value = false
}
} catch (e: Exception) {
// TODO: 에러 처리 로직
}
}
}*/
private val _loginResult = MutableStateFlow<Result<LoginResp>>(Result.Idle)
val loginResult = _loginResult.asStateFlow()

fun performLogin(
userId: String,
userPw: String,
onSuccess: (LoginResp) -> Unit,
onFailure: () -> Unit,
onNetworkError: () -> Unit
userPw: String
) {
viewModelScope.launch {
try {
val loginReq = LoginReq(userId, userPw)
val response = ServicePool.authService.login(loginReq)
if (response.isSuccessful) {
val loginResp = response.body()
if (loginResp != null) {
onSuccess(loginResp)
} else {
onFailure()
}
} else {
onFailure()
}

_loginResult.value = Result.Success(
ServicePool.authService.login(loginReq).body()
?: throw Exception("값을 입력해주세요.")
)
} catch (e: Exception) {
onNetworkError()
_loginResult.value = Result.Error(e)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.sopt.dosopttemplate.presentation.auth

sealed class Result<out T> {
object Idle : Result<Nothing>()
data class Success<out T>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()
}