Skip to content

Commit 8943784

Browse files
committed
database-design branch - feat: display number of friends in the profile screen
1 parent ce45d6b commit 8943784

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

app/src/main/java/com/example/harmonyhub/ui/profile/ProfileScreen.kt

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.harmonyhub.ui.profile
22

3+
import android.widget.Toast
34
import androidx.compose.foundation.Image
45
import androidx.compose.material3.TextButton
56
import androidx.compose.foundation.gestures.detectTapGestures
@@ -21,15 +22,18 @@ import androidx.compose.material3.Icon
2122
import androidx.compose.material3.IconButton
2223
import androidx.compose.material3.Text
2324
import androidx.compose.runtime.Composable
25+
import androidx.compose.runtime.LaunchedEffect
2426
import androidx.compose.runtime.livedata.observeAsState
2527
import androidx.compose.runtime.mutableIntStateOf
28+
import androidx.compose.runtime.mutableStateListOf
2629
import androidx.compose.runtime.mutableStateOf
2730
import androidx.compose.runtime.remember
2831
import androidx.compose.ui.Alignment
2932
import androidx.compose.ui.Modifier
3033
import androidx.compose.ui.draw.clip
3134
import androidx.compose.ui.graphics.Color
3235
import androidx.compose.ui.input.pointer.pointerInput
36+
import androidx.compose.ui.platform.LocalContext
3337
import androidx.compose.ui.platform.LocalFocusManager
3438
import androidx.compose.ui.res.painterResource
3539
import androidx.compose.ui.text.TextStyle
@@ -39,6 +43,9 @@ import androidx.compose.ui.unit.dp
3943
import androidx.compose.ui.unit.sp
4044
import androidx.hilt.navigation.compose.hiltViewModel
4145
import com.example.harmonyhub.R
46+
import com.example.harmonyhub.domain.repository.FirebaseUser
47+
import com.example.harmonyhub.presentation.viewmodel.FriendListFetchingState
48+
import com.example.harmonyhub.presentation.viewmodel.FriendListViewModel
4249
import com.example.harmonyhub.presentation.viewmodel.UserDataViewModel
4350
import com.example.harmonyhub.ui.theme.NotoSans
4451

@@ -47,6 +54,7 @@ fun ProfileScreen(
4754
onBackButtonClicked: () -> Unit,
4855
onFriendsButtonClicked: () -> Unit,
4956
userDataViewModel: UserDataViewModel = hiltViewModel(),
57+
friendListViewModel: FriendListViewModel = hiltViewModel()
5058
) {
5159
val focusManager = LocalFocusManager.current
5260

@@ -56,6 +64,39 @@ fun ProfileScreen(
5664

5765
val isImageDialogOpen = remember { mutableStateOf(false) }
5866

67+
val friendListFetchingState = friendListViewModel.dataFetchingState.observeAsState()
68+
val friendList = remember { mutableStateListOf<FirebaseUser>() }
69+
val context = LocalContext.current
70+
71+
LaunchedEffect(Unit) {
72+
friendListViewModel.getFriends()
73+
}
74+
75+
LaunchedEffect(friendListFetchingState.value) {
76+
when(friendListFetchingState.value) {
77+
is FriendListFetchingState.Error -> {
78+
val message = (friendListFetchingState.value as FriendListFetchingState.Error).message
79+
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
80+
friendListViewModel.resetDataFetchingState()
81+
}
82+
is FriendListFetchingState.SuccessOnGetFriends -> {
83+
when (val data = (friendListFetchingState.value as FriendListFetchingState.SuccessOnGetFriends).data) {
84+
is String -> {
85+
val message = data as String
86+
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
87+
friendListViewModel.resetDataFetchingState()
88+
}
89+
is List<*> -> {
90+
friendList.clear()
91+
friendList.addAll(data as List<FirebaseUser>)
92+
friendListViewModel.resetDataFetchingState()
93+
}
94+
}
95+
}
96+
else -> {}
97+
}
98+
}
99+
59100
if (isImageDialogOpen.value) {
60101
AlertDialog(
61102
onDismissRequest = { isImageDialogOpen.value = false },
@@ -207,7 +248,7 @@ fun ProfileScreen(
207248
)
208249
Spacer(modifier = Modifier.height(8.dp))
209250

210-
val numberOfFriends = 10
251+
val numberOfFriends = friendList.size
211252
Text(
212253
text = "$numberOfFriends bạn bè",
213254
style = TextStyle(

0 commit comments

Comments
 (0)