Added reset file button

This commit is contained in:
Abdelilah El Aissaoui 2021-09-30 01:21:46 +02:00 committed by Duba
parent c24908cc5a
commit ccd345d5ea
3 changed files with 102 additions and 39 deletions

View File

@ -137,7 +137,7 @@ class GitManager {
val oldTree = DirCacheIterator(repo.readDirCache()) val oldTree = DirCacheIterator(repo.readDirCache())
val newTree = FileTreeIterator(repo) val newTree = FileTreeIterator(repo)
if(diffEntryType is DiffEntryType.UnstagedDiff) if (diffEntryType is DiffEntryType.UnstagedDiff)
formatter.scan(oldTree, newTree) formatter.scan(oldTree, newTree)
formatter.format(diffEntry) formatter.format(diffEntry)
@ -179,6 +179,14 @@ class GitManager {
fun deleteBranch(branch: Ref) = managerScope.launch { fun deleteBranch(branch: Ref) = managerScope.launch {
branchesManager.deleteBranch(safeGit, branch) branchesManager.deleteBranch(safeGit, branch)
} }
fun resetStaged(diffEntry: DiffEntry) = managerScope.launch {
statusManager.reset(safeGit, diffEntry, staged = true)
}
fun resetUnstaged(diffEntry: DiffEntry) = managerScope.launch {
statusManager.reset(safeGit, diffEntry, staged = false)
}
} }

View File

@ -1,10 +1,10 @@
@file:Suppress("UNUSED_PARAMETER")
import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background import androidx.compose.foundation.*
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.lazy.itemsIndexed
@ -15,10 +15,12 @@ import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Close import androidx.compose.material.icons.filled.Close
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.ContextMenuItem
import androidx.compose.ui.platform.ContextMenuState
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
@ -68,6 +70,9 @@ fun UncommitedChanges(
onDiffEntrySelected = onStagedDiffEntrySelected, onDiffEntrySelected = onStagedDiffEntrySelected,
onDiffEntryOptionSelected = { onDiffEntryOptionSelected = {
gitManager.unstage(it) gitManager.unstage(it)
},
onReset = { diffEntry ->
gitManager.resetStaged(diffEntry)
} }
) )
@ -82,6 +87,9 @@ fun UncommitedChanges(
onDiffEntrySelected = onUnstagedDiffEntrySelected, onDiffEntrySelected = onUnstagedDiffEntrySelected,
onDiffEntryOptionSelected = { onDiffEntryOptionSelected = {
gitManager.stage(it) gitManager.stage(it)
},
onReset = { diffEntry ->
gitManager.resetUnstaged(diffEntry)
} }
) )
@ -130,6 +138,7 @@ private fun EntriesList(
diffEntries: List<DiffEntry>, diffEntries: List<DiffEntry>,
onDiffEntrySelected: (DiffEntry) -> Unit, onDiffEntrySelected: (DiffEntry) -> Unit,
onDiffEntryOptionSelected: (DiffEntry) -> Unit, onDiffEntryOptionSelected: (DiffEntry) -> Unit,
onReset: (DiffEntry) -> Unit,
) { ) {
Card( Card(
modifier = modifier modifier = modifier
@ -157,6 +166,9 @@ private fun EntriesList(
}, },
onButtonClick = { onButtonClick = {
onDiffEntryOptionSelected(diffEntry) onDiffEntryOptionSelected(diffEntry)
},
onReset = {
onReset(diffEntry)
} }
) )
@ -169,44 +181,69 @@ private fun EntriesList(
} }
} }
@OptIn(
ExperimentalComposeUiApi::class, ExperimentalFoundationApi::class,
ExperimentalDesktopApi::class
)
@Composable @Composable
private fun FileEntry(diffEntry: DiffEntry, icon: ImageVector, onClick: () -> Unit, onButtonClick: () -> Unit) { private fun FileEntry(
Row( diffEntry: DiffEntry,
icon: ImageVector,
onClick: () -> Unit,
onButtonClick: () -> Unit,
onReset: () -> Unit,
) {
Box(
modifier = Modifier modifier = Modifier
.height(56.dp) .clickable { onClick() }
.fillMaxWidth()
.clickable(onClick = onClick),
verticalAlignment = Alignment.CenterVertically,
) { ) {
ContextMenuArea(
Icon( items = {
imageVector = diffEntry.icon, listOf(
contentDescription = null, ContextMenuItem(
modifier = Modifier label = "Reset",
.padding(horizontal = 16.dp) onClick = onReset
.size(24.dp), )
tint = MaterialTheme.colors.primary, )
) },
Text(
text = diffEntry.filePath,
modifier = Modifier.weight(1f, fill = true),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
IconButton(
onClick = onButtonClick,
modifier = Modifier
.padding(horizontal = 16.dp)
.size(32.dp)
.border(1.dp, MaterialTheme.colors.primary, RoundedCornerShape(10.dp))
) { ) {
Icon( Row(
imageVector = icon, modifier = Modifier
contentDescription = null, .height(56.dp)
tint = MaterialTheme.colors.primary, .fillMaxWidth(),
) verticalAlignment = Alignment.CenterVertically,
) {
Icon(
imageVector = diffEntry.icon,
contentDescription = null,
modifier = Modifier
.padding(horizontal = 16.dp)
.size(24.dp),
tint = MaterialTheme.colors.primary,
)
Text(
text = diffEntry.filePath,
modifier = Modifier.weight(1f, fill = true),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
IconButton(
onClick = onButtonClick,
modifier = Modifier
.padding(horizontal = 16.dp)
.size(32.dp)
.border(1.dp, MaterialTheme.colors.primary, RoundedCornerShape(10.dp))
) {
Icon(
imageVector = icon,
contentDescription = null,
tint = MaterialTheme.colors.primary,
)
}
}
} }
} }
} }

View File

@ -74,6 +74,24 @@ class StatusManager {
loadStatus(git) loadStatus(git)
} }
suspend fun reset(git: Git, diffEntry: DiffEntry, staged: Boolean) = withContext(Dispatchers.IO) {
println("Staged $staged")
if(staged) {
git
.reset()
.addPath(diffEntry.filePath)
.call()
}
git
.checkout()
.addPath(diffEntry.filePath)
.call()
loadStatus(git)
}
} }
sealed class StageStatus { sealed class StageStatus {