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
This commit is contained in:
Abdelilah El Aissaoui 2022-02-24 15:41:10 +01:00
parent 970132f1d7
commit dd56ccf2fc
3 changed files with 42 additions and 29 deletions

View File

@ -3,6 +3,7 @@ package app.ui
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.selection.SelectionContainer 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.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import app.extensions.* import app.extensions.*
import app.git.DiffEntryType
import app.theme.* import app.theme.*
import app.ui.components.AvatarImage import app.ui.components.AvatarImage
import app.ui.components.ScrollableLazyColumn import app.ui.components.ScrollableLazyColumn
@ -30,8 +32,9 @@ import org.eclipse.jgit.revwalk.RevCommit
@Composable @Composable
fun CommitChanges( fun CommitChanges(
commitChangesViewModel: CommitChangesViewModel, commitChangesViewModel: CommitChangesViewModel,
selectedItem: SelectedItem.CommitBasedItem,
onDiffSelected: (DiffEntry) -> Unit, onDiffSelected: (DiffEntry) -> Unit,
selectedItem: SelectedItem.CommitBasedItem diffSelected: DiffEntryType?
) { ) {
LaunchedEffect(selectedItem) { LaunchedEffect(selectedItem) {
commitChangesViewModel.loadChanges(selectedItem.revCommit) commitChangesViewModel.loadChanges(selectedItem.revCommit)
@ -45,6 +48,7 @@ fun CommitChanges(
} }
is CommitChangesStatus.Loaded -> { is CommitChangesStatus.Loaded -> {
CommitChangesView( CommitChangesView(
diffSelected = diffSelected,
commit = commitChangesStatus.commit, commit = commitChangesStatus.commit,
changes = commitChangesStatus.changes, changes = commitChangesStatus.changes,
onDiffSelected = onDiffSelected, onDiffSelected = onDiffSelected,
@ -57,7 +61,8 @@ fun CommitChanges(
fun CommitChangesView( fun CommitChangesView(
commit: RevCommit, commit: RevCommit,
changes: List<DiffEntry>, changes: List<DiffEntry>,
onDiffSelected: (DiffEntry) -> Unit onDiffSelected: (DiffEntry) -> Unit,
diffSelected: DiffEntryType?
) { ) {
Column( Column(
modifier = Modifier 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 @Composable
fun CommitLogChanges(diffEntries: List<DiffEntry>, onDiffSelected: (DiffEntry) -> Unit) { fun CommitLogChanges(
val selectedIndex = remember(diffEntries) { mutableStateOf(-1) } diffEntries: List<DiffEntry>,
onDiffSelected: (DiffEntry) -> Unit,
diffSelected: DiffEntryType?
) {
ScrollableLazyColumn( ScrollableLazyColumn(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
) { ) {
itemsIndexed(items = diffEntries) { index, diffEntry -> items(items = diffEntries) { diffEntry ->
val textColor: Color val textColor: Color
val secondaryTextColor: Color val secondaryTextColor: Color
if (selectedIndex.value == index) { if (diffSelected?.diffEntry == diffEntry) {
textColor = MaterialTheme.colors.primary textColor = MaterialTheme.colors.primary
secondaryTextColor = MaterialTheme.colors.halfPrimary secondaryTextColor = MaterialTheme.colors.halfPrimary
} else { } else {
@ -199,7 +210,6 @@ fun CommitLogChanges(diffEntries: List<DiffEntry>, onDiffSelected: (DiffEntry) -
.height(40.dp) .height(40.dp)
.fillMaxWidth() .fillMaxWidth()
.clickable { .clickable {
selectedIndex.value = index
onDiffSelected(diffEntry) onDiffSelected(diffEntry)
}, },
verticalArrangement = Arrangement.Center, verticalArrangement = Arrangement.Center,

View File

@ -132,6 +132,7 @@ fun RepositoryOpenPage(tabViewModel: TabViewModel) {
CommitChanges( CommitChanges(
commitChangesViewModel = tabViewModel.commitChangesViewModel, commitChangesViewModel = tabViewModel.commitChangesViewModel,
selectedItem = safeSelectedItem, selectedItem = safeSelectedItem,
diffSelected = diffSelected,
onDiffSelected = { diffEntry -> onDiffSelected = { diffEntry ->
tabViewModel.newDiffSelected = DiffEntryType.CommitDiff(diffEntry) tabViewModel.newDiffSelected = DiffEntryType.CommitDiff(diffEntry)
} }

View File

@ -32,9 +32,8 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import app.extensions.fileName import app.extensions.fileName
import app.extensions.filePath
import app.extensions.parentDirectoryPath
import app.extensions.isMerging import app.extensions.isMerging
import app.extensions.parentDirectoryPath
import app.git.DiffEntryType import app.git.DiffEntryType
import app.git.StatusEntry import app.git.StatusEntry
import app.theme.* import app.theme.*
@ -110,6 +109,7 @@ fun UncommitedChanges(
title = "Staged", title = "Staged",
allActionTitle = "Unstage all", allActionTitle = "Unstage all",
actionTitle = "Unstage", actionTitle = "Unstage",
selectedEntryType = selectedEntryType,
actionColor = MaterialTheme.colors.unstageButton, actionColor = MaterialTheme.colors.unstageButton,
diffEntries = staged, diffEntries = staged,
onDiffEntrySelected = onStagedDiffEntrySelected, onDiffEntrySelected = onStagedDiffEntrySelected,
@ -153,10 +153,11 @@ fun UncommitedChanges(
} }
) )
}, },
allActionTitle = "Stage all",
onAllAction = { onAllAction = {
statusViewModel.stageAll() statusViewModel.stageAll()
} },
allActionTitle = "Stage all",
selectedEntryType = selectedEntryType
) )
Column( Column(
@ -223,7 +224,6 @@ fun UncommitedChangesButtons(
) { ) {
var showDropDownMenu by remember { mutableStateOf(false) } var showDropDownMenu by remember { mutableStateOf(false) }
Row( Row(
modifier = Modifier modifier = Modifier
.padding(top = 2.dp) .padding(top = 2.dp)
@ -237,19 +237,6 @@ fun UncommitedChangesButtons(
enabled = canCommit, enabled = canCommit,
shape = MaterialTheme.shapes.small.copy(topEnd = CornerSize(0.dp), bottomEnd = CornerSize(0.dp)) 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( Spacer(
modifier = Modifier modifier = Modifier
.width(1.dp) .width(1.dp)
@ -454,6 +441,7 @@ private fun EntriesList(
onGenerateContextMenu: (DiffEntry) -> List<ContextMenuItem>, onGenerateContextMenu: (DiffEntry) -> List<ContextMenuItem>,
onAllAction: () -> Unit, onAllAction: () -> Unit,
allActionTitle: String, allActionTitle: String,
selectedEntryType: DiffEntryType?,
) { ) {
Column( Column(
modifier = modifier modifier = modifier
@ -487,8 +475,10 @@ private fun EntriesList(
) { ) {
itemsIndexed(diffEntries) { index, statusEntry -> itemsIndexed(diffEntries) { index, statusEntry ->
val diffEntry = statusEntry.diffEntry val diffEntry = statusEntry.diffEntry
val isEntrySelected = selectedEntryType?.diffEntry == diffEntry
FileEntry( FileEntry(
statusEntry = statusEntry, statusEntry = statusEntry,
isSelected = isEntrySelected,
actionTitle = actionTitle, actionTitle = actionTitle,
actionColor = actionColor, actionColor = actionColor,
onClick = { onClick = {
@ -515,6 +505,7 @@ private fun EntriesList(
@Composable @Composable
private fun FileEntry( private fun FileEntry(
statusEntry: StatusEntry, statusEntry: StatusEntry,
isSelected: Boolean,
actionTitle: String, actionTitle: String,
actionColor: Color, actionColor: Color,
onClick: () -> Unit, onClick: () -> Unit,
@ -524,6 +515,17 @@ private fun FileEntry(
var active by remember { mutableStateOf(false) } var active by remember { mutableStateOf(false) }
val diffEntry = statusEntry.diffEntry 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( Box(
modifier = Modifier modifier = Modifier
.clickable { onClick() } .clickable { onClick() }
@ -567,7 +569,7 @@ private fun FileEntry(
softWrap = false, softWrap = false,
fontSize = 13.sp, fontSize = 13.sp,
overflow = TextOverflow.Ellipsis, overflow = TextOverflow.Ellipsis,
color = MaterialTheme.colors.secondaryTextColor, color = secondaryTextColor,
) )
Text( Text(
text = diffEntry.fileName, text = diffEntry.fileName,
@ -575,7 +577,7 @@ private fun FileEntry(
maxLines = 1, maxLines = 1,
softWrap = false, softWrap = false,
fontSize = 13.sp, fontSize = 13.sp,
color = MaterialTheme.colors.primaryTextColor, color = textColor,
) )
} }
} }