Skip to content
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
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ android {
}
buildFeatures {
viewBinding = true
dataBinding = true

//μ‚¬μš©ν•  객체 κ°€μ Έμ˜€κΈ° (base_url κ°μΆ”λŠ” μž‘μ—…)
buildConfig = true
Expand Down Expand Up @@ -81,5 +82,6 @@ dependencies {
//이미지 λ‘œλ“œ
implementation 'io.coil-kt:coil:2.4.0'


//코루틴
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
}
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
@@ -0,0 +1,16 @@
package org.sopt.dosopttemplate.data.dataclass

object UserInfo {
var userInfoList = User(
nick = "",
self_description = "",
id = "",
pw = "",
mbti = "",
Comment on lines +5 to +9
Copy link
Contributor

Choose a reason for hiding this comment

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

λ””ν΄νŠΈ κ°’μœΌλ‘œ ""λ₯Ό μ€€ 이유λ₯Ό ν•œ 번 μƒκ°ν•΄λ³΄μ‹œλ©΄ 쒋을 것 κ°™μ•„μš”!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

λ””ν΄λ“œ 값을 μ£ΌκΈ΄μ€˜μ•Όλ˜λŠ”λ° λ””ν΄νŠΈκ°’μœΌλ‘œ λ„£μ„λ§Œν•œ 값을 λ”±νžˆ λͺ¨λ₯΄κ² μ–΄μ„œ ""둜 ν‰μΉœκ±΄λ° 보톡 일반적으둜 μ‚¬μš©λ˜λŠ” λ””ν΄λ“œκ°’μ΄ μžˆμ„κΉŒμš”??

Copy link
Contributor

Choose a reason for hiding this comment

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

μ™œ κΌ­ λ””ν΄νŠΈ 값을 μ£Όμ–΄μ•Ό ν• μ§€ nonNull을 μ£Όλ©΄ μ•ˆλ˜λŠ”μ§€ 생각해보면 쒋을 것 κ°™μ•„μ„œμš” 닡은 ν”„λ‘œμ νŠΈμ˜ 성격에 따라 λ‹€λ¦…λ‹ˆλ‹€ string의 기본값은 ""을 λŒ€λΆ€λΆ„ μ‚¬μš©ν•΄μš”

)

fun toUser(inId: String, inPw: String) {
userInfoList.id = inId
userInfoList.pw = inPw
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import kotlinx.serialization.Serializable
data class RequestLoginDto (
@SerialName("username")
val username: String,

@SerialName("password")
val password: String,
)
11 changes: 6 additions & 5 deletions app/src/main/java/org/sopt/dosopttemplate/module/AuthService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.sopt.dosopttemplate.data.dto.request.RequestUserDataDto
import org.sopt.dosopttemplate.data.dto.response.ResponseLoginDto
import retrofit2.Call
import org.sopt.dosopttemplate.data.dto.response.ResponseUserDataDto
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
Expand All @@ -14,17 +15,17 @@ import retrofit2.http.Path
interface AuthService {

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

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

@GET("api/v1/members/{memberId}")
fun getUserInfo(
@Path("memberId") memberId: Int
): Call<ResponseUserDataDto>
): Response<ResponseUserDataDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@ package org.sopt.dosopttemplate.presentation.login
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import org.sopt.dosopttemplate.databinding.ActivityLoginBinding
import org.sopt.dosopttemplate.presentation.mainhome.MainHomeActivity
import org.sopt.dosopttemplate.module.ServicePool.authService
import org.sopt.dosopttemplate.presentation.mainhome.MainHomeViewModel
import org.sopt.dosopttemplate.presentation.signUp.SignUpActivity
import org.sopt.dosopttemplate.databinding.ActivityLoginBinding
import org.sopt.dosopttemplate.data.dto.request.RequestLoginDto
import org.sopt.dosopttemplate.data.dto.response.ResponseLoginDto
import org.sopt.dosopttemplate.utils.toast
import org.sopt.dosopttemplate.utils.snackbar
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class LoginActivity : AppCompatActivity() {

private lateinit var binding: ActivityLoginBinding
private lateinit var loginViewModel: LoginViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)

loginViewModel = ViewModelProvider(this).get(LoginViewModel::class.java)

initLoginClickListener()
// val receivedUserInfoList = intent.getStringArrayListExtra("userInfoList")!!
Expand Down Expand Up @@ -63,22 +62,43 @@ class LoginActivity : AppCompatActivity() {
//νšŒμ›κ°€μž… νŽ˜μ΄μ§€λ‘œ 이동
val intent = Intent(this, SignUpActivity::class.java)
startActivity(intent)

}

}

private fun initLoginClickListener() {
with(binding) {
val id = binding.etLoginId.text.toString()
val password = binding.etLoginPw.text.toString()
binding.btLogin.setOnClickListener {
checkLoginAvailableFromServer(id, password)
loginViewModel.checkLoginFromServer(id, password)
checkLoginAvailableFromServer()
}
}
}

private fun checkLoginAvailableFromServer(id: String, password: String) {
private fun checkLoginAvailableFromServer() {
lifecycleScope.launch {
loginViewModel.loginState.collect { loginState ->
when (loginState) {
is LoginState.Success -> {
toast("둜그인 성곡")
val intent = Intent(this@LoginActivity, MainHomeActivity::class.java)
startActivity(intent)
}

is LoginState.Error -> {
toast("둜그인 μ‹€νŒ¨")
}

is LoginState.Loading -> {}
Copy link
Contributor

Choose a reason for hiding this comment

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

이 StateλŠ” μ–΄λ–€ 역할을 ν•˜κ³  μžˆλŠ”κ±΄κ°€μš”?


else -> {}
}
}
}
}

/*
authService.postLogin(RequestLoginDto(id, password))
.enqueue(object : Callback<ResponseLoginDto> {
override fun onResponse(
Expand All @@ -88,7 +108,7 @@ class LoginActivity : AppCompatActivity() {
if (response.isSuccessful) {
val data: ResponseLoginDto = requireNotNull(response.body()!!)
val userId: Int = data.id
toast("둜그인 성곡, μœ μ €μ˜ IDλŠ” $userId μž…λ‹ˆλ‹€")
toast("둜그인 성곡")
val intent = Intent(this@LoginActivity, MainHomeActivity::class.java)
intent.putExtra("id", userId)
startActivity(intent)
Expand All @@ -101,7 +121,7 @@ class LoginActivity : AppCompatActivity() {
toast("μ„œλ²„ μ—λŸ¬ λ°œμƒ")
}
})
}
*/
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.sopt.dosopttemplate.presentation.login

import org.sopt.dosopttemplate.data.dto.response.ResponseLoginDto

sealed class LoginState {
data class Success(val userId: ResponseLoginDto) : LoginState()
object Loading : LoginState()
object Error : LoginState()
object Unstarted : LoginState()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.sopt.dosopttemplate.presentation.login

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
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.data.dataclass.UserInfo
import org.sopt.dosopttemplate.data.dto.request.RequestLoginDto
import org.sopt.dosopttemplate.module.ApiFactory
import org.sopt.dosopttemplate.module.AuthService

class LoginViewModel() : ViewModel() {
private val _loginSuccess: MutableLiveData<Boolean> = MutableLiveData()
var loginSuccess: LiveData<Boolean> = _loginSuccess

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


fun checkLoginFromServer(id: String, password: String) {
viewModelScope.launch {
val loginService = ApiFactory.create<AuthService>()
_loginState.value = LoginState.Loading
kotlin.runCatching {
loginService.postLogin(RequestLoginDto(id, password))
}.onSuccess {
val body = it.body()
if (it.body() != null) {
_loginState.value = LoginState.Success(requireNotNull(body))
UserInfo.toUser(id, password)

} else {
_loginState.value = LoginState.Error
}
}.onFailure {
_loginState.value = LoginState.Error
}

///*
// ServicePool.authService.postLogin(RequestLoginDto(id, password))
// .enqueue(object : Callback<ResponseLoginDto> {
// override fun onResponse(
// call: Call<ResponseLoginDto>,
// response: Response<ResponseLoginDto>,
// ) {
// if (response.isSuccessful) {
// val data: ResponseLoginDto = requireNotNull(response.body()!!)
//
//
// val userId: Int = data.id
// loginSuccess.value = true
// toast("둜그인 성곡")
// val intent = Intent(context, MainHomeActivity::class.java)
// intent.putExtra("id", userId)
//
//
// startActivity(intent)
// } else {
Comment on lines +43 to +62
Copy link
Member

Choose a reason for hiding this comment

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

μ‹¬ν™”κ³Όμ œν•˜κ³  좔후에 주석은 μ§€μ›Œμ£Όμ„Έμš”!

Copy link
Contributor

Choose a reason for hiding this comment

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

λ™μ˜ν•©λ‹ˆλ‹Ή!

// toast("아이디와 νŒ¨μŠ€μ›Œλ“œκ°€ 일치 ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.")
// }
// }
//
// override fun onFailure(call: Call<ResponseLoginDto>, t: Throwable) {
// toast("μ„œλ²„ μ—λŸ¬ λ°œμƒ")
// }
// })
//*/

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ class MainHomeActivity : AppCompatActivity() {

private fun clickBottomNavigation() {
var userId = intent.getIntExtra("id", -1)

mainHomeViewModel.userId=userId
binding.bnvHome.setOnItemSelectedListener {
mainHomeViewModel.clickBottomNavigation(it.itemId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import org.sopt.dosopttemplate.MyApplication
import org.sopt.dosopttemplate.data.dataclass.Friend
import org.sopt.dosopttemplate.R
import org.sopt.dosopttemplate.data.dataclass.User
import org.sopt.dosopttemplate.data.dataclass.UserInfo
import org.sopt.dosopttemplate.data.dto.request.RequestUserDataDto
import org.sopt.dosopttemplate.data.dto.response.ResponseUserDataDto
import org.sopt.dosopttemplate.module.ServicePool.authService
Expand All @@ -19,10 +22,13 @@ import retrofit2.Response

class MainHomeViewModel : ViewModel() {
private val _navigateTo = MutableLiveData<Fragment>()
val navigateTo: LiveData<Fragment>
get() = _navigateTo
val navigateTo: LiveData<Fragment> get() = _navigateTo
Comment on lines 24 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

ν˜Ήμ‹œ 이 μ½”λ“œλŠ” μ–΄λ–€ λͺ©μ μœΌλ‘œ μ‚¬μš© λ˜λŠ” 것 μΌκΉŒμš”??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

이 μΉœκ΅¬λŠ” μ•„λ§ˆ ν•˜λ‹¨ λ„€λΉ„κ²Œμ΄μ…˜ 뢀뢄이 ν„°μΉ˜ 인식됐을 λ•Œ, viewModelμ—μ„œ μ–΄λ–€ fragmentλ₯Ό λ³΄μ—¬μ€˜μ•Όλ˜λŠ”μ§€ naviagetTo에 μ €μž₯ν•˜κ³ , activityμ—μ„œ navigateTo값을 observeν•΄μ„œ fragmentλ₯Ό λ°”κΎΈλŠ” λͺ©μ μœΌλ‘œ μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€!

Copy link
Contributor

Choose a reason for hiding this comment

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

viewModelμ—μ„œ μ–΄λ–€ ν”„λ ˆκ·Έλ¨ΌνŠΈλ‘œ 이동할지 μ•Œ ν•„μš”κ°€ μžˆμ„κΉŒμš”?
ν”„λ ˆκ·Έλ¨ΌνŠΈ μ „ν™˜μ€ viewλž‘ viewModel쀑에 λˆ„κ°€ μ±…μž„μ„ κ°€μ§€κ³  μžˆμ–΄μ•Όν• κΉŒμš”? MVVM에 λŒ€ν•΄μ„œ κ³΅λΆ€ν•΄λ³΄μ‹œλ©΄ 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€.


private var _userId: MutableLiveData<String> = MutableLiveData<String>()
val userId: LiveData<String> = _userId
val userPw: MutableLiveData<String> = MutableLiveData<String>()


var userId: Int = 0
fun clickBottomNavigation(itemId: Int): Boolean {

when (itemId) {
Expand All @@ -37,7 +43,7 @@ class MainHomeViewModel : ViewModel() {
}

R.id.menu_mypage -> {
myPageUserInfo(userId)
myPageUserInfo()
_navigateTo.value = MyPageFragment()
return true
}
Expand All @@ -52,29 +58,9 @@ class MainHomeViewModel : ViewModel() {
}


private fun myPageUserInfo(userId: Int) {
authService.getUserInfo(userId)
.enqueue(object : retrofit2.Callback<ResponseUserDataDto> {
override fun onResponse(
call: Call<ResponseUserDataDto>,
response: Response<ResponseUserDataDto>,
) {
if (response.isSuccessful) {
val data: ResponseUserDataDto = requireNotNull(response.body())
val userNickname = data.nickname
val userUsername = data.username

//sharedpreference둜 λ„˜κΈ°κΈ°
MyApplication.prefs.setString("nick", userNickname)
MyApplication.prefs.setString("username", userUsername)
MyApplication.prefs.setString("id", userId.toString())
}
}

override fun onFailure(call: Call<ResponseUserDataDto>, t: Throwable) {
_errorMsg.value = "μ„œλ²„ μ—λŸ¬ λ°œμƒ"
}
})
private fun myPageUserInfo() {
_userId.value = UserInfo.userInfoList.id
userPw.value = UserInfo.userInfoList.pw
Copy link
Contributor

Choose a reason for hiding this comment

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

μ–˜λ„ _userPw λ§Œλ“€μ–΄μ„œ μ²˜λ¦¬ν•˜λ©΄ 쒋을 것 κ°™μ•„μš”!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

근데 userPwλŠ” 이 λ·°λͺ¨λΈμ—μ„œ User둜 κ°’ 볡뢙 ν•΄μ£ΌλŠ”κ±° 밖에 μ•ˆν•˜λŠ”λ° κ·Έλž˜λ„ _userPw λ§Œλ“€μ–΄ λ†“λŠ”κ²Œ λ‚˜μ„κΉŒμš”??

Copy link
Contributor

Choose a reason for hiding this comment

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

볡뢙밖에 μ•ˆν•œλ‹€λ©΄ private둜 λ§Œλ“œλŠ”κ²Œ μ’‹μ§€ μ•Šμ„κΉŒμš”?

}

private var _errorMsg = MutableLiveData<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import org.sopt.dosopttemplate.MyApplication
import org.sopt.dosopttemplate.databinding.FragmentMypageBinding
import org.sopt.dosopttemplate.presentation.mainhome.MainHomeViewModel

class MyPageFragment : Fragment() {
private var _binding: FragmentMypageBinding? = null
private val binding: FragmentMypageBinding get() = requireNotNull(_binding!!)
private val binding: FragmentMypageBinding get() = requireNotNull(_binding)
private val viewModel by viewModels<MainHomeViewModel>()

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -38,6 +41,7 @@ class MyPageFragment : Fragment() {
}

override fun onDestroyView() {
_binding = null
super.onDestroyView()
}
}
Loading