Skip to content

Commit

Permalink
[refactor] Repository에서 runCatching으로 작성 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed Jul 2, 2024
1 parent 1638b2c commit e55297f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import javax.inject.Singleton
class FollowerRepository @Inject constructor(
private val followerService: FollowerService,
) {
suspend fun getUserList(page: Int): Response<ResponseUserDto> {
suspend fun getUserList(page: Int): Result<Response<ResponseUserDto>> {
return withContext(Dispatchers.IO) {
followerService.getUserList(page).execute()
runCatching {
followerService.getUserList(page).execute()
}
}
}
}
}
21 changes: 8 additions & 13 deletions app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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
Expand All @@ -12,7 +10,6 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.io.IOException
import javax.inject.Inject

@HiltViewModel
Expand All @@ -35,22 +32,20 @@ class HomeViewModel @Inject constructor(

private fun fetchFollowerList() {
viewModelScope.launch {
try {
val response = followerRepository.getUserList(0)
if (response.isSuccessful) {
followerRepository.getUserList(0)
.onSuccess { response ->
response.body()?.data?.let { data ->
_followerState.value = data
mapFollowersToFriendList(data)
_eventNetworkError.value = false
_isNetworkErrorShown.value = false
} ?: run {
_eventNetworkError.value = true
}
_eventNetworkError.value = false
_isNetworkErrorShown.value = false
} else {
}
.onFailure { exception ->
_eventNetworkError.value = true
}
} catch (networkError: IOException) {
_eventNetworkError.value = true
Log.e("HomeError", "${networkError.message}")
}
}
}

Expand Down

0 comments on commit e55297f

Please sign in to comment.