Skip to content

Commit

Permalink
[Feature/#9] getUserList 기능에 viewModel 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
gaeun5744 committed Jun 28, 2023
1 parent bc414f6 commit f0bc2de
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,30 @@ 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
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
Expand All @@ -48,7 +45,6 @@ class HomeFragment : Fragment() {
}

private fun initAdapter(itemList: List<ResponseListUsersDto.Data>?) {

val multiAdapter = MultiViewAdapter(requireContext())
multiAdapter.submitList(itemList)

Expand All @@ -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<ResponseListUsersDto> {
override fun onResponse(
call: Call<ResponseListUsersDto>,
response: Response<ResponseListUsersDto>
) {
if (response.isSuccessful) {
val userList = response.body()?.data
initAdapter(userList)

}else{
requireActivity().makeToastMessage("서버 실패")
}

}

override fun onFailure(call: Call<ResponseListUsersDto>, t: Throwable) {
requireActivity().makeToastMessage("서버 실패")
}
}
)
viewModel.getUserList()
viewModel.userList.observe(this) { response ->
initAdapter(response)
}
}

}
}
Original file line number Diff line number Diff line change
@@ -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<List<ResponseListUsersDto.Data>>()
val userList: LiveData<List<ResponseListUsersDto.Data>> get() = _userList

fun getUserList() = viewModelScope.launch {
kotlin.runCatching {
mainPageRepoImpl.getUserList()
}.onSuccess { response ->
_userList.value = response.data
}.onFailure {
Log.d("mainPageViewModel", "서버 에러 발생")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import retrofit2.http.GET
interface MainPageService {

@GET("users")
fun getListUsers(): ResponseListUsersDto
suspend fun getListUsers(): ResponseListUsersDto
}

0 comments on commit f0bc2de

Please sign in to comment.