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

View file

@ -2,15 +2,20 @@ package net.mbess.popequer.ui
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.ui.platform.LocalContext 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.AppContext
import net.mbess.popequer.Native import net.mbess.popequer.Native
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
Native.add(143, 54).let { Native.add(143, 54).let {
@ -20,6 +25,21 @@ class MainActivity : ComponentActivity() {
val appContext = AppContext(this) 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 { setContent {
val darkTheme = isSystemInDarkTheme() val darkTheme = isSystemInDarkTheme()
val supportDynamicTheme = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S val supportDynamicTheme = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S

View file

@ -1,11 +1,18 @@
package net.mbess.popequer.ui package net.mbess.popequer.ui
import android.os.Looper
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box 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.foundation.layout.padding
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
@ -17,7 +24,7 @@ import kotlinx.coroutines.launch
import net.mbess.popequer.AppContext import net.mbess.popequer.AppContext
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
@Composable @Composable
fun NoteContainer( fun NoteContainer(
context: AppContext context: AppContext
@ -27,9 +34,17 @@ fun NoteContainer(
Scaffold( Scaffold(
topBar = { topBar = {
TopAppBar(title = { TopAppBar(
Text(text = "Popequer") title = {
}) Text(text = "Popequer")
},
actions = {
IconButton(onClick = {
// TODO: switch to settings view
}) {
Icon(Icons.Filled.Settings, contentDescription = null)
}
})
}, },
floatingActionButton = { floatingActionButton = {
Box( Box(
@ -54,7 +69,34 @@ fun NoteContainer(
Surface( Surface(
modifier = Modifier.padding(padding) 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")
}
}
} }
} }
} }