1
1
package com.example.harmonyhub.ui.profile
2
2
3
+ import android.widget.Toast
3
4
import androidx.compose.foundation.Image
4
5
import androidx.compose.material3.TextButton
5
6
import androidx.compose.foundation.gestures.detectTapGestures
@@ -21,15 +22,18 @@ import androidx.compose.material3.Icon
21
22
import androidx.compose.material3.IconButton
22
23
import androidx.compose.material3.Text
23
24
import androidx.compose.runtime.Composable
25
+ import androidx.compose.runtime.LaunchedEffect
24
26
import androidx.compose.runtime.livedata.observeAsState
25
27
import androidx.compose.runtime.mutableIntStateOf
28
+ import androidx.compose.runtime.mutableStateListOf
26
29
import androidx.compose.runtime.mutableStateOf
27
30
import androidx.compose.runtime.remember
28
31
import androidx.compose.ui.Alignment
29
32
import androidx.compose.ui.Modifier
30
33
import androidx.compose.ui.draw.clip
31
34
import androidx.compose.ui.graphics.Color
32
35
import androidx.compose.ui.input.pointer.pointerInput
36
+ import androidx.compose.ui.platform.LocalContext
33
37
import androidx.compose.ui.platform.LocalFocusManager
34
38
import androidx.compose.ui.res.painterResource
35
39
import androidx.compose.ui.text.TextStyle
@@ -39,6 +43,9 @@ import androidx.compose.ui.unit.dp
39
43
import androidx.compose.ui.unit.sp
40
44
import androidx.hilt.navigation.compose.hiltViewModel
41
45
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
42
49
import com.example.harmonyhub.presentation.viewmodel.UserDataViewModel
43
50
import com.example.harmonyhub.ui.theme.NotoSans
44
51
@@ -47,6 +54,7 @@ fun ProfileScreen(
47
54
onBackButtonClicked : () -> Unit ,
48
55
onFriendsButtonClicked : () -> Unit ,
49
56
userDataViewModel : UserDataViewModel = hiltViewModel(),
57
+ friendListViewModel : FriendListViewModel = hiltViewModel()
50
58
) {
51
59
val focusManager = LocalFocusManager .current
52
60
@@ -56,6 +64,39 @@ fun ProfileScreen(
56
64
57
65
val isImageDialogOpen = remember { mutableStateOf(false ) }
58
66
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
+
59
100
if (isImageDialogOpen.value) {
60
101
AlertDialog (
61
102
onDismissRequest = { isImageDialogOpen.value = false },
@@ -207,7 +248,7 @@ fun ProfileScreen(
207
248
)
208
249
Spacer (modifier = Modifier .height(8 .dp))
209
250
210
- val numberOfFriends = 10
251
+ val numberOfFriends = friendList.size
211
252
Text (
212
253
text = " $numberOfFriends bạn bè" ,
213
254
style = TextStyle (
0 commit comments