Skip to content

Commit 909c833

Browse files
committed
further simplifications
1 parent b9b6d37 commit 909c833

File tree

3 files changed

+37
-39
lines changed

3 files changed

+37
-39
lines changed

src/Database/main.blp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using Adw 1;
33

44
Adw.StatusPage {
55
title: _("Database");
6-
description: _("Search, load and store data in a database");
6+
description: _("Search, load and store data using SQLite");
77

88
Box {
99
orientation: vertical;
@@ -79,5 +79,20 @@ Adw.StatusPage {
7979
}
8080
}
8181
}
82+
83+
Box {
84+
orientation: horizontal;
85+
halign: center;
86+
87+
LinkButton {
88+
label: _("API Reference");
89+
uri: "https://gjs-docs.gnome.org/gom10~1.0/";
90+
}
91+
92+
LinkButton {
93+
label: _("SQLite");
94+
uri: "https://www.sqlite.org/";
95+
}
96+
}
8297
}
8398
}

src/Database/main.js

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ Gio._promisify(
1212
"automatic_migrate_finish",
1313
);
1414
Gio._promisify(Gom.Repository.prototype, "find_async", "find_finish");
15-
Gio._promisify(Gom.Repository.prototype, "find_one_async", "find_one_finish");
1615

1716
Gio._promisify(Gom.Resource.prototype, "save_async", "save_finish");
1817
Gio._promisify(Gom.ResourceGroup.prototype, "fetch_async", "fetch_finish");
1918

2019
const text_entry = workbench.builder.get_object("text_entry");
21-
const id_entry = workbench.builder.get_object("id_entry");
2220
const insert_button = workbench.builder.get_object("insert_button");
2321
const search_entry = workbench.builder.get_object("search_entry");
2422
const column_view = workbench.builder.get_object("column_view");
@@ -64,12 +62,8 @@ async function initDatabase() {
6462

6563
// Perform automatic migration
6664
await repository.automatic_migrate_async(1, [ItemClass]);
67-
68-
await search();
6965
}
7066

71-
initDatabase().catch(console.error);
72-
7367
async function onInsert() {
7468
const text = text_entry.text;
7569
const item = new ItemClass({ repository, text });
@@ -79,38 +73,15 @@ async function onInsert() {
7973
return;
8074
}
8175

82-
data_model.append(item);
83-
}
84-
85-
insert_button.connect("clicked", () => {
86-
onInsert().catch(console.error);
87-
});
88-
89-
async function findById() {
90-
const { text } = id_entry;
91-
if (!text) {
92-
return;
93-
}
94-
95-
data_model.remove_all();
96-
const id = parseInt(text);
97-
const filter = Gom.Filter.new_eq(ItemClass, "id", id);
98-
const found_item = await repository.find_one_async(ItemClass, filter);
99-
if (found_item) {
100-
data_model.append(found_item);
101-
}
76+
await load();
10277
}
10378

104-
id_entry.connect("search-changed", () => {
105-
findById().catch(console.error);
106-
});
79+
async function load() {
80+
const text = search_entry.text || "";
10781

108-
async function search() {
10982
data_model.remove_all();
110-
const filter_text = search_entry.text.trim();
111-
11283
// Create a filter for Text matching
113-
const filter = Gom.Filter.new_glob(ItemClass, "text", `*${filter_text}*`);
84+
const filter = Gom.Filter.new_glob(ItemClass, "text", `*${text}*`);
11485
const resource_group = await repository.find_async(ItemClass, filter);
11586

11687
await resource_group.fetch_async(0, resource_group.count);
@@ -120,10 +91,6 @@ async function search() {
12091
}
12192
}
12293

123-
search_entry.connect("search-changed", () => {
124-
search().catch(console.error);
125-
});
126-
12794
column_text.factory.connect("setup", (_self, list_item) => {
12895
const label = new Gtk.Label({
12996
margin_start: 12,
@@ -155,3 +122,19 @@ column_id.factory.connect("bind", (_self, list_item) => {
155122
column_view.model = new Gtk.SingleSelection({
156123
model: data_model,
157124
});
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+
}

src/Database/main.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"category": "platform",
3-
"description": "Search, load and store data in a database",
3+
"description": "Search, load and store data using SQLite",
44
"panels": ["code", "preview"],
55
"autorun": true
66
}

0 commit comments

Comments
 (0)