Modern Apps git

← Modern-Apps.git

openassistant/notes: tool to insert a new note

Commit
8af22de8777e3b5c46026df35ee612102921dce8
Author
Vayun Mathur <[email protected]>
When
2026-02-15 02:05 UTC (9 months ago)
Parents
04c9345

Diff

library/src/main/java/com/vayunmathur/library/intents/notes/InsertNoteData.kt
diff --git a/library/src/main/java/com/vayunmathur/library/intents/notes/InsertNoteData.kt b/library/src/main/java/com/vayunmathur/library/intents/notes/InsertNoteData.ktnew file mode 100644index 0000000000..c77f0ff9ed--- /dev/null+++ b/library/src/main/java/com/vayunmathur/library/intents/notes/InsertNoteData.kt@@ -0,0 +1,6 @@+package com.vayunmathur.library.intents.notes++import kotlinx.serialization.Serializable++@Serializable+data class InsertNoteData(val title: String, val content: String)
library/src/main/java/com/vayunmathur/library/util/AssistantIntent.kt
diff --git a/library/src/main/java/com/vayunmathur/library/util/AssistantIntent.kt b/library/src/main/java/com/vayunmathur/library/util/AssistantIntent.ktindex 3720d0cabc..c0c1dcd98a 100644--- a/library/src/main/java/com/vayunmathur/library/util/AssistantIntent.kt+++ b/library/src/main/java/com/vayunmathur/library/util/AssistantIntent.kt@@ -62,7 +62,6 @@ class IntentLauncher(activity: ComponentActivity) {      @OptIn(InternalSerializationApi::class)     suspend fun <Input : Any> launch(-        action: String,         packageName: String,         className: String,         kClass: KClass<Input>,@@ -70,7 +69,7 @@ class IntentLauncher(activity: ComponentActivity) {     ): String = suspendCancellableCoroutine { cont ->         continuation = cont -        val intent = Intent(action).apply {+        val intent = Intent().apply {             setClassName(packageName, className)             putExtra("DATA", Json.encodeToString(kClass.serializer(), input))         }
notes/src/main/AndroidManifest.xml
diff --git a/notes/src/main/AndroidManifest.xml b/notes/src/main/AndroidManifest.xmlindex 01fbae15f2..55fdcc64f4 100644--- a/notes/src/main/AndroidManifest.xml+++ b/notes/src/main/AndroidManifest.xml@@ -29,10 +29,13 @@             android:exported="true"             android:theme="@android:style/Theme.NoDisplay"             android:permission="com.vayunmathur.notes.permissions.ACCESS_NOTES">-            <intent-filter>-                <action android:name="com.vayunmathur.notes.ACTION_GET_NOTES" />-                <category android:name="android.intent.category.DEFAULT" />-            </intent-filter>+        </activity>++        <activity+            android:name=".intents.InsertIntent"+            android:exported="true"+            android:theme="@android:style/Theme.NoDisplay"+            android:permission="com.vayunmathur.notes.permissions.ACCESS_NOTES">         </activity>     </application> 
notes/src/main/java/com/vayunmathur/notes/intents/GetIntent.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/intents/GetIntent.kt b/notes/src/main/java/com/vayunmathur/notes/intents/GetIntent.ktindex 999349e9ea..61e404872b 100644--- a/notes/src/main/java/com/vayunmathur/notes/intents/GetIntent.kt+++ b/notes/src/main/java/com/vayunmathur/notes/intents/GetIntent.kt@@ -17,7 +17,6 @@ class GetIntent: AssistantIntent<Unit, List<Note>>(serializer<Unit>(), serialize      override suspend fun performCalculation(input: Unit): List<Note> {         val db = buildDatabase<NoteDatabase>()-        println("RUNNING REQUEST")         return db.noteDao().getAll().first()     } }\ No newline at end of file
notes/src/main/java/com/vayunmathur/notes/intents/InsertIntent.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/intents/InsertIntent.kt b/notes/src/main/java/com/vayunmathur/notes/intents/InsertIntent.ktnew file mode 100644index 0000000000..473d89e0ff--- /dev/null+++ b/notes/src/main/java/com/vayunmathur/notes/intents/InsertIntent.kt@@ -0,0 +1,23 @@+package com.vayunmathur.notes.intents++import android.app.Activity+import android.content.Intent+import android.os.Bundle+import com.vayunmathur.library.intents.notes.InsertNoteData+import com.vayunmathur.library.util.AssistantIntent+import com.vayunmathur.library.util.buildDatabase+import com.vayunmathur.notes.data.Note+import com.vayunmathur.notes.data.database.NoteDatabase+import kotlinx.coroutines.flow.first+import kotlinx.serialization.InternalSerializationApi+import kotlinx.serialization.json.JsonObject+import kotlinx.serialization.serializer++@OptIn(InternalSerializationApi::class)+class InsertIntent: AssistantIntent<InsertNoteData, Unit>(serializer<InsertNoteData>(), serializer<Unit>()) {++    override suspend fun performCalculation(input: InsertNoteData) {+        val db = buildDatabase<NoteDatabase>()+        db.noteDao().upsert(Note(title = input.title, content = input.content))+    }+}\ No newline at end of file
openassistant/src/main/java/com/vayunmathur/openassistant/data/Tools.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/data/Tools.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/data/Tools.ktindex 587f825617..d6d9a0af0e 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/data/Tools.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/data/Tools.kt@@ -6,6 +6,7 @@ import android.content.Intent import android.content.pm.PackageManager import androidx.core.net.toUri import androidx.core.text.util.LocalePreferences+import com.vayunmathur.library.intents.notes.InsertNoteData import com.vayunmathur.library.util.findActivity import com.vayunmathur.openassistant.MainActivity import com.vayunmathur.openassistant.intentLauncher@@ -161,7 +162,14 @@ class Tools {                 }             },             ToolSimple("get_notes", "Get a list of notes (and their content) on the user's phone", listOf()) { _, _ ->-                ToolResult(intentLauncher.launch("com.vayunmathur.notes.ACTION_GET_NOTES", "com.vayunmathur.notes", "com.vayunmathur.notes.intents.GetIntent", Unit::class, Unit), "Retrieved notes")+                ToolResult(intentLauncher.launch("com.vayunmathur.notes", "com.vayunmathur.notes.intents.GetIntent", Unit::class, Unit), "Retrieved notes")+            },+            ToolSimple("insert_note", "Add a note to the user's phone", listOf(+                stringParam("title", "the title of the note"),+                stringParam("content", "the content of the note")+            )) { args, _ ->+                val input = InsertNoteData(args["title"]!!.jsonPrimitive.content, args["content"]!!.jsonPrimitive.content)+                ToolResult(intentLauncher.launch("com.vayunmathur.notes", "com.vayunmathur.notes.intents.InsertIntent", InsertNoteData::class, input), "Added notes")             }         )