From cc787064b6827fd4a826b39a0d660c43ca1711b4 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Sat, 5 Feb 2022 01:28:48 +0100 Subject: [PATCH] Clicking on a remote now colpases it Removed animations from expandable to improve performance in big lists --- src/main/kotlin/app/ui/Remotes.kt | 21 ++++++++++----- .../kotlin/app/ui/components/Expandable.kt | 26 ++++++++++++++----- .../kotlin/app/viewmodels/RemotesViewModel.kt | 21 ++++++++++++--- .../kotlin/app/viewmodels/TabViewModel.kt | 7 +++-- 4 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/app/ui/Remotes.kt b/src/main/kotlin/app/ui/Remotes.kt index 9ed38fa..bffcff7 100644 --- a/src/main/kotlin/app/ui/Remotes.kt +++ b/src/main/kotlin/app/ui/Remotes.kt @@ -10,11 +10,11 @@ import androidx.compose.runtime.remember import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp import app.extensions.simpleVisibleName -import app.git.RemoteInfo import app.ui.components.SideMenuPanel import app.ui.components.SideMenuSubentry import app.ui.components.VerticalExpandable import app.ui.context_menu.remoteBranchesContextMenu +import app.viewmodels.RemoteView import app.viewmodels.RemotesViewModel import org.eclipse.jgit.lib.Ref @@ -26,7 +26,12 @@ fun Remotes( val remotes by remotesViewModel.remotes.collectAsState() val itemsCount = remember(remotes) { - val allBranches = remotes.map { it.branchesList }.flatten() + val allBranches = remotes.filter { remoteView -> + remoteView.isExpanded // Only include in the branches count the nodes expanded + }.map { remoteView -> + remoteView.remoteInfo.branchesList + }.flatten() + allBranches.count() + remotes.count() } @@ -39,7 +44,8 @@ fun Remotes( RemoteRow( remote = remoteInfo, onBranchClicked = { branch -> onBranchClicked(branch) }, - onDeleteBranch = { branch -> remotesViewModel.deleteBranch(branch) } + onDeleteBranch = { branch -> remotesViewModel.deleteBranch(branch) }, + onRemoteClicked = { remotesViewModel.onRemoteClicked(remoteInfo) } ) } ) @@ -48,19 +54,22 @@ fun Remotes( @OptIn(ExperimentalFoundationApi::class) @Composable private fun RemoteRow( - remote: RemoteInfo, + remote: RemoteView, + onRemoteClicked: () -> Unit, onBranchClicked: (Ref) -> Unit, onDeleteBranch: (Ref) -> Unit, ) { VerticalExpandable( + isExpanded = remote.isExpanded, + onExpand = onRemoteClicked, header = { SideMenuSubentry( - text = remote.remoteConfig.name, + text = remote.remoteInfo.remoteConfig.name, iconResourcePath = "cloud.svg", ) } ) { - val branches = remote.branchesList + val branches = remote.remoteInfo.branchesList Column { branches.forEach { branch -> ContextMenuArea( diff --git a/src/main/kotlin/app/ui/components/Expandable.kt b/src/main/kotlin/app/ui/components/Expandable.kt index 35bb60c..ef2cef7 100644 --- a/src/main/kotlin/app/ui/components/Expandable.kt +++ b/src/main/kotlin/app/ui/components/Expandable.kt @@ -11,25 +11,37 @@ import androidx.compose.ui.Modifier @OptIn(ExperimentalAnimationApi::class) @Composable fun VerticalExpandable( + isExpanded: MutableState = 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( + isExpanded: Boolean, + onExpand: () -> Unit, header: @Composable () -> Unit, child: @Composable () -> Unit, ) { - var isExpanded by remember { - mutableStateOf(true) - } Column { Box( modifier = Modifier.clickable { - isExpanded = !isExpanded + onExpand() } ) { header() } - AnimatedVisibility(visible = isExpanded) { + if(isExpanded) { child() } - } - } \ No newline at end of file diff --git a/src/main/kotlin/app/viewmodels/RemotesViewModel.kt b/src/main/kotlin/app/viewmodels/RemotesViewModel.kt index eb228d5..743066f 100644 --- a/src/main/kotlin/app/viewmodels/RemotesViewModel.kt +++ b/src/main/kotlin/app/viewmodels/RemotesViewModel.kt @@ -15,8 +15,8 @@ class RemotesViewModel @Inject constructor( private val branchesManager: BranchesManager, private val tabState: TabState, ) { - private val _remotes = MutableStateFlow>(listOf()) - val remotes: StateFlow> + private val _remotes = MutableStateFlow>(listOf()) + val remotes: StateFlow> get() = _remotes suspend fun loadRemotes(git: Git) = withContext(Dispatchers.IO) { @@ -32,7 +32,11 @@ class RemotesViewModel @Inject constructor( RemoteInfo(remoteConfig, remoteBranches) } - _remotes.value = remoteInfoList + val remoteViewList = remoteInfoList.map { remoteInfo -> + RemoteView(remoteInfo, true) + } + + _remotes.value = remoteViewList } fun deleteBranch(ref: Ref) = tabState.safeProcessing { git -> @@ -44,5 +48,16 @@ class RemotesViewModel @Inject constructor( suspend fun refresh(git: Git) = withContext(Dispatchers.IO) { loadRemotes(git) } + + fun onRemoteClicked(remoteInfo: RemoteView) { + val remotes = _remotes.value + val newRemoteInfo = remoteInfo.copy(isExpanded = !remoteInfo.isExpanded) + val newRemotesList = remotes.toMutableList() + val indexToReplace = newRemotesList.indexOf(remoteInfo) + newRemotesList[indexToReplace] = newRemoteInfo + + _remotes.value = newRemotesList + } } +data class RemoteView(val remoteInfo: RemoteInfo, val isExpanded: Boolean) \ No newline at end of file diff --git a/src/main/kotlin/app/viewmodels/TabViewModel.kt b/src/main/kotlin/app/viewmodels/TabViewModel.kt index 2cdc43e..1896fd5 100644 --- a/src/main/kotlin/app/viewmodels/TabViewModel.kt +++ b/src/main/kotlin/app/viewmodels/TabViewModel.kt @@ -217,12 +217,11 @@ class TabViewModel @Inject constructor( fun newSelectedRef(objectId: ObjectId?) = tabState.runOperation { git -> if (objectId == null) { newSelectedItem(SelectedItem.None) - return@runOperation RefreshType.NONE + } else { + val commit = findCommit(git, objectId) + newSelectedItem(SelectedItem.Ref(commit)) } - val commit = findCommit(git, objectId) - newSelectedItem(SelectedItem.Ref(commit)) - return@runOperation RefreshType.NONE }