|
| 1 | +import GObject from "gi://GObject"; |
| 2 | +import Gom from "gi://Gom"; |
| 3 | +import Gtk from "gi://Gtk"; |
| 4 | +import GLib from "gi://GLib"; |
| 5 | +import Gio from "gi://Gio"; |
| 6 | + |
| 7 | +Gio._promisify(Gom.Adapter.prototype, "open_async", "open_finish"); |
| 8 | + |
| 9 | +Gio._promisify( |
| 10 | + Gom.Repository.prototype, |
| 11 | + "automatic_migrate_async", |
| 12 | + "automatic_migrate_finish", |
| 13 | +); |
| 14 | +Gio._promisify(Gom.Repository.prototype, "find_async", "find_finish"); |
| 15 | + |
| 16 | +Gio._promisify(Gom.Resource.prototype, "save_async", "save_finish"); |
| 17 | +Gio._promisify(Gom.ResourceGroup.prototype, "fetch_async", "fetch_finish"); |
| 18 | + |
| 19 | +const text_entry = workbench.builder.get_object("text_entry"); |
| 20 | +const insert_button = workbench.builder.get_object("insert_button"); |
| 21 | +const search_entry = workbench.builder.get_object("search_entry"); |
| 22 | +const column_view = workbench.builder.get_object("column_view"); |
| 23 | +const column_text = workbench.builder.get_object("column_text"); |
| 24 | +const column_id = workbench.builder.get_object("column_id"); |
| 25 | + |
| 26 | +const ItemClass = GObject.registerClass( |
| 27 | + { |
| 28 | + Properties: { |
| 29 | + id: GObject.ParamSpec.int( |
| 30 | + "id", |
| 31 | + "ID", |
| 32 | + "An ID", |
| 33 | + GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT, |
| 34 | + 0, |
| 35 | + GLib.MAXINT32, |
| 36 | + 0, |
| 37 | + ), |
| 38 | + text: GObject.ParamSpec.string( |
| 39 | + "text", |
| 40 | + "Text", |
| 41 | + "Some Text", |
| 42 | + GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT, |
| 43 | + "", |
| 44 | + ), |
| 45 | + }, |
| 46 | + }, |
| 47 | + class ItemClass extends Gom.Resource {}, |
| 48 | +); |
| 49 | + |
| 50 | +const data_model = new Gio.ListStore({ item_type: ItemClass }); |
| 51 | +let adapter; |
| 52 | +let repository; |
| 53 | + |
| 54 | +async function initDatabase() { |
| 55 | + adapter = new Gom.Adapter(); |
| 56 | + await adapter.open_async(workbench.resolve("db.sqlite")); |
| 57 | + repository = new Gom.Repository({ adapter }); |
| 58 | + |
| 59 | + // Set up table and primary key |
| 60 | + ItemClass.set_table("items"); |
| 61 | + ItemClass.set_primary_key("id"); |
| 62 | + |
| 63 | + // Perform automatic migration |
| 64 | + await repository.automatic_migrate_async(1, [ItemClass]); |
| 65 | +} |
| 66 | + |
| 67 | +async function onInsert() { |
| 68 | + const text = text_entry.text; |
| 69 | + const item = new ItemClass({ repository, text }); |
| 70 | + const success = await item.save_async(); |
| 71 | + if (!success) { |
| 72 | + console.error("Failed to insert"); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + await load(); |
| 77 | +} |
| 78 | + |
| 79 | +async function load() { |
| 80 | + const text = search_entry.text || ""; |
| 81 | + |
| 82 | + data_model.remove_all(); |
| 83 | + // Create a filter for Text matching |
| 84 | + const filter = Gom.Filter.new_glob(ItemClass, "text", `*${text}*`); |
| 85 | + const resource_group = await repository.find_async(ItemClass, filter); |
| 86 | + |
| 87 | + await resource_group.fetch_async(0, resource_group.count); |
| 88 | + for (let i = 0; i < resource_group.count; i++) { |
| 89 | + const item = resource_group.get_index(i); |
| 90 | + if (item) data_model.append(item); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +column_text.factory.connect("setup", (_self, list_item) => { |
| 95 | + const label = new Gtk.Label({ |
| 96 | + margin_start: 12, |
| 97 | + margin_end: 12, |
| 98 | + }); |
| 99 | + list_item.set_child(label); |
| 100 | +}); |
| 101 | + |
| 102 | +column_text.factory.connect("bind", (_self, list_item) => { |
| 103 | + const label_widget = list_item.get_child(); |
| 104 | + const model_item = list_item.get_item(); |
| 105 | + label_widget.label = model_item.text; |
| 106 | +}); |
| 107 | + |
| 108 | +column_id.factory.connect("setup", (_self, list_item) => { |
| 109 | + const label = new Gtk.Label({ |
| 110 | + margin_start: 12, |
| 111 | + margin_end: 12, |
| 112 | + }); |
| 113 | + list_item.set_child(label); |
| 114 | +}); |
| 115 | + |
| 116 | +column_id.factory.connect("bind", (_self, list_item) => { |
| 117 | + const label_widget = list_item.get_child(); |
| 118 | + const model_item = list_item.get_item(); |
| 119 | + label_widget.label = model_item.id.toString(); |
| 120 | +}); |
| 121 | + |
| 122 | +column_view.model = new Gtk.SingleSelection({ |
| 123 | + model: data_model, |
| 124 | +}); |
| 125 | + |
| 126 | +try { |
| 127 | + await initDatabase(); |
| 128 | + |
| 129 | + search_entry.connect("search-changed", () => { |
| 130 | + load().catch(console.error); |
| 131 | + }); |
| 132 | + |
| 133 | + insert_button.connect("clicked", () => { |
| 134 | + onInsert().catch(console.error); |
| 135 | + }); |
| 136 | + |
| 137 | + await load(); |
| 138 | +} catch (err) { |
| 139 | + console.error(err); |
| 140 | +} |
0 commit comments