ReviewOS

also looking at this

craft-native/craft

refactor(android): one SharedPreferences helper, after the third caller turned out to be dead

#170
Merged chris wants to merge refactor/android-prefs into main
3 files +165 -113
packages/zig/src/bridge_android_widgets.zigmodified+7-36
Changes to packages/zig/src/bridge_android_widgets.zig
@@ -34,6 +34,7 @@
3434
3535const std = ;
3636const jni = ;
37const prefs_api = ;
3738
3839const Jni = jni.Jni;
3940const jobject = jni.jobject;
@@ -50,9 +51,9 @@ pub const reject_global = "_craftWidgetReject";
5051pub const updated_result = "{\"updated\":true}";
5152pub const reloaded_result = "{\"reloaded\":true}";
5253
53/// `activity.getSharedPreferences("craft_widget_prefs", MODE_PRIVATE)`.
54/// `activity.getSharedPreferences("craft_widget_prefs", MODE_PRIVATE)` — the
55/// file `CraftWidgetProvider` reads back.
5456const prefs_name = "craft_widget_prefs";
55const mode_private: i32 = 0;
5657
5758/// The four fields, and the preference key each is written to.
5859///
@@ -106,45 +107,16 @@ pub fn writeUpdate(j: Jni, allocator: std.mem.Allocator, activity: jobject, upda
106107 try j.pushLocalFrame(16);
107108 defer _ = j.popLocalFrame(null);
108109
109 const prefs = try j.callObjectMethodA(
110 activity,
111 try j.methodId(
112 try j.objectClass(activity),
113 "getSharedPreferences",
114 "(Ljava/lang/String;I)Landroid/content/SharedPreferences;",
115 ),
116 &.{ .{ .l = try j.newStringUtf(prefs_name) }, .{ .i = mode_private } },
117 );
118
119 const editor = try j.callObjectMethod(
120 prefs,
121 try j.methodId(
122 try j.objectClass(prefs),
123 "edit",
124 "()Landroid/content/SharedPreferences$Editor;",
125 ),
126 );
127 const editor_cls = try j.objectClass(editor);
128 const put_string = try j.methodId(
129 editor_cls,
130 "putString",
131 "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/SharedPreferences$Editor;",
132 );
110 const prefs = try prefs_api.open(j, allocator, activity, prefs_name);
111 const editor = try prefs_api.edit(j, prefs);
133112
134113 inline for (fields, 0..) |field, i| {
135 if (update[i]) |text| {
136 try j.pushLocalFrame(4);
137 defer _ = j.popLocalFrame(null);
138 _ = try j.callObjectMethodA(editor, put_string, &.{
139 .{ .l = try j.newStringUtf(field.key) },
140 .{ .l = try j.newStringUtf8(allocator, text) },
141 });
142 }
114 if (update[i]) |text| try prefs_api.putString(j, allocator, editor, field.key, text);
143115 }
144116
145117 // `apply`, as the shim does — the write lands on a background thread and
146118 // the resolve is a promise about having asked.
147 try j.callVoidMethodA(editor, try j.methodId(editor_cls, "apply", "()V"), &.{});
119 try prefs_api.apply(j, editor);
148120}
149121
150122/// `Intent(action).setPackage(activity.packageName)`, then `sendBroadcast`.
@@ -314,7 +286,6 @@ test "the results are the shim's constants" {
314286
315287test "the preferences file is the one CraftWidgetProvider reads" {
316288 try testing.expectEqualStrings("craft_widget_prefs", prefs_name);
317 try testing.expectEqual(@as(i32, 0), mode_private);
318289}
319290
320291test "the actions and globals match the shim exactly" {