Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes New menu forgets its "Hide accounts" setting #8735

Merged
merged 5 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import app.k9mail.core.ui.compose.designsystem.PreviewWithTheme
import app.k9mail.core.ui.compose.designsystem.atom.Surface
import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.DrawerConfig
import app.k9mail.feature.navigation.drawer.ui.FakeData.DISPLAY_ACCOUNT
import app.k9mail.feature.navigation.drawer.ui.FakeData.DISPLAY_FOLDER
import app.k9mail.feature.navigation.drawer.ui.FakeData.UNIFIED_FOLDER
Expand Down Expand Up @@ -121,7 +122,11 @@ internal fun DrawerContentSingleAccountPreview() {
selectedAccountId = DISPLAY_ACCOUNT.id,
folders = displayFolders,
selectedFolderId = displayFolders[0].id,
showAccountSelector = false,
config = DrawerConfig(
showUnifiedFolders = false,
showStarredCount = false,
showAccountSelector = false,
),
),
onEvent = {},
)
Expand All @@ -142,7 +147,11 @@ internal fun DrawerContentSingleAccountWithAccountSelectionPreview() {
selectedAccountId = DISPLAY_ACCOUNT.id,
folders = displayFolders,
selectedFolderId = displayFolders[0].id,
showAccountSelector = true,
config = DrawerConfig(
showUnifiedFolders = false,
showStarredCount = false,
showAccountSelector = true,
),
),
onEvent = {},
)
Expand All @@ -162,7 +171,11 @@ internal fun DrawerContentMultipleAccountsAccountPreview() {
selectedAccountId = accountList[0].id,
folders = displayFolders,
selectedFolderId = UNIFIED_FOLDER.id,
showAccountSelector = false,
config = DrawerConfig(
showUnifiedFolders = false,
showStarredCount = false,
showAccountSelector = false,
),
),
onEvent = {},
)
Expand All @@ -181,7 +194,11 @@ internal fun DrawerContentMultipleAccountsWithAccountSelectionPreview() {
selectedAccountId = accountList[1].id,
folders = createDisplayFolderList(hasUnifiedFolder = true),
selectedFolderId = UNIFIED_FOLDER.id,
showAccountSelector = true,
config = DrawerConfig(
showUnifiedFolders = false,
showStarredCount = false,
showAccountSelector = true,
),
),
onEvent = {},
)
Expand All @@ -200,7 +217,11 @@ internal fun DrawerContentMultipleAccountsWithDifferentAccountSelectionPreview()
selectedAccountId = accountList[2].id,
folders = createDisplayFolderList(hasUnifiedFolder = true),
selectedFolderId = UNIFIED_FOLDER.id,
showAccountSelector = true,
config = DrawerConfig(
showUnifiedFolders = false,
showStarredCount = false,
showAccountSelector = true,
),
),
onEvent = {},
)
Expand All @@ -224,7 +245,11 @@ internal fun DrawerContentSmallScreenPreview() {
selectedAccountId = accountList[2].id,
folders = createDisplayFolderList(hasUnifiedFolder = true),
selectedFolderId = UNIFIED_FOLDER.id,
showAccountSelector = true,
config = DrawerConfig(
showUnifiedFolders = false,
showStarredCount = false,
showAccountSelector = true,
),
),
onEvent = {},
)
Expand All @@ -249,7 +274,11 @@ internal fun DrawerContentVerySmallScreenPreview() {
selectedAccountId = accountList[2].id,
folders = createDisplayFolderList(hasUnifiedFolder = true),
selectedFolderId = UNIFIED_FOLDER.id,
showAccountSelector = true,
config = DrawerConfig(
showUnifiedFolders = false,
showStarredCount = false,
showAccountSelector = true,
),
),
onEvent = {},
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package app.k9mail.feature.navigation.drawer

import kotlinx.coroutines.flow.Flow

