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.viewmodels.MobileAppViewModelProvider
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.ButtonColor1
import com.example.mobileapp.ui.theme.ButtonColor2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
import java.util.Date
@ -193,11 +195,24 @@ fun DataListItemButton(label: String, backgroundColor: Color, textColor: Color,
}
@Composable
fun MailListItem(item: Mail, navController: NavHostController){
fun MailListItem(item: Mail, navController: NavHostController,
userViewModel: UserViewModel = viewModel(
factory = MobileAppViewModelProvider.Factory
)) {
val isExpanded = remember {
mutableStateOf(false)
}
val userName = remember { mutableStateOf("") }
LaunchedEffect(Unit){
userViewModel.getUser(item.userId).collect {
if (it != null) {
userName.value = it.email
}
}
}
Card(
modifier = Modifier
.fillMaxWidth()
@ -229,7 +244,7 @@ fun MailListItem(item: Mail, navController: NavHostController){
modifier = Modifier.padding(8.dp)
){
Text(
text = "item.username | ${dateFormat.format(Date(item.postdate!!))}",
text = "${userName.value} | ${dateFormat.format(Date(item.postdate!!))}",
fontSize = 20.sp,
fontWeight = FontWeight.Bold)
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.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@ -105,21 +106,25 @@ fun MailViewScreen(navController: NavHostController, mailId: Int,
)) {
val context = LocalContext.current
val userId = remember { mutableStateOf(0) }
val userName = remember { mutableStateOf("") }
val photo = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.photoplaceholder)) }
val message = remember { mutableStateOf("") }
val postdate = remember { mutableStateOf<Long>(0) }
val mail by mailViewModel.getMail(mailId).collectAsState(null)
mail?.let {
userId.value = it.userId
message.value = it.message
postdate.value = it.postdate!!
val user by userViewModel.getUser(userId.value).collectAsState(null)
user?.let {data ->
photo.value = data.photo!!
userName.value = data.login
LaunchedEffect(Unit){
mailViewModel.getMail(mailId).collect{
if (it != null) {
message.value = it.message
postdate.value = it.postdate!!
userViewModel.getUser(it.userId).collect {user ->
if (user != null) {
if(user.photo != null) {
photo.value = user.photo
}
userName.value = user.email
}
}
}
}
}