random songs button

This commit is contained in:
Erica Z 2024-10-10 10:51:12 +00:00
parent 04ae260dc5
commit 31373051ec
5 changed files with 50 additions and 7 deletions

View file

@ -22,6 +22,8 @@ errordomain SubsonicError {
ERROR ERROR
} }
public delegate void Wavelet.SongCallback (Song song);
public class Wavelet.Artist : Object, Json.Serializable { public class Wavelet.Artist : Object, Json.Serializable {
public string index; public string index;
public string id; public string id;
@ -131,7 +133,7 @@ public struct Wavelet.API.PlayQueue {
this.changed_by = reader.get_string_value (); this.changed_by = reader.get_string_value ();
reader.end_member (); reader.end_member ();
print("%s %lli %s %s\n",this.current, this.position, this.changed, this.changed_by); //print("%s %lli %s %s\n",this.current, this.position, this.changed, this.changed_by);
this.songs = new ListStore (typeof (Song)); this.songs = new ListStore (typeof (Song));
@ -193,6 +195,7 @@ public class Wavelet.Subsonic : Object {
public async void ping () throws Error { public async void ping () throws Error {
var msg = new Soup.Message ("GET", @"$(this.url)/rest/ping?$(this.parameters)"); var msg = new Soup.Message ("GET", @"$(this.url)/rest/ping?$(this.parameters)");
var bytes = yield this.session.send_and_read_async (msg, Priority.DEFAULT, null); var bytes = yield this.session.send_and_read_async (msg, Priority.DEFAULT, null);
assert (msg.get_status () == Soup.Status.OK);
var parser = new Json.Parser (); var parser = new Json.Parser ();
parser.load_from_data ((string) bytes.get_data ()); parser.load_from_data ((string) bytes.get_data ());
@ -323,4 +326,34 @@ public class Wavelet.Subsonic : Object {
} }
} }
} }
public async void get_random_songs (string? parameters, SongCallback callback) throws Error {
string str_parameters;
if (parameters == null) {
str_parameters = "";
} else {
str_parameters = @"$parameters&";
}
var msg = new Soup.Message("GET", @"$(this.url)/rest/getRandomSongs?$(str_parameters)size=500&$(this.parameters)");
var bytes = yield this.session.send_and_read_async (msg, Priority.DEFAULT, null);
assert (msg.get_status () == Soup.Status.OK);
var parser = new Json.Parser ();
parser.load_from_data ((string) bytes.get_data ());
var reader = new Json.Reader (parser.get_root ());
this.unwrap_response (reader);
reader.read_member ("randomSongs");
reader.read_member ("song");
for (int i = 0; i < reader.count_elements (); i += 1) {
reader.read_element (i);
callback (new Song (reader));
reader.end_element ();
}
assert (reader.get_error () == null);
}
} }

View file

@ -71,6 +71,18 @@ public class Wavelet.Application : Adw.Application {
win.song_list.set_ready (); win.song_list.set_ready ();
}); });
//api.reload.begin (); //api.reload.begin ();
win.shuffle_all_tracks.sensitive = true;
win.shuffle_all_tracks.activated.connect (() => {
win.shuffle_all_tracks.sensitive = false;
win.play_queue.clear ();
api.get_random_songs.begin (null, (song) => {
win.play_queue.queue (song);
}, (obj, res) => {
api.get_random_songs.end (res);
win.shuffle_all_tracks.sensitive = true;
});
});
}); });
win.setup.load (config_db); win.setup.load (config_db);
} }

View file

@ -10,15 +10,11 @@ template $WaveletPlayQueue: Adw.NavigationPage {
ScrolledWindow { ScrolledWindow {
ListView list_view { ListView list_view {
styles [
"view",
]
factory: BuilderListItemFactory { factory: BuilderListItemFactory {
template ListItem { template ListItem {
child: Label { child: Label {
halign: start; halign: start;
label: bind template.item as <$WaveletSong>.name; label: bind template.item as <$WaveletSong>.title;
}; };
} }
}; };

View file

@ -53,9 +53,10 @@ template $WaveletWindow: Adw.ApplicationWindow {
"navigation-sidebar", "navigation-sidebar",
] ]
Adw.ButtonRow { Adw.ButtonRow shuffle_all_tracks {
title: _("Shuffle all tracks"); title: _("Shuffle all tracks");
start-icon-name: "media-playlist-shuffle"; start-icon-name: "media-playlist-shuffle";
sensitive: false;
} }
} }
}; };

View file

@ -29,6 +29,7 @@ public class Wavelet.Window : Adw.ApplicationWindow {
[GtkChild] public unowned Wavelet.ArtistList artist_list; [GtkChild] public unowned Wavelet.ArtistList artist_list;
[GtkChild] public unowned Wavelet.SongList song_list; [GtkChild] public unowned Wavelet.SongList song_list;
[GtkChild] public unowned Wavelet.PlayQueue play_queue; [GtkChild] public unowned Wavelet.PlayQueue play_queue;
[GtkChild] public unowned Adw.ButtonRow shuffle_all_tracks;
public Window (Gtk.Application app) { public Window (Gtk.Application app) {
Object (application: app); Object (application: app);