Refactor UnittoDateTimeFormatter

This commit is contained in:
sadellie 2023-08-01 23:38:47 +03:00
parent ca14a2d056
commit 9cf90560c0
6 changed files with 68 additions and 121 deletions

View File

@ -1,27 +0,0 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.core.ui.datetime
import java.time.format.DateTimeFormatter
// FIXME Duplicate from date difference
internal val time24Formatter by lazy { DateTimeFormatter.ofPattern("HH:mm") }
internal val time12Formatter by lazy { DateTimeFormatter.ofPattern("hh:mm a") }
internal val dayMonthYear by lazy { DateTimeFormatter.ofPattern("d MMM y") }
internal val zoneFormatPattern by lazy { DateTimeFormatter.ofPattern("O") }

View File

@ -1,48 +0,0 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.core.ui.datetime
import android.text.format.DateFormat
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import java.time.LocalDateTime
@Composable
fun LocalDateTime.formatLocal(): String {
return if (DateFormat.is24HourFormat(LocalContext.current)) format24()
else format12()
}
/**
* Formats [LocalDateTime] into string that looks like
*
* 23:58
*
* @return Formatted string.
*/
fun LocalDateTime.format24(): String = this.format(time24Formatter)
/**
* Formats [LocalDateTime] into string that looks like
*
* 11:58 am
*
* @return Formatted string.
*/
fun LocalDateTime.format12(): String = this.format(time12Formatter)

View File

@ -0,0 +1,58 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.core.ui.datetime
import java.time.format.DateTimeFormatter
data object UnittoDateTimeFormatter {
/**
* 23:59
*/
val time24Formatter: DateTimeFormatter by lazy { DateTimeFormatter.ofPattern("HH:mm") }
/**
* 11:59 AM
*/
val time12FormatterFull: DateTimeFormatter by lazy { DateTimeFormatter.ofPattern("hh:mm a") }
/**
* 11:59
*/
val time12Formatter1: DateTimeFormatter by lazy { DateTimeFormatter.ofPattern("hh:mm") }
/**
* AM
*/
val time12Formatter2: DateTimeFormatter by lazy { DateTimeFormatter.ofPattern("a") }
/**
* 31 Dec 2077
*/
val dayMonthYear: DateTimeFormatter by lazy { DateTimeFormatter.ofPattern("d MMM y") }
/**
* Mon, 31 Dec, 2077
*/
val weekDayMonthYear: DateTimeFormatter by lazy { DateTimeFormatter.ofPattern("EEE, MMM d, y") }
/**
* GMT+3
*/
val zoneFormatPattern: DateTimeFormatter by lazy { DateTimeFormatter.ofPattern("O") }
}

View File

