musicus/src/screens/person.rs

168 lines
5.5 KiB
Rust
Raw Normal View History

use super::{WorkScreen, RecordingScreen};
use crate::backend::Backend;
use crate::database::{Person, Recording, Work};
2020-12-02 15:41:19 +01:00
use crate::editors::PersonEditor;
2021-02-03 16:22:18 +01:00
use crate::navigator::{NavigatorWindow, NavigationHandle, Screen};
use crate::widgets;
use crate::widgets::{List, Section, Widget};
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;
/// A screen for showing works by and recordings with a person.
2020-10-16 21:24:55 +02:00
pub struct PersonScreen {
2021-02-03 16:22:18 +01:00
handle: NavigationHandle<()>,
2020-11-17 16:48:21 +01:00
person: Person,
2021-02-03 16:22:18 +01:00
widget: widgets::Screen,
2021-01-25 14:00:57 +01:00
work_list: Rc<List>,
recording_list: Rc<List>,
works: RefCell<Vec<Work>>,
recordings: RefCell<Vec<Recording>>,
2020-10-16 21:24:55 +02:00
}
2021-02-03 16:22:18 +01:00
impl Screen<Person, ()> for PersonScreen {
/// Create a new person screen for the specified person and load the
/// contents asynchronously.
2021-02-03 16:22:18 +01:00
fn new(person: Person, handle: NavigationHandle<()>) -> Rc<Self> {
let widget = widgets::Screen::new();
widget.set_title(&person.name_fl());
2021-01-25 14:00:57 +01:00
let work_list = List::new();
let recording_list = List::new();
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
person,
2020-10-16 21:24:55 +02:00
widget,
work_list,
recording_list,
2021-01-25 14:00:57 +01:00
works: RefCell::new(Vec::new()),
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-02-03 16:22:18 +01:00
this.widget.add_action(&gettext("Edit person"), 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, PersonEditor, Some(this.person.clone())).await;
2021-02-03 14:09:17 +01:00
});
}));
2021-02-03 16:22:18 +01:00
this.widget.add_action(&gettext("Delete person"), clone!(@weak this => move || {
spawn!(@clone this, async move {
this.handle.backend.db().delete_person(&this.person.id).await.unwrap();
this.handle.backend.library_changed();
});
}));
2021-02-03 16:22:18 +01:00
this.widget.set_search_cb(clone!(@weak this => move || {
this.work_list.invalidate_filter();
this.recording_list.invalidate_filter();
}));
2021-02-03 16:22:18 +01:00
this.work_list.set_make_widget_cb(clone!(@weak this => move |index| {
2021-01-25 14:00:57 +01:00
let work = &this.works.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(&work.title));
let work = work.to_owned();
2021-02-03 16:22:18 +01:00
row.connect_activated(clone!(@weak this => move |_| {
let work = work.clone();
spawn!(@clone this, async move {
push!(this.handle, WorkScreen, work.clone()).await;
});
}));
2021-01-25 14:00:57 +01:00
row.upcast()
}));
2021-02-03 16:22:18 +01:00
this.work_list.set_filter_cb(clone!(@weak this => move |index| {
2021-01-25 14:00:57 +01:00
let work = &this.works.borrow()[index];
let search = this.widget.get_search();
2021-01-25 14:00:57 +01:00
let title = work.title.to_lowercase();
search.is_empty() || title.contains(&search)
}));
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;
});
}));
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];
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-25 14:00:57 +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 works = this.handle
.backend
2020-11-17 15:52:47 +01:00
.db()
2021-02-03 16:22:18 +01:00
.get_works(&this.person.id)
.await
.unwrap();
2021-01-25 14:00:57 +01:00
2021-02-03 16:22:18 +01:00
let recordings = this.handle
.backend
2020-11-17 15:52:47 +01:00
.db()
2021-02-03 16:22:18 +01:00
.get_recordings_for_person(&this.person.id)
.await
.unwrap();
2020-10-16 21:24:55 +02:00
if !works.is_empty() {
let length = works.len();
2021-02-03 16:22:18 +01:00
this.works.replace(works);
this.work_list.update(length);
2020-10-16 21:24:55 +02:00
2021-02-03 16:22:18 +01:00
let section = Section::new("Works", &this.work_list.widget);
this.widget.add_content(&section.widget);
}
2020-10-16 21:24:55 +02:00
if !recordings.is_empty() {
let length = recordings.len();
2021-02-03 16:22:18 +01:00
this.recordings.replace(recordings);
this.recording_list.update(length);
2021-02-03 16:22:18 +01:00
let section = Section::new("Recordings", &this.recording_list.widget);
this.widget.add_content(&section.widget);
2020-10-16 21:24:55 +02: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-16 21:24:55 +02:00
2021-02-03 16:22:18 +01:00
impl Widget for PersonScreen {
fn get_widget(&self) -> gtk::Widget {
self.widget.widget.clone().upcast()
2020-10-16 21:24:55 +02:00
}
}