mirror of
https://github.com/thunderbird/thunderbird-android.git
synced 2024-09-19 19:52:14 +02:00
Add FolderList
This commit is contained in:
parent
7b473d64d0
commit
8d34ada101
@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import app.k9mail.core.ui.compose.designsystem.PreviewWithTheme
|
||||
import app.k9mail.feature.navigation.drawer.ui.FakeData.DISPLAY_ACCOUNT
|
||||
import kotlinx.collections.immutable.persistentListOf
|
||||
|
||||
@Composable
|
||||
@Preview(showBackground = true)
|
||||
@ -11,8 +12,9 @@ internal fun DrawerContentPreview() {
|
||||
PreviewWithTheme {
|
||||
DrawerContent(
|
||||
state = DrawerContract.State(
|
||||
accounts = emptyList(),
|
||||
currentAccount = null,
|
||||
accounts = persistentListOf(),
|
||||
folders = persistentListOf(),
|
||||
),
|
||||
)
|
||||
}
|
||||
@ -24,7 +26,7 @@ fun DrawerContentWithAccountPreview() {
|
||||
PreviewWithTheme {
|
||||
DrawerContent(
|
||||
state = DrawerContract.State(
|
||||
accounts = listOf(DISPLAY_ACCOUNT),
|
||||
accounts = persistentListOf(DISPLAY_ACCOUNT),
|
||||
currentAccount = DISPLAY_ACCOUNT,
|
||||
),
|
||||
)
|
||||
|
@ -49,6 +49,7 @@ val navigationDrawerModule: Module = module {
|
||||
viewModel {
|
||||
DrawerViewModel(
|
||||
getDisplayAccounts = get(),
|
||||
getDisplayFoldersForAccount = get(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -4,16 +4,15 @@ import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import app.k9mail.core.ui.compose.designsystem.atom.DividerHorizontal
|
||||
import app.k9mail.core.ui.compose.designsystem.atom.Surface
|
||||
import app.k9mail.core.ui.compose.designsystem.organism.drawer.NavigationDrawerItem
|
||||
import app.k9mail.core.ui.compose.theme2.MainTheme
|
||||
import app.k9mail.feature.navigation.drawer.ui.DrawerContract.State
|
||||
import app.k9mail.feature.navigation.drawer.ui.account.AccountView
|
||||
import app.k9mail.feature.navigation.drawer.ui.folder.FolderList
|
||||
|
||||
@Composable
|
||||
fun DrawerContent(
|
||||
@ -42,32 +41,12 @@ fun DrawerContent(
|
||||
|
||||
DividerHorizontal()
|
||||
}
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize(),
|
||||
) {
|
||||
item {
|
||||
NavigationDrawerItem(
|
||||
label = "Folder1",
|
||||
selected = true,
|
||||
onClick = {},
|
||||
)
|
||||
}
|
||||
item {
|
||||
NavigationDrawerItem(
|
||||
label = "Folder2",
|
||||
selected = false,
|
||||
onClick = {},
|
||||
)
|
||||
}
|
||||
item {
|
||||
NavigationDrawerItem(
|
||||
label = "Folder3",
|
||||
selected = false,
|
||||
onClick = {},
|
||||
)
|
||||
}
|
||||
}
|
||||
FolderList(
|
||||
folders = state.folders,
|
||||
selectedFolder = state.folders.firstOrNull(), // TODO Use selected folder from state
|
||||
onFolderClick = { },
|
||||
showStarredCount = state.showStarredCount,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,23 @@
|
||||
package app.k9mail.feature.navigation.drawer.ui
|
||||
|
||||
import androidx.compose.runtime.Stable
|
||||
import app.k9mail.core.ui.compose.common.mvi.UnidirectionalViewModel
|
||||
import app.k9mail.feature.navigation.drawer.domain.entity.DisplayAccount
|
||||
import app.k9mail.legacy.ui.folder.DisplayFolder
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import kotlinx.collections.immutable.immutableListOf
|
||||
import kotlinx.collections.immutable.persistentListOf
|
||||
|
||||
interface DrawerContract {
|
||||
|
||||
interface ViewModel : UnidirectionalViewModel<State, Event, Effect>
|
||||
|
||||
@Stable
|
||||
data class State(
|
||||
val currentAccount: DisplayAccount? = null,
|
||||
val accounts: List<DisplayAccount> = emptyList(),
|
||||
val accounts: ImmutableList<DisplayAccount> = persistentListOf(),
|
||||
val folders: ImmutableList<DisplayFolder> = persistentListOf(),
|
||||
val showStarredCount: Boolean = false,
|
||||
val isLoading: Boolean = false,
|
||||
)
|
||||
|
||||
|
@ -4,16 +4,18 @@ import androidx.compose.runtime.Composable
|
||||
import app.k9mail.core.ui.compose.common.mvi.observe
|
||||
import app.k9mail.core.ui.compose.designsystem.molecule.PullToRefreshBox
|
||||
import org.koin.androidx.compose.koinViewModel
|
||||
import app.k9mail.feature.navigation.drawer.ui.DrawerContract.Event
|
||||
import app.k9mail.feature.navigation.drawer.ui.DrawerContract.ViewModel
|
||||
|
||||
@Composable
|
||||
fun DrawerView(
|
||||
viewModel: DrawerContract.ViewModel = koinViewModel<DrawerViewModel>(),
|
||||
viewModel: ViewModel = koinViewModel<DrawerViewModel>(),
|
||||
) {
|
||||
val (state, dispatch) = viewModel.observe { }
|
||||
|
||||
PullToRefreshBox(
|
||||
isRefreshing = state.value.isLoading,
|
||||
onRefresh = { dispatch(DrawerContract.Event.OnRefresh) },
|
||||
onRefresh = { dispatch(Event.OnRefresh) },
|
||||
) {
|
||||
DrawerContent(
|
||||
state = state.value,
|
||||
|
@ -8,12 +8,18 @@ import app.k9mail.feature.navigation.drawer.ui.DrawerContract.Effect
|
||||
import app.k9mail.feature.navigation.drawer.ui.DrawerContract.Event
|
||||
import app.k9mail.feature.navigation.drawer.ui.DrawerContract.State
|
||||
import app.k9mail.feature.navigation.drawer.ui.DrawerContract.ViewModel
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import kotlinx.collections.immutable.toImmutableList
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Suppress("MagicNumber")
|
||||
class DrawerViewModel(
|
||||
private val getDisplayAccounts: UseCase.GetDisplayAccounts,
|
||||
private val getDisplayFoldersForAccount: UseCase.GetDisplayFoldersForAccount,
|
||||
initialState: State = State(),
|
||||
) : BaseViewModel<State, Event, Effect>(
|
||||
initialState = initialState,
|
||||
@ -22,7 +28,17 @@ class DrawerViewModel(
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
getDisplayAccounts().collect { accounts -> updateAccounts(accounts) }
|
||||
loadAccounts()
|
||||
}
|
||||
|
||||
viewModelScope.launch {
|
||||
loadFolders()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun loadAccounts() {
|
||||
getDisplayAccounts().collectLatest { accounts ->
|
||||
updateAccounts(accounts)
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,16 +48,30 @@ class DrawerViewModel(
|
||||
|
||||
updateState {
|
||||
if (isCurrentAccountAvailable) {
|
||||
it.copy(accounts = accounts)
|
||||
it.copy(accounts = accounts.toImmutableList())
|
||||
} else {
|
||||
it.copy(
|
||||
accounts = accounts.toImmutableList(),
|
||||
currentAccount = accounts.firstOrNull(),
|
||||
accounts = accounts,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun loadFolders() {
|
||||
state.map { it.currentAccount }
|
||||
.distinctUntilChanged()
|
||||
.collectLatest { currentAccount ->
|
||||
if (currentAccount != null) {
|
||||
getDisplayFoldersForAccount(currentAccount.account.uuid).collectLatest { folders ->
|
||||
updateState {
|
||||
it.copy(folders = folders.toImmutableList())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun event(event: Event) {
|
||||
when (event) {
|
||||
Event.OnRefresh -> refresh()
|
||||
|
@ -0,0 +1,32 @@
|
||||
package app.k9mail.feature.navigation.drawer.ui.folder
|
||||
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import app.k9mail.legacy.ui.folder.DisplayFolder
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
|
||||
@Composable
|
||||
fun FolderList(
|
||||
folders: ImmutableList<DisplayFolder>,
|
||||
selectedFolder: DisplayFolder?,
|
||||
onFolderClick: (DisplayFolder) -> Unit,
|
||||
showStarredCount: Boolean,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
LazyColumn(
|
||||
modifier = modifier
|
||||
.fillMaxSize(),
|
||||
) {
|
||||
items(folders) { folder ->
|
||||
FolderListItem(
|
||||
displayFolder = folder,
|
||||
selected = folder == selectedFolder,
|
||||
showStarredCount = showStarredCount,
|
||||
onClick = onFolderClick,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ import app.k9mail.feature.navigation.drawer.ui.DrawerContract.Event
|
||||
import app.k9mail.feature.navigation.drawer.ui.DrawerContract.State
|
||||
import app.k9mail.legacy.account.Account
|
||||
import app.k9mail.legacy.account.Identity
|
||||
import app.k9mail.legacy.ui.folder.DisplayFolder
|
||||
import assertk.assertThat
|
||||
import assertk.assertions.isEqualTo
|
||||
import kotlin.test.Test
|
||||
@ -91,9 +92,11 @@ class DrawerViewModelTest {
|
||||
|
||||
private fun createTestSubject(
|
||||
getDisplayAccountsFlow: Flow<List<DisplayAccount>> = flow { emit(emptyList()) },
|
||||
getDisplayFoldersForAccount: Flow<List<DisplayFolder>> = flow { emit(emptyList()) },
|
||||
): DrawerViewModel {
|
||||
return DrawerViewModel(
|
||||
getDisplayAccounts = { getDisplayAccountsFlow },
|
||||
getDisplayFoldersForAccount = { getDisplayFoldersForAccount },
|
||||
)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user