generated from NOW-SOPT-ANDROID/now-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
27 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/sopt/now/compose/repository/FollowerRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
64
app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
}) | ||
} | ||
} | ||
} |