Короче, надо будет все coroutineScope посмотреть, перенсти их в ViewModel + вроде бы доделал дезигн...
This commit is contained in:
parent
34f56e6c89
commit
94a3838c7c
@ -31,6 +31,7 @@ import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.BasicTextField
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.Card
|
||||
@ -39,6 +40,7 @@ import androidx.compose.material3.DisplayMode
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.TextField
|
||||
import androidx.compose.material3.TextFieldDefaults
|
||||
import androidx.compose.material3.rememberDatePickerState
|
||||
@ -71,6 +73,7 @@ import java.io.IOException
|
||||
import java.io.OutputStream
|
||||
import java.text.ParseException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.time.Year
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
@ -78,6 +81,7 @@ import java.util.Locale
|
||||
@Composable
|
||||
fun ReportPage (navController: NavController?, viewModel: ReportViewModel = viewModel(factory = AppViewModelProvider.Factory))
|
||||
{
|
||||
|
||||
val context = LocalContext.current
|
||||
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
@ -86,8 +90,6 @@ fun ReportPage (navController: NavController?, viewModel: ReportViewModel = view
|
||||
val startDateText = remember { mutableStateOf("") }
|
||||
val endDateText = remember { mutableStateOf("") }
|
||||
|
||||
// Boolean state to track the visibility of the Card
|
||||
var cardVisible by remember { mutableStateOf(false) }
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
@ -160,19 +162,7 @@ fun ReportPage (navController: NavController?, viewModel: ReportViewModel = view
|
||||
Button(
|
||||
onClick = {
|
||||
coroutineScope.launch {
|
||||
val startDate = parseDate(startDateText.value)
|
||||
val endDate = parseDate(endDateText.value)
|
||||
|
||||
if (startDate != null && endDate != null) {
|
||||
viewModel.onUpdate(
|
||||
viewModel.reportPageUiState.reportDetails.copy(startDate = startDate, endDate = endDate)
|
||||
)
|
||||
viewModel.getReport()
|
||||
// Show the Card with animation
|
||||
cardVisible = true
|
||||
} else {
|
||||
// Handle parsing errors here if necessary
|
||||
}
|
||||
viewModel.onButtonClick(startDateText.value, endDateText.value)
|
||||
}
|
||||
},
|
||||
modifier = Modifier
|
||||
@ -185,10 +175,33 @@ fun ReportPage (navController: NavController?, viewModel: ReportViewModel = view
|
||||
Text("Сформировать отчет")
|
||||
}
|
||||
|
||||
viewModel.errorState?.let { errorMessage ->
|
||||
AlertDialog(
|
||||
onDismissRequest = { viewModel.errorState = null },
|
||||
title = { Text(text = "Ошибка") },
|
||||
text = { Text(errorMessage, color = MaterialTheme.colorScheme.onBackground) },
|
||||
buttons = {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(8.dp),
|
||||
horizontalArrangement = Arrangement.End
|
||||
) {
|
||||
TextButton(onClick = { viewModel.errorState = null }) {
|
||||
Text("Закрыть", color = MaterialTheme.colorScheme.onBackground)
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier = Modifier.padding(16.dp),
|
||||
backgroundColor = MaterialTheme.colorScheme.background,
|
||||
contentColor = MaterialTheme.colorScheme.onBackground
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
// Use AnimatedVisibility to animate the Card visibility
|
||||
AnimatedVisibility(visible = cardVisible) {
|
||||
AnimatedVisibility(visible = viewModel.cardVisible) {
|
||||
Card(
|
||||
border = BorderStroke(2.dp, MaterialTheme.colorScheme.onBackground),
|
||||
modifier = Modifier
|
||||
@ -271,21 +284,6 @@ private fun parseDate(dateString: String): Date? {
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun RowScope.TableCell(
|
||||
text: String,
|
||||
weight: Float
|
||||
) {
|
||||
Text(
|
||||
text = text,
|
||||
Modifier
|
||||
.border(1.dp, Color.Black)
|
||||
.weight(weight)
|
||||
.padding(8.dp)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@Composable
|
||||
fun TableScreen(reportData: List<ReportRemote>) {
|
||||
|
||||
|
@ -6,8 +6,11 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.myapplication.api.item.RestItemRepository
|
||||
import com.example.myapplication.api.report.ReportRemote
|
||||
import kotlinx.coroutines.launch
|
||||
import java.text.ParseException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
@ -19,6 +22,11 @@ class ReportViewModel(private val itemRespository: RestItemRepository): ViewMode
|
||||
var reportResultPageUiState by mutableStateOf(ReportResultPageUiState())
|
||||
private set
|
||||
|
||||
var cardVisible by mutableStateOf(false)
|
||||
private set
|
||||
|
||||
var errorState by mutableStateOf<String?>(null)
|
||||
|
||||
fun onUpdate(reportDetails: ReportDetails) {
|
||||
reportPageUiState = ReportPageUiState(reportDetails = reportDetails, isEntryValid = validateInput(reportDetails))
|
||||
}
|
||||
@ -45,6 +53,44 @@ class ReportViewModel(private val itemRespository: RestItemRepository): ViewMode
|
||||
suspend fun generatePdfFile(context: Context, fileName: String, reportData: List<ReportRemote>) {
|
||||
itemRespository.createPdfFile(context, fileName, reportData)
|
||||
}
|
||||
|
||||
fun onButtonClick(startDate: String, endDate: String) {
|
||||
viewModelScope.launch {
|
||||
val parsedStartDate = parseDate(startDate)
|
||||
val parsedEndDate = parseDate(endDate)
|
||||
|
||||
if (validateDates(parsedStartDate, parsedEndDate)) {
|
||||
updateReportDetails(parsedStartDate, parsedEndDate)
|
||||
getReport()
|
||||
cardVisible = true
|
||||
} else {
|
||||
Log.d("Ошибка даты", "Неправильно указаны даты или года не являются 4-значными =${startDate}")
|
||||
errorState = "Ошибка фильтраций: неверно указаны даты"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateDates(startDate: Date?, endDate: Date?): Boolean {
|
||||
return startDate != null && endDate != null && startDate < endDate
|
||||
}
|
||||
|
||||
private fun updateReportDetails(startDate: Date?, endDate: Date?) {
|
||||
if (startDate != null && endDate != null) {
|
||||
onUpdate(reportPageUiState.reportDetails.copy(startDate = startDate, endDate = endDate))
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseDate(dateString: String): Date? {
|
||||
return try {
|
||||
// Use SimpleDateFormat or any other method to parse the string into a Date object
|
||||
// For example:
|
||||
SimpleDateFormat("yyyy", Locale.getDefault()).parse(dateString)
|
||||
} catch (e: ParseException) {
|
||||
// Handle parsing error
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
data class ReportDetails(
|
||||
|
Loading…
Reference in New Issue
Block a user