Skip to content

Commit d17f7a2

Browse files
committed
gtk: connect playlist api calls to playlist edit dialogs
1 parent bc3edc7 commit d17f7a2

14 files changed

+409
-14
lines changed

gtk/data/gtk/window.blp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ template $RbApplicationWindow : Adw.ApplicationWindow {
419419
Adw.ViewStackPage playlist_details_page {
420420
name: "playlist-details-page";
421421
title: _("Playlist");
422-
child: ScrolledWindow {
422+
child: ScrolledWindow playlist_details_scrolled_window {
423423
$PlaylistDetails playlist_details {}
424424
};
425425
}

gtk/src/state.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ mod imp {
2929
pub artists: RefCell<Vec<RockboxArtist>>,
3030
pub current_playlist_folder: RefCell<Option<String>>,
3131
pub parent_playlist_folder: RefCell<Option<String>>,
32+
pub selected_playlist: RefCell<Option<String>>,
33+
pub selected_playlist_folder: RefCell<Option<String>>,
3234
}
3335

3436
#[glib::object_subclass]
@@ -65,6 +67,8 @@ impl AppState {
6567
obj.imp().tracks.replace(Vec::new());
6668
obj.imp().current_playlist_folder.replace(None);
6769
obj.imp().parent_playlist_folder.replace(None);
70+
obj.imp().selected_playlist.replace(None);
71+
obj.imp().selected_playlist_folder.replace(None);
6872
obj
6973
}
7074

@@ -124,6 +128,16 @@ impl AppState {
124128
self_.parent_playlist_folder.borrow().clone()
125129
}
126130

131+
pub fn selected_playlist_folder(&self) -> Option<String> {
132+
let self_ = imp::AppState::from_obj(self);
133+
self_.selected_playlist_folder.borrow().clone()
134+
}
135+
136+
pub fn selected_playlist(&self) -> Option<String> {
137+
let self_ = imp::AppState::from_obj(self);
138+
self_.selected_playlist.borrow().clone()
139+
}
140+
127141
pub fn music_directory(&self) -> Option<String> {
128142
let self_ = imp::AppState::from_obj(self);
129143
self_.music_directory.borrow().clone()
@@ -144,6 +158,16 @@ impl AppState {
144158
*self_.parent_playlist_folder.borrow_mut() = folder_id;
145159
}
146160

161+
pub fn set_selected_playlist(&self, playlist_id: Option<String>) {
162+
let self_ = imp::AppState::from_obj(self);
163+
*self_.selected_playlist.borrow_mut() = playlist_id;
164+
}
165+
166+
pub fn set_selected_playlist_folder(&self, folder_id: Option<String>) {
167+
let self_ = imp::AppState::from_obj(self);
168+
*self_.selected_playlist_folder.borrow_mut() = folder_id;
169+
}
170+
147171
pub fn set_music_directory(&self, path: &str) {
148172
let self_ = imp::AppState::from_obj(self);
149173
*self_.music_directory.borrow_mut() = Some(path.to_string());

gtk/src/ui/delete_playlist.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
use crate::api::rockbox::v1alpha1::playlist_service_client::PlaylistServiceClient;
2+
use crate::api::rockbox::v1alpha1::RemovePlaylistRequest;
3+
use crate::state::AppState;
14
use adw::prelude::*;
25
use adw::subclass::prelude::*;
6+
use anyhow::Error;
37
use glib::subclass;
48
use gtk::{glib, CompositeTemplate};
59
use std::env;
10+
use std::thread;
611

712
mod imp {
813
use super::*;
914

1015
#[derive(Debug, Default, CompositeTemplate)]
1116
#[template(resource = "/io/github/tsirysndr/Rockbox/gtk/delete_playlist.ui")]
12-
pub struct DeletePlaylistDialog {}
17+
pub struct DeletePlaylistDialog {
18+
pub state: glib::WeakRef<AppState>,
19+
}
1320

1421
#[glib::object_subclass]
1522
impl ObjectSubclass for DeletePlaylistDialog {
@@ -24,6 +31,7 @@ mod imp {
2431
"app.delete_playlist_dialog.delete",
2532
None,
2633
move |dialog, _action, _target| {
34+
dialog.delete_playlist();
2735
dialog.close();
2836
},
2937
);
@@ -56,6 +64,23 @@ impl Default for DeletePlaylistDialog {
5664
}
5765
}
5866

67+
#[gtk::template_callbacks]
68+
impl DeletePlaylistDialog {
69+
fn delete_playlist(&self) {
70+
let state = self.imp().state.upgrade().unwrap();
71+
let id = state.selected_playlist().unwrap();
72+
thread::spawn(move || {
73+
let rt = tokio::runtime::Runtime::new().unwrap();
74+
let url = build_url();
75+
let result = rt.block_on(async {
76+
let mut client = PlaylistServiceClient::connect(url).await?;
77+
client.remove_playlist(RemovePlaylistRequest { id }).await?;
78+
Ok::<_, Error>(())
79+
});
80+
});
81+
}
82+
}
83+
5984
fn build_url() -> String {
6085
let host = env::var("ROCKBOX_HOST").unwrap_or_else(|_| "localhost".to_string());
6186
let port = env::var("ROCKBOX_PORT").unwrap_or_else(|_| "6061".to_string());

gtk/src/ui/delete_playlist_folder.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
use crate::api::rockbox::v1alpha1::playlist_service_client::PlaylistServiceClient;
2+
use crate::api::rockbox::v1alpha1::RemoveFolderRequest;
3+
use crate::state::AppState;
14
use adw::prelude::*;
25
use adw::subclass::prelude::*;
6+
use anyhow::Error;
37
use glib::subclass;
48
use gtk::{glib, CompositeTemplate};
59
use std::env;
10+
use std::thread;
611

712
mod imp {
813

914
use super::*;
1015

1116
#[derive(Debug, Default, CompositeTemplate)]
1217
#[template(resource = "/io/github/tsirysndr/Rockbox/gtk/delete_playlist_folder.ui")]
13-
pub struct DeletePlaylistFolderDialog {}
18+
pub struct DeletePlaylistFolderDialog {
19+
pub state: glib::WeakRef<AppState>,
20+
}
1421

1522
#[glib::object_subclass]
1623
impl ObjectSubclass for DeletePlaylistFolderDialog {
@@ -25,6 +32,7 @@ mod imp {
2532
"app.delete_playlist_folder_dialog.delete",
2633
None,
2734
move |dialog, _action, _target| {
35+
dialog.delete_folder();
2836
dialog.close();
2937
},
3038
);
@@ -57,6 +65,23 @@ impl Default for DeletePlaylistFolderDialog {
5765
}
5866
}
5967

68+
#[gtk::template_callbacks]
69+
impl DeletePlaylistFolderDialog {
70+
fn delete_folder(&self) {
71+
let state = self.imp().state.upgrade().unwrap();
72+
let id = state.selected_playlist_folder().unwrap();
73+
thread::spawn(move || {
74+
let rt = tokio::runtime::Runtime::new().unwrap();
75+
let url = build_url();
76+
let result = rt.block_on(async {
77+
let mut client = PlaylistServiceClient::connect(url).await?;
78+
client.remove_folder(RemoveFolderRequest { id }).await?;
79+
Ok::<_, Error>(())
80+
});
81+
});
82+
}
83+
}
84+
6085
fn build_url() -> String {
6186
let host = env::var("ROCKBOX_HOST").unwrap_or_else(|_| "localhost".to_string());
6287
let port = env::var("ROCKBOX_PORT").unwrap_or_else(|_| "6061".to_string());

gtk/src/ui/edit_playlist.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
use crate::api::rockbox::v1alpha1::playlist_service_client::PlaylistServiceClient;
2+
use crate::api::rockbox::v1alpha1::RenamePlaylistRequest;
3+
use crate::state::AppState;
14
use adw::prelude::*;
25
use adw::subclass::prelude::*;
6+
use anyhow::Error;
37
use glib::subclass;
48
use gtk::{glib, CompositeTemplate};
59
use std::env;
10+
use std::thread;
611

712
mod imp {
813

914
use super::*;
1015

1116
#[derive(Debug, Default, CompositeTemplate)]
1217
#[template(resource = "/io/github/tsirysndr/Rockbox/gtk/edit_playlist.ui")]
13-
pub struct EditPlaylistDialog {}
18+
pub struct EditPlaylistDialog {
19+
pub state: glib::WeakRef<AppState>,
20+
}
1421

1522
#[glib::object_subclass]
1623
impl ObjectSubclass for EditPlaylistDialog {
@@ -25,6 +32,7 @@ mod imp {
2532
"app.edit_playlist_dialog.save",
2633
None,
2734
move |dialog, _action, _target| {
35+
dialog.rename_playlist();
2836
dialog.close();
2937
},
3038
);
@@ -57,6 +65,27 @@ impl Default for EditPlaylistDialog {
5765
}
5866
}
5967

68+
#[gtk::template_callbacks]
69+
impl EditPlaylistDialog {
70+
fn rename_playlist(&self) {
71+
let state = self.imp().state.upgrade().unwrap();
72+
let id = state.selected_playlist().unwrap();
73+
let name = String::from("");
74+
75+
thread::spawn(move || {
76+
let rt = tokio::runtime::Runtime::new().unwrap();
77+
let url = build_url();
78+
let result = rt.block_on(async {
79+
let mut client = PlaylistServiceClient::connect(url).await?;
80+
client
81+
.rename_playlist(RenamePlaylistRequest { id, name })
82+
.await?;
83+
Ok::<_, Error>(())
84+
});
85+
});
86+
}
87+
}
88+
6089
fn build_url() -> String {
6190
let host = env::var("ROCKBOX_HOST").unwrap_or_else(|_| "localhost".to_string());
6291
let port = env::var("ROCKBOX_PORT").unwrap_or_else(|_| "6061".to_string());

gtk/src/ui/edit_playlist_folder.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
use crate::api::rockbox::v1alpha1::playlist_service_client::PlaylistServiceClient;
2+
use crate::api::rockbox::v1alpha1::RenameFolderRequest;
3+
use crate::state::AppState;
4+
use adw::prelude::*;
15
use adw::subclass::prelude::*;
6+
use anyhow::Error;
27
use glib::subclass;
38
use gtk::{glib, CompositeTemplate};
9+
use std::cell::RefCell;
410
use std::env;
5-
use adw::prelude::*;
11+
use std::thread;
612

713
mod imp {
8-
914
use super::*;
1015

1116
#[derive(Debug, Default, CompositeTemplate)]
1217
#[template(resource = "/io/github/tsirysndr/Rockbox/gtk/edit_playlist_folder.ui")]
13-
pub struct EditPlaylistFolderDialog {}
18+
pub struct EditPlaylistFolderDialog {
19+
pub state: glib::WeakRef<AppState>,
20+
}
1421

1522
#[glib::object_subclass]
1623
impl ObjectSubclass for EditPlaylistFolderDialog {
@@ -25,6 +32,7 @@ mod imp {
2532
"app.edit_playlist_folder_dialog.save",
2633
None,
2734
move |dialog, _action, _target| {
35+
dialog.rename_folder();
2836
dialog.close();
2937
},
3038
);
@@ -57,6 +65,27 @@ impl Default for EditPlaylistFolderDialog {
5765
}
5866
}
5967

68+
#[gtk::template_callbacks]
69+
impl EditPlaylistFolderDialog {
70+
fn rename_folder(&self) {
71+
let state = self.imp().state.upgrade().unwrap();
72+
let name = String::from("");
73+
let id = state.selected_playlist_folder().unwrap();
74+
75+
thread::spawn(move || {
76+
let rt = tokio::runtime::Runtime::new().unwrap();
77+
let url = build_url();
78+
let result = rt.block_on(async {
79+
let mut client = PlaylistServiceClient::connect(url).await?;
80+
client
81+
.rename_folder(RenameFolderRequest { id, name })
82+
.await?;
83+
Ok::<_, Error>(())
84+
});
85+
});
86+
}
87+
}
88+
6089
fn build_url() -> String {
6190
let host = env::var("ROCKBOX_HOST").unwrap_or_else(|_| "localhost".to_string());
6291
let port = env::var("ROCKBOX_PORT").unwrap_or_else(|_| "6061".to_string());

gtk/src/ui/new_playlist.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1+
use crate::api::rockbox::v1alpha1::playlist_service_client::PlaylistServiceClient;
2+
use crate::api::rockbox::v1alpha1::CreatePlaylistRequest;
3+
use crate::state::AppState;
4+
use adw::prelude::*;
15
use adw::subclass::prelude::*;
6+
use anyhow::Error;
27
use glib::subclass;
38
use gtk::{glib, CompositeTemplate};
9+
use std::cell::RefCell;
410
use std::env;
5-
use adw::prelude::*;
11+
use std::thread;
612

713
mod imp {
8-
914
use super::*;
1015

1116
#[derive(Debug, Default, CompositeTemplate)]
1217
#[template(resource = "/io/github/tsirysndr/Rockbox/gtk/new_playlist.ui")]
13-
pub struct NewPlaylistDialog {}
18+
pub struct NewPlaylistDialog {
19+
pub state: glib::WeakRef<AppState>,
20+
pub song_id: RefCell<Option<String>>,
21+
}
1422

1523
#[glib::object_subclass]
1624
impl ObjectSubclass for NewPlaylistDialog {
@@ -25,6 +33,7 @@ mod imp {
2533
"app.new_playlist_dialog.create",
2634
None,
2735
move |dialog, _action, _target| {
36+
dialog.create_playlist();
2837
dialog.close();
2938
},
3039
);
@@ -57,6 +66,34 @@ impl Default for NewPlaylistDialog {
5766
}
5867
}
5968

69+
#[gtk::template_callbacks]
70+
impl NewPlaylistDialog {
71+
fn create_playlist(&self) {
72+
let song_id = self.imp().song_id.borrow();
73+
let song_id = song_id.as_ref();
74+
let state = self.imp().state.upgrade().unwrap();
75+
let folder_id = state.selected_playlist_folder();
76+
let name = String::from("");
77+
78+
thread::spawn(move || {
79+
let rt = tokio::runtime::Runtime::new().unwrap();
80+
let url = build_url();
81+
let result = rt.block_on(async {
82+
let mut client = PlaylistServiceClient::connect(url).await?;
83+
/*
84+
* client
85+
.create_playlist(CreatePlaylistRequest {
86+
name,
87+
tracks: vec![],
88+
})
89+
.await?;
90+
*/
91+
Ok::<_, Error>(())
92+
});
93+
});
94+
}
95+
}
96+
6097
fn build_url() -> String {
6198
let host = env::var("ROCKBOX_HOST").unwrap_or_else(|_| "localhost".to_string());
6299
let port = env::var("ROCKBOX_PORT").unwrap_or_else(|_| "6061".to_string());

0 commit comments

Comments
 (0)