Skip to content

Commit

Permalink
[Feat] follower repository 추가 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed Jun 7, 2024
1 parent d689df0 commit 51db35c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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<ResponseUserDto> {
return withContext(Dispatchers.IO) {
followerService.getUserList(page).execute()
}
}
}
64 changes: 37 additions & 27 deletions app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,59 +1,69 @@
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<List<UserDataDto>>(emptyList())
val followerState = _followerState.asStateFlow()

private var _eventNetworkError = MutableLiveData(false)
val eventNetworkError: LiveData<Boolean>
get() = _eventNetworkError

private var _isNetworkErrorShown = MutableLiveData(false)

val friendList = mutableListOf<Profile>()

init {
fetchFollowerList()
}

private fun fetchFollowerList() {
followerService.getUserList(page = 0).enqueue(object : Callback<ResponseUserDto> {
override fun onResponse(
call: Call<ResponseUserDto>,
response: Response<ResponseUserDto>,
) {
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<ResponseUserDto>, t: Throwable) {
Log.e("HomeError", "${t.message}")
}
})
fun onNetworkErrorShown() {
_isNetworkErrorShown.value = true
}

fun mapFollowersToFriendList(followers: List<UserDataDto>) {
for (follower in followers) {
friendList.add(
Profile(
profileImage = follower.avatar,
name = "${follower.firstName} ${follower.lastName}",
description = follower.email
)
private fun mapFollowersToFriendList(followers: List<UserDataDto>) {
friendList.clear()
friendList.addAll(followers.map { follower ->
Profile(
profileImage = follower.avatar,
name = "${follower.firstName} ${follower.lastName}",
description = follower.email
)
}
})
}
}
}

0 comments on commit 51db35c

Please sign in to comment.