This commit is contained in:
maxnes3 2023-12-09 11:21:16 +04:00
parent 18d1ad1392
commit fb7e7a1a43
2 changed files with 32 additions and 12 deletions

View File

@ -48,10 +48,12 @@ import com.example.mobileapp.database.entities.Mail
import com.example.mobileapp.database.entities.Story import com.example.mobileapp.database.entities.Story
import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider
import com.example.mobileapp.database.viewmodels.StoryViewModel import com.example.mobileapp.database.viewmodels.StoryViewModel
import com.example.mobileapp.database.viewmodels.UserViewModel
import com.example.mobileapp.ui.theme.BackgroundItem2 import com.example.mobileapp.ui.theme.BackgroundItem2
import com.example.mobileapp.ui.theme.ButtonColor1 import com.example.mobileapp.ui.theme.ButtonColor1
import com.example.mobileapp.ui.theme.ButtonColor2 import com.example.mobileapp.ui.theme.ButtonColor2
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
@ -193,11 +195,24 @@ fun DataListItemButton(label: String, backgroundColor: Color, textColor: Color,
} }
@Composable @Composable
fun MailListItem(item: Mail, navController: NavHostController){ fun MailListItem(item: Mail, navController: NavHostController,
userViewModel: UserViewModel = viewModel(
factory = MobileAppViewModelProvider.Factory
)) {
val isExpanded = remember { val isExpanded = remember {
mutableStateOf(false) mutableStateOf(false)
} }
val userName = remember { mutableStateOf("") }
LaunchedEffect(Unit){
userViewModel.getUser(item.userId).collect {
if (it != null) {
userName.value = it.email
}
}
}
Card( Card(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -229,7 +244,7 @@ fun MailListItem(item: Mail, navController: NavHostController){
modifier = Modifier.padding(8.dp) modifier = Modifier.padding(8.dp)
){ ){
Text( Text(
text = "item.username | ${dateFormat.format(Date(item.postdate!!))}", text = "${userName.value} | ${dateFormat.format(Date(item.postdate!!))}",
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold) fontWeight = FontWeight.Bold)
Text(text = item.message) Text(text = item.message)

View File

@ -13,6 +13,7 @@ import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@ -105,21 +106,25 @@ fun MailViewScreen(navController: NavHostController, mailId: Int,
)) { )) {
val context = LocalContext.current val context = LocalContext.current
val userId = remember { mutableStateOf(0) }
val userName = remember { mutableStateOf("") } val userName = remember { mutableStateOf("") }
val photo = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.photoplaceholder)) } val photo = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.photoplaceholder)) }
val message = remember { mutableStateOf("") } val message = remember { mutableStateOf("") }
val postdate = remember { mutableStateOf<Long>(0) } val postdate = remember { mutableStateOf<Long>(0) }
val mail by mailViewModel.getMail(mailId).collectAsState(null) LaunchedEffect(Unit){
mail?.let { mailViewModel.getMail(mailId).collect{
userId.value = it.userId if (it != null) {
message.value = it.message message.value = it.message
postdate.value = it.postdate!! postdate.value = it.postdate!!
val user by userViewModel.getUser(userId.value).collectAsState(null) userViewModel.getUser(it.userId).collect {user ->
user?.let {data -> if (user != null) {
photo.value = data.photo!! if(user.photo != null) {
userName.value = data.login photo.value = user.photo
}
userName.value = user.email
}
}
}
} }
} }