Skip to content

Commit

Permalink
Add SyncAllAccounts to DrawerViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
wmontwe committed Sep 24, 2024
1 parent 6d8f116 commit 137ea0c
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.material.icons.outlined.Menu
import androidx.compose.material.icons.outlined.Report
import androidx.compose.material.icons.outlined.Security
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material.icons.outlined.Sync
import androidx.compose.material.icons.outlined.Visibility
import androidx.compose.ui.graphics.vector.ImageVector
import app.k9mail.core.ui.compose.designsystem.atom.icon.filled.Dot
Expand Down Expand Up @@ -111,6 +112,9 @@ object Icons {
val Settings: ImageVector
get() = MaterialIcons.Outlined.Settings

val Sync: ImageVector
get() = MaterialIcons.Outlined.Sync

val Report: ImageVector
get() = MaterialIcons.Outlined.Report

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal fun AccountListPreview() {
selectedAccount = null,
onAccountClick = { },
onSettingsClick = { },
onSyncAllAccountsClick = { },
)
}
}
Expand All @@ -32,6 +33,7 @@ internal fun AccountListWithSelectedPreview() {
selectedAccount = DISPLAY_ACCOUNT,
onAccountClick = { },
onSettingsClick = { },
onSyncAllAccountsClick = { },
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package app.k9mail.feature.navigation.drawer.ui.setting
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import app.k9mail.core.ui.compose.designsystem.PreviewWithThemes
import app.k9mail.core.ui.compose.designsystem.atom.icon.Icons

@Composable
@Preview(showBackground = true)
internal fun SettingItemPreview() {
PreviewWithThemes {
SettingItem(
icon = Icons.Outlined.Settings,
label = "Setting",
onClick = {},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ val navigationDrawerModule: Module = module {
getDisplayAccounts = get(),
getDisplayFoldersForAccount = get(),
syncAccount = get(),
syncAllAccounts = get(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal fun DrawerContent(
accounts = state.accounts,
selectedAccount = state.selectedAccount,
onAccountClick = { onEvent(Event.OnAccountClick(it)) },
onSyncAllAccountsClick = { onEvent(Event.OnSyncAllAccounts) },
onSettingsClick = { onEvent(Event.OnSettingsClick) },
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ internal interface DrawerContract {
data object OnAccountSelectorClick : Event
data object OnManageFoldersClick : Event
data object OnSettingsClick : Event
data object OnRefresh : Event
data object OnSyncAccount : Event
data object OnSyncAllAccounts : Event
}

sealed interface Effect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal fun DrawerView(

PullToRefreshBox(
isRefreshing = state.value.isLoading,
onRefresh = { dispatch(Event.OnRefresh) },
onRefresh = { dispatch(Event.OnSyncAccount) },
) {
DrawerContent(
state = state.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class DrawerViewModel(
private val getDisplayAccounts: UseCase.GetDisplayAccounts,
private val getDisplayFoldersForAccount: UseCase.GetDisplayFoldersForAccount,
private val syncAccount: UseCase.SyncAccount,
private val syncAllAccounts: UseCase.SyncAllAccounts,
initialState: State = State(),
) : BaseViewModel<State, Event, Effect>(
initialState = initialState,
Expand Down Expand Up @@ -101,7 +102,6 @@ internal class DrawerViewModel(

override fun event(event: Event) {
when (event) {
Event.OnRefresh -> refresh()
is Event.OnAccountClick -> selectAccount(event.account)
is Event.OnFolderClick -> selectFolder(event.folder)
is Event.OnAccountViewClick -> {
Expand All @@ -113,6 +113,8 @@ internal class DrawerViewModel(
Event.OnAccountSelectorClick -> updateState { it.copy(showAccountSelector = it.showAccountSelector.not()) }
Event.OnManageFoldersClick -> emitEffect(Effect.OpenManageFolders)
Event.OnSettingsClick -> emitEffect(Effect.OpenSettings)
Event.OnSyncAccount -> onSyncAccount()
Event.OnSyncAllAccounts -> onSyncAllAccounts()
}
}

Expand Down Expand Up @@ -156,18 +158,34 @@ internal class DrawerViewModel(
}
}

private fun refresh() {
if (state.value.selectedAccount != null) {
viewModelScope.launch {
updateState {
it.copy(isLoading = true)
}
private fun onSyncAccount() {
if (state.value.isLoading || state.value.selectedAccount == null) return

viewModelScope.launch {
updateState {
it.copy(isLoading = true)
}

state.value.selectedAccount?.account?.let { syncAccount(it).collect() }
state.value.selectedAccount?.account?.let { syncAccount(it).collect() }

updateState {
it.copy(isLoading = false)
}
updateState {
it.copy(isLoading = false)
}
}
}

private fun onSyncAllAccounts() {
if (state.value.isLoading) return

viewModelScope.launch {
updateState {
it.copy(isLoading = true)
}

syncAllAccounts().collect()

updateState {
it.copy(isLoading = false)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package app.k9mail.feature.navigation.drawer.ui.account

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import app.k9mail.core.ui.compose.designsystem.atom.Surface
import app.k9mail.core.ui.compose.designsystem.atom.icon.Icons
import app.k9mail.core.ui.compose.theme2.MainTheme
import app.k9mail.feature.navigation.drawer.R
import app.k9mail.feature.navigation.drawer.domain.entity.DisplayAccount
Expand All @@ -21,6 +24,7 @@ internal fun AccountList(
accounts: ImmutableList<DisplayAccount>,
selectedAccount: DisplayAccount?,
onAccountClick: (DisplayAccount) -> Unit,
onSyncAllAccountsClick: () -> Unit,
onSettingsClick: () -> Unit,
modifier: Modifier = Modifier,
) {
Expand Down Expand Up @@ -50,10 +54,21 @@ internal fun AccountList(
)
}
}
SettingItem(
label = stringResource(id = R.string.navigation_drawer_action_settings),
onClick = onSettingsClick,
)
Column(
modifier = Modifier.padding(vertical = MainTheme.spacings.oneHalf),
verticalArrangement = Arrangement.spacedBy(MainTheme.spacings.half),
) {
SettingItem(
icon = Icons.Outlined.Sync,
label = stringResource(id = R.string.navigation_drawer_action_sync_all_accounts),
onClick = onSyncAllAccountsClick,
)
SettingItem(
icon = Icons.Outlined.Settings,
label = stringResource(id = R.string.navigation_drawer_action_settings),
onClick = onSettingsClick,
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import app.k9mail.core.ui.compose.designsystem.atom.Surface
import app.k9mail.core.ui.compose.designsystem.atom.icon.Icon
import app.k9mail.core.ui.compose.designsystem.atom.icon.Icons
import app.k9mail.core.ui.compose.theme2.MainTheme

@Composable
internal fun SettingItem(
icon: ImageVector,
label: String,
onClick: () -> Unit,
modifier: Modifier = Modifier,
Expand All @@ -24,12 +25,11 @@ internal fun SettingItem(
contentAlignment = Alignment.Center,
) {
Surface(
modifier = Modifier.padding(vertical = MainTheme.spacings.oneHalf),
color = MainTheme.colors.surfaceContainer,
shape = CircleShape,
) {
Icon(
imageVector = Icons.Outlined.Settings,
imageVector = icon,
contentDescription = label,
modifier = Modifier
.clickable(onClick = onClick)
Expand Down
1 change: 1 addition & 0 deletions feature/navigation/drawer/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="navigation_drawer_action_settings">Settings</string>
<string name="navigation_drawer_action_manage_folders">Manage folders</string>
<string name="navigation_drawer_action_sync_all_accounts">Sync all accounts</string>
<string name="navigation_drawer_action_show_accounts">Show accounts</string>
<string name="navigation_drawer_action_hide_accounts">Hide accounts</string>
<string name="navigation_drawer_unified_inbox_title">Unified Inbox</string>
Expand Down
Loading

0 comments on commit 137ea0c

Please sign in to comment.