From 542253c518e1acccd535b16ed6add52bf20500b0 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Mon, 29 Jan 2024 12:02:06 +0100 Subject: [PATCH] feat: sync git button --- .../java/net/mbess/popequer/GitActions.kt | 30 ++++++++--- .../net/mbess/popequer/ui/MainActivity.kt | 22 +++++++- .../main/java/net/mbess/popequer/ui/Notes.kt | 52 +++++++++++++++++-- 3 files changed, 90 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/net/mbess/popequer/GitActions.kt b/app/src/main/java/net/mbess/popequer/GitActions.kt index f0b5770..26eb491 100644 --- a/app/src/main/java/net/mbess/popequer/GitActions.kt +++ b/app/src/main/java/net/mbess/popequer/GitActions.kt @@ -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 ! ") diff --git a/app/src/main/java/net/mbess/popequer/ui/MainActivity.kt b/app/src/main/java/net/mbess/popequer/ui/MainActivity.kt index 73952c7..295afc8 100644 --- a/app/src/main/java/net/mbess/popequer/ui/MainActivity.kt +++ b/app/src/main/java/net/mbess/popequer/ui/MainActivity.kt @@ -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 diff --git a/app/src/main/java/net/mbess/popequer/ui/Notes.kt b/app/src/main/java/net/mbess/popequer/ui/Notes.kt index 6c7a340..d85ac66 100644 --- a/app/src/main/java/net/mbess/popequer/ui/Notes.kt +++ b/app/src/main/java/net/mbess/popequer/ui/Notes.kt @@ -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") + } + } } } }