Не подтверждена Коммит 674bfbf5 создал по автору Vitaly.Smirnov's avatar Vitaly.Smirnov Зафиксировано автором GitHub
Просмотр файлов

feat: Add localized time format 12h and 24h (#353)

владелец 3b7f1aaf
...@@ -8,4 +8,6 @@ import java.util.Locale ...@@ -8,4 +8,6 @@ import java.util.Locale
actual fun LocalDateTime.toLocalisedString(pattern: String): String { actual fun LocalDateTime.toLocalisedString(pattern: String): String {
val formatter = DateTimeFormatter.ofPattern(pattern, Locale.getDefault()) val formatter = DateTimeFormatter.ofPattern(pattern, Locale.getDefault())
return this.toJavaLocalDateTime().format(formatter) return this.toJavaLocalDateTime().format(formatter)
} }
\ No newline at end of file
actual fun getCurrentLanguageCode(): String = Locale.getDefault().language
\ No newline at end of file
package band.effective.office.tablet.core.ui.utils package band.effective.office.tablet.core.ui.utils
import kotlinx.datetime.LocalDateTime import kotlinx.datetime.LocalDateTime
/**
* Data class to store date and time format patterns for a specific locale.
*
* @param default Format for full date and time.
* @param future Format for future dates.
* @param time Format for time only.
*/
private data class LocaleFormatPatterns(
val default: String,
val future: String,
val time: String
)
/**
* Utility object for formatting dates and times based on the current locale.
* Falls back to English formats for unsupported locales.
*/
object DateDisplayMapper { object DateDisplayMapper {
private const val FUTURE_DATE_FORMAT = "d MMMM" private val formatPatterns: Map<String, LocaleFormatPatterns> = mapOf(
private const val DEFAULT_DATE_FORMAT = "HH:mm, d MMMM" "ru" to LocaleFormatPatterns(
default = "d MMMM, HH:mm",
future = "d MMMM",
time = "HH:mm"
),
"en" to LocaleFormatPatterns(
default = "MMM d h:mm a",
future = "MMM d",
time = "h:mm a"
)
)
private val defaultFormats: LocaleFormatPatterns = formatPatterns["en"]!!
private fun getPatternsForLocale(): LocaleFormatPatterns {
val currentLanguage = getCurrentLanguageCode()
return formatPatterns[currentLanguage] ?: defaultFormats
}
fun map(selectDate: LocalDateTime, currentDate: LocalDateTime?): String { fun map(selectDate: LocalDateTime, currentDate: LocalDateTime?): String {
val isFutureDate = currentDate != null && selectDate.date > currentDate.date val patterns = getPatternsForLocale()
val pattern = if (isFutureDate) FUTURE_DATE_FORMAT else DEFAULT_DATE_FORMAT val pattern = if (currentDate != null && selectDate.date > currentDate.date) {
patterns.future
} else {
patterns.default
}
return selectDate.toLocalisedString(pattern) return selectDate.toLocalisedString(pattern)
} }
fun formatForPicker(date: LocalDateTime): String { fun formatForPicker(date: LocalDateTime): String {
return date.toLocalisedString(FUTURE_DATE_FORMAT) val patterns = getPatternsForLocale()
return date.toLocalisedString(patterns.future)
}
fun formatTime(time: LocalDateTime): String {
val patterns = getPatternsForLocale()
return time.toLocalisedString(patterns.time)
} }
} }
\ No newline at end of file
...@@ -4,3 +4,4 @@ import kotlinx.datetime.LocalDateTime ...@@ -4,3 +4,4 @@ import kotlinx.datetime.LocalDateTime
@OptIn(kotlinx.datetime.format.FormatStringsInDatetimeFormats::class) @OptIn(kotlinx.datetime.format.FormatStringsInDatetimeFormats::class)
expect fun LocalDateTime.toLocalisedString(pattern: String): String expect fun LocalDateTime.toLocalisedString(pattern: String): String
expect fun getCurrentLanguageCode(): String
\ No newline at end of file
...@@ -20,4 +20,6 @@ actual fun LocalDateTime.toLocalisedString(pattern: String): String { ...@@ -20,4 +20,6 @@ actual fun LocalDateTime.toLocalisedString(pattern: String): String {
val date = calendar.dateFromComponents(dateComponents) ?: NSDate() val date = calendar.dateFromComponents(dateComponents) ?: NSDate()
return dateFormatter.stringFromDate(date) return dateFormatter.stringFromDate(date)
} }
\ No newline at end of file
actual fun getCurrentLanguageCode(): String = NSLocale.currentLocale.languageCode
\ No newline at end of file
...@@ -9,9 +9,9 @@ import androidx.compose.runtime.Composable ...@@ -9,9 +9,9 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import band.effective.office.tablet.core.domain.util.toFormattedString
import band.effective.office.tablet.core.ui.theme.h5 import band.effective.office.tablet.core.ui.theme.h5
import band.effective.office.tablet.core.ui.theme.h7 import band.effective.office.tablet.core.ui.theme.h7
import band.effective.office.tablet.core.ui.utils.DateDisplayMapper
import band.effective.office.tablet.feature.slot.presentation.SlotUi import band.effective.office.tablet.feature.slot.presentation.SlotUi
@Composable @Composable
...@@ -28,7 +28,7 @@ fun CommonSlotView( ...@@ -28,7 +28,7 @@ fun CommonSlotView(
) { ) {
Column { Column {
Text( Text(
text = "${slot.start.toFormattedString("HH:mm")} - ${slot.finish.toFormattedString("HH:mm")}", text = "${DateDisplayMapper.formatTime(slot.start)} ${DateDisplayMapper.formatTime(slot.finish)}",
style = MaterialTheme.typography.h5, style = MaterialTheme.typography.h5,
color = MaterialTheme.colorScheme.onPrimary color = MaterialTheme.colorScheme.onPrimary
) )
......
...@@ -9,11 +9,11 @@ import androidx.compose.runtime.Composable ...@@ -9,11 +9,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import band.effective.office.tablet.core.domain.util.toFormattedString
import band.effective.office.tablet.core.ui.Res import band.effective.office.tablet.core.ui.Res
import band.effective.office.tablet.core.ui.loading_slot_for_time import band.effective.office.tablet.core.ui.loading_slot_for_time
import band.effective.office.tablet.core.ui.theme.h5 import band.effective.office.tablet.core.ui.theme.h5
import band.effective.office.tablet.core.ui.theme.h7 import band.effective.office.tablet.core.ui.theme.h7
import band.effective.office.tablet.core.ui.utils.DateDisplayMapper
import band.effective.office.tablet.feature.slot.presentation.SlotUi import band.effective.office.tablet.feature.slot.presentation.SlotUi
import org.jetbrains.compose.resources.stringResource import org.jetbrains.compose.resources.stringResource
...@@ -33,8 +33,8 @@ fun LoadingSlotView( ...@@ -33,8 +33,8 @@ fun LoadingSlotView(
Text( Text(
text = stringResource( text = stringResource(
Res.string.loading_slot_for_time, Res.string.loading_slot_for_time,
slot.start.toFormattedString("HH:mm"), DateDisplayMapper.formatTime(slot.start),
slot.finish.toFormattedString("HH:mm") DateDisplayMapper.formatTime(slot.finish)
), ),
style = MaterialTheme.typography.h5, style = MaterialTheme.typography.h5,
color = MaterialTheme.colorScheme.onPrimary color = MaterialTheme.colorScheme.onPrimary
......
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать