fixed bug that made window crash when there were no playlists

This commit is contained in:
liyaowhen
2024-07-24 19:21:06 -07:00
parent c09d61bcdd
commit b84c1a5844
6 changed files with 122 additions and 58 deletions
+3 -1
View File
@@ -15,7 +15,9 @@ namespace Song {
public static SongControls song_controls; public static SongControls song_controls;
public static Adw.ApplicationWindow window; public static Song.Window window;
public static Adw.ToastOverlay toast_overlay = new Adw.ToastOverlay();
} }
+15 -5
View File
@@ -87,17 +87,25 @@ namespace Song {
// Parse settings from JSON // Parse settings from JSON
if (json_config.has_member("playlists")) { if (json_config.has_member("playlists")) {
Json.Object _playlists = json_config.get_object_member("playlists"); Json.Object _playlists = json_config.get_object_member("playlists");
print("\n." + _playlists.get_members().data + ".\n");
// invalid methoud down below
var members = _playlists.get_members(); var members = _playlists.get_members();
if (members != null) {
members.foreach((member_name) => { members.foreach((member_name) => {
PlaylistObject _playList = new PlaylistObject(); PlaylistObject? _playList = new PlaylistObject();
var object = _playlists.get_member(member_name).get_object(); var object = _playlists.get_member(member_name).get_object();
if (object != null) {
_playList.name = member_name; _playList.name = member_name;
// TODO: ADD PROPERTIES _playList.properties = whatever // TODO: ADD PROPERTIES _playList.properties = whatever
// TODO: fix item iterator, it is invalid as it calls on each member, not each "object" // TODO: fix item iterator, it is invalid as it calls on each member, not each "object"
var items_object = object.get_object_member("items"); Json.Object? items_object = null;
if (object.has_member("items")) {
items_object = object.get_object_member("items");
}
if (items_object != null) {
var items_list = items_object.get_members(); var items_list = items_object.get_members();
items_list.foreach((item) => { items_list.foreach((item) => {
PlaylistItem _item = new PlaylistItem(); PlaylistItem _item = new PlaylistItem();
@@ -113,12 +121,14 @@ namespace Song {
} }
_playList.items.append(_item); _playList.items.append(_item);
}); });
}
}
playlists.append(_playList); playlists.append(_playList);
}); });
} }
} }
}
loaded(); loaded();
} catch (Error e) { } catch (Error e) {
+2
View File
@@ -86,12 +86,14 @@ namespace Song {
var config = Config.get_instance(); var config = Config.get_instance();
config.config_changed.connect(() => { config.config_changed.connect(() => {
if (config.playlists != null) {
if (config.playlists.length() == 1) { if (config.playlists.length() == 1) {
config.playlists.foreach((i) => { config.playlists.foreach((i) => {
change_page(i); change_page(i);
return; return;
}); });
} }
}
}); });
} }
+33 -1
View File
@@ -3,7 +3,7 @@ namespace Song {
private Settings settings = new Settings ("com.liyaowhen.Song"); private Settings settings = new Settings ("com.liyaowhen.Song");
private PlaylistObject playlist; public PlaylistObject playlist;
private Gtk.Button remove_button; private Gtk.Button remove_button;
private Gtk.Revealer remove_button_revealer; private Gtk.Revealer remove_button_revealer;
@@ -13,10 +13,17 @@ namespace Song {
} }
construct { construct {
orientation = Gtk.Orientation.HORIZONTAL; orientation = Gtk.Orientation.HORIZONTAL;
add_css_class("toolbar"); add_css_class("toolbar");
remove_button = new Gtk.Button.from_icon_name("app-remove-symbolic"); remove_button = new Gtk.Button.from_icon_name("app-remove-symbolic");
remove_button.clicked.connect(() => {
print("clicked");
confirm_delete_popup();
});
remove_button_revealer = new Gtk.Revealer(); remove_button_revealer = new Gtk.Revealer();
remove_button_revealer.set_child(remove_button); remove_button_revealer.set_child(remove_button);
remove_button_revealer.transition_type = Gtk.RevealerTransitionType.SWING_LEFT; remove_button_revealer.transition_type = Gtk.RevealerTransitionType.SWING_LEFT;
@@ -39,6 +46,7 @@ namespace Song {
if (playlist.name != null) { if (playlist.name != null) {
main_button.label = playlist.name; main_button.label = playlist.name;
} }
}); });
Config.get_instance().loaded.connect(() => { Config.get_instance().loaded.connect(() => {
@@ -60,5 +68,29 @@ namespace Song {
public void exit_removal_mode() { public void exit_removal_mode() {
remove_button_revealer.set_reveal_child(false); remove_button_revealer.set_reveal_child(false);
} }
private void confirm_delete_popup() {
var alert_dialog = new Adw.AlertDialog("Remove Playlist?",
"Deleting a playlist is a non-reversable action,
however the playlist's items will remain intact in the configuration folder");
alert_dialog.add_response("Cancel", "Cancel");
alert_dialog.add_response("Confirm", "Confirm");
alert_dialog.set_response_appearance("Confirm", Adw.ResponseAppearance.DESTRUCTIVE);
alert_dialog.present(this);
alert_dialog.response.connect((i, e) => {
if (e == "Confirm") {
Config.get_instance().playlists.remove(playlist);
Config.get_instance().save.begin();
}
});
}
} }
} }
+25 -13
View File
@@ -60,7 +60,7 @@ namespace Song {
vexpand_set = true; vexpand_set = true;
hexpand_set = true; hexpand_set = true;
PlaylistObject initial_playlist; PlaylistObject? initial_playlist = null;
/*var playlist_names = settings.get_strv("playlists"); /*var playlist_names = settings.get_strv("playlists");
foreach (string _name in playlist_names) { foreach (string _name in playlist_names) {
@@ -75,22 +75,32 @@ namespace Song {
Config config = Config.get_instance(); Config config = Config.get_instance();
bool firstDeclared = false; bool firstDeclared = false;
config.playlists.foreach((_playlist) => { if (config.playlists != null) {
foreach (PlaylistObject _playlist in config.playlists) {
var button = new PlaylistButton(_playlist); var button = new PlaylistButton(_playlist);
if (!firstDeclared) {initial_playlist = _playlist; firstDeclared = true;} if (!firstDeclared) {initial_playlist = _playlist; firstDeclared = true;}
playlist_buttons.add(button); playlist_buttons.add(button);
append(button); append(button);
}); }
}
print("\n aaaaaa" + playlist_buttons.size.to_string() + "\n");
config.config_changed.connect(() => { config.config_changed.connect(() => {
playlist_buttons.clear(); foreach (PlaylistButton e in playlist_buttons) {
print("\n" + e.playlist.name + "eee");
e.destroy();
}
if (config.playlists != null) {
config.playlists.foreach((_playlist) => { config.playlists.foreach((_playlist) => {
var button = new PlaylistButton(_playlist); var button = new PlaylistButton(_playlist);
playlist_buttons.add(button); playlist_buttons.add(button);
append(button); append(button);
}); });
}
}); });
Timeout.add(1000, () => { Timeout.add(1000, () => {
@@ -105,9 +115,10 @@ namespace Song {
//print("sidbar requesting main_view_content switch to playlist of name:\n" + playlists.nth_data(0).name); //print("sidbar requesting main_view_content switch to playlist of name:\n" + playlists.nth_data(0).name);
Timeout.add(1, () => { Timeout.add(1, () => {
if (SongController.main_view_content != null) { if (SongController.main_view_content != null) {
if (initial_playlist != null) {
SongController.main_view_content.change_page(initial_playlist); SongController.main_view_content.change_page(initial_playlist);
return false; return false;
}
} }
return true; return true;
}, 1); }, 1);
@@ -163,16 +174,17 @@ namespace Song {
print("\n sidebar buttons are null"); print("\n sidebar buttons are null");
} }
if (i.get_active()) { if (i.get_active()) {
sidebar_content.playlist_buttons.foreach((e) => { foreach (PlaylistButton e in sidebar_content.playlist_buttons) {
e.enter_removal_mode(); e.enter_removal_mode();
print(e.name); print(e.playlist.name);
});
} else {
sidebar_content.playlist_buttons.foreach((e) => {
e.exit_removal_mode();
print(e.name);
});
} }
} else {
foreach (PlaylistButton e in sidebar_content.playlist_buttons) {
e.exit_removal_mode();
print(e.playlist.name);
}
}
print("\n" + sidebar_content.playlist_buttons.size.to_string() + "\n");
}); });
+6
View File
@@ -5,6 +5,7 @@ namespace Song {
private Settings settings = new Settings ("com.liyaowhen.Song"); private Settings settings = new Settings ("com.liyaowhen.Song");
public SideBar sidebar; public SideBar sidebar;
public MainView main_view; public MainView main_view;
public Gtk.ShortcutAction ctrl_z = new Gtk.SignalAction("ctrl_z");
public Window (Gtk.Application app) { public Window (Gtk.Application app) {
Object (application: app); Object (application: app);
@@ -70,6 +71,11 @@ namespace Song {
_content.add_top_bar (title_bar); _content.add_top_bar (title_bar);
_content.set_content (split_view); _content.set_content (split_view);
var controller = new Gtk.ShortcutController();
Gtk.ShortcutTrigger trigger = Gtk.ShortcutTrigger.parse_string("<Control>Z");
controller.add_shortcut(new Gtk.Shortcut(trigger, ctrl_z));
add_controller(new Gtk.ShortcutController());
collapse_button.clicked.connect(() => { collapse_button.clicked.connect(() => {
if(split_view.get_collapsed ()){ if(split_view.get_collapsed ()){