mirror of
https://github.com/Suwayomi/TachideskJUI.git
synced 2025-12-10 06:42:05 +01:00
Move date handler out of data
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package ca.gosyer.jui.core.util
|
||||
|
||||
import kotlinx.datetime.Instant
|
||||
|
||||
@Suppress("NO_ACTUAL_FOR_EXPECT")
|
||||
expect class DateHandler {
|
||||
val formatOptions: List<String>
|
||||
|
||||
fun getDateFormat(format: String): (Instant) -> String
|
||||
|
||||
val dateTimeFormat: (Instant) -> String
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package ca.gosyer.jui.core.util
|
||||
|
||||
import androidx.compose.ui.text.intl.Locale
|
||||
import ca.gosyer.jui.core.lang.toPlatform
|
||||
import kotlinx.datetime.Instant
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
|
||||
actual class DateHandler
|
||||
@Inject
|
||||
constructor() {
|
||||
actual val formatOptions by lazy {
|
||||
listOf(
|
||||
"",
|
||||
"MM/dd/yy",
|
||||
"dd/MM/yy",
|
||||
"yyyy-MM-dd",
|
||||
)
|
||||
}
|
||||
|
||||
actual fun getDateFormat(format: String): (Instant) -> String =
|
||||
when (format) {
|
||||
"" -> NSDateFormatter()
|
||||
.apply {
|
||||
setDateStyle(NSDateFormatterShortStyle)
|
||||
setTimeStyle(NSDateFormatterNoStyle)
|
||||
setLocale(Locale.current.toPlatform())
|
||||
}
|
||||
|
||||
else -> NSDateFormatter()
|
||||
.apply {
|
||||
setDateFormat(format)
|
||||
}
|
||||
}.let { formatter ->
|
||||
{
|
||||
formatter.stringFromDate(it.toNSDate())
|
||||
}
|
||||
}
|
||||
|
||||
actual val dateTimeFormat: (Instant) -> String by lazy {
|
||||
NSDateFormatter()
|
||||
.apply {
|
||||
setDateStyle(NSDateFormatterShortStyle)
|
||||
setTimeStyle(NSDateFormatterShortStyle)
|
||||
setLocale(Locale.current.toPlatform())
|
||||
}
|
||||
.let { formatter ->
|
||||
{
|
||||
formatter.stringFromDate(it.toNSDate())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package ca.gosyer.jui.core.util
|
||||
|
||||
import androidx.compose.ui.text.intl.Locale
|
||||
import ca.gosyer.jui.core.lang.toPlatform
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.datetime.toJavaInstant
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
import java.time.ZoneId
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.format.FormatStyle
|
||||
|
||||
actual class DateHandler
|
||||
@Inject
|
||||
constructor() {
|
||||
actual val formatOptions by lazy {
|
||||
listOf(
|
||||
"",
|
||||
"MM/dd/yy",
|
||||
"dd/MM/yy",
|
||||
"yyyy-MM-dd",
|
||||
)
|
||||
}
|
||||
|
||||
actual fun getDateFormat(format: String): (Instant) -> String =
|
||||
when (format) {
|
||||
"" -> DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
|
||||
.withLocale(Locale.current.toPlatform())
|
||||
.withZone(ZoneId.systemDefault())
|
||||
|
||||
else -> DateTimeFormatter.ofPattern(format)
|
||||
.withZone(ZoneId.systemDefault())
|
||||
}.let { formatter ->
|
||||
{
|
||||
formatter.format(it.toJavaInstant())
|
||||
}
|
||||
}
|
||||
|
||||
actual val dateTimeFormat: (Instant) -> String by lazy {
|
||||
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
|
||||
.withLocale(Locale.current.toPlatform())
|
||||
.withZone(ZoneId.systemDefault())
|
||||
.let { formatter ->
|
||||
{
|
||||
formatter.format(it.toJavaInstant())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user