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.amshove.kluent.shouldBe
import org.junit.Test import org.junit.Test
import java.lang.Thread.sleep import java.lang.Thread.sleep
import kotlin.time.Duration.Companion.milliseconds
class TemporaryStoreTest { class TemporaryStoreTest {
@Test @Test
fun testTemporaryStore() { fun testTemporaryStore() {
// Keep the data 300 millis // Keep the data 300 millis
val store = TemporaryStore<String>(300) val store = TemporaryStore<String>(300.milliseconds)
store.data = "test" store.data = "test"
store.data shouldBe "test" store.data shouldBe "test"

View File

@ -18,15 +18,14 @@ package im.vector.app.core.utils
import java.util.Timer import java.util.Timer
import java.util.TimerTask import java.util.TimerTask
import kotlin.time.Duration
const val THREE_MINUTES = 3 * 60_000L
/** /**
* Store an object T for a specific period of time. * Store an object T for a specific period of time.
* @param T type of the data to store * @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 private var timer: Timer? = null
@ -40,7 +39,7 @@ open class TemporaryStore<T>(private val delay: Long = THREE_MINUTES) {
override fun run() { override fun run() {
field = null 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 im.vector.app.core.utils.TemporaryStore
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
import kotlin.time.Duration.Companion.seconds
// Store to keep a pending action from sub screen of a room detail // Store to keep a pending action from sub screen of a room detail
@Singleton @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 im.vector.app.core.utils.TemporaryStore
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
import kotlin.time.Duration.Companion.minutes
/** /**
* Will store the account password for 3 minutes. * Will store the account password for 3 minutes.
*/ */
@Singleton @Singleton
class ReAuthHelper @Inject constructor() : TemporaryStore<String>() class ReAuthHelper @Inject constructor() : TemporaryStore<String>(3.minutes)