From ea3a307f96b350f19d221917ed0749758837ba2f Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Mon, 5 Feb 2024 15:28:45 +0100 Subject: [PATCH] feat(rustadapter): index notebook --- app/build.gradle.kts | 1 + .../java/net/mbess/popequer/AppContext.kt | 3 +- .../java/net/mbess/popequer/GitActions.kt | 2 +- .../net/mbess/popequer/PopequerAdapter.java | 47 +++++++++++++ .../mbess/popequer/PopequerAdapterErr.java | 23 +++++++ .../net/mbess/popequer/PopequerProvider.kt | 10 --- .../main/java/net/mbess/popequer/ui/App.kt | 2 +- .../net/mbess/popequer/ui/MainActivity.kt | 12 ++-- rust-adapter/Cargo.toml | 2 + rust-adapter/build.rs | 4 +- rust-adapter/glue.rs | 15 +++++ rust-adapter/src/lib.rs | 67 +++++++++++++++++++ 12 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/net/mbess/popequer/PopequerAdapter.java create mode 100644 app/src/main/java/net/mbess/popequer/PopequerAdapterErr.java delete mode 100644 app/src/main/java/net/mbess/popequer/PopequerProvider.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index ecf9f76..104b27e 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -63,5 +63,6 @@ dependencies { debugImplementation("androidx.compose.ui:ui-tooling") implementation("androidx.compose.ui:ui-tooling-preview") + implementation("androidx.annotation:annotation") } diff --git a/app/src/main/java/net/mbess/popequer/AppContext.kt b/app/src/main/java/net/mbess/popequer/AppContext.kt index d9f1c26..975638a 100644 --- a/app/src/main/java/net/mbess/popequer/AppContext.kt +++ b/app/src/main/java/net/mbess/popequer/AppContext.kt @@ -3,7 +3,8 @@ package net.mbess.popequer import android.content.Context class AppContext( - val androidContext: Context + val androidContext: Context, ) { val gitActions = GitActions(androidContext) + val popequer = PopequerAdapter(gitActions.cloneFolder.absolutePath) } \ No newline at end of file diff --git a/app/src/main/java/net/mbess/popequer/GitActions.kt b/app/src/main/java/net/mbess/popequer/GitActions.kt index 26eb491..9be9bf9 100644 --- a/app/src/main/java/net/mbess/popequer/GitActions.kt +++ b/app/src/main/java/net/mbess/popequer/GitActions.kt @@ -11,7 +11,7 @@ import kotlin.math.absoluteValue class GitActions( private val context: Context ) { - private val cloneFolder = context.filesDir.resolve("popequer") + val cloneFolder = context.filesDir.resolve("popequer") private val myCommitter = "Mobile Sandbox" to "example@example.org" private val credentials = UsernamePasswordCredentialsProvider( "popequer-mobile-sandbox", diff --git a/app/src/main/java/net/mbess/popequer/PopequerAdapter.java b/app/src/main/java/net/mbess/popequer/PopequerAdapter.java new file mode 100644 index 0000000..d89ebaa --- /dev/null +++ b/app/src/main/java/net/mbess/popequer/PopequerAdapter.java @@ -0,0 +1,47 @@ +// Automatically generated by flapigen +package net.mbess.popequer; +import androidx.annotation.NonNull; + +public final class PopequerAdapter { + + public PopequerAdapter(@NonNull String notebook_fs_path) { + mNativeObj = init(notebook_fs_path); + } + private static native long init(@NonNull String notebook_fs_path); + + public final @NonNull String index() { + String ret = do_index(mNativeObj); + + return ret; + } + private static native @NonNull String do_index(long self); + + public final @NonNull String upcomingEvents() { + String ret = do_upcomingEvents(mNativeObj); + + return ret; + } + private static native @NonNull String do_upcomingEvents(long self); + + public synchronized void delete() { + if (mNativeObj != 0) { + do_delete(mNativeObj); + mNativeObj = 0; + } + } + @Override + protected void finalize() throws Throwable { + try { + delete(); + } + finally { + super.finalize(); + } + } + private static native void do_delete(long me); + /*package*/ PopequerAdapter(InternalPointerMarker marker, long ptr) { + assert marker == InternalPointerMarker.RAW_PTR; + this.mNativeObj = ptr; + } + /*package*/ long mNativeObj; +} \ No newline at end of file diff --git a/app/src/main/java/net/mbess/popequer/PopequerAdapterErr.java b/app/src/main/java/net/mbess/popequer/PopequerAdapterErr.java new file mode 100644 index 0000000..c9a80f5 --- /dev/null +++ b/app/src/main/java/net/mbess/popequer/PopequerAdapterErr.java @@ -0,0 +1,23 @@ +// Automatically generated by flapigen +package net.mbess.popequer; + + +public enum PopequerAdapterErr { + ReadDb(0), + Indexing(1), + View(2); + + private final int value; + PopequerAdapterErr(int value) { + this.value = value; + } + public final int getValue() { return value; } + /*package*/ static PopequerAdapterErr fromInt(int x) { + switch (x) { + case 0: return ReadDb; + case 1: return Indexing; + case 2: return View; + default: throw new Error("Invalid value for enum PopequerAdapterErr: " + x); + } + } +} diff --git a/app/src/main/java/net/mbess/popequer/PopequerProvider.kt b/app/src/main/java/net/mbess/popequer/PopequerProvider.kt deleted file mode 100644 index 4d8d1b2..0000000 --- a/app/src/main/java/net/mbess/popequer/PopequerProvider.kt +++ /dev/null @@ -1,10 +0,0 @@ -package net.mbess.popequer - -import android.content.Context -import android.util.Log -import kotlin.math.absoluteValue - -class PopequerProvider( - private val context: Context -) { -} diff --git a/app/src/main/java/net/mbess/popequer/ui/App.kt b/app/src/main/java/net/mbess/popequer/ui/App.kt index a711283..bfcecff 100644 --- a/app/src/main/java/net/mbess/popequer/ui/App.kt +++ b/app/src/main/java/net/mbess/popequer/ui/App.kt @@ -99,7 +99,7 @@ fun AppContainer( onClick = { scope.launch(Dispatchers.IO) { runCatching { - context.gitActions.trigger() + // context.gitActions.trigger() }.onFailure { Log.e("MainActivity", "Failed to trigger git actions", it) } 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 e6764c4..e8f4a73 100644 --- a/app/src/main/java/net/mbess/popequer/ui/MainActivity.kt +++ b/app/src/main/java/net/mbess/popequer/ui/MainActivity.kt @@ -19,17 +19,17 @@ import net.mbess.popequer.Foo class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + System.loadLibrary("rustadapter") val appContext = AppContext(this) - //Native.fsInfo(appContext.gitActions.cloneFolder.absolutePath).let { - // Log.d("MainActivity", "From foreign rust result fsInfo $it") - //} - System.loadLibrary("rustadapter") + + val foo_instance = Foo(10) Log.d("MainActivity", "From rust: " + foo_instance.f(32, 43)) foo_instance.setField(154) Log.d("MainActivity", "${foo_instance.data}") + CoroutineScope(Dispatchers.IO).launch { Looper.prepare() runCatching { @@ -40,6 +40,10 @@ class MainActivity : ComponentActivity() { Toast.makeText(appContext.androidContext, "A repo was just cloned", Toast.LENGTH_LONG) .show() } + appContext.gitActions.sync() + Log.d("Ppq", "${appContext.popequer}"); + Log.d("Ppq", "Index: ${appContext.popequer.index()}"); + Log.d("Ppq", "Upcoming: ${appContext.popequer.upcomingEvents()}"); }.onFailure { Log.e("MainActivity", "Failed to prepare git repo", it) } diff --git a/rust-adapter/Cargo.toml b/rust-adapter/Cargo.toml index d94fc12..e8a8b96 100644 --- a/rust-adapter/Cargo.toml +++ b/rust-adapter/Cargo.toml @@ -13,6 +13,8 @@ rifgen = "0.1" popequer_rust = { path = "../../../popequer_rust" } jni-sys = "0.3" log = "0.4.20" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" [build-dependencies] rifgen = "0.1" diff --git a/rust-adapter/build.rs b/rust-adapter/build.rs index 0ac339f..0541e62 100644 --- a/rust-adapter/build.rs +++ b/rust-adapter/build.rs @@ -2,6 +2,8 @@ use std::env; fn main() { + env::set_var("RUST_LOG", "DEBUG"); + let source_folder = "/mnt/extramedia3/mbess/workspace/popequer/android_app/rust-adapter/src"; //use your projects folder let out_file = "/mnt/extramedia3/mbess/workspace/popequer/android_app/rust-adapter/glue.rs"; rifgen::Generator::new(rifgen::TypeCases::CamelCase, rifgen::Language::Java,source_folder) @@ -18,7 +20,7 @@ .join("popequer"), "net.mbess.popequer".into(), ) - .use_null_annotation_from_package("android.support.annotation".into()), + .use_null_annotation_from_package("androidx.annotation".into()), )) .rustfmt_bindings(true); diff --git a/rust-adapter/glue.rs b/rust-adapter/glue.rs index 3321dc9..6fd1832 100644 --- a/rust-adapter/glue.rs +++ b/rust-adapter/glue.rs @@ -1,6 +1,13 @@ //Automatically generated by rifgen use crate::*; use jni_sys::*; +foreign_enum!( + enum PopequerAdapterErr { + ReadDb = PopequerAdapterErr::ReadDb, + Indexing = PopequerAdapterErr::Indexing, + View = PopequerAdapterErr::View, + } +); foreign_class!( class Foo { self_type Foo; @@ -11,3 +18,11 @@ foreign_class!( fn Foo::set_field(& mut self , v : i32); alias setField; } ); +foreign_class!( + class PopequerAdapter { + self_type PopequerAdapter; + constructor PopequerAdapter::new(notebook_fs_path : String)->PopequerAdapter; + fn PopequerAdapter::index(& self)->String; alias index; + fn PopequerAdapter::upcoming_events(& self)->String; alias upcomingEvents; + } +); diff --git a/rust-adapter/src/lib.rs b/rust-adapter/src/lib.rs index 1964754..e9461ea 100644 --- a/rust-adapter/src/lib.rs +++ b/rust-adapter/src/lib.rs @@ -4,6 +4,73 @@ pub use crate::java_glue::*; use rifgen::rifgen_attr::*; +use popequer::{database::read::read_db, fs_notebook::NotebookContext, indexer::index_and_save, views::calendar::get_upcoming_events}; + +use serde::Serialize; + +struct PopequerAdapter { + context: NotebookContext +} + +#[generate_interface] +enum PopequerAdapterErr { + ReadDb, + Indexing, + View +} + +#[derive(Serialize)] +struct EventItem { + name: String, + start_time: String, + end_time: Option +} + +impl PopequerAdapter { + #[generate_interface(constructor)] + fn new(notebook_fs_path: String) -> PopequerAdapter { + PopequerAdapter { + context: NotebookContext { + base_path: notebook_fs_path.into() + } + } + } + + #[generate_interface] + fn index(&self) -> String { + match index_and_save( + &self.context + ) { + Ok(_n) => format!("Indexing OK"), + Err(err) => format!("Indexing ERR {err:?}") + } + } + + #[generate_interface] + fn upcoming_events(&self) -> String { + let notebook = match read_db(&self.context) { + Ok(n) => n, + Err(err) => { + return format!("Err: {err:?}"); + } + }; + let events = match get_upcoming_events(¬ebook) { + Ok(x) => x, + Err(err) => { + return format!("Err: {err:?}"); + } + }; + let formatted_events: Vec = events.iter().map(|e| { + EventItem { + name: e.name.clone(), + start_time: e.start_time.to_rfc3339(), + end_time: e.end_time.map(|x| x.to_rfc3339()) + } + }).collect(); + serde_json::to_string(&formatted_events).expect("Cannot serialize into json") + } +} + struct Foo { data: i32 }