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 ccd8901..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,8 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.Toast import androidx.fragment.app.Fragment +import androidx.fragment.app.viewModels import androidx.recyclerview.selection.SelectionPredicates import androidx.recyclerview.selection.SelectionTracker import androidx.recyclerview.selection.StableIdKeyProvider @@ -13,24 +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.remote.ServicePool +import org.android.go.sopt.present.viewModel.MainPageViewModel 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: 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 @@ -48,7 +45,6 @@ class HomeFragment : Fragment() { } private fun initAdapter(itemList: List?) { - val multiAdapter = MultiViewAdapter(requireContext()) multiAdapter.submitList(itemList) @@ -62,35 +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() { - getListUsersService.getListUsers().enqueue( - object : retrofit2.Callback { - override fun onResponse( - call: Call, - response: Response - ) { - if (response.isSuccessful) { - val userList = response.body()?.data - initAdapter(userList) - - }else{ - requireActivity().makeToastMessage("서버 실패") - } - - } - - override fun onFailure(call: Call, t: Throwable) { - requireActivity().makeToastMessage("서버 실패") - } - } - ) + 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 new file mode 100644 index 0000000..d41c4e5 --- /dev/null +++ b/app/src/main/java/org/android/go/sopt/present/viewModel/MainPageViewModel.kt @@ -0,0 +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 androidx.lifecycle.viewModelScope +import kotlinx.coroutines.launch +import org.android.go.sopt.remote.remoteData.model.ResponseListUsersDto +import org.android.go.sopt.remote.remoteData.repoImpl.MainPageRepoImpl + +class MainPageViewModel(private val mainPageRepoImpl: MainPageRepoImpl) : ViewModel() { + + private val _userList = MutableLiveData>() + val userList: LiveData> get() = _userList + + fun getUserList() = viewModelScope.launch { + kotlin.runCatching { + mainPageRepoImpl.getUserList() + }.onSuccess { response -> + _userList.value = response.data + }.onFailure { + Log.d("mainPageViewModel", "서버 에러 발생") + } + } +} 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 }