mirror of
https://github.com/readrops/Readrops.git
synced 2025-02-08 07:58:41 +01:00
Add initial drawer state in TimelineViewModel
This commit is contained in:
parent
deb6426edf
commit
cffc102b20
@ -13,27 +13,45 @@ import androidx.compose.material3.NavigationDrawerItem
|
|||||||
import androidx.compose.material3.NavigationDrawerItemDefaults
|
import androidx.compose.material3.NavigationDrawerItemDefaults
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.readrops.app.compose.R
|
import com.readrops.app.compose.R
|
||||||
import com.readrops.app.compose.util.theme.spacing
|
import com.readrops.app.compose.util.theme.spacing
|
||||||
|
|
||||||
|
enum class DrawerDefaultItemsSelection {
|
||||||
|
ARTICLES,
|
||||||
|
NEW,
|
||||||
|
FAVORITES,
|
||||||
|
READ_LATER
|
||||||
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun TimelineDrawer(
|
fun TimelineDrawer(
|
||||||
viewModel: TimelineViewModel,
|
viewModel: TimelineViewModel,
|
||||||
|
onClickDefaultItem: (DrawerDefaultItemsSelection) -> Unit,
|
||||||
) {
|
) {
|
||||||
|
val state by viewModel.drawerState.collectAsState()
|
||||||
|
|
||||||
ModalDrawerSheet {
|
ModalDrawerSheet {
|
||||||
Spacer(modifier = Modifier.size(MaterialTheme.spacing.drawerSpacing))
|
Spacer(modifier = Modifier.size(MaterialTheme.spacing.drawerSpacing))
|
||||||
|
|
||||||
DrawerDefaultItems()
|
DrawerDefaultItems(
|
||||||
|
selectedItem = state.selection,
|
||||||
|
onClick = { onClickDefaultItem(it) }
|
||||||
|
)
|
||||||
|
|
||||||
DrawerDivider()
|
DrawerDivider()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun DrawerDefaultItems() {
|
fun DrawerDefaultItems(
|
||||||
|
selectedItem: DrawerDefaultItemsSelection,
|
||||||
|
onClick: (DrawerDefaultItemsSelection) -> Unit,
|
||||||
|
) {
|
||||||
NavigationDrawerItem(
|
NavigationDrawerItem(
|
||||||
label = { Text("Articles") },
|
label = { Text("Articles") },
|
||||||
icon = {
|
icon = {
|
||||||
@ -42,8 +60,8 @@ fun DrawerDefaultItems() {
|
|||||||
contentDescription = null
|
contentDescription = null
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
selected = true,
|
selected = selectedItem == DrawerDefaultItemsSelection.ARTICLES,
|
||||||
onClick = { },
|
onClick = { onClick(DrawerDefaultItemsSelection.ARTICLES) },
|
||||||
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
|
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -55,8 +73,8 @@ fun DrawerDefaultItems() {
|
|||||||
contentDescription = null
|
contentDescription = null
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
selected = false,
|
selected = selectedItem == DrawerDefaultItemsSelection.NEW,
|
||||||
onClick = { },
|
onClick = { onClick(DrawerDefaultItemsSelection.NEW) },
|
||||||
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
|
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -68,8 +86,8 @@ fun DrawerDefaultItems() {
|
|||||||
contentDescription = null
|
contentDescription = null
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
selected = false,
|
selected = selectedItem == DrawerDefaultItemsSelection.FAVORITES,
|
||||||
onClick = { },
|
onClick = { onClick(DrawerDefaultItemsSelection.FAVORITES) },
|
||||||
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
|
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -81,8 +99,8 @@ fun DrawerDefaultItems() {
|
|||||||
contentDescription = null
|
contentDescription = null
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
selected = false,
|
selected = selectedItem == DrawerDefaultItemsSelection.READ_LATER,
|
||||||
onClick = { },
|
onClick = { onClick(DrawerDefaultItemsSelection.READ_LATER) },
|
||||||
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
|
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,15 @@ object TimelineTab : Tab {
|
|||||||
ModalNavigationDrawer(
|
ModalNavigationDrawer(
|
||||||
drawerState = drawerState,
|
drawerState = drawerState,
|
||||||
drawerContent = {
|
drawerContent = {
|
||||||
TimelineDrawer(viewModel = viewModel)
|
TimelineDrawer(
|
||||||
|
viewModel = viewModel,
|
||||||
|
onClickDefaultItem = {
|
||||||
|
viewModel.updateDrawerDefaultItem(it)
|
||||||
|
scope.launch {
|
||||||
|
drawerState.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
Scaffold(
|
Scaffold(
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package com.readrops.app.compose.timelime
|
package com.readrops.app.compose.timelime
|
||||||
|
|
||||||
|
import androidx.compose.runtime.Immutable
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.readrops.app.compose.base.TabViewModel
|
import com.readrops.app.compose.base.TabViewModel
|
||||||
import com.readrops.db.Database
|
import com.readrops.db.Database
|
||||||
|
import com.readrops.db.entities.Feed
|
||||||
|
import com.readrops.db.entities.Folder
|
||||||
import com.readrops.db.pojo.ItemWithFeed
|
import com.readrops.db.pojo.ItemWithFeed
|
||||||
import com.readrops.db.queries.ItemsQueryBuilder
|
import com.readrops.db.queries.ItemsQueryBuilder
|
||||||
import com.readrops.db.queries.QueryFilters
|
import com.readrops.db.queries.QueryFilters
|
||||||
@ -13,6 +16,7 @@ import kotlinx.coroutines.flow.asStateFlow
|
|||||||
import kotlinx.coroutines.flow.catch
|
import kotlinx.coroutines.flow.catch
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
import kotlinx.coroutines.flow.consumeAsFlow
|
import kotlinx.coroutines.flow.consumeAsFlow
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class TimelineViewModel(
|
class TimelineViewModel(
|
||||||
@ -26,6 +30,9 @@ class TimelineViewModel(
|
|||||||
private var _isRefreshing = MutableStateFlow(false)
|
private var _isRefreshing = MutableStateFlow(false)
|
||||||
val isRefreshing = _isRefreshing.asStateFlow()
|
val isRefreshing = _isRefreshing.asStateFlow()
|
||||||
|
|
||||||
|
private val _drawerState = MutableStateFlow(DrawerState())
|
||||||
|
val drawerState = _drawerState.asStateFlow()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
viewModelScope.launch(dispatcher) {
|
viewModelScope.launch(dispatcher) {
|
||||||
accountEvent.consumeAsFlow().collectLatest { account ->
|
accountEvent.consumeAsFlow().collectLatest { account ->
|
||||||
@ -50,11 +57,27 @@ class TimelineViewModel(
|
|||||||
_isRefreshing.value = false
|
_isRefreshing.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun updateDrawerDefaultItem(selection: DrawerDefaultItemsSelection) {
|
||||||
|
_drawerState.update { it.copy(selection = selection) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class TimelineState {
|
sealed class TimelineState {
|
||||||
object Loading : TimelineState()
|
object Loading : TimelineState()
|
||||||
|
|
||||||
|
@Immutable
|
||||||
data class Error(val exception: Exception) : TimelineState()
|
data class Error(val exception: Exception) : TimelineState()
|
||||||
|
|
||||||
|
@Immutable
|
||||||
data class Loaded(val items: List<ItemWithFeed>) : TimelineState()
|
data class Loaded(val items: List<ItemWithFeed>) : TimelineState()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
data class DrawerState(
|
||||||
|
val selection: DrawerDefaultItemsSelection = DrawerDefaultItemsSelection.ARTICLES,
|
||||||
|
val folderSelection: Int = 0,
|
||||||
|
val feedSelection: Int = 0,
|
||||||
|
val foldersAndFeeds: Map<Folder?, List<Feed>> = emptyMap()
|
||||||
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user