Skip to content

Commit

Permalink
Get live subject adding working
Browse files Browse the repository at this point in the history
  • Loading branch information
leolost2605 committed Nov 29, 2023
1 parent b98879c commit 7111348
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MyApp : Adw.Application {
var dialog = new NewSubjectDialog (main_window);
dialog.close_request.connect (() => {
if (dialog.accept) {
SubjectManager.get_default ().new_subject (dialog.name_entry_box.get_text (), dialog.categories);
SubjectManager.get_default ().new_subject (dialog.name_entry_box.get_text (), dialog.get_categories ());
}
dialog.destroy ();
return true;
Expand Down
17 changes: 7 additions & 10 deletions src/new-subject-dialog.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public class NewSubjectDialog : Adw.Window {
public Category[] categories;
private Category[] categories = {};
public Gtk.Box main_box;
public Adw.EntryRow name_entry_box;
private Gtk.Button new_cat_button;
Expand All @@ -16,8 +16,6 @@ public class NewSubjectDialog : Adw.Window {
height_request: 360
);

categories = new Category[5];

var tbv = new Adw.ToolbarView ();
this.set_content (tbv);

Expand Down Expand Up @@ -101,13 +99,12 @@ public class NewSubjectDialog : Adw.Window {
}

public void add_cat (string n, double p) {
for (int i = 0; i < categories.length; i++) {
if (categories[i] == null) {
categories[i] = new Category (n, p);
i = categories.length;
categories_list_ui ();
}
}
categories += new Category (n, p);
categories_list_ui ();
}

public Category[] get_categories () {
return categories;
}

public void categories_list_ui () {
Expand Down
9 changes: 9 additions & 0 deletions src/subject-manager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public class SubjectManager : Object {
return instance.once (() => new SubjectManager ());
}

public signal void subject_added (Subject subject);

public HashTable<string, Subject> subjects;

construct {
Expand Down Expand Up @@ -50,6 +52,7 @@ public class SubjectManager : Object {
var parser = new SubjectParser ();
Subject sub = parser.to_object (read_from_file (file));
subjects[sub.name] = sub;
subject_added (sub);
}
}

Expand Down Expand Up @@ -79,5 +82,11 @@ public class SubjectManager : Object {
bool worked = false;

subjects[name] = new Subject (name);

foreach (var cat in c) {
subjects[name].categories_by_name[cat.name] = cat;
}

subject_added (subjects[name]);
}
}
10 changes: 5 additions & 5 deletions src/window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ public class Window : Adw.ApplicationWindow {

var subject_manager = SubjectManager.get_default ();

subject_manager.read_data ();

foreach (var subject in subject_manager.subjects.get_values ()) {
subject_manager.subject_added.connect ((subject) => {
subject.notify["deleted"].connect (() => {
if (subject.deleted) {
//TODO: Remove without crash
}
warning ("removed");
});
stack.add_titled (new SubjectPage (subject), subject.name, subject.name);
}
});

subject_manager.read_data ();

close_request.connect (() => {
set_visible (false);
subject_manager.write_data ();
return true;
return false;
});
}
}

0 comments on commit 7111348

Please sign in to comment.