Skip to content

Commit

Permalink
[Feat] 팔로워 리스트 불러오기 API 구현 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed May 3, 2024
1 parent 3c9c7c7 commit bc186b0
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 76 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,7 @@ dependencies {
// Glide
implementation 'com.github.bumptech.glide:glide:4.13.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'

// Coil
implementation 'io.coil-kt:coil-compose:2.6.0'
}
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,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sopt.now.compose.ui.home

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -14,12 +13,12 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage

@Composable
fun HomeFriendProfile(
profileImage: Int,
profileImage: String,
name: String,
description: String,
) {
Expand All @@ -29,8 +28,8 @@ fun HomeFriendProfile(
.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Image(
painter = painterResource(id = profileImage),
AsyncImage(
model = profileImage,
contentDescription = null,
modifier = Modifier
.width(36.dp)
Expand Down
83 changes: 13 additions & 70 deletions app/src/main/java/com/sopt/now/compose/ui/home/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,28 @@ package com.sopt.now.compose.ui.home

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import com.sopt.now.compose.R
import com.sopt.now.compose.data.model.Profile

@Composable
fun HomeScreen() {
val profileList: List<Profile> =
listOf(
Profile(
profileImage = R.drawable.img_profile0,
name = "의피티",
description = "김아린 과제 마감 30분전"
),
Profile(
profileImage = R.drawable.img_profile1,
name = "최준서",
description = "오운완 ㅋㅋ",
),
Profile(
profileImage = R.drawable.img_profile2,
name = "이연진",
description = "아리니 넘 기엽당..",
),
Profile(
profileImage = R.drawable.img_profile3,
name = "손민재",
description = "점심 뭐 먹지?",
),
Profile(
profileImage = R.drawable.img_profile4,
name = "홍해인",
description = "난 눈물의 여왕이야",
),
Profile(
profileImage = R.drawable.img_profile5,
name = "백현우",
description = "눈물의여왕시작하지말걸공부가안된다",
),
Profile(
profileImage = R.drawable.img_profile6,
name = "이서경",
description = "저는 환연 과몰입러예요",
),
Profile(
profileImage = R.drawable.img_profile7,
name = "이주원",
description = "너가 자기야 미안해 했잖아?",
),
Profile(
profileImage = R.drawable.img_profile8,
name = "김광태",
description = "내일 뭐 해?",
),
Profile(
profileImage = R.drawable.img_profile9,
name = "정현규",
description = "내봬누",
),
Profile(
profileImage = R.drawable.img_profile10,
name = "성해은",
description = "벌써 스물 아홉이야",
),
Profile(
profileImage = R.drawable.img_profile11,
name = "정규민",
description = "오마카세 사줄게",
),
)
fun HomeScreen(homeViewModel: HomeViewModel = viewModel()) {
val followerState by homeViewModel.followerState.collectAsState()

LazyColumn(modifier = Modifier.fillMaxSize()) {
item { HomeMyProfile(R.drawable.img_arin, "김아린", "업보 청산의 끝이 보인다.") }
items(profileList.size) { index ->
item {
HomeMyProfile(R.drawable.img_arin, "김아린", "업보 청산의 끝이 보인다.")
}
items(followerState) { follower ->
HomeFriendProfile(
profileImage = profileList[index].profileImage,
name = profileList[index].name,
description = profileList[index].description,
profileImage = follower.avatar,
name = "${follower.firstName} ${follower.lastName}",
description = follower.email,
)
}
}
Expand Down
59 changes: 59 additions & 0 deletions app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt
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
)
)
}
}
}

0 comments on commit bc186b0

Please sign in to comment.