ReviewOS

also looking at this

craft-native/craft

feat(android): deleteCalendarEvent — the first action to answer through the channel

#155
Merged glennmichael123 wants to merge feat/android-delete-calendar-event into main
7 files +288 -3
packages/zig/src/android_dispatch.zigmodified+56-1
Changes to packages/zig/src/android_dispatch.zig
@@ -45,6 +45,7 @@ const securestore = @import("bridge_android_securestore.zig");
4545const haptics = @import("bridge_android_haptics.zig");
4646const notifcancel = @import("bridge_android_notifcancel.zig");
4747const events = @import("android_events.zig");
48const calendar = @import("bridge_android_calendar.zig");
4849
4950const Jni = jni.Jni;
5051
@@ -217,6 +218,14 @@ const natives = [_]jni.JNINativeMethod{
217218 .signature = "(Landroid/app/Activity;)Z",
218219 .fnPtr = @ptrCast(&nativeCancelAllNotifications),
219220 },
221 // The first action that answers through the reply channel rather than by
222 // returning. The boolean still means "Zig served it" — the *result* goes
223 // to the page separately, which is exactly the shape iOS's seam has.
224 .{
225 .name = "nativeDeleteCalendarEvent",
226 .signature = "(Landroid/app/Activity;Ljava/lang/String;)Z",
227 .fnPtr = @ptrCast(&nativeDeleteCalendarEvent),
228 },
220229};
221230
222231/// How binding went. A value rather than a log line, so every path is
@@ -587,6 +596,52 @@ fn nativeCancelAllNotifications(env: jni.JNIEnv, _: jni.jobject, activity: jni.j
587596 return jni.JNI_TRUE;
588597}
589598
599/// `nativeDeleteCalendarEvent(activity, eventId)`.
600///
601/// Returns whether Zig took the action, not whether the delete succeeded. The
602/// outcome reaches the page through the reply channel, so a `true` here means
603/// "do not also run the Kotlin" and nothing more — the same distinction
604/// `ios_dispatch` draws between claiming an action and answering it.
605fn nativeDeleteCalendarEvent(
606 env: jni.JNIEnv,
607 _: jni.jobject,
608 activity: jni.jobject,
609 event_id: jni.jstring,
610) callconv(.c) jni.jboolean {
611 const j = Jni.init(env);
612 var arena = std.heap.ArenaAllocator.init(backing);
613 defer arena.deinit();
614 const allocator = arena.allocator();
615
616 const text = j.stringToUtf8(allocator, event_id) catch return jni.JNI_FALSE;
617
618 const id = calendar.parseEventId(text) orelse {
619 // What the shim's NumberFormatException produces, with the message it
620 // produces — but escaped, so an id containing a quote rejects instead
621 // of hanging the promise. See #154.
622 var message: std.ArrayListUnmanaged(u8) = .empty;
623 defer message.deinit(allocator);
624 message.appendSlice(allocator, "For input string: \"") catch return jni.JNI_FALSE;
625 message.appendSlice(allocator, text) catch return jni.JNI_FALSE;
626 message.append(allocator, '"') catch return jni.JNI_FALSE;
627
628 calendar.rejectWith(allocator, message.items) catch return jni.JNI_FALSE;
629 return jni.JNI_TRUE;
630 };
631
632 calendar.deleteEvent(j, activity, id) catch |err| {
633 // A SecurityException for a missing WRITE_CALENDAR permission lands
634 // here, as it lands in the shim's catch. The error name rather than
635 // the Java message: the throwable was already described to logcat and
636 // cleared by `Jni.check`, so its text is gone by now.
637 calendar.rejectWith(allocator, @errorName(err)) catch {};
638 return jni.JNI_TRUE;
639 };
640
641 events.settle(allocator, calendar.resolve_global, "true") catch return jni.JNI_FALSE;
642 return jni.JNI_TRUE;
643}
644
590645// =============================================================================
591646// Tests
592647// =============================================================================
@@ -698,7 +753,7 @@ test "the registered natives name methods the Kotlin actually declares" {
698753 // A descriptor is checked by the JVM at registration, so a wrong one fails
699754 // at load rather than at call — but only if the *name* matches something.
700755 // These two strings are the contract with CraftBridge.kt.
701 try testing.expectEqual(@as(usize, 16), natives.len);
756 try testing.expectEqual(@as(usize, 17), natives.len);
702757 try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name));
703758
704759 // And the class they bind to is the fixed one, not the templated bridge.