feat: sync git button

This commit is contained in:
Matthieu Bessat 2024-01-29 12:02:06 +01:00
parent a29ebe422f
commit 542253c518
3 changed files with 90 additions and 14 deletions

View file

@ -2,6 +2,7 @@ package net.mbess.popequer
import android.content.Context
import android.util.Log
import android.widget.Toast
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.ResetCommand
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
@ -18,22 +19,35 @@ class GitActions(
)
private lateinit var git: Git
fun cloneOrInit() {
git = runCatching {
Git.open(cloneFolder)
/**
* Will return true if the repo was cloned
*/
fun prepareGit(): Boolean {
return runCatching {
Log.i("GitActions", "Opening existing clone")
git = Git.open(cloneFolder) ?: throw IllegalStateException("Failed to open git repository")
false
}.getOrElse {
Log.i("GitActions", "Will clone repo")
cloneFolder.deleteRecursively()
cloneFolder.mkdirs()
Git.cloneRepository()
git = Git.cloneRepository()
.setURI("https://forge.lefuturiste.fr/mbess/popequer-sandbox-notebook-2.git")
.setCredentialsProvider(credentials)
.setDirectory(cloneFolder)
.call()
} ?: throw IllegalStateException("Failed to open git repository")
.call()?: throw IllegalStateException("Failed to clone git repository")
true
}
}
fun sync() {
git.fetch().setCredentialsProvider(credentials).call()
git.rebase().setUpstream("origin/master").call().also { result ->
Log.i("GitActions", "Rebase status: ${result.status} ${result.conflicts}")
}
Log.i("GitActions", "Successfully sync repo")
}
fun trigger() {
cloneOrInit()
val hash = System.currentTimeMillis().hashCode().absoluteValue.toString(16)
val myFile = cloneFolder.resolve("my_file_$hash.txt").also {
it.writeText("$hash ! ")

View file

@ -2,15 +2,20 @@ package net.mbess.popequer.ui
import android.os.Build
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.*
import androidx.compose.ui.platform.LocalContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.mbess.popequer.AppContext
import net.mbess.popequer.Native
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Native.add(143, 54).let {
@ -20,6 +25,21 @@ class MainActivity : ComponentActivity() {
val appContext = AppContext(this)
CoroutineScope(Dispatchers.IO).launch {
Looper.prepare()
runCatching {
Log.i("MainActivity", "Running prepare git")
val isRepoNew = appContext.gitActions.prepareGit()
if (isRepoNew) {
Log.i("MainActivity", "A repo was cloned")
Toast.makeText(appContext.androidContext, "A repo was just cloned", Toast.LENGTH_LONG)
.show()
}
}.onFailure {
Log.e("MainActivity", "Failed to prepare git repo", it)
}
}
setContent {
val darkTheme = isSystemInDarkTheme()
val supportDynamicTheme = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S

View file

@ -1,11 +1,18 @@
package net.mbess.popequer.ui
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
@ -17,7 +24,7 @@ import kotlinx.coroutines.launch
import net.mbess.popequer.AppContext
@OptIn(ExperimentalMaterial3Api::class)
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
@Composable
fun NoteContainer(
context: AppContext
@ -27,9 +34,17 @@ fun NoteContainer(
Scaffold(
topBar = {
TopAppBar(title = {
Text(text = "Popequer")
})
TopAppBar(
title = {
Text(text = "Popequer")
},
actions = {
IconButton(onClick = {
// TODO: switch to settings view
}) {
Icon(Icons.Filled.Settings, contentDescription = null)
}
})
},
floatingActionButton = {
Box(
@ -54,7 +69,34 @@ fun NoteContainer(
Surface(
modifier = Modifier.padding(padding)
){
Editor()
//Editor()
FlowRow (
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceAround
) {
Button(
onClick = {
Toast.makeText(context.androidContext, "Will pull repo changes", Toast.LENGTH_SHORT ).show()
scope.launch(Dispatchers.IO) {
runCatching {
context.gitActions.sync()
Toast.makeText(context.androidContext, "Repo changes are sync", Toast.LENGTH_LONG ).show()
}.onFailure {
Log.e("MainActivity", "Failed to trigger git actions", it)
}
}
}
) {
Text("Pull")
}
Button(
onClick = {
Toast.makeText(context.androidContext, "Will pull repo changes", Toast.LENGTH_LONG ).show()
}
) {
Text("New quick note")
}
}
}
}
}