interface NavigationDrawerExternalContract {

data class DrawerConfig(
val showUnifiedFolders: Boolean,
val showStarredCount: Boolean,
val showAccountSelector: Boolean,
)

fun interface DrawerConfigLoader {
fun loadDrawerConfig(): DrawerConfig
fun loadDrawerConfigFlow(): Flow<DrawerConfig>
}

fun interface DrawerConfigWriter {
fun writeDrawerConfig(drawerConfig: DrawerConfig)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import app.k9mail.feature.navigation.drawer.domain.DomainContract.UseCase
import app.k9mail.feature.navigation.drawer.domain.usecase.GetDisplayAccounts
import app.k9mail.feature.navigation.drawer.domain.usecase.GetDisplayFoldersForAccount
import app.k9mail.feature.navigation.drawer.domain.usecase.GetDrawerConfig
import app.k9mail.feature.navigation.drawer.domain.usecase.SaveDrawerConfig
import app.k9mail.feature.navigation.drawer.domain.usecase.SyncAccount
import app.k9mail.feature.navigation.drawer.domain.usecase.SyncAllAccounts
import app.k9mail.feature.navigation.drawer.ui.DrawerViewModel
Expand All @@ -18,6 +19,11 @@ val navigationDrawerModule: Module = module {
configProver = get(),
)
}
single<UseCase.SaveDrawerConfig> {
SaveDrawerConfig(
drawerConfigWriter = get(),
)
}

single<UseCase.GetDisplayAccounts> {
GetDisplayAccounts(
Expand Down Expand Up @@ -50,6 +56,7 @@ val navigationDrawerModule: Module = module {
viewModel {
DrawerViewModel(
getDrawerConfig = get(),
saveDrawerConfig = get(),
getDisplayAccounts = get(),
getDisplayFoldersForAccount = get(),
syncAccount = get(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ internal interface DomainContract {
operator fun invoke(): Flow<DrawerConfig>
}

fun interface SaveDrawerConfig {
operator fun invoke(drawerConfig: DrawerConfig): Flow<Unit>
}

fun interface GetDisplayAccounts {
operator fun invoke(): Flow<List<DisplayAccount>>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.Dra
import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.DrawerConfigLoader
import app.k9mail.feature.navigation.drawer.domain.DomainContract.UseCase
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

internal class GetDrawerConfig(
private val configProver: DrawerConfigLoader,
) : UseCase.GetDrawerConfig {
override operator fun invoke(): Flow<DrawerConfig> {
// TODO This needs to be updated when the config changes
return flow {
emit(configProver.loadDrawerConfig())
}
return configProver.loadDrawerConfigFlow()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package app.k9mail.feature.navigation.drawer.domain.usecase

import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract
import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.DrawerConfigWriter
import app.k9mail.feature.navigation.drawer.domain.DomainContract.UseCase
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

internal class SaveDrawerConfig(
private val drawerConfigWriter: DrawerConfigWriter,
) : UseCase.SaveDrawerConfig {
override fun invoke(drawerConfig: NavigationDrawerExternalContract.DrawerConfig): Flow<Unit> {
return flow {
emit(drawerConfigWriter.writeDrawerConfig(drawerConfig))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ internal fun DrawerContent(
AccountView(
account = selectedAccount,
onClick = { onEvent(Event.OnAccountViewClick(selectedAccount)) },
showAvatar = state.showAccountSelector,
showAvatar = state.config.showAccountSelector,
)

DividerHorizontal()
}
Row {
AnimatedVisibility(
visible = state.showAccountSelector,
visible = state.config.showAccountSelector,
) {
AccountList(
accounts = state.accounts,
Expand Down Expand Up @@ -76,7 +76,7 @@ internal fun DrawerContent(
SettingList(
onAccountSelectorClick = { onEvent(Event.OnAccountSelectorClick) },
onManageFoldersClick = { onEvent(Event.OnManageFoldersClick) },
showAccountSelector = state.showAccountSelector,
showAccountSelector = state.config.showAccountSelector,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ internal interface DrawerContract {
val config: DrawerConfig = DrawerConfig(
showUnifiedFolders = false,
showStarredCount = false,
showAccountSelector = true,
),
val accounts: ImmutableList<DisplayAccount> = persistentListOf(),
val selectedAccountId: String? = null,
val folders: ImmutableList<DisplayFolder> = persistentListOf(),
val selectedFolderId: String? = null,
val showAccountSelector: Boolean = true,
val isLoading: Boolean = false,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

@Suppress("MagicNumber", "TooManyFunctions")
internal class DrawerViewModel(
private val getDrawerConfig: UseCase.GetDrawerConfig,
private val saveDrawerConfig: UseCase.SaveDrawerConfig,
private val getDisplayAccounts: UseCase.GetDisplayAccounts,
private val getDisplayFoldersForAccount: UseCase.GetDisplayFoldersForAccount,
private val syncAccount: UseCase.SyncAccount,
Expand Down Expand Up @@ -113,7 +115,11 @@ internal class DrawerViewModel(
)
}

Event.OnAccountSelectorClick -> updateState { it.copy(showAccountSelector = it.showAccountSelector.not()) }
Event.OnAccountSelectorClick -> {
saveDrawerConfig(
state.value.config.copy(showAccountSelector = state.value.config.showAccountSelector.not()),
).launchIn(viewModelScope)
}
Event.OnManageFoldersClick -> emitEffect(Effect.OpenManageFolders)
Event.OnSettingsClick -> emitEffect(Effect.OpenSettings)
Event.OnSyncAccount -> onSyncAccount()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package app.k9mail.feature.navigation.drawer.domain.usecase

import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.DrawerConfig
import app.k9mail.feature.navigation.drawer.NavigationDrawerExternalContract.DrawerConfigLoader
import assertk.assertThat
import assertk.assertions.isEqualTo
import kotlin.test.Test
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.mockito.Mockito.mock
import org.mockito.kotlin.whenever

internal class GetDrawerConfigTest {

@Test
fun `should get drawer config`() = runTest {
val configProver: DrawerConfigLoader = mock()
val drawerConfig = DrawerConfig(
showUnifiedFolders = true,
showStarredCount = true,
showAccountSelector = true,
)

val testSubject = GetDrawerConfig(
configProver = { drawerConfig },
)
val testSubject = GetDrawerConfig(configProver = configProver)
whenever(configProver.loadDrawerConfigFlow()).thenReturn(flowOf(drawerConfig))

val result = testSubject().first()

assertThat(result).isEqualTo(
DrawerConfig(
showUnifiedFolders = true,
showStarredCount = true,
),
)
assertThat(result).isEqualTo(drawerConfig)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ internal class DrawerStateTest {
config = DrawerConfig(
showUnifiedFolders = false,
showStarredCount = false,
showAccountSelector = true,
),
accounts = persistentListOf(),
selectedAccountId = null,
folders = persistentListOf(),
selectedFolderId = null,
showAccountSelector = true,
isLoading = false,
),
)
Expand Down
Loading