Skip to content

Commit

Permalink
__Unstable changes__
Browse files Browse the repository at this point in the history
  • Loading branch information
Rirusha committed Mar 19, 2024
1 parent f03a83e commit e685fe9
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 123 deletions.
22 changes: 11 additions & 11 deletions data/ui/player_bar.blp
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,6 @@ template $CassettePlayerBar : Adw.Bin {
tooltip-text: _("Change shuffle mode");
}

Button flow_settings_button {
styles [
"flat",
"circular"
]

valign: center;
icon-name: "io.github.Rirusha.Cassette-symbolic";
tooltip-text: _("Show wave settings");
}

Button prev_track_button {
styles [
"circular",
Expand Down Expand Up @@ -218,6 +207,17 @@ template $CassettePlayerBar : Adw.Bin {
content-type: "track";
}

Button flow_settings_button {
styles [
"flat",
"circular"
]

valign: center;
icon-name: "io.github.Rirusha.Cassette-symbolic";
tooltip-text: _("Show wave settings");
}

Button queue_show_button {
styles [
"flat"
Expand Down
19 changes: 17 additions & 2 deletions src/client/api/yam_client.vala
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,23 @@ namespace Cassette.Client.YaMAPI {
/**
* TODO: Placeholder
*/
public void rotor_session_new () throws ClientError, BadStatusCodeError {

public StationTracks rotor_session_new (
SessionNew session_new
) throws ClientError, BadStatusCodeError {
PostContent post_content = {
PostContentType.JSON,
Jsoner.serialize (session_new, Case.CAMEL)
};

Bytes bytes = soup_wrapper.post_sync (
@"$(YAM_BASE_URL)/rotor/session/new",
{"default"},
post_content
);

var jsoner = Jsoner.from_bytes (bytes, {"result"}, Case.CAMEL);

return (StationTracks) jsoner.deserialize_object (typeof (StationTracks));
}

/**
Expand Down
77 changes: 67 additions & 10 deletions src/client/player/player.vala
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,53 @@ namespace Cassette.Client.Player {
Logger.debug ("Context descriprion: %s".printf (context_description));
}

protected void queue_post_action () {
queue_changed (
queue,
context_type,
context_id,
current_index,
context_description
);

near_changed (get_current_track_info ());
}

public abstract int get_prev_index ();

/**
* Asynchronous getting previous track info.
*
* @return track information object
*/
public abstract async YaMAPI.Track? get_prev_track_info_async ();
public YaMAPI.Track? get_prev_track_info () {
var index = get_prev_index ();

if (queue.size > index) {
return queue[index];

} else {
return null;
}
}

/**
* Getting current track info.
*
* @return track information object
*/
public abstract YaMAPI.Track? get_current_track_info ();
public YaMAPI.Track? get_current_track_info () {
if (current_index != -1) {
if (current_index >= queue.size) {
current_index = 0;
Logger.warning (_("Problems with queue"));
}

return queue[current_index];
} else {
return null;
}
}

/**
* Asynchronous getting next track info.
Expand All @@ -169,7 +203,11 @@ namespace Cassette.Client.Player {
/**
* Change current track to previous in queue.
*/
public abstract void prev ();
public void prev () {
current_index = get_prev_index ();

queue_post_action ();
}

/**
* Try to find track and play it.
Expand Down Expand Up @@ -494,9 +532,9 @@ namespace Cassette.Client.Player {
playbin.seek_simple (Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, ms * Gst.MSECOND);
}

public async YaMAPI.Track? get_prev_track_info_async () {
public YaMAPI.Track? get_prev_track_info () {
if (player_mode != null) {
return yield player_mode.get_prev_track_info_async ();
return player_mode.get_prev_track_info ();
} else {
return null;
}
Expand All @@ -516,15 +554,25 @@ namespace Cassette.Client.Player {

public void start_flow (
string station_id,
ArrayList<YaMAPI.Track>? queue = null
ArrayList<YaMAPI.Track> queue = new ArrayList<YaMAPI.Track> ()
) {
stop ();

player_mode = new PlayerFlow (
var player_flow = new PlayerFlow (
this,
station_id,
queue
);

player_mode = player_flow;

current_track_start_loading ();

player_flow.init_async.begin ((obj, res) => {
if (player_flow.init_async.end (res)) {
start_current_track.begin ();
}
});
}

void set_track_list_queue (
Expand Down Expand Up @@ -638,13 +686,22 @@ namespace Cassette.Client.Player {
public void prev () {
if (playback_pos_sec > 3.0) {
seek (0);

} else {
stop ();

var index = player_mode.current_index;

player_mode.prev ();
start_current_track.begin (() => {
prev_done ();
});

if (index == player_mode.current_index) {
seek (0);

} else {
start_current_track.begin (() => {
prev_done ();
});
}
}
}

Expand Down
62 changes: 52 additions & 10 deletions src/client/player/player_flow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
*/

using Gee;
using Cassette.Client.YaMAPI;

namespace Cassette.Client.Player {

public class PlayerFlow : PlayerMode {

public string station_id { get; construct; }

string radio_session_id;

public PlayerFlow (
Player player,
string station_id,
Expand All @@ -35,23 +38,62 @@ namespace Cassette.Client.Player {
);
}

public async override YaMAPI.Track? get_prev_track_info_async () {
assert_not_reached ();
}
public override YaMAPI.Track? get_current_track_info () {
assert_not_reached ();
public async bool init_async () {
Rotor.StationTracks? station_tracks = null;

threader.add (() => {
station_tracks = yam_talker.start_new_session (station_id);

Idle.add (init_async.callback);
});

yield;

if (station_tracks != null) {
radio_session_id = station_tracks.radio_session_id;

queue.add (station_tracks.sequence[0].track);

current_index = 0;

queue_post_action ();

return true;

} else {
return false;
}
}

public async override YaMAPI.Track? get_next_track_info_async () {
assert_not_reached ();
return null;
}

public override YaMAPI.Play form_play_obj () {
assert_not_reached ();
var current_track = get_current_track_info ();

return new YaMAPI.Play () {
track_length_seconds = ((double) current_track.duration_ms) / 1000.0,
track_id = current_track.id,
album_id = current_track.albums.size > 0 ? current_track.albums[0].id : null,
context = context_type,
context_item = context_id,
radio_session_id = radio_session_id
};
}

public override void next (bool consider_repeat_mode) {
assert_not_reached ();
return;
}
public override void prev () {
assert_not_reached ();

public override int get_prev_index () {
int index = current_index;

if (index - 1 != -1) {
index--;
}

return index;
}
}
}
49 changes: 6 additions & 43 deletions src/client/player/player_track_list.vala
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,6 @@ namespace Cassette.Client.Player {
queue_post_action ();
}

void queue_post_action () {
queue_changed (
queue,
context_type,
context_id,
current_index,
context_description
);
near_changed (get_current_track_info ());
}

public void shuffle () {
var type_utils = new TypeUtils<Track> ();

Expand Down Expand Up @@ -182,30 +171,12 @@ namespace Cassette.Client.Player {
queue_post_action ();
}

public override async override YaMAPI.Track? get_prev_track_info_async () {
if (queue.size > get_prev_index ()) {
return queue[get_prev_index ()];
} else {
return null;
}
}
public override async override YaMAPI.Track? get_next_track_info_async () {
var index = get_next_index (true);

public override YaMAPI.Track? get_current_track_info () {
if (current_index != -1) {
if (current_index >= queue.size) {
current_index = 0;
Logger.warning (_("Problems with queue"));
}
if (queue.size > index) {
return queue[index];

return queue[current_index];
} else {
return null;
}
}

public override async override YaMAPI.Track? get_next_track_info_async () {
if (queue.size > get_next_index (true)) {
return queue[get_next_index (true)];
} else {
return null;
}
Expand Down Expand Up @@ -251,19 +222,11 @@ namespace Cassette.Client.Player {
return index;
}

public override void prev () {
current_index = get_prev_index ();

queue_post_action ();
}

public int get_prev_index () {
public override int get_prev_index () {
int index = current_index;

if (index - 1 == -1) {
if (player.repeat_mode == RepeatMode.REPEAT_ONE || player.repeat_mode == RepeatMode.OFF) {
player.seek (0);
} else {
if (player.repeat_mode == RepeatMode.REPEAT_ALL) {
index = queue.size - 1;
}

Expand Down
15 changes: 15 additions & 0 deletions src/client/talkers/yam_talker.vala
Original file line number Diff line number Diff line change
Expand Up @@ -521,5 +521,20 @@ namespace Cassette.Client {

return track_list;
}

public Rotor.StationTracks? start_new_session (
string station_id
) {
Rotor.StationTracks? station_tracks = null;

net_run_wout_code (() => {
var ses_new = new Rotor.SessionNew ();
ses_new.seeds.add (station_id);

station_tracks = client.rotor_session_new (ses_new);
});

return station_tracks;
}
}
}
4 changes: 4 additions & 0 deletions src/widgets/action_cards/station.vala
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,9 @@ public class Cassette.ActionCardStation : ActionCardCustom {
construct {
content_label.label = station_info.name;
content_image.icon_name = station_info.icon.get_internal_icon_name (station_info.id.normal);

clicked.connect (() => {
player.start_flow (station_info.id.normal);
});
}
}
Loading

0 comments on commit e685fe9

Please sign in to comment.