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/seminar7 essential #6

Open
wants to merge 3 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
3 changes: 1 addition & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.8.20'
id("kotlin-parcelize")
}

Properties properties = new Properties()
Expand All @@ -10,7 +11,6 @@ properties.load(project.rootProject.file('local.properties').newDataInputStream(
android {
namespace 'org.sopt.dosopttemplate'
compileSdk 33

defaultConfig {
applicationId "org.sopt.dosopttemplate"
minSdk 28
Expand Down Expand Up @@ -45,7 +45,6 @@ android {
}

dependencies {

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.5.0'
Expand Down
16 changes: 11 additions & 5 deletions app/src/main/java/org/sopt/dosopttemplate/AuthViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.map
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.sopt.dosopttemplate.Dto.RequestDto.RequestLoginDto
import org.sopt.dosopttemplate.Dto.RequestDto.RequestSignUpDto
Expand All @@ -16,6 +19,9 @@ import java.util.regex.Pattern

class AuthViewModel : ViewModel() {

private val _loginState = MutableStateFlow<LoginState>(LoginState.Loading)
val loginState: StateFlow<LoginState> = _loginState.asStateFlow()

private val loginResult: MutableLiveData<ResponseLoginDto> = MutableLiveData()

private val _loginSuccess: MutableLiveData<Boolean> = MutableLiveData()
Expand Down Expand Up @@ -73,14 +79,14 @@ class AuthViewModel : ViewModel() {
kotlin.runCatching {
authServiceLogin.login(RequestLoginDto(id, password))
}.onSuccess {
if (it.isSuccessful) {
loginResult.value = it.body()
loginSuccess.value = true
response ->
if (response != null && response.isSuccessful) {
_loginState.value = LoginState.Success(response.body()!!)
Comment on lines +82 to +84

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ•„κΉŒ μΉ΄ν†‘λ°©μ—μ„œ 무슨 μ΄μŠˆκ°€ μžˆμ—ˆλ˜κ±°μ£ ?! γ…‹γ…‹γ…‹γ…‹γ…‹

} else {
loginSuccess.value = false
_loginState.value = LoginState.Error
}
}.onFailure {
loginSuccess.value = false
_loginState.value = LoginState.Error
}
}
}
Expand Down
37 changes: 21 additions & 16 deletions app/src/main/java/org/sopt/dosopttemplate/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import org.sopt.dosopttemplate.databinding.ActivityLoginBinding
import org.sopt.dosopttemplate.home.HomeActivity
import org.sopt.dosopttemplate.model.UserInfo
Expand All @@ -18,7 +20,8 @@ class LoginActivity : AppCompatActivity() {
private lateinit var pw: String
private lateinit var nk: String
private lateinit var hm: String

private val authViewModel: AuthViewModel by viewModels()
private var userInfo: UserInfo? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
Expand All @@ -39,7 +42,6 @@ class LoginActivity : AppCompatActivity() {
)
}
}

binding.btnLoginSignup.setOnClickListener {
val intent = Intent(this, SignUpActivity::class.java)
getStringResult.launch(intent)
Expand All @@ -62,21 +64,24 @@ class LoginActivity : AppCompatActivity() {
}

private fun observerLoginResult() {
authViewModel.loginSuccess.observe(this) {
if (it) {
Toast.makeText(this, "둜그인 성곡", Toast.LENGTH_SHORT).show()
startActivity(
Intent(
this,
HomeActivity::class.java
).putExtra("userInfo", userInfo)
)
} else {
Toast.makeText(this, "둜그인 μ‹€νŒ¨", Toast.LENGTH_SHORT).show()
lifecycleScope.launch {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

잘 λ„£μ—ˆκ΅°μš” μ’‹μŠ΅λ‹ˆλ‹€!!

authViewModel.loginState.collect { loginState ->
when (loginState) {
is LoginState.Success -> {
Toast.makeText(this@LoginActivity, "둜그인 성곡", Toast.LENGTH_SHORT).show()
startActivity(Intent(this@LoginActivity, HomeActivity::class.java))
}

is LoginState.Error -> {
Toast.makeText(this@LoginActivity, "둜그인 μ‹€νŒ¨", Toast.LENGTH_SHORT).show()
}

is LoginState.Loading -> {
Toast.makeText(this@LoginActivity, "둜그인 쀑", Toast.LENGTH_SHORT).show()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 λ©”μ‹œμ§€ λ„ˆλ¬΄ μŠˆμŠˆμŠ‰ν•˜κ³  μ§€λ‚˜κ°ˆκ±°κ°™μ€λ° γ…‹γ…‹γ…‹γ…‹γ…‹

}
}
}
}

}
}


}
9 changes: 9 additions & 0 deletions app/src/main/java/org/sopt/dosopttemplate/LoginState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.dosopttemplate

import org.sopt.dosopttemplate.Dto.ResponseDto.ResponseLoginDto

sealed class LoginState {
object Loading : LoginState()
data class Success(val data: ResponseLoginDto) : LoginState()
object Error : LoginState()
}
17 changes: 7 additions & 10 deletions app/src/main/java/org/sopt/dosopttemplate/home/MyPageFragment.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.sopt.dosopttemplate.home

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import org.sopt.dosopttemplate.databinding.FragmentMypageBinding
import org.sopt.dosopttemplate.model.UserInfo
import org.sopt.dosopttemplate.util.parcelable

class MyPageFragment : Fragment() {
private var _binding: FragmentMypageBinding? = null
Expand All @@ -23,19 +23,16 @@ class MyPageFragment : Fragment() {
return binding.root
}

@SuppressLint("NewApi")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

//val receivedUserInfo = arguments?.getSerializable("userInfo") as? UserInfo
val receivedUserInfo =
activity?.intent?.getSerializableExtra("userInfo", UserInfo::class.java) as? UserInfo
?: return

val id = receivedUserInfo.id
val pw = receivedUserInfo.pw
val nk = receivedUserInfo.nk
val hm = receivedUserInfo.hm
val receivedUserInfo = activity?.intent?.extras?.parcelable<UserInfo>("userInfo")

val id = receivedUserInfo?.id
val pw = receivedUserInfo?.pw
val nk = receivedUserInfo?.nk
val hm = receivedUserInfo?.hm

binding.tvMypageUserId.text = id
binding.tvMypageUserNk.text = nk
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/org/sopt/dosopttemplate/model/UserInfo.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.sopt.dosopttemplate.model

import java.io.Serializable
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class UserInfo(
val id: String = "",
val pw: String = "",
val nk: String = "",
val hm: String = "",
) : Serializable
) : Parcelable
16 changes: 16 additions & 0 deletions app/src/main/java/org/sopt/dosopttemplate/util/ParcelabelExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.sopt.dosopttemplate.util

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Parcelable

inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? = when {
Build.VERSION.SDK_INT >= 33 -> getParcelableExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableExtra(key) as? T
}

inline fun <reified T : Parcelable> Bundle.parcelable(key: String): T? = when {
Build.VERSION.SDK_INT >= 33 -> getParcelable(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelable(key) as? T
}