From 9e8fb056de50fe8a5614c71f5b57e8c7824f0040 Mon Sep 17 00:00:00 2001 From: youjin09222 Date: Fri, 10 May 2024 19:14:58 +0900 Subject: [PATCH] =?UTF-8?q?refactor/#9:=20LoginViewModel=EC=97=90=EC=84=9C?= =?UTF-8?q?=20userPreference=20=EC=B4=88=EA=B8=B0=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sopt/now/test/presentation/LoginActivity.kt | 13 +++---------- .../sopt/now/test/presentation/LoginViewModel.kt | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/sopt/now/test/presentation/LoginActivity.kt b/app/src/main/java/com/sopt/now/test/presentation/LoginActivity.kt index 696a757..ceb5b38 100644 --- a/app/src/main/java/com/sopt/now/test/presentation/LoginActivity.kt +++ b/app/src/main/java/com/sopt/now/test/presentation/LoginActivity.kt @@ -14,14 +14,12 @@ class LoginActivity : AppCompatActivity() { private val binding by lazy { ActivityLoginBinding.inflate(layoutInflater) } private val viewModel by viewModels() - 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() @@ -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) } } diff --git a/app/src/main/java/com/sopt/now/test/presentation/LoginViewModel.kt b/app/src/main/java/com/sopt/now/test/presentation/LoginViewModel.kt index 07b2b41..1dd7001 100644 --- a/app/src/main/java/com/sopt/now/test/presentation/LoginViewModel.kt +++ b/app/src/main/java/com/sopt/now/test/presentation/LoginViewModel.kt @@ -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 @@ -14,7 +17,7 @@ import retrofit2.Response class LoginViewModel : ViewModel() { private val authService by lazy { ServicePool.authService } val liveData = MutableLiveData() - val userIdLiveData = MutableLiveData() + private lateinit var userPreference: UserPreference fun login(request: RequestLoginDto) { authService.login(request).enqueue(object : Callback { @@ -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 입니다." @@ -48,4 +53,10 @@ class LoginViewModel : ViewModel() { } }) } + + // 초기화 + fun initializeApiFactory(context: Context) { + userPreference = UserPreference(context) + ApiFactory.initializeUserPreference(userPreference) + } } \ No newline at end of file