Code review fixes.

This commit is contained in:
Onuray Sahin 2022-08-31 20:19:01 +03:00 committed by Maxime NATUREL
parent ed1244aa8f
commit 8d378408c5
5 changed files with 36 additions and 41 deletions

View File

@ -3231,6 +3231,6 @@
<string name="device_manager_unverified_sessions_title">Unverified sessions</string> <string name="device_manager_unverified_sessions_title">Unverified sessions</string>
<string name="device_manager_unverified_sessions_description">Verify or sign out from unverified sessions.</string> <string name="device_manager_unverified_sessions_description">Verify or sign out from unverified sessions.</string>
<string name="device_manager_inactive_sessions_title">Inactive sessions</string> <string name="device_manager_inactive_sessions_title">Inactive sessions</string>
<string name="device_manager_inactive_sessions_description">Consider signing out from old sessions (90 days or older) you dont use anymore.</string> <string name="device_manager_inactive_sessions_description">Consider signing out from old sessions (%1$d days or more) that you dont use anymore.</string>
</resources> </resources>

View File

@ -40,6 +40,7 @@ import im.vector.app.features.settings.devices.DeviceFullInfo
import im.vector.app.features.settings.devices.DevicesAction import im.vector.app.features.settings.devices.DevicesAction
import im.vector.app.features.settings.devices.DevicesViewEvents import im.vector.app.features.settings.devices.DevicesViewEvents
import im.vector.app.features.settings.devices.DevicesViewModel import im.vector.app.features.settings.devices.DevicesViewModel
import im.vector.app.features.settings.devices.v2.list.SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS
/** /**
* Display the list of the user's devices and sessions. * Display the list of the user's devices and sessions.
@ -153,6 +154,9 @@ class VectorSettingsDevicesFragment :
views.deviceListInactiveSessionsRecommendation.isVisible = inactiveSessionsCount > 0 views.deviceListInactiveSessionsRecommendation.isVisible = inactiveSessionsCount > 0
views.deviceListUnverifiedSessionsRecommendation.setCount(unverifiedSessionsCount) views.deviceListUnverifiedSessionsRecommendation.setCount(unverifiedSessionsCount)
views.deviceListInactiveSessionsRecommendation.setCount(inactiveSessionsCount) views.deviceListInactiveSessionsRecommendation.setCount(inactiveSessionsCount)
views.deviceListInactiveSessionsRecommendation.setDescription(
getString(R.string.device_manager_inactive_sessions_description, SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS)
)
} }
} }

View File

@ -16,18 +16,19 @@
package im.vector.app.features.settings.devices.v2.list package im.vector.app.features.settings.devices.v2.list
import im.vector.app.core.resources.DateProvider import im.vector.app.core.time.Clock
import im.vector.app.core.resources.toTimestamp
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
class CheckIfSessionIsInactiveUseCase @Inject constructor() { class CheckIfSessionIsInactiveUseCase @Inject constructor(
private val clock: Clock,
) {
fun execute(lastSeenTs: Long): Boolean { fun execute(lastSeenTs: Long): Boolean {
val lastSeenDate = DateProvider.toLocalDateTime(lastSeenTs) // In case of the server doesn't send the last seen date.
val currentDate = DateProvider.currentLocalDateTime() if (lastSeenTs == 0L) return true
val diffMilliseconds = currentDate.toTimestamp() - lastSeenDate.toTimestamp()
val diffDays = TimeUnit.MILLISECONDS.toDays(diffMilliseconds) val diffMilliseconds = clock.epochMillis() - lastSeenTs
return diffDays >= SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS return diffMilliseconds >= TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong())
} }
} }

View File

@ -66,6 +66,14 @@ class SecurityRecommendationView @JvmOverloads constructor(
views.recommendationShieldImageView.backgroundTintList = ColorStateList.valueOf(backgroundTint) views.recommendationShieldImageView.backgroundTintList = ColorStateList.valueOf(backgroundTint)
} }
fun setTitle(title: String) {
views.recommendationTitleTextView.text = title
}
fun setDescription(description: String) {
views.recommendationDescriptionTextView.text = description
}
fun setCount(sessionsCount: Int) { fun setCount(sessionsCount: Int) {
views.recommendationViewAllButton.text = context.getString(R.string.device_manager_other_sessions_view_all, sessionsCount) views.recommendationViewAllButton.text = context.getString(R.string.device_manager_other_sessions_view_all, sessionsCount)
} }

View File

@ -16,62 +16,44 @@
package im.vector.app.features.settings.devices.v2.list package im.vector.app.features.settings.devices.v2.list
import im.vector.app.core.resources.DateProvider import im.vector.app.test.fakes.FakeClock
import im.vector.app.core.resources.toTimestamp
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import org.amshove.kluent.shouldBeEqualTo import org.amshove.kluent.shouldBeEqualTo
import org.junit.Before
import org.junit.Test import org.junit.Test
import org.threeten.bp.Instant import java.util.concurrent.TimeUnit
import org.threeten.bp.LocalDateTime
import org.threeten.bp.ZoneId private const val A_TIMESTAMP = 1654689143L
import org.threeten.bp.ZoneId.systemDefault
import org.threeten.bp.ZoneOffset
import org.threeten.bp.zone.ZoneRules
class CheckIfSessionIsInactiveUseCaseTest { class CheckIfSessionIsInactiveUseCaseTest {
private val checkIfSessionIsInactiveUseCase = CheckIfSessionIsInactiveUseCase() private val clock = FakeClock().apply { givenEpoch(A_TIMESTAMP) }
private val zoneId = mockk<ZoneId>() private val checkIfSessionIsInactiveUseCase = CheckIfSessionIsInactiveUseCase(clock)
private val zoneRules = mockk<ZoneRules>()
@Before
fun setup() {
mockkStatic(ZoneId::class)
every { systemDefault() } returns zoneId
every { zoneId.rules } returns zoneRules
every { zoneRules.getOffset(any<Instant>()) } returns ZoneOffset.UTC
every { zoneRules.getOffset(any<LocalDateTime>()) } returns ZoneOffset.UTC
}
@Test @Test
fun `given an old last seen date then session is inactive`() { fun `given an old last seen date then session is inactive`() {
val lastSeenDate = DateProvider.currentLocalDateTime().minusDays((SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS + 1).toLong()) val lastSeenDate = A_TIMESTAMP - TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong()) - 1
checkIfSessionIsInactiveUseCase.execute(lastSeenDate.toTimestamp()) shouldBeEqualTo true checkIfSessionIsInactiveUseCase.execute(lastSeenDate) shouldBeEqualTo true
} }
@Test @Test
fun `given a last seen date equal to the threshold then session is inactive`() { fun `given a last seen date equal to the threshold then session is inactive`() {
val lastSeenDate = DateProvider.currentLocalDateTime().minusDays((SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS).toLong()) val lastSeenDate = A_TIMESTAMP - TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong())
checkIfSessionIsInactiveUseCase.execute(lastSeenDate.toTimestamp()) shouldBeEqualTo true checkIfSessionIsInactiveUseCase.execute(lastSeenDate) shouldBeEqualTo true
} }
@Test @Test
fun `given a recent last seen date then session is active`() { fun `given a recent last seen date then session is active`() {
val lastSeenDate = DateProvider.currentLocalDateTime().minusDays((SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS - 1).toLong()) val lastSeenDate = A_TIMESTAMP - TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong()) + 1
checkIfSessionIsInactiveUseCase.execute(lastSeenDate.toTimestamp()) shouldBeEqualTo false checkIfSessionIsInactiveUseCase.execute(lastSeenDate) shouldBeEqualTo false
} }
@Test @Test
fun `given a last seen date as zero then session is inactive`() { fun `given a last seen date as zero then session is inactive`() {
// In case of the server doesn't send the last seen date. // In case of the server doesn't send the last seen date.
val lastSeenDate = DateProvider.toLocalDateTime(0) val lastSeenDate = 0L
checkIfSessionIsInactiveUseCase.execute(lastSeenDate.toTimestamp()) shouldBeEqualTo true checkIfSessionIsInactiveUseCase.execute(lastSeenDate) shouldBeEqualTo true
} }
} }