From bc186b0fdb6a9e7b6ab9e62d4ef9da9a7ffe38f6 Mon Sep 17 00:00:00 2001 From: arinming <jyandysys1@naver.com> Date: Fri, 3 May 2024 16:33:21 +0900 Subject: [PATCH] =?UTF-8?q?[Feat]=20=ED=8C=94=EB=A1=9C=EC=9B=8C=20?= =?UTF-8?q?=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EB=B6=88=EB=9F=AC=EC=98=A4?= =?UTF-8?q?=EA=B8=B0=20API=20=EA=B5=AC=ED=98=84=20(#10)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 3 + .../sopt/now/compose/data/model/Profile.kt | 2 +- .../now/compose/ui/home/HomeFriendProfile.kt | 9 +- .../sopt/now/compose/ui/home/HomeScreen.kt | 83 +++---------------- .../sopt/now/compose/ui/home/HomeViewModel.kt | 59 +++++++++++++ 5 files changed, 80 insertions(+), 76 deletions(-) create mode 100644 app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt diff --git a/app/build.gradle b/app/build.gradle index 63fdb13..19652a1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/compose/data/model/Profile.kt b/app/src/main/java/com/sopt/now/compose/data/model/Profile.kt index 9025131..d42f62e 100644 --- a/app/src/main/java/com/sopt/now/compose/data/model/Profile.kt +++ b/app/src/main/java/com/sopt/now/compose/data/model/Profile.kt @@ -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, ) diff --git a/app/src/main/java/com/sopt/now/compose/ui/home/HomeFriendProfile.kt b/app/src/main/java/com/sopt/now/compose/ui/home/HomeFriendProfile.kt index 17617bf..4760514 100644 --- a/app/src/main/java/com/sopt/now/compose/ui/home/HomeFriendProfile.kt +++ b/app/src/main/java/com/sopt/now/compose/ui/home/HomeFriendProfile.kt @@ -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 @@ -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, ) { @@ -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) diff --git a/app/src/main/java/com/sopt/now/compose/ui/home/HomeScreen.kt b/app/src/main/java/com/sopt/now/compose/ui/home/HomeScreen.kt index 01371c3..ec787af 100644 --- a/app/src/main/java/com/sopt/now/compose/ui/home/HomeScreen.kt +++ b/app/src/main/java/com/sopt/now/compose/ui/home/HomeScreen.kt @@ -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, ) } } diff --git a/app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt b/app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt new file mode 100644 index 0000000..91fc766 --- /dev/null +++ b/app/src/main/java/com/sopt/now/compose/ui/home/HomeViewModel.kt @@ -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 + ) + ) + } + } +} \ No newline at end of file