Use handy kotlin.time.Duration.Companion and remove default value.

This commit is contained in:
Benoit Marty 2022-06-17 16:53:33 +02:00
parent 663812b90b
commit 705b55c57a
4 changed files with 10 additions and 8 deletions

View file

@ -19,13 +19,14 @@ package im.vector.app.core.utils
import org.amshove.kluent.shouldBe
import org.junit.Test
import java.lang.Thread.sleep
import kotlin.time.Duration.Companion.milliseconds
class TemporaryStoreTest {
@Test
fun testTemporaryStore() {
// Keep the data 300 millis
val store = TemporaryStore<String>(300)
val store = TemporaryStore<String>(300.milliseconds)
store.data = "test"
store.data shouldBe "test"

View file

@ -18,15 +18,14 @@ package im.vector.app.core.utils
import java.util.Timer
import java.util.TimerTask
const val THREE_MINUTES = 3 * 60_000L
import kotlin.time.Duration
/**
* Store an object T for a specific period of time.
* @param T type of the data to store
* @property delay delay to keep the data, in millis
* @property delay delay to keep the data
*/
open class TemporaryStore<T>(private val delay: Long = THREE_MINUTES) {
open class TemporaryStore<T>(private val delay: Duration) {
private var timer: Timer? = null
@ -40,7 +39,7 @@ open class TemporaryStore<T>(private val delay: Long = THREE_MINUTES) {
override fun run() {
field = null
}
}, delay)
}, delay.inWholeMilliseconds)
}
}
}

View file

@ -19,7 +19,8 @@ package im.vector.app.features.home.room.detail
import im.vector.app.core.utils.TemporaryStore
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.time.Duration.Companion.seconds
// Store to keep a pending action from sub screen of a room detail
@Singleton
class RoomDetailPendingActionStore @Inject constructor() : TemporaryStore<RoomDetailPendingAction>(10_000)
class RoomDetailPendingActionStore @Inject constructor() : TemporaryStore<RoomDetailPendingAction>(10.seconds)

View file

@ -19,9 +19,10 @@ package im.vector.app.features.login
import im.vector.app.core.utils.TemporaryStore
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.time.Duration.Companion.minutes
/**
* Will store the account password for 3 minutes.
*/
@Singleton
class ReAuthHelper @Inject constructor() : TemporaryStore<String>()
class ReAuthHelper @Inject constructor() : TemporaryStore<String>(3.minutes)