From d91ec8a26aca807e464acf11c8fff1e35c1ea652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AA=85=EC=84=9D?= Date: Sat, 15 Jun 2024 06:16:13 +0900 Subject: [PATCH] =?UTF-8?q?[Feat]=20#18=20getinfo=20repository=20=ED=8C=A8?= =?UTF-8?q?=ED=84=B4=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/sopt/now/ui/ViewModelFactory.kt | 34 +++++++++++++------ .../java/com/sopt/now/ui/main/MainActivity.kt | 4 +-- .../com/sopt/now/ui/main/MainViewModel.kt | 29 +++++----------- .../ui/main/profile/MainProfileFragment.kt | 4 +-- 4 files changed, 36 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/sopt/now/ui/ViewModelFactory.kt b/app/src/main/java/com/sopt/now/ui/ViewModelFactory.kt index 7f45e47..34acbcc 100644 --- a/app/src/main/java/com/sopt/now/ui/ViewModelFactory.kt +++ b/app/src/main/java/com/sopt/now/ui/ViewModelFactory.kt @@ -1,27 +1,39 @@ package com.sopt.now.ui -import SignUpViewModel import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import com.sopt.now.data.repositoryimpl.AuthRepositoryImpl import com.sopt.now.data.api.ServicePool +import com.sopt.now.domain.usecase.GetUserInfoUseCase import com.sopt.now.domain.usecase.LogInUseCase import com.sopt.now.domain.usecase.SignUpUseCase import com.sopt.now.ui.login.LoginViewModel +import com.sopt.now.ui.main.MainViewModel +import com.sopt.now.ui.signup.SignUpViewModel class ViewModelFactory : ViewModelProvider.Factory { override fun create(modelClass: Class): T { - if (modelClass.isAssignableFrom(SignUpViewModel::class.java)) { - val repository = AuthRepositoryImpl(ServicePool.authService) - val signUpUseCase = SignUpUseCase(repository) - return SignUpViewModel(signUpUseCase) as T - } else if (modelClass.isAssignableFrom(LoginViewModel::class.java)) { - val repository = AuthRepositoryImpl(ServicePool.authService) - val loginUseCase = LogInUseCase(repository) - return LoginViewModel(loginUseCase) as T + when (modelClass) { + SignUpViewModel::class.java -> { + val repository = AuthRepositoryImpl(ServicePool.authService) + val signUpUseCase = SignUpUseCase(repository) + return SignUpViewModel(signUpUseCase) as T + } - } else { - throw IllegalArgumentException("Failed to create ViewModel: ${modelClass.name}") + LoginViewModel::class.java -> { + val repository = AuthRepositoryImpl(ServicePool.authService) + val loginUseCase = LogInUseCase(repository) + return LoginViewModel(loginUseCase) as T + } + + MainViewModel::class.java -> { + val repository = AuthRepositoryImpl(ServicePool.authService) + val getUserInfoUseCase = GetUserInfoUseCase(repository) + return MainViewModel(getUserInfoUseCase) as T + } + + else -> throw IllegalArgumentException("Failed to create ViewModel: ${modelClass.name}") } + } } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/ui/main/MainActivity.kt b/app/src/main/java/com/sopt/now/ui/main/MainActivity.kt index a0d0b53..26242fa 100644 --- a/app/src/main/java/com/sopt/now/ui/main/MainActivity.kt +++ b/app/src/main/java/com/sopt/now/ui/main/MainActivity.kt @@ -6,6 +6,7 @@ import androidx.fragment.app.Fragment import com.sopt.now.R import com.sopt.now.ui.model.User import com.sopt.now.databinding.ActivityMainBinding +import com.sopt.now.ui.ViewModelFactory import com.sopt.now.ui.login.LoginActivity.Companion.TAG_USER import com.sopt.now.ui.main.home.MainHomeFragment import com.sopt.now.ui.main.profile.MainProfileFragment @@ -16,7 +17,7 @@ import com.sopt.now.util.getSafeParcelable import com.sopt.now.util.toast class MainActivity : BindingActivity(R.layout.activity_main) { - private val viewModel by viewModels() + private val viewModel: MainViewModel by viewModels { ViewModelFactory() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -38,7 +39,6 @@ class MainActivity : BindingActivity(R.layout.activity_main viewModel.setMyProfile(state.data) viewModel.updateProfileWithMyProfile() } - is UiState.Error -> toast(state.errorMessage) } diff --git a/app/src/main/java/com/sopt/now/ui/main/MainViewModel.kt b/app/src/main/java/com/sopt/now/ui/main/MainViewModel.kt index 948a748..1595a1d 100644 --- a/app/src/main/java/com/sopt/now/ui/main/MainViewModel.kt +++ b/app/src/main/java/com/sopt/now/ui/main/MainViewModel.kt @@ -3,24 +3,23 @@ package com.sopt.now.ui.main import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.sopt.now.data.api.ServicePool.infoService import com.sopt.now.data.model.Profile -import com.sopt.now.ui.model.User +import com.sopt.now.domain.entity.UserEntity +import com.sopt.now.domain.usecase.GetUserInfoUseCase import com.sopt.now.util.StringNetworkError.FAIL_ERROR import com.sopt.now.util.StringNetworkError.LOGIN import com.sopt.now.util.UiState import kotlinx.coroutines.launch -import org.json.JSONObject import retrofit2.HttpException -class MainViewModel : ViewModel() { +class MainViewModel(private val getUserInfoUseCase: GetUserInfoUseCase) : ViewModel() { private val _userData = MutableLiveData>() val userData = _userData - private val _myInfo = MutableLiveData>() + private val _myInfo = MutableLiveData>() val myInfo = _myInfo - private val _myProfile = MutableLiveData() + private val _myProfile = MutableLiveData() val myProfile = _myProfile init { @@ -34,18 +33,8 @@ class MainViewModel : ViewModel() { fun getInfo(userid: String) { _myInfo.value = UiState.Loading viewModelScope.launch { - runCatching { - infoService.getUserInfo(userid) - }.onSuccess { response -> - if (response.isSuccessful) { - val user = response.body()?.data - _myInfo.value = UiState.Success(user?.toUser() ?: User("", "", "", "")) - } else { - val errorMessage = - JSONObject(response.errorBody()?.string()).getString("message") - _myInfo.value = UiState.Error(errorMessage.toString()) - } - + getUserInfoUseCase(userid).onSuccess { response -> + _myInfo.value = UiState.Success(response) }.onFailure { e -> if (e is HttpException) { _myInfo.value = UiState.Error(e.message()) @@ -56,14 +45,14 @@ class MainViewModel : ViewModel() { } } - fun setMyProfile(data: User) { + fun setMyProfile(data: UserEntity) { _myProfile.value = data } fun updateProfileWithMyProfile() { _userData.value = listOf( - Profile(_myProfile.value!!.nickname, _myProfile.value!!.phoneNumber), + Profile(_myProfile.value!!.nickname, _myProfile.value!!.phone), Profile("주효은", "INFP"), Profile("이유빈", "ENFP"), Profile("김민우", "ISTP"), diff --git a/app/src/main/java/com/sopt/now/ui/main/profile/MainProfileFragment.kt b/app/src/main/java/com/sopt/now/ui/main/profile/MainProfileFragment.kt index c6a7268..a31564a 100644 --- a/app/src/main/java/com/sopt/now/ui/main/profile/MainProfileFragment.kt +++ b/app/src/main/java/com/sopt/now/ui/main/profile/MainProfileFragment.kt @@ -19,10 +19,10 @@ class MainProfileFragment : private fun initLayout() { with(binding) { - tvMainIdContent.text = viewModel.myProfile.value?.id + tvMainIdContent.text = viewModel.myProfile.value?.authenticationId tvMainPasswordContent.text = viewModel.myProfile.value?.password tvMainNickname.text = getString(R.string.main_welcome).format(viewModel.myProfile.value?.nickname) - tvMainNumberContent.text = viewModel.myProfile.value?.phoneNumber + tvMainNumberContent.text = viewModel.myProfile.value?.phone } } } \ No newline at end of file