Added option to stage/unstage file from diff header

This commit is contained in:
Abdelilah El Aissaoui 2022-05-16 20:16:12 +02:00
parent a24ae5fdad
commit 364fa53558
2 changed files with 52 additions and 3 deletions

View file

@ -11,7 +11,6 @@ import androidx.compose.material.LinearProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
@ -26,6 +25,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import app.git.DiffEntryType
import app.git.EntryContent
import app.git.StatusEntry
import app.git.StatusType
import app.git.diff.DiffResult
import app.git.diff.Hunk
@ -65,7 +65,13 @@ fun Diff(
val diffEntry = viewDiffResult.diffResult.diffEntry
val diffResult = viewDiffResult.diffResult
DiffHeader(diffEntry, onCloseDiffView)
DiffHeader(
diffEntryType = diffEntryType,
diffEntry = diffEntry,
onCloseDiffView = onCloseDiffView,
stageFile = { diffViewModel.stageFile(it) },
unstageFile = { diffViewModel.unstageFile(it) },
)
if (diffResult is DiffResult.Text) {
TextDiff(diffEntryType, diffViewModel, diffResult)
@ -266,7 +272,13 @@ fun HunkHeader(
}
@Composable
fun DiffHeader(diffEntry: DiffEntry, onCloseDiffView: () -> Unit) {
fun DiffHeader(
diffEntryType: DiffEntryType,
diffEntry: DiffEntry,
onCloseDiffView: () -> Unit,
stageFile: (StatusEntry) -> Unit,
unstageFile: (StatusEntry) -> Unit,
) {
Row(
modifier = Modifier
.fillMaxWidth()
@ -287,6 +299,31 @@ fun DiffHeader(diffEntry: DiffEntry, onCloseDiffView: () -> Unit) {
Spacer(modifier = Modifier.weight(1f))
if(diffEntryType is DiffEntryType.UncommitedDiff) {
val buttonText: String
val color: Color
if (diffEntryType is DiffEntryType.StagedDiff) {
buttonText = "Unstage file"
color = MaterialTheme.colors.unstageButton
} else {
buttonText = "Stage file"
color = MaterialTheme.colors.stageButton
}
SecondaryButton(
text = buttonText,
backgroundButton = color,
onClick = {
if (diffEntryType is DiffEntryType.StagedDiff) {
unstageFile(diffEntryType.statusEntry)
} else {
stageFile(diffEntryType.statusEntry)
}
}
)
}
IconButton(
onClick = onCloseDiffView
) {

View file

@ -72,6 +72,18 @@ class DiffViewModel @Inject constructor(
) { git ->
statusManager.unstageHunk(git, diffEntry, hunk)
}
fun stageFile(statusEntry: StatusEntry) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
) { git ->
statusManager.stage(git, statusEntry)
}
fun unstageFile(statusEntry: StatusEntry) = tabState.runOperation(
refreshType = RefreshType.UNCOMMITED_CHANGES,
) { git ->
statusManager.unstage(git, statusEntry)
}
}