From c8ae238bfc16f0895d741b2f383e0038c040a36d Mon Sep 17 00:00:00 2001 From: gaeun Date: Wed, 28 Jun 2023 12:44:27 +0900 Subject: [PATCH] =?UTF-8?q?[Feature/#9]=20getUserList=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=EC=97=90=20viewModel=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sopt/present/menuFragment/HomeFragment.kt | 28 ++++--------- .../present/viewModel/MainPageViewModel.kt | 41 ++++++++----------- .../go/sopt/remote/service/MainPageService.kt | 2 +- 3 files changed, 25 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/org/android/go/sopt/present/menuFragment/HomeFragment.kt b/app/src/main/java/org/android/go/sopt/present/menuFragment/HomeFragment.kt index 95d8fcb..90a15fb 100644 --- a/app/src/main/java/org/android/go/sopt/present/menuFragment/HomeFragment.kt +++ b/app/src/main/java/org/android/go/sopt/present/menuFragment/HomeFragment.kt @@ -4,8 +4,6 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.Toast -import androidx.activity.viewModels import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.recyclerview.selection.SelectionPredicates @@ -15,27 +13,21 @@ import androidx.recyclerview.selection.StorageStrategy import androidx.recyclerview.widget.LinearLayoutManager import org.android.go.sopt.MultiViewAdapter import org.android.go.sopt.databinding.FragmentHomeBinding -import org.android.go.sopt.present.viewModel.LoginViewModel import org.android.go.sopt.present.viewModel.MainPageViewModel -import org.android.go.sopt.remote.ServicePool import org.android.go.sopt.remote.remoteData.model.ResponseListUsersDto -import org.android.go.sopt.util.makeToastMessage -import retrofit2.Call -import retrofit2.Response +import org.android.go.sopt.util.ViewModelFactory class HomeFragment : Fragment() { private var _binding: FragmentHomeBinding? = null private val binding: FragmentHomeBinding get() = requireNotNull(_binding) { "앗 ! _binding이 null이다 !" } - private val getListUsersService = ServicePool.mainPageService - private val viewModel by viewModels() - + private val viewModel: MainPageViewModel by viewModels { ViewModelFactory() } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? + savedInstanceState: Bundle?, ): View { _binding = FragmentHomeBinding.inflate(inflater, container, false) return binding.root @@ -53,7 +45,6 @@ class HomeFragment : Fragment() { } private fun initAdapter(itemList: List?) { - val multiAdapter = MultiViewAdapter(requireContext()) multiAdapter.submitList(itemList) @@ -67,18 +58,15 @@ class HomeFragment : Fragment() { binding.rv, StableIdKeyProvider(binding.rv), MultiViewAdapter.MyItemDetailsLookup(binding.rv), - StorageStrategy.createLongStorage() + StorageStrategy.createLongStorage(), ).withSelectionPredicate(SelectionPredicates.createSelectAnything()).build() multiAdapter.setSelectionTracker(itemSelectionTracker) - - } private fun getUserList() { - viewModel.gerUserList() - viewModel.userList.observe(this){ - initAdapter(it.data) + viewModel.getUserList() + viewModel.userList.observe(this) { response -> + initAdapter(response) } } - -} \ No newline at end of file +} diff --git a/app/src/main/java/org/android/go/sopt/present/viewModel/MainPageViewModel.kt b/app/src/main/java/org/android/go/sopt/present/viewModel/MainPageViewModel.kt index 15d784d..d41c4e5 100644 --- a/app/src/main/java/org/android/go/sopt/present/viewModel/MainPageViewModel.kt +++ b/app/src/main/java/org/android/go/sopt/present/viewModel/MainPageViewModel.kt @@ -1,35 +1,26 @@ package org.android.go.sopt.present.viewModel +import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel -import org.android.go.sopt.remote.ServicePool +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.launch import org.android.go.sopt.remote.remoteData.model.ResponseListUsersDto -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response +import org.android.go.sopt.remote.remoteData.repoImpl.MainPageRepoImpl -class MainPageViewModel : ViewModel() { - private val mainPageService = ServicePool.mainPageService +class MainPageViewModel(private val mainPageRepoImpl: MainPageRepoImpl) : ViewModel() { - private val _userList = MutableLiveData() - val userList: LiveData get() = _userList + private val _userList = MutableLiveData>() + val userList: LiveData> get() = _userList - fun gerUserList() { - mainPageService.getListUsers().enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - if (response.isSuccessful){ - _userList.value = response.body() - } - } - - override fun onFailure(call: Call, t: Throwable) { - - } - }) + fun getUserList() = viewModelScope.launch { + kotlin.runCatching { + mainPageRepoImpl.getUserList() + }.onSuccess { response -> + _userList.value = response.data + }.onFailure { + Log.d("mainPageViewModel", "서버 에러 발생") + } } - -} \ No newline at end of file +} diff --git a/app/src/main/java/org/android/go/sopt/remote/service/MainPageService.kt b/app/src/main/java/org/android/go/sopt/remote/service/MainPageService.kt index 8b5d31c..d6fb1e1 100644 --- a/app/src/main/java/org/android/go/sopt/remote/service/MainPageService.kt +++ b/app/src/main/java/org/android/go/sopt/remote/service/MainPageService.kt @@ -6,5 +6,5 @@ import retrofit2.http.GET interface MainPageService { @GET("users") - fun getListUsers(): ResponseListUsersDto + suspend fun getListUsers(): ResponseListUsersDto }