@ -29,39 +29,10 @@ import kotlin.math.absoluteValue
@Composable @Composable
fun ZonedDateTime.formatLocal(): String { fun ZonedDateTime.formatLocal(): String {
return if (DateFormat.is24HourFormat(LocalContext.current)) format24() return if (DateFormat.is24HourFormat(LocalContext.current)) format(UnittoDateTimeFormatter.time24Formatter)
else format12() else format(UnittoDateTimeFormatter.time12FormatterFull)
} }
/**
* Formats [ZonedDateTime] into string that looks like
*
* 23:58
*
* @return Formatted string.
*/
fun ZonedDateTime.format24(): String = this.format(time24Formatter)
/**
* Formats [ZonedDateTime] into string that looks like
*
* 11:58 am
*
* @return Formatted string.
*/
fun ZonedDateTime.format12(): String = this.format(time12Formatter)
/**
* Formats [ZonedDateTime] into string that looks like
*
* 21 Jul 2023
*
* @return Formatted string.
*/
fun ZonedDateTime.formatDayMonthYear(): String = this.format(dayMonthYear)
fun ZonedDateTime.formatTimeZoneOffset(): String = this.format(zoneFormatPattern)
/** /**
* Format offset string. Examples: * Format offset string. Examples:
* *

View File

@ -36,8 +36,8 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.sadellie.unitto.core.ui.common.squashable import com.sadellie.unitto.core.ui.common.squashable
import com.sadellie.unitto.core.ui.datetime.UnittoDateTimeFormatter
import java.time.ZonedDateTime import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
@Composable @Composable
internal fun DateTimeSelectorBlock( internal fun DateTimeSelectorBlock(
@ -70,7 +70,7 @@ internal fun DateTimeSelectorBlock(
interactionSource = remember { MutableInteractionSource() }, interactionSource = remember { MutableInteractionSource() },
onClick = onTimeClick onClick = onTimeClick
), ),
text = dateTime.format(time24Formatter), text = dateTime.format(UnittoDateTimeFormatter.time24Formatter),
style = MaterialTheme.typography.displaySmall, style = MaterialTheme.typography.displaySmall,
maxLines = 1 maxLines = 1
) )
@ -83,12 +83,12 @@ internal fun DateTimeSelectorBlock(
) )
) { ) {
Text( Text(
text = dateTime.format(time12Formatter), text = dateTime.format(UnittoDateTimeFormatter.time12Formatter1),
style = MaterialTheme.typography.displaySmall, style = MaterialTheme.typography.displaySmall,
maxLines = 1 maxLines = 1
) )
Text( Text(
text = dateTime.format(mTimeFormatter), text = dateTime.format(UnittoDateTimeFormatter.time12Formatter2),
style = MaterialTheme.typography.bodyLarge, style = MaterialTheme.typography.bodyLarge,
maxLines = 1 maxLines = 1
) )
@ -101,17 +101,12 @@ internal fun DateTimeSelectorBlock(
interactionSource = remember { MutableInteractionSource() }, interactionSource = remember { MutableInteractionSource() },
onClick = onDateClick onClick = onDateClick
), ),
text = dateTime.format(dateFormatter), text = dateTime.format(UnittoDateTimeFormatter.weekDayMonthYear),
style = MaterialTheme.typography.bodySmall style = MaterialTheme.typography.bodySmall
) )
} }
} }
private val time24Formatter by lazy { DateTimeFormatter.ofPattern("HH:mm") }
private val time12Formatter by lazy { DateTimeFormatter.ofPattern("hh:mm") }
private val dateFormatter by lazy { DateTimeFormatter.ofPattern("EEE, MMM d, y") }
private val mTimeFormatter by lazy { DateTimeFormatter.ofPattern("a") }
@Preview @Preview
@Composable @Composable
fun DateTimeSelectorBlockPreview() { fun DateTimeSelectorBlockPreview() {

View File

@ -83,9 +83,8 @@ import com.sadellie.unitto.core.ui.common.SettingsButton
import com.sadellie.unitto.core.ui.common.TimePickerDialog import com.sadellie.unitto.core.ui.common.TimePickerDialog
import com.sadellie.unitto.core.ui.common.UnittoScreenWithTopBar import com.sadellie.unitto.core.ui.common.UnittoScreenWithTopBar
import com.sadellie.unitto.core.ui.common.squashable import com.sadellie.unitto.core.ui.common.squashable
import com.sadellie.unitto.core.ui.datetime.formatDayMonthYear import com.sadellie.unitto.core.ui.datetime.UnittoDateTimeFormatter
import com.sadellie.unitto.core.ui.datetime.formatLocal import com.sadellie.unitto.core.ui.datetime.formatLocal
import com.sadellie.unitto.core.ui.datetime.formatTimeZoneOffset
import com.sadellie.unitto.core.ui.theme.AppTypography import com.sadellie.unitto.core.ui.theme.AppTypography
import com.sadellie.unitto.core.ui.theme.DarkThemeColors import com.sadellie.unitto.core.ui.theme.DarkThemeColors
import com.sadellie.unitto.core.ui.theme.LightThemeColors import com.sadellie.unitto.core.ui.theme.LightThemeColors
@ -286,11 +285,10 @@ private fun UserTimeZone(
) { ) {
Column(Modifier.weight(1f)) { Column(Modifier.weight(1f)) {
Text( Text(
text = userTime.formatTimeZoneOffset(), text = userTime.format(UnittoDateTimeFormatter.zoneFormatPattern),
style = MaterialTheme.typography.bodyLarge, style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onTertiaryContainer color = MaterialTheme.colorScheme.onTertiaryContainer
) )
// TODO Swipe to increase, touch to set
AnimatedContent( AnimatedContent(
targetState = userTime.formatLocal(), targetState = userTime.formatLocal(),
label = "user time change", label = "user time change",
@ -307,7 +305,7 @@ private fun UserTimeZone(
) )
} }
Text( Text(
text = userTime.formatDayMonthYear(), text = userTime.format(UnittoDateTimeFormatter.dayMonthYear),
style = MaterialTheme.typography.headlineMedium, style = MaterialTheme.typography.headlineMedium,
color = MaterialTheme.colorScheme.onTertiaryContainer color = MaterialTheme.colorScheme.onTertiaryContainer
) )