Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): format time with system locale default #617

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions app/src/main/java/me/ash/reader/ui/ext/DateExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package me.ash.reader.ui.ext

import android.annotation.SuppressLint
import android.content.Context
import androidx.core.os.ConfigurationCompat
import me.ash.reader.R
import java.text.DateFormat
import java.text.ParsePosition
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

@SuppressLint("SimpleDateFormat")
object DateFormat {
Expand All @@ -25,18 +25,18 @@ fun Date.formatAsString(
onlyHourMinute: Boolean? = false,
atHourMinute: Boolean? = false,
): String {
val locale = ConfigurationCompat.getLocales(context.resources.configuration)[0]
val locale = Locale.getDefault()
val df = DateFormat.getDateInstance(DateFormat.FULL, locale)
return when {
onlyHourMinute == true -> {
SimpleDateFormat("HH:mm", locale).format(this)
this.toTimeString(context = context)
}

atHourMinute == true -> {
context.getString(
R.string.date_at_time,
df.format(this),
SimpleDateFormat("HH:mm", locale).format(this),
this.toTimeString(context = context),
)
}

Expand All @@ -59,6 +59,10 @@ fun Date.formatAsString(
}
}

private fun Date.toTimeString(context: Context): String =
android.text.format.DateFormat.getTimeFormat(context).format(this)


private fun String.parseToDate(
patterns: Array<String> = arrayOf(
"yyyy-MM-dd'T'HH:mm:ss'Z'",
Expand Down
Loading