From 6142ecef5b5c427b51b9ddfaf194156e9a0538bf Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Wed, 2 Feb 2022 00:23:31 +0100 Subject: [PATCH] Added option to delete file in uncommited changes context menu --- src/main/kotlin/app/ui/UncommitedChanges.kt | 51 +++++++++++-------- .../context_menu/StagedEntriesContextMenu.kt | 22 ++++++++ .../UnstagedEntriesContextMenu.kt | 32 ++++++++++++ .../kotlin/app/viewmodels/StatusViewModel.kt | 11 ++++ 4 files changed, 96 insertions(+), 20 deletions(-) create mode 100644 src/main/kotlin/app/ui/context_menu/StagedEntriesContextMenu.kt create mode 100644 src/main/kotlin/app/ui/context_menu/UnstagedEntriesContextMenu.kt diff --git a/src/main/kotlin/app/ui/UncommitedChanges.kt b/src/main/kotlin/app/ui/UncommitedChanges.kt index 44761c1..974fc33 100644 --- a/src/main/kotlin/app/ui/UncommitedChanges.kt +++ b/src/main/kotlin/app/ui/UncommitedChanges.kt @@ -1,3 +1,5 @@ +@file:OptIn(ExperimentalAnimationApi::class, ExperimentalComposeUiApi::class, ExperimentalFoundationApi::class) + package app.ui import androidx.compose.animation.AnimatedVisibility @@ -31,12 +33,13 @@ import app.git.StatusEntry import app.theme.* import app.ui.components.ScrollableLazyColumn import app.ui.components.SecondaryButton +import app.ui.context_menu.stagedEntriesContextMenuItems +import app.ui.context_menu.unstagedEntriesContextMenuItems import app.viewmodels.StageStatus import app.viewmodels.StatusViewModel import org.eclipse.jgit.diff.DiffEntry import org.eclipse.jgit.lib.RepositoryState -@OptIn(ExperimentalAnimationApi::class, ExperimentalComposeUiApi::class) @Composable fun UncommitedChanges( statusViewModel: StatusViewModel, @@ -51,9 +54,11 @@ fun UncommitedChanges( val stageStatus = stageStatusState.value val staged: List val unstaged: List + if (stageStatus is StageStatus.Loaded) { staged = stageStatus.staged unstaged = stageStatus.unstaged + LaunchedEffect(staged) { if (selectedEntryType != null) { checkIfSelectedEntryShouldBeUpdated( @@ -101,8 +106,13 @@ fun UncommitedChanges( onDiffEntryOptionSelected = { statusViewModel.unstage(it) }, - onReset = { diffEntry -> - statusViewModel.resetStaged(diffEntry) + onGenerateContextMenu = { diffEntry -> + stagedEntriesContextMenuItems( + diffEntry = diffEntry, + onReset = { + statusViewModel.resetUnstaged(diffEntry) + }, + ) }, onAllAction = { statusViewModel.unstageAll() @@ -122,13 +132,21 @@ fun UncommitedChanges( onDiffEntryOptionSelected = { statusViewModel.stage(it) }, - onReset = { diffEntry -> - statusViewModel.resetUnstaged(diffEntry) + onGenerateContextMenu = { diffEntry -> + unstagedEntriesContextMenuItems( + diffEntry = diffEntry, + onReset = { + statusViewModel.resetUnstaged(diffEntry) + }, + onDelete = { + statusViewModel.deleteFile(diffEntry) + } + ) }, - { + allActionTitle = "Stage all", + onAllAction = { statusViewModel.stageAll() - }, - allActionTitle = "Stage all" + } ) Column( @@ -326,7 +344,7 @@ fun checkIfSelectedEntryShouldBeUpdated( } } -@OptIn(ExperimentalAnimationApi::class) +@OptIn(ExperimentalAnimationApi::class, ExperimentalFoundationApi::class) @Composable private fun EntriesList( modifier: Modifier, @@ -336,7 +354,7 @@ private fun EntriesList( diffEntries: List, onDiffEntrySelected: (DiffEntry) -> Unit, onDiffEntryOptionSelected: (DiffEntry) -> Unit, - onReset: (DiffEntry) -> Unit, + onGenerateContextMenu: (DiffEntry) -> List, onAllAction: () -> Unit, allActionTitle: String, ) { @@ -382,9 +400,7 @@ private fun EntriesList( onButtonClick = { onDiffEntryOptionSelected(diffEntry) }, - onReset = { - onReset(diffEntry) - } + onGenerateContextMenu = onGenerateContextMenu, ) if (index < diffEntries.size - 1) { @@ -406,7 +422,7 @@ private fun FileEntry( actionColor: Color, onClick: () -> Unit, onButtonClick: () -> Unit, - onReset: () -> Unit, + onGenerateContextMenu: (DiffEntry) -> List, ) { var active by remember { mutableStateOf(false) } val diffEntry = statusEntry.diffEntry @@ -428,12 +444,7 @@ private fun FileEntry( ) { ContextMenuArea( items = { - listOf( - ContextMenuItem( - label = "Reset", - onClick = onReset - ) - ) + onGenerateContextMenu(diffEntry) }, ) { Row( diff --git a/src/main/kotlin/app/ui/context_menu/StagedEntriesContextMenu.kt b/src/main/kotlin/app/ui/context_menu/StagedEntriesContextMenu.kt new file mode 100644 index 0000000..899fd50 --- /dev/null +++ b/src/main/kotlin/app/ui/context_menu/StagedEntriesContextMenu.kt @@ -0,0 +1,22 @@ +package app.ui.context_menu + +import androidx.compose.foundation.ContextMenuItem +import androidx.compose.foundation.ExperimentalFoundationApi +import org.eclipse.jgit.diff.DiffEntry + +@OptIn(ExperimentalFoundationApi::class) +fun stagedEntriesContextMenuItems( + diffEntry: DiffEntry, + onReset: () -> Unit, +): List { + return mutableListOf().apply { + if (diffEntry.changeType != DiffEntry.ChangeType.ADD) { + add( + ContextMenuItem( + label = "Reset", + onClick = onReset, + ) + ) + } + } +} diff --git a/src/main/kotlin/app/ui/context_menu/UnstagedEntriesContextMenu.kt b/src/main/kotlin/app/ui/context_menu/UnstagedEntriesContextMenu.kt new file mode 100644 index 0000000..d49ed07 --- /dev/null +++ b/src/main/kotlin/app/ui/context_menu/UnstagedEntriesContextMenu.kt @@ -0,0 +1,32 @@ +package app.ui.context_menu + +import androidx.compose.foundation.ContextMenuItem +import androidx.compose.foundation.ExperimentalFoundationApi +import org.eclipse.jgit.diff.DiffEntry + +@OptIn(ExperimentalFoundationApi::class) +fun unstagedEntriesContextMenuItems( + diffEntry: DiffEntry, + onReset: () -> Unit, + onDelete: () -> Unit, +): List { + return mutableListOf().apply { + if (diffEntry.changeType != DiffEntry.ChangeType.ADD) { + add( + ContextMenuItem( + label = "Reset", + onClick = onReset, + ) + ) + } + + if (diffEntry.changeType != DiffEntry.ChangeType.DELETE) { + add( + ContextMenuItem( + label = "Delete file", + onClick = onDelete, + ) + ) + } + } +} diff --git a/src/main/kotlin/app/viewmodels/StatusViewModel.kt b/src/main/kotlin/app/viewmodels/StatusViewModel.kt index 1879ec9..07e6d3d 100644 --- a/src/main/kotlin/app/viewmodels/StatusViewModel.kt +++ b/src/main/kotlin/app/viewmodels/StatusViewModel.kt @@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.withContext import org.eclipse.jgit.api.Git import org.eclipse.jgit.diff.DiffEntry +import java.io.File import javax.inject.Inject class StatusViewModel @Inject constructor( @@ -138,6 +139,16 @@ class StatusViewModel @Inject constructor( return@safeProcessing RefreshType.ALL_DATA } + + fun deleteFile(diffEntry: DiffEntry) = tabState.runOperation { + val path = diffEntry.newPath + + val fileToDelete = File(path) + + fileToDelete.delete() + + return@runOperation RefreshType.UNCOMMITED_CHANGES + } } sealed class StageStatus {