musicus/src/screens/person.rs

185 lines
6 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;
use crate::widgets::{List, Navigator, NavigatorScreen, NavigatorWindow, Screen, Section};
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 {
backend: Rc<Backend>,
2020-11-17 16:48:21 +01:00
person: Person,
widget: 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>>,
navigator: RefCell<Option<Rc<Navigator>>>,
2020-10-16 21:24:55 +02:00
}
impl PersonScreen {
/// Create a new person screen for the specified person and load the
/// contents asynchronously.
2020-12-02 15:54:07 +01:00
pub fn new(backend: Rc<Backend>, person: Person) -> Rc<Self> {
let widget = 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 {
backend,
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()),
navigator: RefCell::new(None),
2020-10-16 21:24:55 +02:00
});
this.widget.set_back_cb(clone!(@strong this => move || {
2021-01-25 14:00:57 +01:00
let navigator = this.navigator.borrow().clone();
if let Some(navigator) = navigator {
navigator.pop();
2020-10-16 21:24:55 +02:00
}
}));
this.widget.add_action(&gettext("Edit person"), clone!(@strong this => move || {
let editor = PersonEditor::new(this.backend.clone(), Some(this.person.clone()));
let window = NavigatorWindow::new(editor);
window.show();
}));
this.widget.add_action(&gettext("Delete person"), clone!(@strong this => move || {
let context = glib::MainContext::default();
let clone = this.clone();
context.spawn_local(async move {
clone.backend.db().delete_person(&clone.person.id).await.unwrap();
clone.backend.library_changed();
});
}));
this.widget.set_search_cb(clone!(@strong this => move || {
this.work_list.invalidate_filter();
this.recording_list.invalidate_filter();
}));
2021-01-25 14:00:57 +01:00
this.work_list.set_make_widget_cb(clone!(@strong this => move |index| {
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();
row.connect_activated(clone!(@strong this => move |_| {
let navigator = this.navigator.borrow().clone();
if let Some(navigator) = navigator {
2021-01-25 14:00:57 +01:00
navigator.push(WorkScreen::new(this.backend.clone(), work.clone()));
}
}));
2021-01-25 14:00:57 +01:00
row.upcast()
}));
this.work_list.set_filter_cb(clone!(@strong this => move |index| {
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)
}));
this.recording_list.set_make_widget_cb(clone!(@strong this => move |index| {
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();
row.connect_activated(clone!(@strong this => move |_| {
let navigator = this.navigator.borrow().clone();
if let Some(navigator) = navigator {
2021-01-25 14:00:57 +01:00
navigator.push(RecordingScreen::new(this.backend.clone(), recording.clone()));
}
}));
2021-01-25 14:00:57 +01:00
row.upcast()
}));
this.recording_list.set_filter_cb(clone!(@strong this => move |index| {
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
2020-10-16 21:24:55 +02:00
let context = glib::MainContext::default();
let clone = Rc::clone(&this);
2020-10-16 21:24:55 +02:00
context.spawn_local(async move {
let works = clone
.backend
2020-11-17 15:52:47 +01:00
.db()
2020-11-28 21:45:22 +01:00
.get_works(&clone.person.id)
.await
.unwrap();
2021-01-25 14:00:57 +01:00
let recordings = clone
.backend
2020-11-17 15:52:47 +01:00
.db()
2020-11-28 21:45:22 +01:00
.get_recordings_for_person(&clone.person.id)
.await
.unwrap();
2020-10-16 21:24:55 +02:00
if !works.is_empty() {
let length = works.len();
clone.works.replace(works);
clone.work_list.update(length);
2020-10-16 21:24:55 +02:00
let section = Section::new("Works", &clone.work_list.widget);
clone.widget.add_content(&section.widget);
}
2020-10-16 21:24:55 +02:00
if !recordings.is_empty() {
let length = recordings.len();
clone.recordings.replace(recordings);
clone.recording_list.update(length);
let section = Section::new("Recordings", &clone.recording_list.widget);
clone.widget.add_content(&section.widget);
2020-10-16 21:24:55 +02:00
}
clone.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
impl NavigatorScreen for PersonScreen {
fn attach_navigator(&self, navigator: Rc<Navigator>) {
self.navigator.replace(Some(navigator));
2020-10-16 21:24:55 +02:00
}
fn get_widget(&self) -> gtk::Widget {
self.widget.widget.clone().upcast()
2020-10-16 21:24:55 +02:00
}
fn detach_navigator(&self) {
self.navigator.replace(None);
2020-10-16 21:24:55 +02:00
}
}