Skip to content

Commit

Permalink
[Feat] #18 getinfo repository 패턴 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
cacaocoffee committed Jun 14, 2024
1 parent 32a8c17 commit d91ec8a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
34 changes: 23 additions & 11 deletions app/src/main/java/com/sopt/now/ui/ViewModelFactory.kt
Original file line number Diff line number Diff line change
@@ -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 <T : ViewModel> create(modelClass: Class<T>): 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}")
}

}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/sopt/now/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +17,7 @@ import com.sopt.now.util.getSafeParcelable
import com.sopt.now.util.toast

class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main) {
private val viewModel by viewModels<MainViewModel>()
private val viewModel: MainViewModel by viewModels { ViewModelFactory() }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -38,7 +39,6 @@ class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main
viewModel.setMyProfile(state.data)
viewModel.updateProfileWithMyProfile()
}

is UiState.Error -> toast(state.errorMessage)

}
Expand Down
29 changes: 9 additions & 20 deletions app/src/main/java/com/sopt/now/ui/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Profile>>()
val userData = _userData

private val _myInfo = MutableLiveData<UiState<User>>()
private val _myInfo = MutableLiveData<UiState<UserEntity>>()
val myInfo = _myInfo

private val _myProfile = MutableLiveData<User>()
private val _myProfile = MutableLiveData<UserEntity>()
val myProfile = _myProfile

init {
Expand All @@ -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())
Expand All @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit d91ec8a

Please sign in to comment.