Added option to delete file in uncommited changes context menu

This commit is contained in:
Abdelilah El Aissaoui 2022-02-02 00:23:31 +01:00
parent 2efaf654c7
commit 6142ecef5b
4 changed files with 96 additions and 20 deletions

View File

@ -1,3 +1,5 @@
@file:OptIn(ExperimentalAnimationApi::class, ExperimentalComposeUiApi::class, ExperimentalFoundationApi::class)
package app.ui package app.ui
import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.AnimatedVisibility
@ -31,12 +33,13 @@ import app.git.StatusEntry
import app.theme.* import app.theme.*
import app.ui.components.ScrollableLazyColumn import app.ui.components.ScrollableLazyColumn
import app.ui.components.SecondaryButton import app.ui.components.SecondaryButton
import app.ui.context_menu.stagedEntriesContextMenuItems
import app.ui.context_menu.unstagedEntriesContextMenuItems
import app.viewmodels.StageStatus import app.viewmodels.StageStatus
import app.viewmodels.StatusViewModel import app.viewmodels.StatusViewModel
import org.eclipse.jgit.diff.DiffEntry import org.eclipse.jgit.diff.DiffEntry
import org.eclipse.jgit.lib.RepositoryState import org.eclipse.jgit.lib.RepositoryState
@OptIn(ExperimentalAnimationApi::class, ExperimentalComposeUiApi::class)
@Composable @Composable
fun UncommitedChanges( fun UncommitedChanges(
statusViewModel: StatusViewModel, statusViewModel: StatusViewModel,
@ -51,9 +54,11 @@ fun UncommitedChanges(
val stageStatus = stageStatusState.value val stageStatus = stageStatusState.value
val staged: List<StatusEntry> val staged: List<StatusEntry>
val unstaged: List<StatusEntry> val unstaged: List<StatusEntry>
if (stageStatus is StageStatus.Loaded) { if (stageStatus is StageStatus.Loaded) {
staged = stageStatus.staged staged = stageStatus.staged
unstaged = stageStatus.unstaged unstaged = stageStatus.unstaged
LaunchedEffect(staged) { LaunchedEffect(staged) {
if (selectedEntryType != null) { if (selectedEntryType != null) {
checkIfSelectedEntryShouldBeUpdated( checkIfSelectedEntryShouldBeUpdated(
@ -101,8 +106,13 @@ fun UncommitedChanges(
onDiffEntryOptionSelected = { onDiffEntryOptionSelected = {
statusViewModel.unstage(it) statusViewModel.unstage(it)
}, },
onReset = { diffEntry -> onGenerateContextMenu = { diffEntry ->
statusViewModel.resetStaged(diffEntry) stagedEntriesContextMenuItems(
diffEntry = diffEntry,
onReset = {
statusViewModel.resetUnstaged(diffEntry)
},
)
}, },
onAllAction = { onAllAction = {
statusViewModel.unstageAll() statusViewModel.unstageAll()
@ -122,13 +132,21 @@ fun UncommitedChanges(
onDiffEntryOptionSelected = { onDiffEntryOptionSelected = {
statusViewModel.stage(it) statusViewModel.stage(it)
}, },
onReset = { diffEntry -> onGenerateContextMenu = { diffEntry ->
unstagedEntriesContextMenuItems(
diffEntry = diffEntry,
onReset = {
statusViewModel.resetUnstaged(diffEntry) statusViewModel.resetUnstaged(diffEntry)
}, },
{ onDelete = {
statusViewModel.stageAll() statusViewModel.deleteFile(diffEntry)
}
)
}, },
allActionTitle = "Stage all" allActionTitle = "Stage all",
onAllAction = {
statusViewModel.stageAll()
}
) )
Column( Column(
@ -326,7 +344,7 @@ fun checkIfSelectedEntryShouldBeUpdated(
} }
} }
@OptIn(ExperimentalAnimationApi::class) @OptIn(ExperimentalAnimationApi::class, ExperimentalFoundationApi::class)
@Composable @Composable
private fun EntriesList( private fun EntriesList(
modifier: Modifier, modifier: Modifier,
@ -336,7 +354,7 @@ private fun EntriesList(
diffEntries: List<StatusEntry>, diffEntries: List<StatusEntry>,
onDiffEntrySelected: (DiffEntry) -> Unit, onDiffEntrySelected: (DiffEntry) -> Unit,
onDiffEntryOptionSelected: (DiffEntry) -> Unit, onDiffEntryOptionSelected: (DiffEntry) -> Unit,
onReset: (DiffEntry) -> Unit, onGenerateContextMenu: (DiffEntry) -> List<ContextMenuItem>,
onAllAction: () -> Unit, onAllAction: () -> Unit,
allActionTitle: String, allActionTitle: String,
) { ) {
@ -382,9 +400,7 @@ private fun EntriesList(
onButtonClick = { onButtonClick = {
onDiffEntryOptionSelected(diffEntry) onDiffEntryOptionSelected(diffEntry)
}, },
onReset = { onGenerateContextMenu = onGenerateContextMenu,
onReset(diffEntry)
}
) )
if (index < diffEntries.size - 1) { if (index < diffEntries.size - 1) {
@ -406,7 +422,7 @@ private fun FileEntry(
actionColor: Color, actionColor: Color,
onClick: () -> Unit, onClick: () -> Unit,
onButtonClick: () -> Unit, onButtonClick: () -> Unit,
onReset: () -> Unit, onGenerateContextMenu: (DiffEntry) -> List<ContextMenuItem>,
) { ) {
var active by remember { mutableStateOf(false) } var active by remember { mutableStateOf(false) }
val diffEntry = statusEntry.diffEntry val diffEntry = statusEntry.diffEntry
@ -428,12 +444,7 @@ private fun FileEntry(
) { ) {
ContextMenuArea( ContextMenuArea(
items = { items = {
listOf( onGenerateContextMenu(diffEntry)
ContextMenuItem(
label = "Reset",
onClick = onReset
)
)
}, },
) { ) {
Row( Row(

View File

@ -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<ContextMenuItem> {
return mutableListOf<ContextMenuItem>().apply {
if (diffEntry.changeType != DiffEntry.ChangeType.ADD) {
add(
ContextMenuItem(
label = "Reset",
onClick = onReset,
)
)
}
}
}

View File

@ -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<ContextMenuItem> {
return mutableListOf<ContextMenuItem>().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,
)
)
}
}
}

View File

@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.Git
import org.eclipse.jgit.diff.DiffEntry import org.eclipse.jgit.diff.DiffEntry
import java.io.File
import javax.inject.Inject import javax.inject.Inject
class StatusViewModel @Inject constructor( class StatusViewModel @Inject constructor(
@ -138,6 +139,16 @@ class StatusViewModel @Inject constructor(
return@safeProcessing RefreshType.ALL_DATA 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 { sealed class StageStatus {