From 6ddb77bf6012a31ee5961a1c2ce7eab89e3f9385 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Tue, 28 Dec 2021 02:40:18 +0100 Subject: [PATCH] Added diff update when staging/unstagins (files & hunks) --- src/main/kotlin/app/ui/Diff.kt | 2 +- src/main/kotlin/app/ui/RepositoryOpen.kt | 1 + src/main/kotlin/app/ui/UncommitedChanges.kt | 58 +++++++++++++++++++-- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/app/ui/Diff.kt b/src/main/kotlin/app/ui/Diff.kt index 4e342e3..e6dbd3a 100644 --- a/src/main/kotlin/app/ui/Diff.kt +++ b/src/main/kotlin/app/ui/Diff.kt @@ -28,7 +28,7 @@ import org.eclipse.jgit.diff.DiffEntry fun Diff(gitManager: GitManager, diffEntryType: DiffEntryType, onCloseDiffView: () -> Unit) { var text by remember { mutableStateOf(listOf()) } - LaunchedEffect(diffEntryType.diffEntry) { + LaunchedEffect(Unit) { text = gitManager.diffFormat(diffEntryType) diff --git a/src/main/kotlin/app/ui/RepositoryOpen.kt b/src/main/kotlin/app/ui/RepositoryOpen.kt index f8cdd52..3a0b3bc 100644 --- a/src/main/kotlin/app/ui/RepositoryOpen.kt +++ b/src/main/kotlin/app/ui/RepositoryOpen.kt @@ -127,6 +127,7 @@ fun RepositoryOpenPage(gitManager: GitManager) { if (selectedItem == SelectedItem.UncommitedChanges) { UncommitedChanges( gitManager = gitManager, + selectedEntryType = diffSelected, onStagedDiffEntrySelected = { diffEntry -> diffSelected = if (diffEntry != null) DiffEntryType.StagedDiff(diffEntry) diff --git a/src/main/kotlin/app/ui/UncommitedChanges.kt b/src/main/kotlin/app/ui/UncommitedChanges.kt index 1cd24d8..582f669 100644 --- a/src/main/kotlin/app/ui/UncommitedChanges.kt +++ b/src/main/kotlin/app/ui/UncommitedChanges.kt @@ -30,6 +30,7 @@ import androidx.compose.ui.unit.sp import app.extensions.filePath import app.extensions.icon import app.extensions.iconColor +import app.git.DiffEntryType import app.git.GitManager import app.git.StageStatus import app.theme.headerBackground @@ -43,6 +44,7 @@ import org.eclipse.jgit.diff.DiffEntry @Composable fun UncommitedChanges( gitManager: GitManager, + selectedEntryType: DiffEntryType?, onStagedDiffEntrySelected: (DiffEntry?) -> Unit, onUnstagedDiffEntrySelected: (DiffEntry) -> Unit, ) { @@ -55,12 +57,28 @@ fun UncommitedChanges( gitManager.loadStatus() } - val (staged, unstaged) = if (stageStatus is StageStatus.Loaded) { - stageStatus.staged to stageStatus.unstaged + val staged: List + val unstaged: List + if (stageStatus is StageStatus.Loaded) { + staged = stageStatus.staged + unstaged = stageStatus.unstaged + LaunchedEffect(staged) { + if(selectedEntryType != null) { + checkIfSelectedEntryShouldBeUpdated( + selectedEntryType = selectedEntryType, + staged = staged, + unstaged = unstaged, + onStagedDiffEntrySelected = onStagedDiffEntrySelected, + onUnstagedDiffEntrySelected = onUnstagedDiffEntrySelected, + ) + } + } } else { - listOf() to listOf() // return 2 empty lists if still loading + staged = listOf() + unstaged = listOf() // return empty lists if still loading } + var commitMessage by remember { mutableStateOf("") } val doCommit = { gitManager.commit(commitMessage) @@ -170,6 +188,40 @@ fun UncommitedChanges( } } +fun checkIfSelectedEntryShouldBeUpdated( + selectedEntryType: DiffEntryType, + staged: List, + unstaged: List, + onStagedDiffEntrySelected: (DiffEntry?) -> Unit, + onUnstagedDiffEntrySelected: (DiffEntry) -> Unit, +) { + val selectedDiffEntry = selectedEntryType.diffEntry + val selectedEntryTypeNewId = selectedDiffEntry.newId.name() + + if (selectedEntryType is DiffEntryType.StagedDiff) { + val entryType = staged.firstOrNull { it.newPath == selectedDiffEntry.newPath } + + if( + entryType != null && + selectedEntryTypeNewId != entryType.newId.name() + ) { + onStagedDiffEntrySelected(entryType) + } else if (entryType == null) + onStagedDiffEntrySelected(null) + } else if(selectedEntryType is DiffEntryType.UnstagedDiff) { + val entryType = unstaged.firstOrNull { + if(selectedDiffEntry.changeType == DiffEntry.ChangeType.DELETE) + it.oldPath == selectedDiffEntry.oldPath + else + it.newPath == selectedDiffEntry.newPath + } + + if(entryType != null) { + onUnstagedDiffEntrySelected(entryType) + } else onStagedDiffEntrySelected(null) + } +} + @OptIn(ExperimentalAnimationApi::class) @Composable private fun EntriesList(