diff --git a/app/src/main/java/com/sopt/now/compose/repository/FollowerRepository.kt b/app/src/main/java/com/sopt/now/compose/repository/FollowerRepository.kt new file mode 100644 index 0000000..99ede21 --- /dev/null +++ b/app/src/main/java/com/sopt/now/compose/repository/FollowerRepository.kt @@ -0,0 +1,17 @@ +package com.sopt.now.compose.repository + +import com.sopt.now.compose.data.model.ResponseUserDto +import com.sopt.now.compose.data.module.ServicePool +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import retrofit2.Response + +class FollowerRepository { + private val followerService = ServicePool.followerService + + suspend fun getUserList(page: Int): Response { + return withContext(Dispatchers.IO) { + followerService.getUserList(page).execute() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt b/app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt index a79e0bb..409ddf5 100644 --- a/app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt +++ b/app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt @@ -1,23 +1,30 @@ package com.sopt.now.compose.ui.home import android.util.Log +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.sopt.now.compose.data.model.Profile -import com.sopt.now.compose.data.model.ResponseUserDto import com.sopt.now.compose.data.model.UserDataDto -import com.sopt.now.compose.data.module.ServicePool +import com.sopt.now.compose.repository.FollowerRepository import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response +import kotlinx.coroutines.launch +import java.io.IOException class HomeViewModel : ViewModel() { - private val followerService by lazy { ServicePool.followerService } + private val followerRepository = FollowerRepository() private val _followerState = MutableStateFlow>(emptyList()) val followerState = _followerState.asStateFlow() + private var _eventNetworkError = MutableLiveData(false) + val eventNetworkError: LiveData + get() = _eventNetworkError + + private var _isNetworkErrorShown = MutableLiveData(false) + val friendList = mutableListOf() init { @@ -25,35 +32,38 @@ class HomeViewModel : ViewModel() { } private fun fetchFollowerList() { - followerService.getUserList(page = 0).enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response, - ) { + viewModelScope.launch { + try { + val response = followerRepository.getUserList(0) if (response.isSuccessful) { - val data = response.body()?.data - if (data != null) { + response.body()?.data?.let { data -> _followerState.value = data mapFollowersToFriendList(data) } + _eventNetworkError.value = false + _isNetworkErrorShown.value = false + } else { + _eventNetworkError.value = true } + } catch (networkError: IOException) { + _eventNetworkError.value = true + Log.e("HomeError", "${networkError.message}") } + } + } - override fun onFailure(call: Call, t: Throwable) { - Log.e("HomeError", "${t.message}") - } - }) + fun onNetworkErrorShown() { + _isNetworkErrorShown.value = true } - fun mapFollowersToFriendList(followers: List) { - for (follower in followers) { - friendList.add( - Profile( - profileImage = follower.avatar, - name = "${follower.firstName} ${follower.lastName}", - description = follower.email - ) + private fun mapFollowersToFriendList(followers: List) { + friendList.clear() + friendList.addAll(followers.map { follower -> + Profile( + profileImage = follower.avatar, + name = "${follower.firstName} ${follower.lastName}", + description = follower.email ) - } + }) } -} \ No newline at end of file +}