Improved diff view and moved to a separate file

This commit is contained in:
Abdelilah El Aissaoui 2021-09-25 03:04:41 +02:00
parent 1e3b6d02b7
commit 4d24b2d5bf
2 changed files with 72 additions and 53 deletions

71
src/main/kotlin/Diff.kt Normal file
View File

@ -0,0 +1,71 @@
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import org.eclipse.jgit.diff.DiffEntry
import theme.primaryTextColor
@Composable
fun Diff(gitManager: GitManager, diffEntry: DiffEntry, onCloseDiffView: () -> Unit) {
val text = remember(diffEntry) {
gitManager.diffFormat(diffEntry)
}
Card(
modifier = Modifier
.padding(8.dp)
.background(MaterialTheme.colors.surface)
.fillMaxSize()
) {
Column {
OutlinedButton(
modifier = Modifier
.padding(vertical = 16.dp, horizontal = 16.dp)
.align(Alignment.End),
onClick = onCloseDiffView,
) {
Text("Close diff")
}
val textLines = text.split("\n", "\r\n")
LazyColumn(modifier = Modifier
.fillMaxSize()
.padding(16.dp)) {
items(textLines) { line ->
val backgroundColor = if (line.startsWith("+")) {
Color(0x77a9d49b)
} else if (line.startsWith("-")) {
Color(0x77dea2a2)
} else {
MaterialTheme.colors.surface
}
SelectionContainer {
Text(
text = line,
modifier = Modifier
.background(backgroundColor)
.fillMaxWidth(),
color = MaterialTheme.colors.primaryTextColor,
maxLines = 1,
fontFamily = FontFamily.Monospace,
)
}
}
}
}
}
}

View File

@ -1,17 +1,7 @@
import androidx.compose.animation.Crossfade import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import extensions.filePath import extensions.filePath
import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.Git
import org.eclipse.jgit.diff.DiffEntry import org.eclipse.jgit.diff.DiffEntry
@ -21,7 +11,6 @@ import org.eclipse.jgit.revwalk.RevTree
import org.eclipse.jgit.revwalk.RevWalk import org.eclipse.jgit.revwalk.RevWalk
import org.eclipse.jgit.treewalk.AbstractTreeIterator import org.eclipse.jgit.treewalk.AbstractTreeIterator
import org.eclipse.jgit.treewalk.CanonicalTreeParser import org.eclipse.jgit.treewalk.CanonicalTreeParser
import theme.primaryTextColor
import java.io.IOException import java.io.IOException
@Composable @Composable
@ -85,7 +74,7 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) {
) )
} }
else -> { else -> {
DiffView( Diff(
gitManager = gitManager, gitManager = gitManager,
diffEntry = diffEntry, diffEntry = diffEntry,
onCloseDiffView = { diffSelected = null }) onCloseDiffView = { diffSelected = null })
@ -122,47 +111,6 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) {
} }
@Composable
fun DiffView(gitManager: GitManager, diffEntry: DiffEntry, onCloseDiffView: () -> Unit) {
val text = remember(diffEntry) {
gitManager.diffFormat(diffEntry)
}
Column {
Button(
modifier = Modifier
.padding(vertical = 16.dp, horizontal = 16.dp)
.align(Alignment.End),
onClick = onCloseDiffView,
) {
Text("Close")
}
val textLines = text.split("\n", "\r\n")
LazyColumn(modifier = Modifier.fillMaxSize()) {
items(textLines) { line ->
val color = if (line.startsWith("+")) {
Color(0xFF094f00)
} else if (line.startsWith("-")) {
Color(0xFF4f0000)
} else {
MaterialTheme.colors.primaryTextColor
}
SelectionContainer {
Text(
text = line,
color = color,
maxLines = 1,
fontFamily = FontFamily.Monospace,
)
}
}
}
}
}
@Throws(IOException::class) @Throws(IOException::class)
fun prepareTreeParser(repository: Repository, commit: RevCommit): AbstractTreeIterator? { fun prepareTreeParser(repository: Repository, commit: RevCommit): AbstractTreeIterator? {
// from the commit we can build the tree which allows us to construct the TreeParser // from the commit we can build the tree which allows us to construct the TreeParser