From dd56ccf2fc1e8f51515ee2611791ccaed357059c Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Thu, 24 Feb 2022 15:41:10 +0100 Subject: [PATCH] Now uncommited changes shows which diff entry has been clicked Also refactored selected index from commit changes to use the same logic as uncommited changes --- src/main/kotlin/app/ui/CommitChanges.kt | 28 +++++++++----- src/main/kotlin/app/ui/RepositoryOpen.kt | 1 + src/main/kotlin/app/ui/UncommitedChanges.kt | 42 +++++++++++---------- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/app/ui/CommitChanges.kt b/src/main/kotlin/app/ui/CommitChanges.kt index 10d0070..f4531bd 100644 --- a/src/main/kotlin/app/ui/CommitChanges.kt +++ b/src/main/kotlin/app/ui/CommitChanges.kt @@ -3,6 +3,7 @@ package app.ui import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* +import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.text.selection.SelectionContainer @@ -18,6 +19,7 @@ import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import app.extensions.* +import app.git.DiffEntryType import app.theme.* import app.ui.components.AvatarImage import app.ui.components.ScrollableLazyColumn @@ -30,8 +32,9 @@ import org.eclipse.jgit.revwalk.RevCommit @Composable fun CommitChanges( commitChangesViewModel: CommitChangesViewModel, + selectedItem: SelectedItem.CommitBasedItem, onDiffSelected: (DiffEntry) -> Unit, - selectedItem: SelectedItem.CommitBasedItem + diffSelected: DiffEntryType? ) { LaunchedEffect(selectedItem) { commitChangesViewModel.loadChanges(selectedItem.revCommit) @@ -45,6 +48,7 @@ fun CommitChanges( } is CommitChangesStatus.Loaded -> { CommitChangesView( + diffSelected = diffSelected, commit = commitChangesStatus.commit, changes = commitChangesStatus.changes, onDiffSelected = onDiffSelected, @@ -57,7 +61,8 @@ fun CommitChanges( fun CommitChangesView( commit: RevCommit, changes: List, - onDiffSelected: (DiffEntry) -> Unit + onDiffSelected: (DiffEntry) -> Unit, + diffSelected: DiffEntryType? ) { Column( modifier = Modifier @@ -110,7 +115,11 @@ fun CommitChangesView( ) - CommitLogChanges(changes, onDiffSelected = onDiffSelected) + CommitLogChanges( + diffSelected = diffSelected, + diffEntries = changes, + onDiffSelected = onDiffSelected + ) } } } @@ -175,18 +184,20 @@ fun Author(commit: RevCommit) { } @Composable -fun CommitLogChanges(diffEntries: List, onDiffSelected: (DiffEntry) -> Unit) { - val selectedIndex = remember(diffEntries) { mutableStateOf(-1) } - +fun CommitLogChanges( + diffEntries: List, + onDiffSelected: (DiffEntry) -> Unit, + diffSelected: DiffEntryType? +) { ScrollableLazyColumn( modifier = Modifier .fillMaxSize() ) { - itemsIndexed(items = diffEntries) { index, diffEntry -> + items(items = diffEntries) { diffEntry -> val textColor: Color val secondaryTextColor: Color - if (selectedIndex.value == index) { + if (diffSelected?.diffEntry == diffEntry) { textColor = MaterialTheme.colors.primary secondaryTextColor = MaterialTheme.colors.halfPrimary } else { @@ -199,7 +210,6 @@ fun CommitLogChanges(diffEntries: List, onDiffSelected: (DiffEntry) - .height(40.dp) .fillMaxWidth() .clickable { - selectedIndex.value = index onDiffSelected(diffEntry) }, verticalArrangement = Arrangement.Center, diff --git a/src/main/kotlin/app/ui/RepositoryOpen.kt b/src/main/kotlin/app/ui/RepositoryOpen.kt index 87dbe99..50e5782 100644 --- a/src/main/kotlin/app/ui/RepositoryOpen.kt +++ b/src/main/kotlin/app/ui/RepositoryOpen.kt @@ -132,6 +132,7 @@ fun RepositoryOpenPage(tabViewModel: TabViewModel) { CommitChanges( commitChangesViewModel = tabViewModel.commitChangesViewModel, selectedItem = safeSelectedItem, + diffSelected = diffSelected, onDiffSelected = { diffEntry -> tabViewModel.newDiffSelected = DiffEntryType.CommitDiff(diffEntry) } diff --git a/src/main/kotlin/app/ui/UncommitedChanges.kt b/src/main/kotlin/app/ui/UncommitedChanges.kt index f4d5aac..2a9ae9b 100644 --- a/src/main/kotlin/app/ui/UncommitedChanges.kt +++ b/src/main/kotlin/app/ui/UncommitedChanges.kt @@ -32,9 +32,8 @@ import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import app.extensions.fileName -import app.extensions.filePath -import app.extensions.parentDirectoryPath import app.extensions.isMerging +import app.extensions.parentDirectoryPath import app.git.DiffEntryType import app.git.StatusEntry import app.theme.* @@ -110,6 +109,7 @@ fun UncommitedChanges( title = "Staged", allActionTitle = "Unstage all", actionTitle = "Unstage", + selectedEntryType = selectedEntryType, actionColor = MaterialTheme.colors.unstageButton, diffEntries = staged, onDiffEntrySelected = onStagedDiffEntrySelected, @@ -153,10 +153,11 @@ fun UncommitedChanges( } ) }, - allActionTitle = "Stage all", onAllAction = { statusViewModel.stageAll() - } + }, + allActionTitle = "Stage all", + selectedEntryType = selectedEntryType ) Column( @@ -223,7 +224,6 @@ fun UncommitedChangesButtons( ) { var showDropDownMenu by remember { mutableStateOf(false) } - Row( modifier = Modifier .padding(top = 2.dp) @@ -237,19 +237,6 @@ fun UncommitedChangesButtons( enabled = canCommit, shape = MaterialTheme.shapes.small.copy(topEnd = CornerSize(0.dp), bottomEnd = CornerSize(0.dp)) ) -// Button( -// modifier = Modifier -// .weight(1f) -// .height(40.dp), -// onClick = { onCommit(false) }, -// enabled = canCommit, -// shape = RectangleShape, -// ) { -// Text( -// text = "Commit", -// fontSize = 14.sp, -// ) -// } Spacer( modifier = Modifier .width(1.dp) @@ -454,6 +441,7 @@ private fun EntriesList( onGenerateContextMenu: (DiffEntry) -> List, onAllAction: () -> Unit, allActionTitle: String, + selectedEntryType: DiffEntryType?, ) { Column( modifier = modifier @@ -487,8 +475,10 @@ private fun EntriesList( ) { itemsIndexed(diffEntries) { index, statusEntry -> val diffEntry = statusEntry.diffEntry + val isEntrySelected = selectedEntryType?.diffEntry == diffEntry FileEntry( statusEntry = statusEntry, + isSelected = isEntrySelected, actionTitle = actionTitle, actionColor = actionColor, onClick = { @@ -515,6 +505,7 @@ private fun EntriesList( @Composable private fun FileEntry( statusEntry: StatusEntry, + isSelected: Boolean, actionTitle: String, actionColor: Color, onClick: () -> Unit, @@ -524,6 +515,17 @@ private fun FileEntry( var active by remember { mutableStateOf(false) } val diffEntry = statusEntry.diffEntry + val textColor: Color + val secondaryTextColor: Color + + if (isSelected) { + textColor = MaterialTheme.colors.primary + secondaryTextColor = MaterialTheme.colors.halfPrimary + } else { + textColor = MaterialTheme.colors.primaryTextColor + secondaryTextColor = MaterialTheme.colors.secondaryTextColor + } + Box( modifier = Modifier .clickable { onClick() } @@ -567,7 +569,7 @@ private fun FileEntry( softWrap = false, fontSize = 13.sp, overflow = TextOverflow.Ellipsis, - color = MaterialTheme.colors.secondaryTextColor, + color = secondaryTextColor, ) Text( text = diffEntry.fileName, @@ -575,7 +577,7 @@ private fun FileEntry( maxLines = 1, softWrap = false, fontSize = 13.sp, - color = MaterialTheme.colors.primaryTextColor, + color = textColor, ) } }