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 3459f2d commit c8ae238
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<MainPageViewModel>()

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 @@ -53,7 +45,6 @@ class HomeFragment : Fragment() {
}

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

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

Expand All @@ -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)
}
}

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

fun gerUserList() {
mainPageService.getListUsers().enqueue(object : Callback<ResponseListUsersDto> {
override fun onResponse(
call: Call<ResponseListUsersDto>,
response: Response<ResponseListUsersDto>
) {
if (response.isSuccessful){
_userList.value = response.body()
}
}

override fun onFailure(call: Call<ResponseListUsersDto>, t: Throwable) {

}
})
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 c8ae238

Please sign in to comment.