1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-3081 Settings | Tech | Safer observe settings (#1827)

This commit is contained in:
Evgenii Kozlov 2024-11-21 13:07:03 +01:00 committed by GitHub
parent f4d804f134
commit a982ae87a7
Signed by: github
GPG key ID: B5690EEEBB952194
3 changed files with 112 additions and 23 deletions

View file

@ -1,5 +1,7 @@
package com.anytypeio.anytype.core_models.settings
import com.anytypeio.anytype.core_models.DEFAULT_RELATIVE_DATES
import com.anytypeio.anytype.core_models.FALLBACK_DATE_PATTERN
import com.anytypeio.anytype.core_models.Id
data class VaultSettings(
@ -7,4 +9,13 @@ data class VaultSettings(
val orderOfSpaces: List<Id> = emptyList(),
val isRelativeDates: Boolean,
val dateFormat: String
)
) {
companion object {
fun default() : VaultSettings = VaultSettings(
showIntroduceVault = false,
orderOfSpaces = emptyList(),
isRelativeDates = DEFAULT_RELATIVE_DATES,
dateFormat = FALLBACK_DATE_PATTERN
)
}
}

View file

@ -1,8 +1,7 @@
package com.anytypeio.anytype.domain.vault
import com.anytypeio.anytype.core_models.FALLBACK_DATE_PATTERN
import com.anytypeio.anytype.core_models.DEFAULT_RELATIVE_DATES
import com.anytypeio.anytype.core_models.settings.VaultSettings
import com.anytypeio.anytype.domain.account.AwaitAccountStartManager
import com.anytypeio.anytype.domain.auth.repo.AuthRepository
import com.anytypeio.anytype.domain.base.AppCoroutineDispatchers
import com.anytypeio.anytype.domain.base.FlowInteractor
@ -11,32 +10,25 @@ import com.anytypeio.anytype.domain.debugging.Logger
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flatMapLatest
class ObserveVaultSettings @Inject constructor(
private val awaitAccountStart: AwaitAccountStartManager,
private val settings: UserSettingsRepository,
private val auth: AuthRepository,
private val logger: Logger,
dispatchers: AppCoroutineDispatchers
): FlowInteractor<Unit, VaultSettings>(dispatchers.io) {
override fun build(): Flow<VaultSettings> = flow {
val acc = auth.getCurrentAccount()
emitAll(
settings
.observeVaultSettings(acc)
.catch {
logger.logException(it, "Error while observing vault settings")
emit(
VaultSettings(
showIntroduceVault = false,
orderOfSpaces = emptyList(),
isRelativeDates = DEFAULT_RELATIVE_DATES,
dateFormat = FALLBACK_DATE_PATTERN
)
)
}
)
}
override fun build(): Flow<VaultSettings> = awaitAccountStart
.awaitStart()
.flatMapLatest {
val acc = auth.getCurrentAccount()
settings.observeVaultSettings(acc)
}.catch {
logger.logException(it, "Error while observing vault settings")
emit(VaultSettings.default())
}
override fun build(params: Unit): Flow<VaultSettings> = build()
}

View file

@ -0,0 +1,86 @@
package com.anytypeio.anytype.domain.settings
import app.cash.turbine.test
import com.anytypeio.anytype.core_models.settings.VaultSettings
import com.anytypeio.anytype.domain.account.AwaitAccountStartManager
import com.anytypeio.anytype.domain.auth.repo.AuthRepository
import com.anytypeio.anytype.domain.base.AppCoroutineDispatchers
import com.anytypeio.anytype.domain.common.DefaultCoroutineTestRule
import com.anytypeio.anytype.domain.config.UserSettingsRepository
import com.anytypeio.anytype.domain.debugging.Logger
import com.anytypeio.anytype.domain.vault.ObserveVaultSettings
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Rule
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.doThrow
import org.mockito.kotlin.stub
import org.mockito.kotlin.verifyNoInteractions
class ObserveVaultSettingsTest {
@get:Rule
val rule = DefaultCoroutineTestRule()
val dispatchers = AppCoroutineDispatchers(
io = rule.dispatcher,
computation = rule.dispatcher,
main = rule.dispatcher
)
@Mock
lateinit var awaitAccountStartManager: AwaitAccountStartManager
@Mock
lateinit var logger: Logger
@Mock
lateinit var authRepository: AuthRepository
@Mock
lateinit var settingsRepository: UserSettingsRepository
@Before
fun setup() {
MockitoAnnotations.openMocks(this)
}
@Test
fun `should emit default vault settings when getAccount throws an exceptiom`() = runTest {
awaitAccountStartManager.stub {
on {
awaitStart()
} doReturn flowOf(AwaitAccountStartManager.State.Started)
}
authRepository.stub {
onBlocking {
getCurrentAccount()
} doThrow IllegalStateException()
}
val useCase = ObserveVaultSettings(
settings = settingsRepository,
auth = authRepository,
logger = logger,
dispatchers = dispatchers,
awaitAccountStart = awaitAccountStartManager
)
useCase.flow().test {
assertEquals(
expected = VaultSettings.default(),
actual = awaitItem()
)
awaitComplete()
}
verifyNoInteractions(settingsRepository)
}
}