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
5 changed files
with
80 additions
and
76 deletions.
There are no files selected for viewing
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
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,7 +1,7 @@ | ||
package com.sopt.now.compose.data.model | ||
|
||
data class Profile( | ||
val profileImage: Int, | ||
val profileImage: String, | ||
val name: String, | ||
val description: String, | ||
) |
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
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
59 changes: 59 additions & 0 deletions
59
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.sopt.now.compose.ui.home | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.ViewModel | ||
import com.sopt.now.compose.data.model.Profile | ||
import com.sopt.now.compose.data.model.ResponseUserDto | ||
import com.sopt.now.compose.data.model.UserData | ||
import com.sopt.now.compose.data.module.ServicePool | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
class HomeViewModel : ViewModel() { | ||
private val followerService by lazy { ServicePool.followerService } | ||
|
||
private val _followerState = MutableStateFlow<List<UserData>>(emptyList()) | ||
val followerState = _followerState.asStateFlow() | ||
|
||
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>, | ||
) { | ||
if (response.isSuccessful) { | ||
val data = response.body()?.data | ||
if (data != null) { | ||
_followerState.value = data | ||
mapFollowersToFriendList(data) | ||
} | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<ResponseUserDto>, t: Throwable) { | ||
Log.e("HomeError", "${t.message}") | ||
} | ||
}) | ||
} | ||
|
||
fun mapFollowersToFriendList(followers: List<UserData>) { | ||
for (follower in followers) { | ||
friendList.add( | ||
Profile( | ||
profileImage = follower.avatar, | ||
name = "${follower.firstName} ${follower.lastName}", | ||
description = follower.email | ||
) | ||
) | ||
} | ||
} | ||
} |