-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Week7] XML 필수 과제 #19
base: develop-xml
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.sopt.now.repository | ||
|
||
import com.sopt.now.data.model.UserDataResponse | ||
import com.sopt.now.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<UserDataResponse> { | ||
return withContext(Dispatchers.IO) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dispatchers.IO를 추가하신 이유가 무엇인가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코루틴을 사용할 때 서버에서 파일 다운로드시 IO를 주입해야한다고 알고있숩니다.! |
||
followerService.getUserList(page).execute() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,27 @@ | ||
package com.sopt.now.ui.home.viewModel | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.sopt.now.R | ||
import com.sopt.now.data.model.ItemData | ||
import com.sopt.now.data.model.UserData | ||
import com.sopt.now.data.model.UserDataResponse | ||
import com.sopt.now.data.module.ServicePool | ||
import com.sopt.now.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<UserData>>(emptyList()) | ||
val followerState = _followerState.asStateFlow() | ||
private var _eventNetworkError = MutableLiveData(false) | ||
val eventNetworkError: LiveData<Boolean> | ||
get() = _eventNetworkError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [3] private var _eventNetworkError = MutableLiveData(false)
val eventNetworkError: LiveData<Boolean> = _eventNetworkError 위와 같이 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [2] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LiveData가 MutableLiveData를 감싸서 MutableLiveData의 변화를 관찰할 수 있기 때문에 get을 지워도 되는 것 맞을까요..!??!
Comment on lines
19
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [5] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. StateFlow로 통일하려다가 추가적으로 고치지 못하였습니다..! ㅜㅜ |
||
private var _isNetworkErrorShown = MutableLiveData(false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [2] 또 backing property 형태를 사용하지 않고 있는데 앞에 언더바를 붙인 이유도 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _isNetworkErrorShown 자체가 변경되지 않아서 val로 선언하는 것이 적절해보이네욥..! |
||
|
||
val friendList = mutableListOf<ItemData>( | ||
ItemData.MyProfile( | ||
|
@@ -32,28 +36,38 @@ class HomeViewModel : ViewModel() { | |
} | ||
|
||
private fun fetchFollowerList() { | ||
followerService.getUserList(page = 0).enqueue(object : Callback<UserDataResponse> { | ||
override fun onResponse( | ||
call: Call<UserDataResponse>, | ||
response: Response<UserDataResponse>, | ||
) { | ||
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) | ||
Log.d("FOLLOWER", "$data") | ||
} | ||
_eventNetworkError.value = false | ||
_isNetworkErrorShown.value = false | ||
} else { | ||
_eventNetworkError.value = true | ||
} | ||
} catch (networkError: IOException) { | ||
_eventNetworkError.value = true | ||
} | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<UserDataResponse>, t: Throwable) { | ||
Log.e("HomeError", "${t.message}") | ||
} | ||
}) | ||
fun onNetworkErrorShown() { | ||
_isNetworkErrorShown.value = true | ||
} | ||
|
||
fun mapFollowersToFriendList(followers: List<UserData>) { | ||
private fun mapFollowersToFriendList(followers: List<UserData>) { | ||
friendList.clear() | ||
friendList.add( | ||
ItemData.MyProfile( | ||
profileImage = R.drawable.img_arin, | ||
name = "김아린", | ||
description = "업보 청산 중..", | ||
), | ||
) | ||
friendList.addAll(followers.map { follower -> | ||
ItemData.Friend( | ||
profileImage = follower.avatar, | ||
|
@@ -62,4 +76,4 @@ class HomeViewModel : ViewModel() { | |
) | ||
}) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
follower 라는 네이밍을 한 이유가 있나요?