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

fix: make SelfDeletionTimerTest unit instead of instrumented #2657

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package com.wire.android.ui.home.conversations

import android.content.Context
import android.content.res.Resources
import androidx.annotation.VisibleForTesting
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand Down Expand Up @@ -50,23 +48,44 @@ import kotlin.time.toDuration

@Composable
fun rememberSelfDeletionTimer(expirationStatus: ExpirationStatus): SelfDeletionTimerHelper.SelfDeletionTimerState {
val context = LocalContext.current
val stringResourceProvider: StringResourceProvider = stringResourceProvider()
val currentTimeProvider: CurrentTimeProvider = { Clock.System.now() }

return remember(
(expirationStatus as? ExpirationStatus.Expirable)?.selfDeletionStatus ?: true
) { SelfDeletionTimerHelper(context).fromExpirationStatus(expirationStatus) }
return remember((expirationStatus as? ExpirationStatus.Expirable)?.selfDeletionStatus ?: true) {
SelfDeletionTimerHelper(stringResourceProvider, currentTimeProvider)
.fromExpirationStatus(expirationStatus)
}
}

class SelfDeletionTimerHelper(private val context: Context) {
@Composable
private fun stringResourceProvider(): StringResourceProvider {
with(LocalContext.current.resources) {
return object : StringResourceProvider {
override fun quantityString(type: StringResourceType, quantity: Int): String =
getQuantityString(
when (type) {
StringResourceType.WEEKS -> R.plurals.weeks_left
StringResourceType.DAYS -> R.plurals.days_left
StringResourceType.HOURS -> R.plurals.hours_left
StringResourceType.MINUTES -> R.plurals.minutes_left
StringResourceType.SECONDS -> R.plurals.seconds_left
}, quantity, quantity
)
}
}
}

class SelfDeletionTimerHelper(private val stringResourceProvider: StringResourceProvider, private val currentTime: CurrentTimeProvider) {

fun fromExpirationStatus(expirationStatus: ExpirationStatus): SelfDeletionTimerState {
return if (expirationStatus is ExpirationStatus.Expirable) {
with(expirationStatus) {
val expireAt = calculateExpireAt(selfDeletionStatus, expireAfter)
SelfDeletionTimerState.Expirable(
context.resources,
stringResourceProvider,
expireAfter,
expireAt,
currentTime
)
}
} else {
Expand All @@ -87,9 +106,10 @@ class SelfDeletionTimerHelper(private val context: Context) {
sealed class SelfDeletionTimerState {

class Expirable(
private val resources: Resources,
private val stringResourceProvider: StringResourceProvider,
private val expireAfter: Duration,
private val expireAt: Instant,
private val currentTime: CurrentTimeProvider,
) : SelfDeletionTimerState() {
companion object {
/**
Expand All @@ -115,60 +135,28 @@ class SelfDeletionTimerHelper(private val context: Context) {
val timeLeftFormatted: String by derivedStateOf {
when {
timeLeft > 28.days ->
resources.getQuantityString(
R.plurals.weeks_left,
4,
4
)
stringResourceProvider.quantityString(StringResourceType.WEEKS, 4)
// 4 weeks
timeLeft >= 27.days && timeLeft <= 28.days ->
resources.getQuantityString(
R.plurals.weeks_left,
4,
4
)
stringResourceProvider.quantityString(StringResourceType.WEEKS, 4)
// days below 4 weeks
timeLeft <= 27.days && timeLeft > 7.days ->
resources.getQuantityString(
R.plurals.days_left,
timeLeft.inWholeDays.toInt(),
timeLeft.inWholeDays.toInt()
)
stringResourceProvider.quantityString(StringResourceType.DAYS, timeLeft.inWholeDays.toInt())
// one week
timeLeft >= 6.days && timeLeft <= 7.days ->
resources.getQuantityString(
R.plurals.weeks_left,
1,
1
)
stringResourceProvider.quantityString(StringResourceType.WEEKS, 1)
// days below 1 week
timeLeft < 7.days && timeLeft >= 1.days ->
resources.getQuantityString(
R.plurals.days_left,
timeLeft.inWholeDays.toInt(),
timeLeft.inWholeDays.toInt()
)
stringResourceProvider.quantityString(StringResourceType.DAYS, timeLeft.inWholeDays.toInt())
// hours below one day
timeLeft >= 1.hours && timeLeft < 24.hours ->
resources.getQuantityString(
R.plurals.hours_left,
timeLeft.inWholeHours.toInt(),
timeLeft.inWholeHours.toInt()
)
stringResourceProvider.quantityString(StringResourceType.HOURS, timeLeft.inWholeHours.toInt())
// minutes below hour
timeLeft >= 1.minutes && timeLeft < 60.minutes ->
resources.getQuantityString(
R.plurals.minutes_left,
timeLeft.inWholeMinutes.toInt(),
timeLeft.inWholeMinutes.toInt()
)
stringResourceProvider.quantityString(StringResourceType.MINUTES, timeLeft.inWholeMinutes.toInt())
// seconds below minute
timeLeft < 60.seconds ->
resources.getQuantityString(
R.plurals.seconds_left,
timeLeft.inWholeSeconds.toInt(),
timeLeft.inWholeSeconds.toInt()
)
stringResourceProvider.quantityString(StringResourceType.SECONDS, timeLeft.inWholeSeconds.toInt())

else -> throw IllegalStateException("Not possible state for a time left label")
}
Expand Down Expand Up @@ -327,8 +315,10 @@ class SelfDeletionTimerHelper(private val context: Context) {

object NotExpirable : SelfDeletionTimerState()
}
}

companion object {
fun currentTime(): Instant = Clock.System.now()
}
typealias CurrentTimeProvider = () -> Instant
enum class StringResourceType { WEEKS, DAYS, HOURS, MINUTES, SECONDS; }
interface StringResourceProvider {
fun quantityString(type: StringResourceType, quantity: Int): String
}