Side menu items expand state is now stored in the viewModels to restore it's value after changing tabs

This commit is contained in:
Abdelilah El Aissaoui 2022-04-08 22:05:48 +02:00
parent 8650b1a43c
commit 855b57196d
11 changed files with 36 additions and 20 deletions

View File

@ -26,6 +26,7 @@ fun Branches(
) {
val branches by branchesViewModel.branches.collectAsState()
val currentBranchState = branchesViewModel.currentBranch.collectAsState()
val isExpanded by branchesViewModel.isExpanded.collectAsState()
val currentBranch = currentBranchState.value
val (mergeBranch, setMergeBranch) = remember { mutableStateOf<Ref?>(null) }
@ -35,6 +36,8 @@ fun Branches(
title = "Local branches",
icon = painterResource("branch.svg"),
items = branches,
isExpanded = isExpanded,
onExpand = { branchesViewModel.onExpand() },
itemContent = { branch ->
BranchLineEntry(
branch = branch,

View File

@ -33,6 +33,7 @@ fun Remotes(
) {
val remotes by remotesViewModel.remotes.collectAsState()
var showEditRemotesDialog by remember { mutableStateOf(false) }
val isExpanded by remotesViewModel.isExpanded.collectAsState()
val itemsCount = remember(remotes) {
val allBranches = remotes.filter { remoteView ->
@ -58,6 +59,8 @@ fun Remotes(
icon = painterResource("cloud.svg"),
items = remotes,
itemsCountForMaxHeight = itemsCount,
isExpanded = isExpanded,
onExpand = { remotesViewModel.onExpand() },
contextItems = {
remoteContextMenu { showEditRemotesDialog = true }
},
@ -84,7 +87,7 @@ fun Remotes(
onDeleteBranch = { branch -> remotesViewModel.deleteRemoteBranch(branch) },
onRemoteClicked = { remotesViewModel.onRemoteClicked(remoteInfo) }
)
}
},
)
}

View File

@ -7,6 +7,7 @@ import androidx.compose.foundation.ContextMenuItem
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.res.painterResource
import app.ui.components.SideMenuPanel
import app.ui.components.SideMenuSubentry
@ -21,6 +22,7 @@ fun Stashes(
) {
val stashStatusState = stashesViewModel.stashStatus.collectAsState()
val stashStatus = stashStatusState.value
val isExpanded by stashesViewModel.isExpanded.collectAsState()
val stashList = if (stashStatus is StashStatus.Loaded)
stashStatus.stashes
@ -31,6 +33,8 @@ fun Stashes(
title = "Stashes",
icon = painterResource("stash.svg"),
items = stashList,
isExpanded = isExpanded,
onExpand = { stashesViewModel.onExpand() },
itemContent = { stash ->
StashRow(
stash = stash,

View File

@ -4,6 +4,7 @@ import androidx.compose.foundation.ContextMenuArea
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.res.painterResource
import app.extensions.simpleName
import app.ui.components.SideMenuPanel
@ -19,11 +20,14 @@ fun Tags(
) {
val tagsState = tagsViewModel.tags.collectAsState()
val tags = tagsState.value
val isExpanded by tagsViewModel.isExpanded.collectAsState()
SideMenuPanel(
title = "Tags",
items = tags,
icon = painterResource("tag.svg"),
isExpanded = isExpanded,
onExpand = { tagsViewModel.onExpand() },
itemContent = { tag ->
TagRow(
tag = tag,

View File

@ -10,21 +10,6 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
@OptIn(ExperimentalAnimationApi::class)
@Composable
fun VerticalExpandable(
isExpanded: MutableState<Boolean> = remember { mutableStateOf(true) },
header: @Composable () -> Unit,
child: @Composable () -> Unit,
) {
VerticalExpandable(
isExpanded = isExpanded.value,
onExpand = { isExpanded.value = !isExpanded.value },
header = header,
child = child,
)
}
@OptIn(ExperimentalAnimationApi::class)
@Composable
fun VerticalExpandable(

View File

@ -21,6 +21,8 @@ fun <T> SideMenuPanel(
title: String,
icon: Painter? = null,
items: List<T>,
isExpanded: Boolean = false,
onExpand: () -> Unit,
itemsCountForMaxHeight: Int = items.count(),
itemContent: @Composable (T) -> Unit,
headerHoverIcon: @Composable (() -> Unit)? = null,
@ -29,6 +31,8 @@ fun <T> SideMenuPanel(
val maxHeight = remember(items) { maxSidePanelHeight(itemsCountForMaxHeight) }
VerticalExpandable(
isExpanded = isExpanded,
onExpand = onExpand,
header = {
ContextMenuArea(
items = contextItems

View File

@ -13,7 +13,7 @@ class BranchesViewModel @Inject constructor(
private val mergeManager: MergeManager,
private val remoteOperationsManager: RemoteOperationsManager,
private val tabState: TabState,
) {
) : ExpandableViewModel() {
private val _branches = MutableStateFlow<List<Ref>>(listOf())
val branches: StateFlow<List<Ref>>
get() = _branches

View File

@ -0,0 +1,13 @@
package app.viewmodels
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
abstract class ExpandableViewModel {
private val _isExpanded = MutableStateFlow(true)
val isExpanded: StateFlow<Boolean> = _isExpanded
fun onExpand() {
_isExpanded.value = !isExpanded.value
}
}

View File

@ -17,7 +17,7 @@ class RemotesViewModel @Inject constructor(
private val remoteOperationsManager: RemoteOperationsManager,
private val branchesManager: BranchesManager,
private val tabState: TabState,
) {
) : ExpandableViewModel() {
private val _remotes = MutableStateFlow<List<RemoteView>>(listOf())
val remotes: StateFlow<List<RemoteView>>
get() = _remotes

View File

@ -13,7 +13,7 @@ import javax.inject.Inject
class StashesViewModel @Inject constructor(
private val stashManager: StashManager,
private val tabState: TabState,
) {
) : ExpandableViewModel() {
private val _stashStatus = MutableStateFlow<StashStatus>(StashStatus.Loaded(listOf()))
val stashStatus: StateFlow<StashStatus>
get() = _stashStatus

View File

@ -16,7 +16,7 @@ class TagsViewModel @Inject constructor(
private val tabState: TabState,
private val branchesManager: BranchesManager,
private val tagsManager: TagsManager,
) {
) : ExpandableViewModel() {
private val _tags = MutableStateFlow<List<Ref>>(listOf())
val tags: StateFlow<List<Ref>>
get() = _tags