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 Adw.ApplicationWindow window;
public static Song.Window window;
public static Adw.ToastOverlay toast_overlay = new Adw.ToastOverlay();
}
+36 -26
View File
@@ -87,36 +87,46 @@ namespace Song {
// Parse settings from JSON
if (json_config.has_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();
members.foreach((member_name) => {
PlaylistObject _playList = new PlaylistObject();
var object = _playlists.get_member(member_name).get_object();
_playList.name = member_name;
// TODO: ADD PROPERTIES _playList.properties = whatever
// TODO: fix item iterator, it is invalid as it calls on each member, not each "object"
var items_object = object.get_object_member("items");
var items_list = items_object.get_members();
items_list.foreach((item) => {
PlaylistItem _item = new PlaylistItem();
var _item_object = items_object.get_object_member(item);
_item.item_type = _item_object.get_int_member("item_type");
_item.name = _item_object.get_string_member("name");
_item.file = _item_object.get_string_member("file");
if (_item_object.has_member("picture")) {
_item.picture = _item_object.get_string_member("picture");
if (members != null) {
members.foreach((member_name) => {
PlaylistObject? _playList = new PlaylistObject();
var object = _playlists.get_member(member_name).get_object();
if (object != null) {
_playList.name = member_name;
// TODO: ADD PROPERTIES _playList.properties = whatever
// TODO: fix item iterator, it is invalid as it calls on each member, not each "object"
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();
items_list.foreach((item) => {
PlaylistItem _item = new PlaylistItem();
var _item_object = items_object.get_object_member(item);
_item.item_type = _item_object.get_int_member("item_type");
_item.name = _item_object.get_string_member("name");
_item.file = _item_object.get_string_member("file");
if (_item_object.has_member("picture")) {
_item.picture = _item_object.get_string_member("picture");
}
if (_item_object.has_member("source")) {
_item.picture = _item_object.get_string_member("source");
}
_playList.items.append(_item);
});
}
}
if (_item_object.has_member("source")) {
_item.picture = _item_object.get_string_member("source");
}
_playList.items.append(_item);
playlists.append(_playList);
});
playlists.append(_playList);
});
}
}
}
loaded();
+7 -5
View File
@@ -86,11 +86,13 @@ namespace Song {
var config = Config.get_instance();
config.config_changed.connect(() => {
if (config.playlists.length() == 1) {
config.playlists.foreach((i) => {
change_page(i);
return;
});
if (config.playlists != null) {
if (config.playlists.length() == 1) {
config.playlists.foreach((i) => {
change_page(i);
return;
});
}
}
});
}
+33 -1
View File
@@ -3,7 +3,7 @@ namespace Song {
private Settings settings = new Settings ("com.liyaowhen.Song");
private PlaylistObject playlist;
public PlaylistObject playlist;
private Gtk.Button remove_button;
private Gtk.Revealer remove_button_revealer;
@@ -13,10 +13,17 @@ namespace Song {
}
construct {
orientation = Gtk.Orientation.HORIZONTAL;
add_css_class("toolbar");
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.set_child(remove_button);
remove_button_revealer.transition_type = Gtk.RevealerTransitionType.SWING_LEFT;
@@ -39,6 +46,7 @@ namespace Song {
if (playlist.name != null) {
main_button.label = playlist.name;
}
});
Config.get_instance().loaded.connect(() => {
@@ -60,5 +68,29 @@ namespace Song {
public void exit_removal_mode() {
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();
}
});
}
}
}
+34 -22
View File
@@ -60,7 +60,7 @@ namespace Song {
vexpand_set = true;
hexpand_set = true;
PlaylistObject initial_playlist;
PlaylistObject? initial_playlist = null;
/*var playlist_names = settings.get_strv("playlists");
foreach (string _name in playlist_names) {
@@ -75,22 +75,32 @@ namespace Song {
Config config = Config.get_instance();
bool firstDeclared = false;
config.playlists.foreach((_playlist) => {
var button = new PlaylistButton(_playlist);
if (!firstDeclared) {initial_playlist = _playlist; firstDeclared = true;}
playlist_buttons.add(button);
append(button);
});
config.config_changed.connect(() => {
playlist_buttons.clear();
config.playlists.foreach((_playlist) => {
if (config.playlists != null) {
foreach (PlaylistObject _playlist in config.playlists) {
var button = new PlaylistButton(_playlist);
if (!firstDeclared) {initial_playlist = _playlist; firstDeclared = true;}
playlist_buttons.add(button);
append(button);
});
}
}
print("\n aaaaaa" + playlist_buttons.size.to_string() + "\n");
config.config_changed.connect(() => {
foreach (PlaylistButton e in playlist_buttons) {
print("\n" + e.playlist.name + "eee");
e.destroy();
}
if (config.playlists != null) {
config.playlists.foreach((_playlist) => {
var button = new PlaylistButton(_playlist);
playlist_buttons.add(button);
append(button);
});
}
});
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);
Timeout.add(1, () => {
if (SongController.main_view_content != null) {
SongController.main_view_content.change_page(initial_playlist);
return false;
if (initial_playlist != null) {
SongController.main_view_content.change_page(initial_playlist);
return false;
}
}
return true;
}, 1);
@@ -163,16 +174,17 @@ namespace Song {
print("\n sidebar buttons are null");
}
if (i.get_active()) {
sidebar_content.playlist_buttons.foreach((e) => {
foreach (PlaylistButton e in sidebar_content.playlist_buttons) {
e.enter_removal_mode();
print(e.name);
});
print(e.playlist.name);
}
} else {
sidebar_content.playlist_buttons.foreach((e) => {
foreach (PlaylistButton e in sidebar_content.playlist_buttons) {
e.exit_removal_mode();
print(e.name);
});
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");
public SideBar sidebar;
public MainView main_view;
public Gtk.ShortcutAction ctrl_z = new Gtk.SignalAction("ctrl_z");
public Window (Gtk.Application app) {
Object (application: app);
@@ -70,6 +71,11 @@ namespace Song {
_content.add_top_bar (title_bar);
_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(() => {
if(split_view.get_collapsed ()){