From ee745bd4e0776038b81eb20be30b7e4d22b5a3db Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Sun, 10 Oct 2021 02:59:26 +0200 Subject: [PATCH] Added un/stage all buttons --- src/main/kotlin/app/git/GitManager.kt | 10 ++++ src/main/kotlin/app/git/StatusManager.kt | 21 ++++++++- src/main/kotlin/app/ui/UncommitedChanges.kt | 51 +++++++++++++++------ 3 files changed, 67 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/app/git/GitManager.kt b/src/main/kotlin/app/git/GitManager.kt index bce781f..2e6e19c 100644 --- a/src/main/kotlin/app/git/GitManager.kt +++ b/src/main/kotlin/app/git/GitManager.kt @@ -86,6 +86,8 @@ class GitManager @Inject constructor( } fun openRepository(directory: File) { + println("Trying to open repository ${directory.absoluteFile}") + val gitDirectory = if (directory.name == ".git") { directory } else { @@ -199,6 +201,14 @@ class GitManager @Inject constructor( suspend fun diffListFromCommit(commit: RevCommit): List { return diffManager.commitDiffEntries(safeGit, commit) } + + fun unstageAll() = managerScope.launch { + statusManager.unstageAll(safeGit) + } + + fun stageAll() = managerScope.launch { + statusManager.stageAll(safeGit) + } } diff --git a/src/main/kotlin/app/git/StatusManager.kt b/src/main/kotlin/app/git/StatusManager.kt index f253aa4..5856c1e 100644 --- a/src/main/kotlin/app/git/StatusManager.kt +++ b/src/main/kotlin/app/git/StatusManager.kt @@ -53,7 +53,7 @@ class StatusManager @Inject constructor() { } suspend fun stage(git: Git, diffEntry: DiffEntry) = withContext(Dispatchers.IO) { - if(diffEntry.changeType == DiffEntry.ChangeType.DELETE) { + if (diffEntry.changeType == DiffEntry.ChangeType.DELETE) { git.rm() .addFilepattern(diffEntry.filePath) .call() @@ -83,7 +83,7 @@ class StatusManager @Inject constructor() { } suspend fun reset(git: Git, diffEntry: DiffEntry, staged: Boolean) = withContext(Dispatchers.IO) { - if(staged) { + if (staged) { git .reset() .addPath(diffEntry.filePath) @@ -97,6 +97,23 @@ class StatusManager @Inject constructor() { loadStatus(git) } + + suspend fun unstageAll(git: Git) = withContext(Dispatchers.IO) { + git + .reset() + .call() + + loadStatus(git) + } + + suspend fun stageAll(git: Git) = withContext(Dispatchers.IO) { + git + .add() + .addFilepattern(".") + .call() + + loadStatus(git) + } } sealed class StageStatus { diff --git a/src/main/kotlin/app/ui/UncommitedChanges.kt b/src/main/kotlin/app/ui/UncommitedChanges.kt index 6e97296..5b08c4e 100644 --- a/src/main/kotlin/app/ui/UncommitedChanges.kt +++ b/src/main/kotlin/app/ui/UncommitedChanges.kt @@ -72,6 +72,7 @@ fun UncommitedChanges( .weight(5f) .fillMaxWidth(), title = "Staged", + allActionTitle = "Unstage all", actionTitle = "Unstage", actionColor = MaterialTheme.colors.error, diffEntries = staged, @@ -81,6 +82,9 @@ fun UncommitedChanges( }, onReset = { diffEntry -> gitManager.resetStaged(diffEntry) + }, + onAllAction = { + gitManager.unstageAll() } ) @@ -99,7 +103,11 @@ fun UncommitedChanges( }, onReset = { diffEntry -> gitManager.resetUnstaged(diffEntry) - } + }, + { + gitManager.stageAll() + }, + allActionTitle = "Stage all" ) Card( @@ -143,6 +151,7 @@ fun UncommitedChanges( } } +@OptIn(ExperimentalAnimationApi::class) @Composable private fun EntriesList( modifier: Modifier, @@ -153,24 +162,40 @@ private fun EntriesList( onDiffEntrySelected: (DiffEntry) -> Unit, onDiffEntryOptionSelected: (DiffEntry) -> Unit, onReset: (DiffEntry) -> Unit, + onAllAction: () -> Unit, + allActionTitle: String, ) { Card( modifier = modifier ) { Column { - Text( - modifier = Modifier - .background(color = MaterialTheme.colors.headerBackground) - .padding(vertical = 8.dp) - .fillMaxWidth(), - text = title, - fontWeight = FontWeight.Bold, - textAlign = TextAlign.Center, - color = MaterialTheme.colors.primary, - fontSize = 14.sp, - maxLines = 1, - ) + Box { + Text( + modifier = Modifier + .background(color = MaterialTheme.colors.headerBackground) + .padding(vertical = 8.dp) + .fillMaxWidth(), + text = title, + fontWeight = FontWeight.Bold, + textAlign = TextAlign.Center, + color = MaterialTheme.colors.primary, + fontSize = 14.sp, + maxLines = 1, + ) + + Button( + onClick = onAllAction, + modifier = Modifier + .padding(horizontal = 16.dp) + .align(Alignment.CenterEnd), + elevation = ButtonDefaults.elevation(0.dp, 0.dp, 0.dp), + colors = ButtonDefaults.buttonColors(backgroundColor = actionColor) + ) { + Text(allActionTitle, fontSize = 10.sp) + } + + } ScrollableLazyColumn( modifier = Modifier.fillMaxSize(),