Skip to content

Commit

Permalink
Localised time strings working - even if the unit tests aren't
Browse files Browse the repository at this point in the history
  • Loading branch information
alansley committed Aug 9, 2024
1 parent 436175d commit 73fdb16
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 2 deletions.
127 changes: 127 additions & 0 deletions app/src/main/java/org/thoughtcrime/securesms/home/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.format.DateUtils
import android.widget.Toast
import androidx.activity.viewModels
import androidx.annotation.PluralsRes
Expand Down Expand Up @@ -88,6 +89,11 @@ import java.io.IOException
import java.util.Calendar
import java.util.Locale
import javax.inject.Inject
import kotlin.math.abs
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

private const val NEW_ACCOUNT = "HomeActivity_NEW_ACCOUNT"
private const val FROM_ONBOARDING = "HomeActivity_FROM_ONBOARDING"
Expand Down Expand Up @@ -154,10 +160,131 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
private val isFromOnboarding: Boolean get() = intent.getBooleanExtra(FROM_ONBOARDING, false)
private val isNewAccount: Boolean get() = intent.getBooleanExtra(NEW_ACCOUNT, false)







// ACL REMOVE BLOCK START

fun getRelativeTimeLocalized(timestampMS: Long): String {
// Get the current system time
val nowMS = System.currentTimeMillis()

// Calculate the time difference in milliseconds - this value will be negative if it's in the
// future or positive if it's in the past.
val timeDifferenceMS = nowMS - timestampMS

// Choose a desired time resolution based on the time difference.
// Note: We do this against the absolute time difference so this function can still work for
// both future/past times without having separate future/past cases.
val desiredResolution = when (abs(timeDifferenceMS)) {
in 0..DateUtils.MINUTE_IN_MILLIS -> DateUtils.SECOND_IN_MILLIS
in DateUtils.MINUTE_IN_MILLIS..DateUtils.HOUR_IN_MILLIS -> DateUtils.MINUTE_IN_MILLIS
in DateUtils.HOUR_IN_MILLIS..DateUtils.DAY_IN_MILLIS -> DateUtils.HOUR_IN_MILLIS
in DateUtils.DAY_IN_MILLIS..DateUtils.WEEK_IN_MILLIS -> DateUtils.DAY_IN_MILLIS

// We don't do months or years, so if the result is 53 weeks then so be it - also, the
// getRelativeTimeSpanString method's resolution maxes out at weeks!
else -> DateUtils.WEEK_IN_MILLIS
}

// Use DateUtils to get the relative time span string
return DateUtils.getRelativeTimeSpanString(
timestampMS,
nowMS,
desiredResolution,
DateUtils.FORMAT_ABBREV_RELATIVE
// Using either DateUtils.FORMAT_ABBREV_RELATIVE or DateUtils.FORMAT_ABBREV_ALL gives:
// - 1 sec. ago / 2 sec. ago
// - 1 min. ago / 2 min. ago
// - 1 hr. ago / 2 hr. ago
// - Yesterday / 2 days ago
// - August 2 / 2 wk. ago / 14 wk. ago <-- Note: Date running this test is August 9th.

// Using either 0 or DateUtils.FORMAT_ABBREV_TIME gives:
// - 1 second ago / 2 seconds ago
// - 1 minute ago / 2 minutes ago
// - 1 hour ago / 2 hours ago
// - Yesterday / 2 days ago
// - August 2nd / 2 weeks ago / 14 weeks ago <-- Note: Date running this test is August 9th.

//DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_ABBREV_RELATIVE // Try this w/ just FORMAT_ABBREV_RELATIVE
).toString()
}

fun testRelativeTimes(op: (Long, Long) -> Long) {
var t = 0L

// 1 and 2 seconds
t = op(System.currentTimeMillis(), 1.seconds.inWholeMilliseconds)
Log.w("ACL", "1s -> ${getRelativeTimeLocalized(t)}")
t = op(System.currentTimeMillis(), 2.seconds.inWholeMilliseconds)
Log.w("ACL", "2s -> ${getRelativeTimeLocalized(t)}")

// 1 and 2 minutes
t = op(System.currentTimeMillis(), 1.minutes.inWholeMilliseconds)
Log.w("ACL", "1m -> ${getRelativeTimeLocalized(t)}")
t = op(System.currentTimeMillis(), 2.minutes.inWholeMilliseconds)
Log.w("ACL", "2m -> ${getRelativeTimeLocalized(t)}")

// 1 and 2 hours
t = op(System.currentTimeMillis(), 1.hours.inWholeMilliseconds)
Log.w("ACL", "1h -> ${getRelativeTimeLocalized(t)}")
t = op(System.currentTimeMillis(), 2.hours.inWholeMilliseconds)
Log.w("ACL", "2h -> ${getRelativeTimeLocalized(t)}")

// 1 and 2 days
t = op(System.currentTimeMillis(), 1.days.inWholeMilliseconds)
Log.w("ACL", "1d -> ${getRelativeTimeLocalized(t)}")
t = op(System.currentTimeMillis(), 2.days.inWholeMilliseconds)
Log.w("ACL", "2d -> ${getRelativeTimeLocalized(t)}")

// 1 week, 2 weeks, and 100 days (14.285 weeks)
t = op(System.currentTimeMillis(), 7.days.inWholeMilliseconds)
Log.w("ACL", "1w -> ${getRelativeTimeLocalized(t)}")
t = op(System.currentTimeMillis(), 14.days.inWholeMilliseconds)
Log.w("ACL", "2w -> ${getRelativeTimeLocalized(t)}")
t = op(System.currentTimeMillis(), 100.days.inWholeMilliseconds)
Log.w("ACL", "100d -> ${getRelativeTimeLocalized(t)}")
}

fun testSystemGeneratedRelativeTimes() {
// Print relative times in the past
var op: (Long, Long) -> Long = Long::minus
testRelativeTimes(op)

// Print relative times in the future
op = Long::plus
testRelativeTimes(op)
}

// ACL REMOVE BLOCK END
















// region Lifecycle
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
super.onCreate(savedInstanceState, isReady)

// ACL REMOVE THIS WHEN UNIT TESTS ARE FIXED
//Locale.setDefault(Locale.FRENCH)
//testSystemGeneratedRelativeTimes()

// Set content view
binding = ActivityHomeBinding.inflate(layoutInflater)
setContentView(binding.root)
Expand Down
6 changes: 6 additions & 0 deletions libsession/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ android {
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}

sourceSets {
test {
java.srcDirs = ['src/AndroidTest/java/org/session/libsession']
}
}
}

compileOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import org.session.libsession.LocalisedTimeUtil.toShortTwoPartString
import org.session.libsignal.utilities.Log

import android.text.format.DateUtils
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlin.math.abs


Expand Down

0 comments on commit 73fdb16

Please sign in to comment.