Skip to content

Commit

Permalink
#539 PrettyTime Missing Unit Workaround (#687)
Browse files Browse the repository at this point in the history
A bug in PrettyTime causes four languages to not get any time units when formatted. We can work around this by detecting an invalid prettyDate (rather than hardcoding a list of broken languages) and switching to English formatting instead.

ocpsoft/prettytime#259

Co-authored-by: Dessalines <[email protected]>
  • Loading branch information
lbenedetto and dessalines authored Jun 19, 2023
1 parent 7799993 commit 6a8b71b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
24 changes: 22 additions & 2 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ import java.util.*
import kotlin.math.abs
import kotlin.math.pow

val prettyTime = PrettyTime(Locale.getDefault())

val gson = Gson()

const val DEBOUNCE_DELAY = 1000L
Expand Down Expand Up @@ -394,6 +392,28 @@ fun openLink(url: String, navController: NavController, useCustomTab: Boolean, u
}
}

var prettyTime = PrettyTime(Locale.getDefault())
var prettyTimeEnglish = PrettyTime(Locale.ENGLISH)
val invalidPrettyDateRegex = "^[0123456789 ]+$".toRegex()
fun formatDuration(date: Date, longTimeFormat: Boolean = false): String {
if (prettyTime.locale != Locale.getDefault()) {
prettyTime = PrettyTime(Locale.getDefault())
}

var prettyDate = prettyTime.formatDuration(date)

// A bug in PrettyTime means that some languages (pl, ru, uk, kk) will not include any time unit
if (prettyDate.matches(invalidPrettyDateRegex)) {
prettyDate = prettyTimeEnglish.formatDuration(date)
}

return if (longTimeFormat) {
prettyDate
} else {
prettyTimeShortener(prettyDate)
}
}

fun prettyTimeShortener(timeString: String): String {
return if (prettyTime.locale.language == "en") {
if (timeString.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ fun Sidebar(
}
TimeAgo(
precedingString = stringResource(R.string.AppBars_created),
includeAgo = true,
longTimeFormat = true,
published = published,
)
CommentsAndPosts(
Expand Down
19 changes: 6 additions & 13 deletions app/src/main/java/com/jerboa/ui/components/common/TimeAgo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import androidx.compose.ui.unit.dp
import com.jerboa.R
import com.jerboa.datatypes.samplePerson
import com.jerboa.datatypes.samplePost
import com.jerboa.prettyTime
import com.jerboa.prettyTimeShortener
import com.jerboa.formatDuration
import com.jerboa.ui.theme.SMALL_PADDING
import com.jerboa.ui.theme.muted
import java.time.Instant
Expand All @@ -37,9 +36,9 @@ fun TimeAgo(
published: String,
updated: String? = null,
precedingString: String? = null,
includeAgo: Boolean = false,
longTimeFormat: Boolean = false,
) {
val publishedPretty = dateStringToPretty(published, includeAgo)
val publishedPretty = dateStringToPretty(published, longTimeFormat)

val afterPreceding = precedingString?.let {
stringResource(R.string.time_ago_ago, it, publishedPretty)
Expand All @@ -53,7 +52,7 @@ fun TimeAgo(
)

updated?.also {
val updatedPretty = dateStringToPretty(it, includeAgo)
val updatedPretty = dateStringToPretty(it, longTimeFormat)

DotSpacer(
padding = SMALL_PADDING,
Expand All @@ -69,15 +68,9 @@ fun TimeAgo(
}
}

fun dateStringToPretty(dateStr: String, includeAgo: Boolean = false): String {
fun dateStringToPretty(dateStr: String, longTimeFormat: Boolean = false): String {
val publishedDate = Date.from(Instant.parse(dateStr + "Z"))
val prettyPublished = prettyTime.formatDuration(publishedDate)

return if (includeAgo) {
prettyPublished
} else {
prettyTimeShortener(prettyPublished)
}
return formatDuration(publishedDate, longTimeFormat)
}

@Preview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fun PersonProfileTopSection(

TimeAgo(
precedingString = stringResource(R.string.person_profile_joined),
includeAgo = true,
longTimeFormat = true,
published = personView.person.published,
)
CommentsAndPosts(personView)
Expand Down
25 changes: 25 additions & 0 deletions app/src/test/java/com/jerboa/UtilsKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import com.jerboa.api.API
import com.jerboa.ui.theme.SMALL_PADDING
import org.junit.Assert.*
import org.junit.Test
import org.ocpsoft.prettytime.PrettyTime
import java.time.Duration
import java.time.Instant
import java.util.Date
import java.util.Locale

class UtilsKtTest {
@Test
Expand Down Expand Up @@ -147,4 +152,24 @@ class UtilsKtTest {

cases.forEach { (url, exp) -> assertEquals(exp, parseUrl(url)) }
}

@Test
fun testBrokenLanguagesRemappedToEnglish() {
listOf("pl", "ru", "uk", "kk").forEach { locale ->
val date = Date.from(Instant.now().minus(Duration.ofDays(1)))
prettyTime = PrettyTime(Locale(locale))

val durationString = formatDuration(date, true)
assertNotEquals("1", durationString)
}
}

@Test
fun testEnglish() {
val date = Date.from(Instant.now().minus(Duration.ofDays(1)))
Locale.setDefault(Locale.ENGLISH)

val durationString = formatDuration(date, true)
assertEquals("1 day", durationString)
}
}

0 comments on commit 6a8b71b

Please sign in to comment.