Skip to content

Commit

Permalink
refactor/#9: LoginViewModel에서 userPreference 초기화
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 10, 2024
1 parent 07f388f commit 9e8fb05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
13 changes: 3 additions & 10 deletions app/src/main/java/com/sopt/now/test/presentation/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ class LoginActivity : AppCompatActivity() {

private val binding by lazy { ActivityLoginBinding.inflate(layoutInflater) }
private val viewModel by viewModels<LoginViewModel>()
private lateinit var userPreference: UserPreference

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)

userPreference = UserPreference(this)
ApiFactory.initializeUserPreference(userPreference)
viewModel.initializeApiFactory(this)

initObserver()
setupLoginButton()
Expand All @@ -39,15 +37,10 @@ class LoginActivity : AppCompatActivity() {
}

private fun initObserver() {
// 사용자 데이터 저장
viewModel.userIdLiveData.observe(this) { userId ->
userId?.let {
userPreference.saveUserId(userId)
viewModel.liveData.observe(this) { response ->
if(response.isSuccess){
moveToMain()
}
}

viewModel.liveData.observe(this) { response ->
showToast(response.message)
}
}
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/com/sopt/now/test/presentation/LoginViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.sopt.now.test.presentation

import android.content.Context
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.sopt.now.test.data.ApiFactory
import com.sopt.now.test.data.ServicePool
import com.sopt.now.test.data.BaseState
import com.sopt.now.test.data.UserPreference
import com.sopt.now.test.data.dto.request.RequestLoginDto
import com.sopt.now.test.data.dto.response.ResponseAuthDto
import retrofit2.Call
Expand All @@ -14,7 +17,7 @@ import retrofit2.Response
class LoginViewModel : ViewModel() {
private val authService by lazy { ServicePool.authService }
val liveData = MutableLiveData<BaseState>()
val userIdLiveData = MutableLiveData<String?>()
private lateinit var userPreference: UserPreference

fun login(request: RequestLoginDto) {
authService.login(request).enqueue(object : Callback<ResponseAuthDto> {
Expand All @@ -25,7 +28,9 @@ class LoginViewModel : ViewModel() {
if (response.isSuccessful) {
val data: ResponseAuthDto? = response.body()
val userId = response.headers()["location"]
userIdLiveData.value = userId
if (userId != null) {
userPreference.saveUserId(userId)
}
liveData.value = BaseState(
isSuccess = true,
message = "로그인 성공! 유저의 ID는 $userId 입니다."
Expand All @@ -48,4 +53,10 @@ class LoginViewModel : ViewModel() {
}
})
}

// 초기화
fun initializeApiFactory(context: Context) {
userPreference = UserPreference(context)
ApiFactory.initializeUserPreference(userPreference)
}
}

0 comments on commit 9e8fb05

Please sign in to comment.