Загрузка данных


package ru.notesapp.ui

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import ru.notesapp.domain.Note
import ru.notesapp.domain.NoteType
import ru.notesapp.viewmodel.NotesUiState
import ru.notesapp.viewmodel.NotesViewModel
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NoteDetailScreen(
    noteId: Long,
    onBack: () -> Unit,
    viewModel: NotesViewModel = hiltViewModel(),
    modifier: Modifier = Modifier,
) {
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
    val note: Note? = (uiState as? NotesUiState.Success)
        ?.notes?.firstOrNull { it.id == noteId }

    Scaffold(
        modifier = modifier.fillMaxSize(),
        topBar = {
            TopAppBar(
                title = { Text("Заметка #$noteId") },
                navigationIcon = {
                    IconButton(onClick = onBack) {
                        Icon(
                            imageVector = Icons.AutoMirrored.Filled.ArrowBack,
                            contentDescription = "Назад"
                        )
                    }
                }
            )
        }
    ) { innerPadding ->
        if (note == null) {
            EmptyState(
                title = "Заметка не найдена",
                subtitle = "Возможно, она была удалена",
                modifier = Modifier.padding(innerPadding)
            )
        } else {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(innerPadding)
                    .padding(16.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                if (!note.imageUrl.isNullOrBlank()) {
                    NoteImage(
                        url = note.imageUrl,
                        contentDescription = note.title,
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(200.dp)
                    )
                }
                Text(
                    text = note.title,
                    style = MaterialTheme.typography.titleLarge
                )
                Text(
                    text = note.type.detailLabel(),
                    style = MaterialTheme.typography.labelMedium,
                    color = note.type.detailColor()
                )
                Text(
                    text = formatDate(note.createdAt),
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.outline
                )
                Text(
                    text = note.content,
                    style = MaterialTheme.typography.bodyLarge
                )
            }
        }
    }
}

private fun formatDate(millis: Long): String {
    val sdf = SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.getDefault())
    return sdf.format(Date(millis))
}

private fun NoteType.detailLabel(): String = when (this) {
    is NoteType.Text  -> "ТЕКСТ"
    is NoteType.Image -> "КАРТИНКА"
    is NoteType.Audio -> "АУДИО"
}

private fun NoteType.detailColor(): Color = when (this) {
    is NoteType.Text  -> Color(0xFF3592C0)
    is NoteType.Image -> Color(0xFF3DDC84)
    is NoteType.Audio -> Color(0xFFFFA726)
}