fix: make sure account changes are observed (#172)

* rename login screen

* update usages of login screen

* minor readability changes in login vm/use case

* add possibility to observe accounts

* update manage accounts view model
This commit is contained in:
Dieguitux 2024-12-18 21:32:20 +01:00 committed by GitHub
parent 087d231319
commit 55a15e6b0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 48 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package com.livefast.eattrash.raccoonforlemmy.core.persistence.repository
import app.cash.sqldelight.Query
import app.cash.turbine.test
import com.livefast.eattrash.raccoonforlemmy.core.persistence.AccountEntity
import com.livefast.eattrash.raccoonforlemmy.core.persistence.AccountsQueries
import com.livefast.eattrash.raccoonforlemmy.core.persistence.data.AccountModel
@ -70,6 +71,21 @@ class DefaultAccountRepositoryTest {
}
}
@Test
fun givenExitingAccounts_whenObserveAll_thenResultIsAsExpected() =
runTest {
val accounts = listOf(createFakeEntity())
every { query.executeAsList() } returns accounts
sut.observeAll().test {
val res = awaitItem()
assertTrue(res.size == 1)
}
verify {
queries.getAll()
}
}
@Test
fun givenNoActiveAccount_whenGetActive_thenResultIsAsExpected() =
runTest {

View File

@ -2,11 +2,14 @@ package com.livefast.eattrash.raccoonforlemmy.core.persistence.repository
import androidx.compose.runtime.Stable
import com.livefast.eattrash.raccoonforlemmy.core.persistence.data.AccountModel
import kotlinx.coroutines.flow.Flow
@Stable
interface AccountRepository {
suspend fun getAll(): List<AccountModel>
fun observeAll(): Flow<List<AccountModel>>
suspend fun getBy(
username: String,
instance: String,

View File

@ -5,6 +5,11 @@ import com.livefast.eattrash.raccoonforlemmy.core.persistence.data.AccountModel
import com.livefast.eattrash.raccoonforlemmy.core.persistence.provider.DatabaseProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.isActive
import kotlinx.coroutines.withContext
import org.koin.core.annotation.Single
@ -22,6 +27,14 @@ internal class DefaultAccountRepository(
.map { it.toModel() }
}
override fun observeAll(): Flow<List<AccountModel>> =
channelFlow {
while (isActive) {
send(getAll())
delay(1000)
}
}.distinctUntilChanged()
override suspend fun getBy(
username: String,
instance: String,

View File

@ -65,7 +65,7 @@ internal class DefaultLoginUseCase(
instance = instance,
jwt = auth,
)
val existing = accountRepository.getBy(username, instance)
val existing = accountRepository.getBy(username = username, instance = instance)
val accountId =
if (existing == null) {
// new account with a copy of the anonymous settings

View File

@ -64,7 +64,7 @@ import com.livefast.eattrash.raccoonforlemmy.unit.editcommunity.EditCommunityScr
import com.livefast.eattrash.raccoonforlemmy.unit.filteredcontents.FilteredContentsScreen
import com.livefast.eattrash.raccoonforlemmy.unit.filteredcontents.FilteredContentsType
import com.livefast.eattrash.raccoonforlemmy.unit.filteredcontents.toInt
import com.livefast.eattrash.raccoonforlemmy.unit.login.LoginBottomSheet
import com.livefast.eattrash.raccoonforlemmy.unit.login.LoginScreen
import com.livefast.eattrash.raccoonforlemmy.unit.manageaccounts.ManageAccountsBottomSheet
import com.livefast.eattrash.raccoonforlemmy.unit.managesubscriptions.ManageSubscriptionsScreen
import com.livefast.eattrash.raccoonforlemmy.unit.modlog.ModlogScreen
@ -365,7 +365,7 @@ internal object ProfileMainScreen : Tab {
onDismiss = { openLogin ->
manageAccountsBottomSheetOpened = false
if (openLogin) {
navigationCoordinator.pushScreen(LoginBottomSheet())
navigationCoordinator.pushScreen(LoginScreen())
}
},
)

View File

@ -20,7 +20,7 @@ import cafe.adriel.voyager.navigator.tab.TabOptions
import com.livefast.eattrash.raccoonforlemmy.core.appearance.theme.Spacing
import com.livefast.eattrash.raccoonforlemmy.core.l10n.LocalStrings
import com.livefast.eattrash.raccoonforlemmy.core.navigation.di.getNavigationCoordinator
import com.livefast.eattrash.raccoonforlemmy.unit.login.LoginBottomSheet
import com.livefast.eattrash.raccoonforlemmy.unit.login.LoginScreen
internal object ProfileNotLoggedScreen : Tab {
override val options: TabOptions
@ -61,7 +61,7 @@ internal object ProfileNotLoggedScreen : Tab {
Button(
modifier = Modifier.align(Alignment.CenterHorizontally),
onClick = {
navigationCoordinator.pushScreen(LoginBottomSheet())
navigationCoordinator.pushScreen(LoginScreen())
},
) {
Text(LocalStrings.current.profileButtonLogin)

View File

@ -48,7 +48,7 @@ import com.livefast.eattrash.raccoonforlemmy.feature.home.ui.HomeTab
import com.livefast.eattrash.raccoonforlemmy.feature.settings.main.SettingsScreen
import com.livefast.eattrash.raccoonforlemmy.navigation.TabNavigationItem
import com.livefast.eattrash.raccoonforlemmy.navigation.toTab
import com.livefast.eattrash.raccoonforlemmy.unit.login.LoginBottomSheet
import com.livefast.eattrash.raccoonforlemmy.unit.login.LoginScreen
import com.livefast.eattrash.raccoonforlemmy.unit.manageaccounts.ManageAccountsBottomSheet
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.drop
@ -317,7 +317,7 @@ internal object MainScreen : Screen {
onDismiss = { openLogin ->
manageAccountsBottomSheetOpened = false
if (openLogin) {
navigationCoordinator.pushScreen(LoginBottomSheet())
navigationCoordinator.pushScreen(LoginScreen())
}
},
)

View File

@ -50,7 +50,7 @@ import com.livefast.eattrash.raccoonforlemmy.domain.lemmy.data.toReadableName
import com.livefast.eattrash.raccoonforlemmy.unit.drawer.components.DrawerCommunityItem
import com.livefast.eattrash.raccoonforlemmy.unit.drawer.components.DrawerHeader
import com.livefast.eattrash.raccoonforlemmy.unit.drawer.components.DrawerShortcut
import com.livefast.eattrash.raccoonforlemmy.unit.login.LoginBottomSheet
import com.livefast.eattrash.raccoonforlemmy.unit.login.LoginScreen
import com.livefast.eattrash.raccoonforlemmy.unit.manageaccounts.ManageAccountsBottomSheet
import com.livefast.eattrash.raccoonforlemmy.unit.selectinstance.SelectInstanceBottomSheet
import kotlinx.coroutines.delay
@ -372,7 +372,7 @@ object ModalDrawerContent : Tab {
onDismiss = { openLogin ->
manageAccountsBottomSheetOpened = false
if (openLogin) {
navigationCoordinator.pushScreen(LoginBottomSheet())
navigationCoordinator.pushScreen(LoginScreen())
}
},
)

View File

@ -65,7 +65,7 @@ import com.livefast.eattrash.raccoonforlemmy.core.utils.toReadableMessage
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
class LoginBottomSheet : Screen {
class LoginScreen : Screen {
companion object {
private const val HELP_URL = "https://join-lemmy.org/docs/users/01-getting-started.html"
}

View File

@ -18,12 +18,12 @@ import org.koin.core.annotation.Factory
@Factory(binds = [LoginMviModel::class])
class LoginViewModel(
private val login: LoginUseCase,
apiConfigurationRepository: ApiConfigurationRepository,
private val identityRepository: IdentityRepository,
private val accountRepository: AccountRepository,
private val siteRepository: SiteRepository,
private val communityRepository: CommunityRepository,
private val login: LoginUseCase,
private val notificationCenter: NotificationCenter,
) : DefaultMviModel<LoginMviModel.Intent, LoginMviModel.UiState, LoginMviModel.Effect>(
initialState = LoginMviModel.UiState(),

View File

@ -42,8 +42,11 @@ class ManageAccountsViewModel(
}
}.launchIn(this)
val accounts = accountRepository.getAll()
updateState { it.copy(accounts = accounts) }
accountRepository
.observeAll()
.onEach { accounts ->
updateState { it.copy(accounts = accounts) }
}.launchIn(this)
}
}
}