diff --git a/app/src/main/java/com/example/myapplication/database/entities/composeui/ReportPage.kt b/app/src/main/java/com/example/myapplication/database/entities/composeui/ReportPage.kt index ac3443e..10b4fa2 100644 --- a/app/src/main/java/com/example/myapplication/database/entities/composeui/ReportPage.kt +++ b/app/src/main/java/com/example/myapplication/database/entities/composeui/ReportPage.kt @@ -11,6 +11,8 @@ import android.net.Uri import android.os.Environment import android.provider.MediaStore import android.util.Log +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.layout.Arrangement @@ -18,38 +20,57 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxHeight +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.foundation.layout.size +import androidx.compose.foundation.layout.width 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.material3.Button import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.Card import androidx.compose.material3.DatePicker import androidx.compose.material3.DisplayMode import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text +import androidx.compose.material3.TextField +import androidx.compose.material3.TextFieldDefaults import androidx.compose.material3.rememberDatePickerState import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavController import com.example.myapplication.R import com.example.myapplication.api.report.ReportRemote +import com.example.myapplication.composeui.navigation.Screen import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.IOException import java.io.OutputStream +import java.text.ParseException +import java.text.SimpleDateFormat import java.util.Date +import java.util.Locale @OptIn(ExperimentalMaterial3Api::class) @Composable @@ -57,61 +78,101 @@ fun ReportPage (navController: NavController?, viewModel: ReportViewModel = view { val context = LocalContext.current - //viewModel.getReport() - - val dateStateStart = rememberDatePickerState(initialDisplayMode = DisplayMode.Input) - val dateStateEnd = rememberDatePickerState(initialDisplayMode = DisplayMode.Input) - val coroutineScope = rememberCoroutineScope() val reportResultPageState = viewModel.reportResultPageUiState + + 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 - .verticalScroll(rememberScrollState()), - verticalArrangement = Arrangement.Center, - horizontalAlignment = Alignment.CenterHorizontally - ) - { - Text( - text = stringResource(id = R.string.startDate), - style = MaterialTheme.typography.headlineLarge - ) - DatePicker( - state = dateStateStart, - ) - val selectedDateStart = dateStateStart.selectedDateMillis - if (selectedDateStart != null) { - val resultDate= Date(selectedDateStart) - viewModel.onUpdate(viewModel.reportPageUiState.reportDetails.copy(startDate = resultDate)) - } - else - { - viewModel.onUpdate(viewModel.reportPageUiState.reportDetails.copy(startDate = Date(0))) - } - Text( - text = stringResource(id = R.string.endDate), - style = MaterialTheme.typography.headlineLarge - ) + .verticalScroll(rememberScrollState()) + .padding(16.dp) + ) { + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceBetween + ) { + Column( + modifier = Modifier + .width(180.dp) // Adjust the width as needed + .fillMaxHeight() + .padding(8.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center + ) { + Text( + text = stringResource(id = R.string.startDate), + textAlign = TextAlign.Center, + ) + Spacer(modifier = Modifier.height(8.dp)) + BasicTextField( + value = startDateText.value, + onValueChange = { startDateText.value = it }, + modifier = Modifier + .fillMaxWidth() + .size(36.dp) + .background( + MaterialTheme.colorScheme.onBackground, + RoundedCornerShape(18.dp) + ) + .padding(start = 13.dp, top = 8.dp), + textStyle = TextStyle(color = MaterialTheme.colorScheme.background), + ) + } - DatePicker( - state = dateStateEnd, - ) - val selectedDateEnd = dateStateEnd.selectedDateMillis - if (selectedDateEnd != null) { - val resultDate = Date(selectedDateEnd) - viewModel.onUpdate(viewModel.reportPageUiState.reportDetails.copy(endDate = resultDate)) - } - else - { - viewModel.onUpdate(viewModel.reportPageUiState.reportDetails.copy(endDate = Date(0))) + Column( + modifier = Modifier + .width(180.dp) // Adjust the width as needed + .fillMaxHeight() + .padding(8.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center + ) { + Text( + text = stringResource(id = R.string.endDate), + textAlign = TextAlign.Center, + ) + Spacer(modifier = Modifier.height(8.dp)) + BasicTextField( + value = endDateText.value, + onValueChange = { endDateText.value = it }, + modifier = Modifier + .fillMaxWidth() + .size(36.dp) + .background( + MaterialTheme.colorScheme.onBackground, + RoundedCornerShape(18.dp) + ) + .padding(start = 13.dp, top = 8.dp), + textStyle = TextStyle(color = MaterialTheme.colorScheme.background), + ) + } } Spacer(modifier = Modifier.height(16.dp)) + Button( - onClick = {coroutineScope.launch { - viewModel.getReport() - //createPdfFile(context = context, fileName = "отчет.pdf",reportData = reportResultPageState.resReport) - } }, - enabled = viewModel.reportPageUiState.isEntryValid, + 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 + } + } + }, modifier = Modifier .fillMaxWidth() .padding(all = 10.dp) @@ -121,19 +182,70 @@ fun ReportPage (navController: NavController?, viewModel: ReportViewModel = view ) { Text("Сформировать отчет") } - Spacer(modifier = Modifier.height(32.dp)) - Text( - text = "Результат", - style = MaterialTheme.typography.headlineLarge - ) - TableScreen(reportData = reportResultPageState.resReport) - Spacer(modifier = Modifier.height(16.dp)) + + Spacer(modifier = Modifier.height(8.dp)) + + // Use AnimatedVisibility to animate the Card visibility + AnimatedVisibility(visible = cardVisible) { + Card( + border = BorderStroke(2.dp, MaterialTheme.colorScheme.onBackground), + modifier = Modifier + .background(MaterialTheme.colorScheme.background) + .fillMaxWidth() + .padding(8.dp) + ) { + Column( + modifier = Modifier + .fillMaxSize() + .background(MaterialTheme.colorScheme.background) + .padding(8.dp), + verticalArrangement = Arrangement.Center, + horizontalAlignment = Alignment.CenterHorizontally + ) { + Text( + text = "Результат", + textAlign = TextAlign.Center, + style = TextStyle( + color = MaterialTheme.colorScheme.onSurface, + fontSize = 24.sp + ) + ) + + TableScreen(reportData = reportResultPageState.resReport) + + Spacer(modifier = Modifier.height(16.dp)) + + if (viewModel.reportPageUiState.isEntryValid) { + Button( + onClick = { + coroutineScope.launch { + viewModel.generatePdfFile( + context = context, + fileName = "отчет.pdf", + reportData = reportResultPageState.resReport + ) + } + }, + modifier = Modifier + .fillMaxWidth() + .padding(all = 10.dp) + .border(4.dp, MaterialTheme.colorScheme.onPrimary, shape = RoundedCornerShape(10.dp)), + shape = RoundedCornerShape(10.dp), + colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.primary) + ) { + Text("Сформировать отчет PDF") + } + } + } + } + } + Button( - onClick = {coroutineScope.launch { - //viewModel.getReport() - viewModel.generatePdfFile(context = context, fileName = "отчет.pdf",reportData = reportResultPageState.resReport) - } }, - enabled = viewModel.reportPageUiState.isEntryValid, + onClick = { + coroutineScope.launch { + navController?.navigate(Screen.BikeList.route) + } + }, modifier = Modifier .fillMaxWidth() .padding(all = 10.dp) @@ -141,11 +253,22 @@ fun ReportPage (navController: NavController?, viewModel: ReportViewModel = view shape = RoundedCornerShape(10.dp), colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.primary) ) { - Text("Сформировать отчет PDF") + Text("Выйти") } } } +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 + } +} + @Composable fun RowScope.TableCell( text: String, @@ -173,11 +296,11 @@ fun TableScreen(reportData: List) { ) { Row(Modifier.background(Color.White)) { - TableCell(text = "ID", weight = column1Weight) - TableCell(text = "Дата и время", weight = column1Weight) - TableCell(text = "Вес", weight = column1Weight) - TableCell(text = "Макс. количество", weight = column1Weight) - TableCell(text = "ID велосипеда", weight = column1Weight) + TableCell(text = "ID\n", weight = column1Weight) + TableCell(text = "Дата\n", weight = column1Weight) + TableCell(text = "Вес\n", weight = column1Weight) + TableCell(text = "Кол-во", weight = column1Weight) + TableCell(text = "ID вел.", weight = column1Weight) } // Here are all the lines of your table. diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 14985ca..f32b9ba 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,8 +7,8 @@ Название Описание Отчет - Дата начала - Дата конца + Дата начала отчета + Дата конца отчета Изображение Подтверждение Аренда