From 15962d32ac07f940f9531e45544e2e085c65cf14 Mon Sep 17 00:00:00 2001 From: flaviuvsp Date: Fri, 9 Aug 2024 10:54:36 +0300 Subject: [PATCH 1/6] Edit date and time --- .../bulkEditMetadata/EditDateTimeFragment.kt | 91 ++++++++ .../bulkEditMetadata/EditMetadataFragment.kt | 4 +- .../compose/EditDateTimeScreen.kt | 215 ++++++++++++++++++ .../compose/EditMetadataScreen.kt | 8 + app/src/main/res/values/strings.xml | 1 - 5 files changed, 317 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt create mode 100644 app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt new file mode 100644 index 00000000..4646c34f --- /dev/null +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt @@ -0,0 +1,91 @@ +package org.permanent.permanent.ui.bulkEditMetadata + +import android.app.Dialog +import android.content.DialogInterface +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import androidx.compose.material3.MaterialTheme +import androidx.compose.ui.platform.ComposeView +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProvider +import com.google.android.libraries.places.api.Places +import com.google.android.material.bottomsheet.BottomSheetBehavior +import com.google.android.material.bottomsheet.BottomSheetDialog +import org.permanent.permanent.BuildConfig +import org.permanent.permanent.models.Record +import org.permanent.permanent.ui.PermanentBottomSheetFragment +import org.permanent.permanent.ui.bulkEditMetadata.compose.EditDateTimeScreen +import org.permanent.permanent.ui.myFiles.PARCELABLE_FILES_KEY +import org.permanent.permanent.viewmodels.EditLocationViewModel + +class EditDateTimeFragment : PermanentBottomSheetFragment() { + private lateinit var viewModel: EditLocationViewModel + private val onLocationChanged = MutableLiveData() + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? + ): View { + Places.initialize(requireContext(), BuildConfig.GMP_KEY) + viewModel = ViewModelProvider(this)[EditLocationViewModel::class.java] + + arguments?.getParcelableArrayList(PARCELABLE_FILES_KEY)?.let { + viewModel.setRecords(it) + } + + return ComposeView(requireContext()).apply { + setContent { + MaterialTheme { + EditDateTimeScreen(cancel = { + this@EditDateTimeFragment.dismiss() + }) + } + } + } + } + + fun setBundleArguments(records: ArrayList) { + val bundle = Bundle() + bundle.putParcelableArrayList(PARCELABLE_FILES_KEY, records) + this.arguments = bundle + } + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog + bottomSheetDialog.setOnShowListener { dialog: DialogInterface -> + val sheetDialog = dialog as BottomSheetDialog + val bottomSheet = + sheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet) + BottomSheetBehavior.from(bottomSheet as FrameLayout) + .setState(BottomSheetBehavior.STATE_EXPANDED) + } + return bottomSheetDialog + } + + private val onLocationChangedObserver = Observer { + onLocationChanged.value = it + } + + override fun connectViewModelEvents() { + viewModel.getOnLocationChanged().observe(this, onLocationChangedObserver) + } + + override fun disconnectViewModelEvents() { + viewModel.getOnLocationChanged().removeObserver(onLocationChangedObserver) + } + + override fun onResume() { + super.onResume() + connectViewModelEvents() + } + + override fun onPause() { + super.onPause() + disconnectViewModelEvents() + } + + fun getOnLocationChanged() = onLocationChanged +} \ No newline at end of file diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditMetadataFragment.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditMetadataFragment.kt index 713dd7fe..a2db0857 100644 --- a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditMetadataFragment.kt +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditMetadataFragment.kt @@ -53,7 +53,9 @@ class EditMetadataFragment : PermanentBaseFragment() { fragment?.show(parentFragmentManager, fragment?.tag) }, openDateAndTimeScreen = { - + var fragment = EditDateTimeFragment() + fragment?.setBundleArguments(records) + fragment?.show(parentFragmentManager, fragment?.tag) }, openLocationScreen = { locationFragment = EditLocationFragment() diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt new file mode 100644 index 00000000..baf407bb --- /dev/null +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt @@ -0,0 +1,215 @@ +package org.permanent.permanent.ui.bulkEditMetadata.compose + +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.Button +import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.DatePicker +import androidx.compose.material3.DatePickerDefaults +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.material3.TimePicker +import androidx.compose.material3.rememberDatePickerState +import androidx.compose.material3.rememberTimePickerState +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +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.painterResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.core.content.ContextCompat +import org.permanent.permanent.R +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Date +import java.util.Locale + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun EditDateTimeScreen(cancel: () -> Unit) { + + val context = LocalContext.current + val superLightBlue = Color(ContextCompat.getColor(context, R.color.superLightBlue)) + val blue900 = Color(ContextCompat.getColor(context, R.color.blue900)) + val regularFont = FontFamily(Font(R.font.open_sans_regular_ttf)) + + val datePickerState = rememberDatePickerState() + val selectedDate = datePickerState.selectedDateMillis?.let { + convertMillisToDate(it) + } ?: "" + + var showTimePicker by remember { mutableStateOf(false) } + + val currentTime = Calendar.getInstance() + + val timePickerState = rememberTimePickerState( + initialHour = currentTime.get(Calendar.HOUR_OF_DAY), + initialMinute = currentTime.get(Calendar.MINUTE), + is24Hour = true, + ) + + Column( + horizontalAlignment = Alignment.End + ) { + BottomSheetHeader( + painterResource(id = R.drawable.map_icon), + screenTitle = stringResource(id = R.string.add_location) + ) + + DatePicker( + state = datePickerState, + showModeToggle = false, + title = null, + headline = null, + colors = DatePickerDefaults.colors( + selectedDayContainerColor = blue900, + dayContentColor = Color.Black, + todayDateBorderColor = blue900, + selectedYearContainerColor = blue900, + yearContentColor = Color.Black, + currentYearContentColor = Color.Black + ) + ) + + Row( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 32.dp), + horizontalArrangement = Arrangement.SpaceBetween + ) { + Text( + text = "Time", + style = TextStyle( + fontSize = 17.sp, + lineHeight = 22.sp, + fontWeight = FontWeight(600), + color = Color(0xFF000000) + ) + ) + + Text( + text = "9:41 AM", + style = TextStyle( + fontSize = 17.sp, + lineHeight = 22.sp, + fontWeight = FontWeight(400), + color = Color(0xFF000000), + + ), + modifier = Modifier.clickable { + showTimePicker = true + } + ) + } + + + Row( + modifier = Modifier.padding(24.dp), + horizontalArrangement = Arrangement.spacedBy(24.dp) + ) { + Button(modifier = Modifier + .fillMaxWidth() + .weight(1.0f) + .height(48.dp), + shape = RoundedCornerShape(0.dp), + colors = ButtonDefaults.buttonColors(containerColor = superLightBlue), + onClick = { + cancel() + }) { + Text( + text = stringResource(R.string.button_cancel), + fontSize = 14.sp, + color = blue900, + fontFamily = regularFont, + ) + } + + Button(modifier = Modifier + .fillMaxWidth() + .weight(1.0f) + .height(48.dp), + shape = RoundedCornerShape(0.dp), + colors = ButtonDefaults.buttonColors(containerColor = blue900), + onClick = { +// openAlertDialog.value = true + }) { +// if (viewModel.isBusy.value) { +// CircularProgressIndicator( +// modifier = Modifier.width(32.dp), +// color = primaryColor, +// trackColor = superLightBlue, +// ) +// } else { + Text( + text = stringResource(R.string.set_location), + fontSize = 14.sp, + fontFamily = regularFont, + ) + } + } + + if (showTimePicker) { + TimePickerDialog( + onDismiss = { showTimePicker = false }, + onConfirm = { showTimePicker = false } + ) { + TimePicker( + state = timePickerState, + ) + } + } + } +} + +@Composable +fun TimePickerDialog( + onDismiss: () -> Unit, + onConfirm: () -> Unit, + content: @Composable () -> Unit +) { + AlertDialog( + onDismissRequest = onDismiss, + dismissButton = { + TextButton(onClick = { onDismiss() }) { + Text("Dismiss") + } + }, + confirmButton = { + TextButton(onClick = { onConfirm() }) { + Text("OK") + } + }, + text = { content() } + ) +} + +fun convertMillisToDate(millis: Long): String { + val formatter = SimpleDateFormat("MM/dd/yyyy", Locale.getDefault()) + return formatter.format(Date(millis)) +} + +@Preview(showSystemUi = true) +@Composable +fun SimpleComposablePreview() { + EditDateTimeScreen(cancel = {}) +} \ No newline at end of file diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditMetadataScreen.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditMetadataScreen.kt index 0ee4f166..0f044fa6 100644 --- a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditMetadataScreen.kt +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditMetadataScreen.kt @@ -198,6 +198,14 @@ fun EditMetadataScreen( Divider(modifier = Modifier.padding(vertical = 16.dp)) + FilesMenuView(icon = R.drawable.ic_date_time, + title = stringResource(id = R.string.date_time), + actionTitle = stringResource(id = R.string.menu_toolbar_public_add)) { + openDateAndTimeScreen(viewModel.getRecords()) + } + + Divider(modifier = Modifier.padding(vertical = 16.dp)) + locationMenuName?.let { FilesMenuView(icon = R.drawable.map_icon, title = it, diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4d385405..420570e0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -732,5 +732,4 @@ Various locations New location Are you sure you want set a new location for selected items? - From 4a94703c3c79b7501db247f7a08f98189c533878 Mon Sep 17 00:00:00 2001 From: flaviuvsp Date: Mon, 26 Aug 2024 11:28:20 +0300 Subject: [PATCH 2/6] Add view model --- .../permanent/network/RequestContainer.kt | 1 + .../compose/EditDateTimeScreen.kt | 65 +++++++++++++++---- .../viewmodels/EditDateTimeViewModel.kt | 42 ++++++++++++ app/src/main/res/values/strings.xml | 3 + 4 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt diff --git a/app/src/main/java/org/permanent/permanent/network/RequestContainer.kt b/app/src/main/java/org/permanent/permanent/network/RequestContainer.kt index 0702089c..9e9ec39c 100644 --- a/app/src/main/java/org/permanent/permanent/network/RequestContainer.kt +++ b/app/src/main/java/org/permanent/permanent/network/RequestContainer.kt @@ -286,6 +286,7 @@ class RequestContainer { recordVO.folder_linkId = record.folderLinkId recordVO.parentFolder_linkId = record.parentFolderLinkId recordVO.displayName = record.displayName + recordVO.displayDT = record.displayDate if(locnVO != null) { recordVO.LocnVO = locnVO } diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt index baf407bb..71ceff2c 100644 --- a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt @@ -1,5 +1,6 @@ package org.permanent.permanent.ui.bulkEditMetadata.compose +import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column @@ -17,6 +18,7 @@ import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.material3.TimePicker +import androidx.compose.material3.TimePickerDefaults import androidx.compose.material3.rememberDatePickerState import androidx.compose.material3.rememberTimePickerState import androidx.compose.runtime.Composable @@ -39,6 +41,8 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.core.content.ContextCompat import org.permanent.permanent.R +import org.permanent.permanent.viewmodels.EditDateTimeViewModel +import org.permanent.permanent.viewmodels.EditFileNamesViewModel import java.text.SimpleDateFormat import java.util.Calendar import java.util.Date @@ -46,10 +50,13 @@ import java.util.Locale @OptIn(ExperimentalMaterial3Api::class) @Composable -fun EditDateTimeScreen(cancel: () -> Unit) { +fun EditDateTimeScreen( + viewModel: EditDateTimeViewModel, + cancel: () -> Unit) { val context = LocalContext.current val superLightBlue = Color(ContextCompat.getColor(context, R.color.superLightBlue)) + val blue400 = Color(ContextCompat.getColor(context, R.color.blue400)) val blue900 = Color(ContextCompat.getColor(context, R.color.blue900)) val regularFont = FontFamily(Font(R.font.open_sans_regular_ttf)) @@ -65,15 +72,19 @@ fun EditDateTimeScreen(cancel: () -> Unit) { val timePickerState = rememberTimePickerState( initialHour = currentTime.get(Calendar.HOUR_OF_DAY), initialMinute = currentTime.get(Calendar.MINUTE), - is24Hour = true, + is24Hour = false, ) + var selectedTime by remember { mutableStateOf( + convertTimeToString(timePickerState.hour, timePickerState.minute) + )} + Column( horizontalAlignment = Alignment.End ) { BottomSheetHeader( - painterResource(id = R.drawable.map_icon), - screenTitle = stringResource(id = R.string.add_location) + painterResource(id = R.drawable.ic_date_time), + screenTitle = stringResource(id = R.string.add_date_time) ) DatePicker( @@ -84,6 +95,7 @@ fun EditDateTimeScreen(cancel: () -> Unit) { colors = DatePickerDefaults.colors( selectedDayContainerColor = blue900, dayContentColor = Color.Black, + todayContentColor = Color.Black, todayDateBorderColor = blue900, selectedYearContainerColor = blue900, yearContentColor = Color.Black, @@ -95,7 +107,8 @@ fun EditDateTimeScreen(cancel: () -> Unit) { modifier = Modifier .fillMaxWidth() .padding(horizontal = 32.dp), - horizontalArrangement = Arrangement.SpaceBetween + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically ) { Text( text = "Time", @@ -108,7 +121,7 @@ fun EditDateTimeScreen(cancel: () -> Unit) { ) Text( - text = "9:41 AM", + text = selectedTime, style = TextStyle( fontSize = 17.sp, lineHeight = 22.sp, @@ -116,13 +129,15 @@ fun EditDateTimeScreen(cancel: () -> Unit) { color = Color(0xFF000000), ), - modifier = Modifier.clickable { - showTimePicker = true - } + modifier = Modifier + .clickable { + showTimePicker = true + } + .background(color = Color(0x1F767680), shape = RoundedCornerShape(size = 8.dp)) + .padding(start = 12.dp, top = 6.dp, end = 12.dp, bottom = 6.dp) ) } - Row( modifier = Modifier.padding(24.dp), horizontalArrangement = Arrangement.spacedBy(24.dp) @@ -151,6 +166,7 @@ fun EditDateTimeScreen(cancel: () -> Unit) { shape = RoundedCornerShape(0.dp), colors = ButtonDefaults.buttonColors(containerColor = blue900), onClick = { + viewModel.updateDate(selectedDate) // openAlertDialog.value = true }) { // if (viewModel.isBusy.value) { @@ -161,7 +177,7 @@ fun EditDateTimeScreen(cancel: () -> Unit) { // ) // } else { Text( - text = stringResource(R.string.set_location), + text = stringResource(R.string.add_date), fontSize = 14.sp, fontFamily = regularFont, ) @@ -171,10 +187,20 @@ fun EditDateTimeScreen(cancel: () -> Unit) { if (showTimePicker) { TimePickerDialog( onDismiss = { showTimePicker = false }, - onConfirm = { showTimePicker = false } + onConfirm = { + showTimePicker = false + selectedTime = convertTimeToString(timePickerState.hour, timePickerState.minute) + } ) { TimePicker( state = timePickerState, + colors = TimePickerDefaults.colors( + timeSelectorSelectedContainerColor = blue900, + timeSelectorSelectedContentColor = Color.White, + selectorColor = blue900, + periodSelectorSelectedContainerColor = blue900, + periodSelectorSelectedContentColor = Color.White + ) ) } } @@ -208,6 +234,21 @@ fun convertMillisToDate(millis: Long): String { return formatter.format(Date(millis)) } +fun convertTimeToString(hour: Int, min: Int): String { + val amPM = if (hour < 12) { + "AM" + } else { + "PM" + } + val minString = if (min < 10) { + "0$min" + } else { + "$min" + } + + return "$hour:$minString $amPM" +} + @Preview(showSystemUi = true) @Composable fun SimpleComposablePreview() { diff --git a/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt b/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt new file mode 100644 index 00000000..1f5082b9 --- /dev/null +++ b/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt @@ -0,0 +1,42 @@ +package org.permanent.permanent.viewmodels + +import android.app.Application +import org.permanent.permanent.models.Record +import org.permanent.permanent.network.IResponseListener +import org.permanent.permanent.repositories.FileRepositoryImpl +import org.permanent.permanent.repositories.IFileRepository + +class EditDateTimeViewModel(application: Application) : ObservableAndroidViewModel(application) { + private var fileRepository: IFileRepository = FileRepositoryImpl(application) + private var records: MutableList = mutableListOf() + + fun setRecords(records: ArrayList) { + this.records.addAll(records) + } + + fun updateDate(dateString: String) { + this.records.forEach { + it.displayDate = dateString + } + applyChanges() + } + + private fun applyChanges() { +// toggleLoading() + fileRepository.updateMultipleRecords(records = records, + isFolderRecordType = false, + object : IResponseListener { + override fun onSuccess(message: String?) { +// toggleLoading() +// triggerCloseScreen() + } + + override fun onFailed(error: String?) { +// toggleLoading() +// error?.let { +// updateError(it) +// } + } + }) + } +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 420570e0..996d1a33 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -732,4 +732,7 @@ Various locations New location Are you sure you want set a new location for selected items? + Add date and time + Add date + From 0df47d74e8d86f72627ad2094f18b16fa1de165c Mon Sep 17 00:00:00 2001 From: flaviuvsp Date: Wed, 28 Aug 2024 12:13:42 +0300 Subject: [PATCH 3/6] Update menu based on date --- .../bulkEditMetadata/EditDateTimeFragment.kt | 29 ++++----- .../compose/EditDateTimeScreen.kt | 65 ++++++++++++------- .../compose/EditMetadataScreen.kt | 11 ++-- .../viewmodels/EditDateTimeViewModel.kt | 21 ++++-- .../viewmodels/EditMetadataViewModel.kt | 15 +++++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 93 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt index 4646c34f..0172a19f 100644 --- a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt @@ -9,8 +9,6 @@ import android.view.ViewGroup import android.widget.FrameLayout import androidx.compose.material3.MaterialTheme import androidx.compose.ui.platform.ComposeView -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import com.google.android.libraries.places.api.Places import com.google.android.material.bottomsheet.BottomSheetBehavior @@ -20,17 +18,18 @@ import org.permanent.permanent.models.Record import org.permanent.permanent.ui.PermanentBottomSheetFragment import org.permanent.permanent.ui.bulkEditMetadata.compose.EditDateTimeScreen import org.permanent.permanent.ui.myFiles.PARCELABLE_FILES_KEY -import org.permanent.permanent.viewmodels.EditLocationViewModel +import org.permanent.permanent.viewmodels.EditDateTimeViewModel class EditDateTimeFragment : PermanentBottomSheetFragment() { - private lateinit var viewModel: EditLocationViewModel - private val onLocationChanged = MutableLiveData() + private lateinit var viewModel: EditDateTimeViewModel +// private val onLocationChanged = MutableLiveData() + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { Places.initialize(requireContext(), BuildConfig.GMP_KEY) - viewModel = ViewModelProvider(this)[EditLocationViewModel::class.java] + viewModel = ViewModelProvider(this)[EditDateTimeViewModel::class.java] arguments?.getParcelableArrayList(PARCELABLE_FILES_KEY)?.let { viewModel.setRecords(it) @@ -39,7 +38,7 @@ class EditDateTimeFragment : PermanentBottomSheetFragment() { return ComposeView(requireContext()).apply { setContent { MaterialTheme { - EditDateTimeScreen(cancel = { + EditDateTimeScreen(viewModel = viewModel, cancel = { this@EditDateTimeFragment.dismiss() }) } @@ -64,17 +63,17 @@ class EditDateTimeFragment : PermanentBottomSheetFragment() { } return bottomSheetDialog } - - private val onLocationChangedObserver = Observer { - onLocationChanged.value = it - } +// +// private val onLocationChangedObserver = Observer { +// onLocationChanged.value = it +// } override fun connectViewModelEvents() { - viewModel.getOnLocationChanged().observe(this, onLocationChangedObserver) +// viewModel.getOnLocationChanged().observe(this, onLocationChangedObserver) } - +// override fun disconnectViewModelEvents() { - viewModel.getOnLocationChanged().removeObserver(onLocationChangedObserver) +// viewModel.getOnLocationChanged().removeObserver(onLocationChangedObserver) } override fun onResume() { @@ -87,5 +86,5 @@ class EditDateTimeFragment : PermanentBottomSheetFragment() { disconnectViewModelEvents() } - fun getOnLocationChanged() = onLocationChanged +// fun getOnLocationChanged() = onLocationChanged } \ No newline at end of file diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt index 71ceff2c..90067478 100644 --- a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt @@ -1,5 +1,6 @@ package org.permanent.permanent.ui.bulkEditMetadata.compose +import CustomDialog import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement @@ -8,10 +9,12 @@ import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.AlertDialog import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.DatePicker import androidx.compose.material3.DatePickerDefaults import androidx.compose.material3.ExperimentalMaterial3Api @@ -22,6 +25,7 @@ import androidx.compose.material3.TimePickerDefaults import androidx.compose.material3.rememberDatePickerState import androidx.compose.material3.rememberTimePickerState import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -36,13 +40,11 @@ import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.core.content.ContextCompat import org.permanent.permanent.R import org.permanent.permanent.viewmodels.EditDateTimeViewModel -import org.permanent.permanent.viewmodels.EditFileNamesViewModel import java.text.SimpleDateFormat import java.util.Calendar import java.util.Date @@ -56,10 +58,11 @@ fun EditDateTimeScreen( val context = LocalContext.current val superLightBlue = Color(ContextCompat.getColor(context, R.color.superLightBlue)) - val blue400 = Color(ContextCompat.getColor(context, R.color.blue400)) val blue900 = Color(ContextCompat.getColor(context, R.color.blue900)) val regularFont = FontFamily(Font(R.font.open_sans_regular_ttf)) + val openAlertDialog = remember { mutableStateOf(false) } + val datePickerState = rememberDatePickerState() val selectedDate = datePickerState.selectedDateMillis?.let { convertMillisToDate(it) @@ -166,21 +169,21 @@ fun EditDateTimeScreen( shape = RoundedCornerShape(0.dp), colors = ButtonDefaults.buttonColors(containerColor = blue900), onClick = { - viewModel.updateDate(selectedDate) -// openAlertDialog.value = true + openAlertDialog.value = true }) { -// if (viewModel.isBusy.value) { -// CircularProgressIndicator( -// modifier = Modifier.width(32.dp), -// color = primaryColor, -// trackColor = superLightBlue, -// ) -// } else { - Text( - text = stringResource(R.string.add_date), - fontSize = 14.sp, - fontFamily = regularFont, - ) + if (viewModel.isBusy.value) { + CircularProgressIndicator( + modifier = Modifier.width(32.dp), + color = blue900, + trackColor = superLightBlue, + ) + } else { + Text( + text = stringResource(R.string.add_date), + fontSize = 14.sp, + fontFamily = regularFont, + ) + } } } @@ -204,7 +207,29 @@ fun EditDateTimeScreen( ) } } + + when { + openAlertDialog.value -> { + CustomDialog( + title = stringResource(id = R.string.location_confirmation_title), + subtitle = stringResource(id = R.string.location_confirmation_substring), + okButtonText = stringResource(id = R.string.set_location), + cancelButtonText = stringResource(id = R.string.button_cancel), + onConfirm = { + openAlertDialog.value = false + viewModel.updateDate(dateString = selectedDate) + }) { + openAlertDialog.value = false + } + } + } } + + LaunchedEffect(key1 = viewModel.shouldClose.value, block = { + if (viewModel.shouldClose.value) { + cancel() + } + }) } @Composable @@ -247,10 +272,4 @@ fun convertTimeToString(hour: Int, min: Int): String { } return "$hour:$minString $amPM" -} - -@Preview(showSystemUi = true) -@Composable -fun SimpleComposablePreview() { - EditDateTimeScreen(cancel = {}) } \ No newline at end of file diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditMetadataScreen.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditMetadataScreen.kt index 0f044fa6..6c4b9100 100644 --- a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditMetadataScreen.kt +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditMetadataScreen.kt @@ -91,6 +91,7 @@ fun EditMetadataScreen( val showApplyAllToSelection by viewModel.showApplyAllToSelection.observeAsState() val isBusy by viewModel.getIsBusy().observeAsState() val locationMenuName by viewModel.getLocationMenuName().observeAsState() + val dateMenuName by viewModel.getDateMenuName().observeAsState() val coroutineScope = rememberCoroutineScope() val snackbarEventFlow = remember { MutableSharedFlow() } @@ -198,10 +199,12 @@ fun EditMetadataScreen( Divider(modifier = Modifier.padding(vertical = 16.dp)) - FilesMenuView(icon = R.drawable.ic_date_time, - title = stringResource(id = R.string.date_time), - actionTitle = stringResource(id = R.string.menu_toolbar_public_add)) { - openDateAndTimeScreen(viewModel.getRecords()) + dateMenuName?.let { + FilesMenuView(icon = R.drawable.ic_date_time, + title = it, + actionTitle = stringResource(id = R.string.menu_toolbar_public_add)) { + openDateAndTimeScreen(viewModel.getRecords()) + } } Divider(modifier = Modifier.padding(vertical = 16.dp)) diff --git a/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt b/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt index 1f5082b9..ec5c7bcd 100644 --- a/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt +++ b/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt @@ -1,6 +1,8 @@ package org.permanent.permanent.viewmodels import android.app.Application +import androidx.compose.runtime.MutableState +import androidx.compose.runtime.mutableStateOf import org.permanent.permanent.models.Record import org.permanent.permanent.network.IResponseListener import org.permanent.permanent.repositories.FileRepositoryImpl @@ -9,6 +11,9 @@ import org.permanent.permanent.repositories.IFileRepository class EditDateTimeViewModel(application: Application) : ObservableAndroidViewModel(application) { private var fileRepository: IFileRepository = FileRepositoryImpl(application) private var records: MutableList = mutableListOf() + var shouldClose: MutableState = mutableStateOf(false) + var isBusy: MutableState = mutableStateOf(false) + val showMessage = mutableStateOf("") fun setRecords(records: ArrayList) { this.records.addAll(records) @@ -22,20 +27,22 @@ class EditDateTimeViewModel(application: Application) : ObservableAndroidViewMod } private fun applyChanges() { -// toggleLoading() + if (isBusy.value != null && isBusy.value!!) { + return + } fileRepository.updateMultipleRecords(records = records, isFolderRecordType = false, object : IResponseListener { override fun onSuccess(message: String?) { -// toggleLoading() -// triggerCloseScreen() + isBusy.value = false + shouldClose.value = true } override fun onFailed(error: String?) { -// toggleLoading() -// error?.let { -// updateError(it) -// } + isBusy.value = false + error?.let { + showMessage.value = it + } } }) } diff --git a/app/src/main/java/org/permanent/permanent/viewmodels/EditMetadataViewModel.kt b/app/src/main/java/org/permanent/permanent/viewmodels/EditMetadataViewModel.kt index e1f22f1e..efb6b922 100644 --- a/app/src/main/java/org/permanent/permanent/viewmodels/EditMetadataViewModel.kt +++ b/app/src/main/java/org/permanent/permanent/viewmodels/EditMetadataViewModel.kt @@ -33,6 +33,7 @@ class EditMetadataViewModel(application: Application) : ObservableAndroidViewMod val showApplyAllToSelection = MutableLiveData(true) private val isBusy = MutableLiveData(false) private val locationMenuName = MutableLiveData(appContext.getString(R.string.locations)) + private val dateMenuName = MutableLiveData(appContext.getString(R.string.date_time)) private var fileDataSize = 0 private var tagRepository: ITagRepository = TagRepositoryImpl(application) private var fileRepository: IFileRepository = FileRepositoryImpl(application) @@ -55,6 +56,17 @@ class EditMetadataViewModel(application: Application) : ObservableAndroidViewMod } } + fun setDateMenuName() { + records.firstOrNull()?.fileData?.displayDate?.let { firstDate: String -> + var sameDate = records.all { it.fileData?.displayDate == firstDate} + if (sameDate) { + dateMenuName.value = records.firstOrNull()?.fileData?.displayDate + } else { + dateMenuName.value = appContext.getString(R.string.various_date_times) + } + } + } + private fun requestFileData(record: Record) { val folderLinkId = record.folderLinkId val recordId = record.recordId @@ -71,6 +83,7 @@ class EditMetadataViewModel(application: Application) : ObservableAndroidViewMod checkForCommonDescription() checkForCommonTags() setLocationMenuName() + setDateMenuName() } } @@ -252,4 +265,6 @@ class EditMetadataViewModel(application: Application) : ObservableAndroidViewMod fun getSomeFilesHaveDescription() = showWarningSomeFilesHaveDescription fun getLocationMenuName() = locationMenuName + + fun getDateMenuName() = dateMenuName } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b0c7c14b..3a78b8b7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -737,5 +737,6 @@ Are you sure you want set a new location for selected items? Add date and time Add date + Various dates and times From dfc0b649b51733de47e0de0348c63a86bc42e433 Mon Sep 17 00:00:00 2001 From: flaviuvsp Date: Thu, 29 Aug 2024 14:39:32 +0300 Subject: [PATCH 4/6] Update date menu --- .../bulkEditMetadata/EditDateTimeFragment.kt | 20 +++++++------- .../bulkEditMetadata/EditMetadataFragment.kt | 15 ++++++++--- .../compose/EditDateTimeScreen.kt | 27 +++++++------------ .../viewmodels/EditDateTimeViewModel.kt | 10 +++++-- .../viewmodels/EditMetadataViewModel.kt | 4 +++ app/src/main/res/values/strings.xml | 4 +++ 6 files changed, 49 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt index 0172a19f..d138310c 100644 --- a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditDateTimeFragment.kt @@ -9,6 +9,8 @@ import android.view.ViewGroup import android.widget.FrameLayout import androidx.compose.material3.MaterialTheme import androidx.compose.ui.platform.ComposeView +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import com.google.android.libraries.places.api.Places import com.google.android.material.bottomsheet.BottomSheetBehavior @@ -22,7 +24,7 @@ import org.permanent.permanent.viewmodels.EditDateTimeViewModel class EditDateTimeFragment : PermanentBottomSheetFragment() { private lateinit var viewModel: EditDateTimeViewModel -// private val onLocationChanged = MutableLiveData() + private val onDateChanged = MutableLiveData() override fun onCreateView( @@ -63,17 +65,17 @@ class EditDateTimeFragment : PermanentBottomSheetFragment() { } return bottomSheetDialog } -// -// private val onLocationChangedObserver = Observer { -// onLocationChanged.value = it -// } + + private val onDateChangedObserver = Observer { + onDateChanged.value = it + } override fun connectViewModelEvents() { -// viewModel.getOnLocationChanged().observe(this, onLocationChangedObserver) + viewModel.getOnDateChanged().observe(this, onDateChangedObserver) } -// + override fun disconnectViewModelEvents() { -// viewModel.getOnLocationChanged().removeObserver(onLocationChangedObserver) + viewModel.getOnDateChanged().removeObserver(onDateChangedObserver) } override fun onResume() { @@ -86,5 +88,5 @@ class EditDateTimeFragment : PermanentBottomSheetFragment() { disconnectViewModelEvents() } -// fun getOnLocationChanged() = onLocationChanged + fun getOnDateChanged() = onDateChanged } \ No newline at end of file diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditMetadataFragment.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditMetadataFragment.kt index a2db0857..41cf9eb8 100644 --- a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditMetadataFragment.kt +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/EditMetadataFragment.kt @@ -20,6 +20,7 @@ class EditMetadataFragment : PermanentBaseFragment() { private lateinit var viewModel: EditMetadataViewModel private var newTagFragment: NewTagFragment? = null private var locationFragment: EditLocationFragment? = null + private var dateFragment: EditDateTimeFragment? = null private var records = ArrayList() override fun onCreateView( @@ -53,9 +54,11 @@ class EditMetadataFragment : PermanentBaseFragment() { fragment?.show(parentFragmentManager, fragment?.tag) }, openDateAndTimeScreen = { - var fragment = EditDateTimeFragment() - fragment?.setBundleArguments(records) - fragment?.show(parentFragmentManager, fragment?.tag) + dateFragment = EditDateTimeFragment() + dateFragment?.setBundleArguments(records) + dateFragment?.show(parentFragmentManager, dateFragment?.tag) + dateFragment?.getOnDateChanged() + ?.observe(lifecycleOwner, onDateChangedObserver) }, openLocationScreen = { locationFragment = EditLocationFragment() @@ -78,6 +81,11 @@ class EditMetadataFragment : PermanentBaseFragment() { viewModel.onLocationChanged(it) } + private val onDateChangedObserver = Observer { + viewModel.onDateChanged(it) + } + + override fun connectViewModelEvents() { } @@ -85,6 +93,7 @@ class EditMetadataFragment : PermanentBaseFragment() { override fun disconnectViewModelEvents() { newTagFragment?.getOnTagsAddedToSelection()?.removeObserver(onTagsAddedToSelectionObserver) locationFragment?.getOnLocationChanged()?.removeObserver(onLocationChangedObserver) + dateFragment?.getOnDateChanged()?.removeObserver(onDateChangedObserver) } override fun onResume() { diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt index 90067478..c5fff7dd 100644 --- a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt @@ -211,13 +211,13 @@ fun EditDateTimeScreen( when { openAlertDialog.value -> { CustomDialog( - title = stringResource(id = R.string.location_confirmation_title), - subtitle = stringResource(id = R.string.location_confirmation_substring), - okButtonText = stringResource(id = R.string.set_location), + title = stringResource(id = R.string.date_confirmation_title), + subtitle = stringResource(id = R.string.date_confirmation_substring), + okButtonText = stringResource(id = R.string.set_date_and_time), cancelButtonText = stringResource(id = R.string.button_cancel), onConfirm = { openAlertDialog.value = false - viewModel.updateDate(dateString = selectedDate) + viewModel.updateDate(dateString = "$selectedDate $selectedTime") }) { openAlertDialog.value = false } @@ -242,12 +242,12 @@ fun TimePickerDialog( onDismissRequest = onDismiss, dismissButton = { TextButton(onClick = { onDismiss() }) { - Text("Dismiss") + Text(stringResource(id = R.string.button_cancel)) } }, confirmButton = { TextButton(onClick = { onConfirm() }) { - Text("OK") + Text(stringResource(id = R.string.save_button)) } }, text = { content() } @@ -260,16 +260,9 @@ fun convertMillisToDate(millis: Long): String { } fun convertTimeToString(hour: Int, min: Int): String { - val amPM = if (hour < 12) { - "AM" - } else { - "PM" - } - val minString = if (min < 10) { - "0$min" - } else { - "$min" - } + val amPm = if (hour < 12) "AM" else "PM" + val adjustedHour = if (hour == 0) 12 else hour % 12 // Handle 0 hour as 12 AM + val minString = String.format("%02d", min) - return "$hour:$minString $amPM" + return "$adjustedHour:$minString $amPm" } \ No newline at end of file diff --git a/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt b/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt index ec5c7bcd..8993d97c 100644 --- a/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt +++ b/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt @@ -3,6 +3,7 @@ package org.permanent.permanent.viewmodels import android.app.Application import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateOf +import androidx.lifecycle.MutableLiveData import org.permanent.permanent.models.Record import org.permanent.permanent.network.IResponseListener import org.permanent.permanent.repositories.FileRepositoryImpl @@ -14,6 +15,7 @@ class EditDateTimeViewModel(application: Application) : ObservableAndroidViewMod var shouldClose: MutableState = mutableStateOf(false) var isBusy: MutableState = mutableStateOf(false) val showMessage = mutableStateOf("") + private val onDateChanged = MutableLiveData() fun setRecords(records: ArrayList) { this.records.addAll(records) @@ -23,19 +25,21 @@ class EditDateTimeViewModel(application: Application) : ObservableAndroidViewMod this.records.forEach { it.displayDate = dateString } - applyChanges() + applyChanges(dateString) } - private fun applyChanges() { + private fun applyChanges(dateString: String) { if (isBusy.value != null && isBusy.value!!) { return } + isBusy.value = true fileRepository.updateMultipleRecords(records = records, isFolderRecordType = false, object : IResponseListener { override fun onSuccess(message: String?) { isBusy.value = false shouldClose.value = true + onDateChanged.value = dateString } override fun onFailed(error: String?) { @@ -46,4 +50,6 @@ class EditDateTimeViewModel(application: Application) : ObservableAndroidViewMod } }) } + + fun getOnDateChanged() = onDateChanged } \ No newline at end of file diff --git a/app/src/main/java/org/permanent/permanent/viewmodels/EditMetadataViewModel.kt b/app/src/main/java/org/permanent/permanent/viewmodels/EditMetadataViewModel.kt index efb6b922..4862dce4 100644 --- a/app/src/main/java/org/permanent/permanent/viewmodels/EditMetadataViewModel.kt +++ b/app/src/main/java/org/permanent/permanent/viewmodels/EditMetadataViewModel.kt @@ -256,6 +256,10 @@ class EditMetadataViewModel(application: Application) : ObservableAndroidViewMod locationMenuName.value = address } + fun onDateChanged(dateString: String) { + dateMenuName.value = dateString + } + fun getIsBusy() = isBusy fun getRecords() = records diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3a78b8b7..a3800280 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -738,5 +738,9 @@ Add date and time Add date Various dates and times + New Date & Time + Are you sure you want set a new date & time for selected items? + Set date and time + From b120a81a9d36a861a288a1b5c291a1c276552801 Mon Sep 17 00:00:00 2001 From: flaviuvsp Date: Mon, 2 Sep 2024 11:02:58 +0300 Subject: [PATCH 5/6] load display date --- .../org/permanent/permanent/models/Record.kt | 8 ++--- .../permanent/network/models/FileData.kt | 2 +- .../compose/EditDateTimeScreen.kt | 29 ++++++++--------- .../viewmodels/EditDateTimeViewModel.kt | 32 ++++++++++++++++++- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/permanent/permanent/models/Record.kt b/app/src/main/java/org/permanent/permanent/models/Record.kt index 4ca1c10f..f013a923 100644 --- a/app/src/main/java/org/permanent/permanent/models/Record.kt +++ b/app/src/main/java/org/permanent/permanent/models/Record.kt @@ -74,7 +74,7 @@ open class Record : Parcelable { folderLinkId = recordInfo.folder_linkId parentFolderLinkId = recordInfo.parentFolder_linkId displayName = recordInfo.displayName - displayDate = recordInfo.displayDT?.substringBefore("T") + displayDate = recordInfo.displayDT showArchiveThumb = false thumbURL200 = recordInfo.thumbURL200 thumbURL2000 = recordInfo.thumbURL2000 @@ -100,7 +100,7 @@ open class Record : Parcelable { folderLinkId = recordInfo.folder_linkId parentFolderLinkId = recordInfo.parentFolder_linkId displayName = recordInfo.displayName - displayDate = recordInfo.displayDT?.substringBefore("T") + displayDate = recordInfo.displayDT showArchiveThumb = false thumbURL200 = recordInfo.thumbURL200 thumbURL2000 = recordInfo.thumbURL2000 @@ -127,7 +127,7 @@ open class Record : Parcelable { folderLinkId = itemVO.folder_linkId parentFolderLinkId = itemVO.parentFolder_linkId displayName = itemVO.displayName - displayDate = itemVO.displayDT?.substringBefore("T") + displayDate = itemVO.displayDT archiveFullName = "The ${archiveVO.fullName} Archive" archiveThumbURL200 = archiveVO.thumbURL200 showArchiveThumb = showArchiveThumbnail @@ -172,7 +172,7 @@ open class Record : Parcelable { folderLinkId = recordInfo?.folder_linkId parentFolderLinkId = recordInfo?.parentFolder_linkId displayName = recordInfo?.displayName - displayDate = recordInfo?.displayDT?.substringBefore("T") + displayDate = recordInfo?.displayDT thumbURL200 = recordInfo?.thumbURL200 thumbURL2000 = recordInfo?.thumbURL2000 isThumbBlurred = shareByUrlVO.previewToggle == null || shareByUrlVO.previewToggle == 0 diff --git a/app/src/main/java/org/permanent/permanent/network/models/FileData.kt b/app/src/main/java/org/permanent/permanent/network/models/FileData.kt index ad25a22c..24d2707a 100644 --- a/app/src/main/java/org/permanent/permanent/network/models/FileData.kt +++ b/app/src/main/java/org/permanent/permanent/network/models/FileData.kt @@ -52,7 +52,7 @@ class FileData private constructor() : Parcelable { } displayName = recordVO.displayName description = recordVO.description - displayDate = recordVO.displayDT?.substringBefore("T") + displayDate = recordVO.displayDT createdDate = recordVO.createdDT?.replace("T", " ") updatedDate = recordVO.updatedDT?.replace("T", " ") derivedDate = recordVO.derivedDT?.replace("T", " ") diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt index c5fff7dd..bebf83a3 100644 --- a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt @@ -46,7 +46,6 @@ import androidx.core.content.ContextCompat import org.permanent.permanent.R import org.permanent.permanent.viewmodels.EditDateTimeViewModel import java.text.SimpleDateFormat -import java.util.Calendar import java.util.Date import java.util.Locale @@ -63,23 +62,23 @@ fun EditDateTimeScreen( val openAlertDialog = remember { mutableStateOf(false) } - val datePickerState = rememberDatePickerState() + val datePickerState = rememberDatePickerState( + initialSelectedDateMillis = viewModel.initialDateMilis + ) val selectedDate = datePickerState.selectedDateMillis?.let { convertMillisToDate(it) } ?: "" var showTimePicker by remember { mutableStateOf(false) } - val currentTime = Calendar.getInstance() - val timePickerState = rememberTimePickerState( - initialHour = currentTime.get(Calendar.HOUR_OF_DAY), - initialMinute = currentTime.get(Calendar.MINUTE), - is24Hour = false, + initialHour = viewModel.initialHour, + initialMinute = viewModel.initialMinute, + is24Hour = true, ) var selectedTime by remember { mutableStateOf( - convertTimeToString(timePickerState.hour, timePickerState.minute) + convertTimeToString(timePickerState.hour, timePickerState.minute, 0) )} Column( @@ -192,7 +191,7 @@ fun EditDateTimeScreen( onDismiss = { showTimePicker = false }, onConfirm = { showTimePicker = false - selectedTime = convertTimeToString(timePickerState.hour, timePickerState.minute) + selectedTime = convertTimeToString(timePickerState.hour, timePickerState.minute, 0) } ) { TimePicker( @@ -217,7 +216,7 @@ fun EditDateTimeScreen( cancelButtonText = stringResource(id = R.string.button_cancel), onConfirm = { openAlertDialog.value = false - viewModel.updateDate(dateString = "$selectedDate $selectedTime") + viewModel.updateDate(dateString = "${selectedDate}T${selectedTime}") }) { openAlertDialog.value = false } @@ -255,14 +254,14 @@ fun TimePickerDialog( } fun convertMillisToDate(millis: Long): String { - val formatter = SimpleDateFormat("MM/dd/yyyy", Locale.getDefault()) + val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) return formatter.format(Date(millis)) } -fun convertTimeToString(hour: Int, min: Int): String { - val amPm = if (hour < 12) "AM" else "PM" - val adjustedHour = if (hour == 0) 12 else hour % 12 // Handle 0 hour as 12 AM +fun convertTimeToString(hour: Int, min: Int, sec: Int): String { + val hourString = String.format("%02d", hour) val minString = String.format("%02d", min) + val secString = String.format("%02d", sec) - return "$adjustedHour:$minString $amPm" + return "$hourString:$minString:$secString" } \ No newline at end of file diff --git a/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt b/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt index 8993d97c..b75cd1fd 100644 --- a/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt +++ b/app/src/main/java/org/permanent/permanent/viewmodels/EditDateTimeViewModel.kt @@ -8,6 +8,9 @@ import org.permanent.permanent.models.Record import org.permanent.permanent.network.IResponseListener import org.permanent.permanent.repositories.FileRepositoryImpl import org.permanent.permanent.repositories.IFileRepository +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Locale class EditDateTimeViewModel(application: Application) : ObservableAndroidViewModel(application) { private var fileRepository: IFileRepository = FileRepositoryImpl(application) @@ -15,10 +18,19 @@ class EditDateTimeViewModel(application: Application) : ObservableAndroidViewMod var shouldClose: MutableState = mutableStateOf(false) var isBusy: MutableState = mutableStateOf(false) val showMessage = mutableStateOf("") + private val onDateChanged = MutableLiveData() + private val currentTime = Calendar.getInstance() + var initialDateMilis: Long = currentTime.timeInMillis + var initialHour: Int = currentTime.get(Calendar.HOUR_OF_DAY) + var initialMinute = currentTime.get(Calendar.MINUTE) fun setRecords(records: ArrayList) { this.records.addAll(records) + records.firstOrNull()?.displayDate?.let { + val dateFormat = "yyyy-MM-dd'T'HH:mm:ss" + extractDateHourMinute(it, dateFormat) + } } fun updateDate(dateString: String) { @@ -52,4 +64,22 @@ class EditDateTimeViewModel(application: Application) : ObservableAndroidViewMod } fun getOnDateChanged() = onDateChanged -} \ No newline at end of file + + private fun extractDateHourMinute(dateString: String, dateFormat: String) { + val simpleDateFormat = SimpleDateFormat(dateFormat, Locale.getDefault()) + val date = simpleDateFormat.parse(dateString) + + val hourFormatter = SimpleDateFormat("HH", Locale.getDefault()) + val minuteFormatter = SimpleDateFormat("mm", Locale.getDefault()) + + date?.time?.let { + initialDateMilis = it + } + hourFormatter.format(date).toIntOrNull()?.let { + initialHour = it + } + minuteFormatter.format(date).toIntOrNull()?.let { + initialMinute = it + } + } +} From 6cfd7bede4345ebb8fcc24cfbc3f6aa7a465d8b9 Mon Sep 17 00:00:00 2001 From: flaviuvsp Date: Wed, 4 Sep 2024 11:48:52 +0300 Subject: [PATCH 6/6] use colorResource --- .../ui/bulkEditMetadata/compose/EditDateTimeScreen.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt index bebf83a3..8b486363 100644 --- a/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt +++ b/app/src/main/java/org/permanent/permanent/ui/bulkEditMetadata/compose/EditDateTimeScreen.kt @@ -34,6 +34,7 @@ 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.colorResource import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.TextStyle @@ -55,9 +56,8 @@ fun EditDateTimeScreen( viewModel: EditDateTimeViewModel, cancel: () -> Unit) { - val context = LocalContext.current - val superLightBlue = Color(ContextCompat.getColor(context, R.color.superLightBlue)) - val blue900 = Color(ContextCompat.getColor(context, R.color.blue900)) + val superLightBlue = colorResource(id = R.color.superLightBlue) + val blue900 = colorResource(R.color.blue900) val regularFont = FontFamily(Font(R.font.open_sans_regular_ttf)) val openAlertDialog = remember { mutableStateOf(false) }