2021-01-31 20:03:20 +01:00
|
|
|
use super::RecordingScreen;
|
|
|
|
|
use crate::backend::Backend;
|
|
|
|
|
use crate::database::{Ensemble, Recording};
|
2020-12-02 15:41:19 +01:00
|
|
|
use crate::editors::EnsembleEditor;
|
2021-02-03 16:22:18 +01:00
|
|
|
use crate::navigator::{NavigatorWindow, NavigationHandle, Screen};
|
|
|
|
|
use crate::widgets;
|
|
|
|
|
use crate::widgets::{List, Section, Widget};
|
2021-01-31 20:03:20 +01:00
|
|
|
use gettextrs::gettext;
|
2020-10-16 21:24:55 +02:00
|
|
|
use glib::clone;
|
|
|
|
|
use gtk::prelude::*;
|
2021-01-29 15:17:27 +01:00
|
|
|
use libadwaita::prelude::*;
|
2020-10-16 21:24:55 +02:00
|
|
|
use std::cell::RefCell;
|
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
2021-01-31 20:03:20 +01:00
|
|
|
/// A screen for showing recordings with a ensemble.
|
2020-10-16 21:24:55 +02:00
|
|
|
pub struct EnsembleScreen {
|
2021-02-03 16:22:18 +01:00
|
|
|
handle: NavigationHandle<()>,
|
2020-11-17 16:48:21 +01:00
|
|
|
ensemble: Ensemble,
|
2021-02-03 16:22:18 +01:00
|
|
|
widget: widgets::Screen,
|
2021-01-25 14:00:57 +01:00
|
|
|
recording_list: Rc<List>,
|
|
|
|
|
recordings: RefCell<Vec<Recording>>,
|
2020-10-16 21:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-03 16:22:18 +01:00
|
|
|
impl Screen<Ensemble, ()> for EnsembleScreen {
|
2021-01-31 20:03:20 +01:00
|
|
|
/// Create a new ensemble screen for the specified ensemble and load the
|
|
|
|
|
/// contents asynchronously.
|
2021-02-03 16:22:18 +01:00
|
|
|
fn new(ensemble: Ensemble, handle: NavigationHandle<()>) -> Rc<Self> {
|
|
|
|
|
let widget = widgets::Screen::new();
|
2021-01-31 20:03:20 +01:00
|
|
|
widget.set_title(&ensemble.name);
|
2020-10-22 15:59:55 +02:00
|
|
|
|
2021-01-25 14:00:57 +01:00
|
|
|
let recording_list = List::new();
|
2020-10-16 21:24:55 +02:00
|
|
|
|
2021-01-25 14:00:57 +01:00
|
|
|
let this = Rc::new(Self {
|
2021-02-03 16:22:18 +01:00
|
|
|
handle,
|
2020-11-17 16:48:21 +01:00
|
|
|
ensemble,
|
2020-10-16 21:24:55 +02:00
|
|
|
widget,
|
|
|
|
|
recording_list,
|
2021-01-25 14:00:57 +01:00
|
|
|
recordings: RefCell::new(Vec::new()),
|
2020-10-16 21:24:55 +02:00
|
|
|
});
|
|
|
|
|
|
2021-02-03 16:22:18 +01:00
|
|
|
this.widget.set_back_cb(clone!(@weak this => move || {
|
|
|
|
|
this.handle.pop(None);
|
2020-10-16 21:24:55 +02:00
|
|
|
}));
|
|
|
|
|
|
2021-01-31 20:03:20 +01:00
|
|
|
|
2021-02-03 16:22:18 +01:00
|
|
|
this.widget.add_action(&gettext("Edit ensemble"), clone!(@weak this => move || {
|
2021-02-03 14:09:17 +01:00
|
|
|
spawn!(@clone this, async move {
|
2021-02-03 16:22:18 +01:00
|
|
|
let window = NavigatorWindow::new(this.handle.backend.clone());
|
|
|
|
|
replace!(window.navigator, EnsembleEditor, Some(this.ensemble.clone())).await;
|
2021-02-03 14:09:17 +01:00
|
|
|
});
|
2021-01-31 20:03:20 +01:00
|
|
|
}));
|
|
|
|
|
|
2021-02-03 16:22:18 +01:00
|
|
|
this.widget.add_action(&gettext("Delete ensemble"), clone!(@weak this => move || {
|
|
|
|
|
spawn!(@clone this, async move {
|
|
|
|
|
this.handle.backend.db().delete_ensemble(&this.ensemble.id).await.unwrap();
|
|
|
|
|
this.handle.backend.library_changed();
|
2021-01-31 20:03:20 +01:00
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
2021-02-03 16:22:18 +01:00
|
|
|
this.widget.set_search_cb(clone!(@weak this => move || {
|
2021-01-31 20:03:20 +01:00
|
|
|
this.recording_list.invalidate_filter();
|
|
|
|
|
}));
|
|
|
|
|
|
2021-02-03 16:22:18 +01:00
|
|
|
this.recording_list.set_make_widget_cb(clone!(@weak this => move |index| {
|
2021-01-25 14:00:57 +01:00
|
|
|
let recording = &this.recordings.borrow()[index];
|
|
|
|
|
|
2021-01-29 15:17:27 +01:00
|
|
|
let row = libadwaita::ActionRow::new();
|
2021-01-25 14:00:57 +01:00
|
|
|
row.set_activatable(true);
|
|
|
|
|
row.set_title(Some(&recording.work.get_title()));
|
|
|
|
|
row.set_subtitle(Some(&recording.get_performers()));
|
|
|
|
|
|
|
|
|
|
let recording = recording.to_owned();
|
2021-02-03 16:22:18 +01:00
|
|
|
row.connect_activated(clone!(@weak this => move |_| {
|
|
|
|
|
let recording = recording.clone();
|
|
|
|
|
spawn!(@clone this, async move {
|
|
|
|
|
push!(this.handle, RecordingScreen, recording.clone()).await;
|
|
|
|
|
});
|
2020-10-17 11:31:43 +02:00
|
|
|
}));
|
|
|
|
|
|
2021-01-25 14:00:57 +01:00
|
|
|
row.upcast()
|
|
|
|
|
}));
|
|
|
|
|
|
2021-02-03 16:22:18 +01:00
|
|
|
this.recording_list.set_filter_cb(clone!(@weak this => move |index| {
|
2021-01-25 14:00:57 +01:00
|
|
|
let recording = &this.recordings.borrow()[index];
|
2021-01-31 20:03:20 +01:00
|
|
|
let search = this.widget.get_search();
|
2021-01-25 14:00:57 +01:00
|
|
|
let text = recording.work.get_title() + &recording.get_performers();
|
|
|
|
|
search.is_empty() || text.to_lowercase().contains(&search)
|
|
|
|
|
}));
|
|
|
|
|
|
2021-01-31 20:03:20 +01:00
|
|
|
// Load the content asynchronously.
|
2020-11-17 16:48:21 +01:00
|
|
|
|
2021-02-03 16:22:18 +01:00
|
|
|
spawn!(@clone this, async move {
|
|
|
|
|
let recordings = this.handle
|
2020-10-17 11:31:43 +02:00
|
|
|
.backend
|
2020-11-17 15:52:47 +01:00
|
|
|
.db()
|
2021-02-03 16:22:18 +01:00
|
|
|
.get_recordings_for_ensemble(&this.ensemble.id)
|
2020-10-16 21:24:55 +02:00
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2021-01-31 20:03:20 +01:00
|
|
|
if !recordings.is_empty() {
|
2021-01-25 14:00:57 +01:00
|
|
|
let length = recordings.len();
|
2021-02-03 16:22:18 +01:00
|
|
|
this.recordings.replace(recordings);
|
|
|
|
|
this.recording_list.update(length);
|
2021-01-31 20:03:20 +01:00
|
|
|
|
2021-02-03 16:22:18 +01:00
|
|
|
let section = Section::new("Recordings", &this.recording_list.widget);
|
|
|
|
|
this.widget.add_content(§ion.widget);
|
2020-10-16 21:24:55 +02:00
|
|
|
}
|
2021-01-31 20:03:20 +01:00
|
|
|
|
2021-02-03 16:22:18 +01:00
|
|
|
this.widget.ready();
|
2020-10-16 21:24:55 +02:00
|
|
|
});
|
|
|
|
|
|
2021-01-25 14:00:57 +01:00
|
|
|
this
|
2020-10-16 21:24:55 +02:00
|
|
|
}
|
2020-10-17 11:31:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-03 16:22:18 +01:00
|
|
|
impl Widget for EnsembleScreen {
|
2020-10-17 11:31:43 +02:00
|
|
|
fn get_widget(&self) -> gtk::Widget {
|
2021-01-31 20:03:20 +01:00
|
|
|
self.widget.widget.clone().upcast()
|
2020-10-16 21:24:55 +02:00
|
|
|
}
|
|
|
|
|
}
|