mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 11:47:25 +01:00
Move database access to background thread
This commit is contained in:
parent
96188929d4
commit
c2d40fe56e
10 changed files with 542 additions and 148 deletions
349
src/backend.rs
Normal file
349
src/backend.rs
Normal file
|
|
@ -0,0 +1,349 @@
|
|||
use super::database::*;
|
||||
use glib::Sender;
|
||||
|
||||
enum BackendAction {
|
||||
UpdatePerson(Person, Sender<Result<(), String>>),
|
||||
GetPerson(i64, Sender<Option<Person>>),
|
||||
DeletePerson(i64, Sender<Result<(), String>>),
|
||||
GetPersons(Sender<Vec<Person>>),
|
||||
UpdateInstrument(Instrument, Sender<Result<(), String>>),
|
||||
GetInstrument(i64, Sender<Option<Instrument>>),
|
||||
DeleteInstrument(i64, Sender<Result<(), String>>),
|
||||
GetInstruments(Sender<Vec<Instrument>>),
|
||||
UpdateWork(WorkInsertion, Sender<Result<(), String>>),
|
||||
GetWorkDescriptions(i64, Sender<Vec<WorkDescription>>),
|
||||
UpdateEnsemble(Ensemble, Sender<Result<(), String>>),
|
||||
GetEnsemble(i64, Sender<Option<Ensemble>>),
|
||||
DeleteEnsemble(i64, Sender<Result<(), String>>),
|
||||
GetEnsembles(Sender<Vec<Ensemble>>),
|
||||
}
|
||||
|
||||
use BackendAction::*;
|
||||
|
||||
pub struct Backend {
|
||||
action_sender: std::sync::mpsc::Sender<BackendAction>,
|
||||
}
|
||||
|
||||
impl Backend {
|
||||
pub fn new(url: &str) -> Self {
|
||||
let url = url.to_string();
|
||||
|
||||
let (action_sender, action_receiver) = std::sync::mpsc::channel::<BackendAction>();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let db = Database::new(&url);
|
||||
|
||||
for action in action_receiver {
|
||||
match action {
|
||||
UpdatePerson(person, sender) => {
|
||||
db.update_person(person);
|
||||
sender
|
||||
.send(Ok(()))
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
GetPerson(id, sender) => {
|
||||
let person = db.get_person(id);
|
||||
sender
|
||||
.send(person)
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
DeletePerson(id, sender) => {
|
||||
db.delete_person(id);
|
||||
sender
|
||||
.send(Ok(()))
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
GetPersons(sender) => {
|
||||
let persons = db.get_persons();
|
||||
sender
|
||||
.send(persons)
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
UpdateInstrument(instrument, sender) => {
|
||||
db.update_instrument(instrument);
|
||||
sender
|
||||
.send(Ok(()))
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
GetInstrument(id, sender) => {
|
||||
let instrument = db.get_instrument(id);
|
||||
sender
|
||||
.send(instrument)
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
DeleteInstrument(id, sender) => {
|
||||
db.delete_instrument(id);
|
||||
sender
|
||||
.send(Ok(()))
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
GetInstruments(sender) => {
|
||||
let instruments = db.get_instruments();
|
||||
sender
|
||||
.send(instruments)
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
UpdateWork(work, sender) => {
|
||||
db.update_work(work);
|
||||
sender
|
||||
.send(Ok(()))
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
GetWorkDescriptions(id, sender) => {
|
||||
let works = db.get_work_descriptions(id);
|
||||
sender
|
||||
.send(works)
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
UpdateEnsemble(ensemble, sender) => {
|
||||
db.update_ensemble(ensemble);
|
||||
sender
|
||||
.send(Ok(()))
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
GetEnsemble(id, sender) => {
|
||||
let ensemble = db.get_ensemble(id);
|
||||
sender
|
||||
.send(ensemble)
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
DeleteEnsemble(id, sender) => {
|
||||
db.delete_ensemble(id);
|
||||
sender
|
||||
.send(Ok(()))
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
GetEnsembles(sender) => {
|
||||
let ensembles = db.get_ensembles();
|
||||
sender
|
||||
.send(ensembles)
|
||||
.expect("Failed to send result from database thread!");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Backend {
|
||||
action_sender: action_sender,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_person<F: Fn(Result<(), String>) -> () + 'static>(
|
||||
&self,
|
||||
person: Person,
|
||||
callback: F,
|
||||
) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Result<(), String>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(UpdatePerson(person, sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn get_person<F: Fn(Option<Person>) -> () + 'static>(&self, id: i64, callback: F) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Option<Person>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(GetPerson(id, sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn delete_person<F: Fn(Result<(), String>) -> () + 'static>(&self, id: i64, callback: F) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Result<(), String>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(DeletePerson(id, sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn get_persons<F: Fn(Vec<Person>) -> () + 'static>(&self, callback: F) {
|
||||
let (sender, receiver) = glib::MainContext::channel::<Vec<Person>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(GetPersons(sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn update_instrument<F: Fn(Result<(), String>) -> () + 'static>(
|
||||
&self,
|
||||
instrument: Instrument,
|
||||
callback: F,
|
||||
) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Result<(), String>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(UpdateInstrument(instrument, sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn get_instrument<F: Fn(Option<Instrument>) -> () + 'static>(&self, id: i64, callback: F) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Option<Instrument>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(GetInstrument(id, sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn delete_instrument<F: Fn(Result<(), String>) -> () + 'static>(
|
||||
&self,
|
||||
id: i64,
|
||||
callback: F,
|
||||
) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Result<(), String>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(DeleteInstrument(id, sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn get_instruments<F: Fn(Vec<Instrument>) -> () + 'static>(&self, callback: F) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Vec<Instrument>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(GetInstruments(sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn update_work<F: Fn(Result<(), String>) -> () + 'static>(
|
||||
&self,
|
||||
work: WorkInsertion,
|
||||
callback: F,
|
||||
) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Result<(), String>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(UpdateWork(work, sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn get_work_descriptions<F: Fn(Vec<WorkDescription>) -> () + 'static>(
|
||||
&self,
|
||||
id: i64,
|
||||
callback: F,
|
||||
) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Vec<WorkDescription>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(GetWorkDescriptions(id, sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn update_ensemble<F: Fn(Result<(), String>) -> () + 'static>(
|
||||
&self,
|
||||
ensemble: Ensemble,
|
||||
callback: F,
|
||||
) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Result<(), String>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(UpdateEnsemble(ensemble, sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn get_ensemble<F: Fn(Option<Ensemble>) -> () + 'static>(&self, id: i64, callback: F) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Option<Ensemble>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(GetEnsemble(id, sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn delete_ensemble<F: Fn(Result<(), String>) -> () + 'static>(&self, id: i64, callback: F) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Result<(), String>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(DeleteEnsemble(id, sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
|
||||
pub fn get_ensembles<F: Fn(Vec<Ensemble>) -> () + 'static>(&self, callback: F) {
|
||||
let (sender, receiver) =
|
||||
glib::MainContext::channel::<Vec<Ensemble>>(glib::PRIORITY_DEFAULT);
|
||||
|
||||
receiver.attach(None, move |result| {
|
||||
callback(result);
|
||||
glib::Continue(true)
|
||||
});
|
||||
|
||||
self.action_sender
|
||||
.send(GetEnsembles(sender))
|
||||
.expect("Failed to send action to database thread!");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +1,26 @@
|
|||
use crate::backend::*;
|
||||
use crate::database::*;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct EnsembleEditor {
|
||||
pub struct EnsembleEditor<F>
|
||||
where
|
||||
F: Fn(Ensemble) -> () + 'static,
|
||||
{
|
||||
window: gtk::Window,
|
||||
callback: F,
|
||||
id: i64,
|
||||
name_entry: gtk::Entry,
|
||||
}
|
||||
|
||||
impl EnsembleEditor {
|
||||
pub fn new<F: Fn(Ensemble) -> () + 'static, P: IsA<gtk::Window>>(
|
||||
db: Rc<Database>,
|
||||
impl<F> EnsembleEditor<F>
|
||||
where
|
||||
F: Fn(Ensemble) -> () + 'static,
|
||||
{
|
||||
pub fn new<P: IsA<gtk::Window>>(
|
||||
backend: Rc<Backend>,
|
||||
parent: &P,
|
||||
ensemble: Option<Ensemble>,
|
||||
callback: F,
|
||||
|
|
@ -34,8 +42,9 @@ impl EnsembleEditor {
|
|||
};
|
||||
|
||||
let result = Rc::new(EnsembleEditor {
|
||||
id: id,
|
||||
window: window,
|
||||
callback: callback,
|
||||
id: id,
|
||||
name_entry: name_entry,
|
||||
});
|
||||
|
||||
|
|
@ -44,15 +53,15 @@ impl EnsembleEditor {
|
|||
}));
|
||||
|
||||
save_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
result.window.close();
|
||||
|
||||
let ensemble = Ensemble {
|
||||
id: result.id,
|
||||
name: result.name_entry.get_text().to_string(),
|
||||
};
|
||||
|
||||
db.update_ensemble(ensemble.clone());
|
||||
callback(ensemble);
|
||||
backend.update_ensemble(ensemble.clone(), clone!(@strong result => move |_| {
|
||||
result.window.close();
|
||||
(result.callback)(ensemble.clone());
|
||||
}));
|
||||
}));
|
||||
|
||||
result.window.set_transient_for(Some(parent));
|
||||
|
|
|
|||
|
|
@ -1,18 +1,26 @@
|
|||
use crate::backend::*;
|
||||
use crate::database::*;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct InstrumentEditor {
|
||||
pub struct InstrumentEditor<F>
|
||||
where
|
||||
F: Fn(Instrument) -> () + 'static,
|
||||
{
|
||||
window: gtk::Window,
|
||||
callback: F,
|
||||
id: i64,
|
||||
name_entry: gtk::Entry,
|
||||
}
|
||||
|
||||
impl InstrumentEditor {
|
||||
pub fn new<F: Fn(Instrument) -> () + 'static, P: IsA<gtk::Window>>(
|
||||
db: Rc<Database>,
|
||||
impl<F> InstrumentEditor<F>
|
||||
where
|
||||
F: Fn(Instrument) -> () + 'static,
|
||||
{
|
||||
pub fn new<P: IsA<gtk::Window>>(
|
||||
backend: Rc<Backend>,
|
||||
parent: &P,
|
||||
instrument: Option<Instrument>,
|
||||
callback: F,
|
||||
|
|
@ -34,8 +42,9 @@ impl InstrumentEditor {
|
|||
};
|
||||
|
||||
let result = Rc::new(InstrumentEditor {
|
||||
id: id,
|
||||
window: window,
|
||||
callback: callback,
|
||||
id: id,
|
||||
name_entry: name_entry,
|
||||
});
|
||||
|
||||
|
|
@ -44,15 +53,15 @@ impl InstrumentEditor {
|
|||
}));
|
||||
|
||||
save_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
result.window.close();
|
||||
|
||||
let instrument = Instrument {
|
||||
id: result.id,
|
||||
name: result.name_entry.get_text().to_string(),
|
||||
};
|
||||
|
||||
db.update_instrument(instrument.clone());
|
||||
callback(instrument);
|
||||
backend.update_instrument(instrument.clone(), clone!(@strong result => move |_| {
|
||||
result.window.close();
|
||||
(result.callback)(instrument.clone());
|
||||
}));
|
||||
}));
|
||||
|
||||
result.window.set_transient_for(Some(parent));
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
use super::selector_row::SelectorRow;
|
||||
use super::InstrumentEditor;
|
||||
use crate::backend::Backend;
|
||||
use crate::database::*;
|
||||
use gio::prelude::*;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::convert::TryInto;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
|
@ -13,10 +13,9 @@ pub struct InstrumentSelector<F>
|
|||
where
|
||||
F: Fn(Instrument) -> () + 'static,
|
||||
{
|
||||
db: Rc<Database>,
|
||||
backend: Rc<Backend>,
|
||||
window: gtk::Window,
|
||||
callback: F,
|
||||
instruments: RefCell<Vec<Instrument>>,
|
||||
list: gtk::ListBox,
|
||||
search_entry: gtk::SearchEntry,
|
||||
}
|
||||
|
|
@ -25,7 +24,7 @@ impl<F> InstrumentSelector<F>
|
|||
where
|
||||
F: Fn(Instrument) -> () + 'static,
|
||||
{
|
||||
pub fn new<P: IsA<gtk::Window>>(db: Rc<Database>, parent: &P, callback: F) -> Rc<Self> {
|
||||
pub fn new<P: IsA<gtk::Window>>(backend: Rc<Backend>, parent: &P, callback: F) -> Rc<Self> {
|
||||
let builder =
|
||||
gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/instrument_selector.ui");
|
||||
|
||||
|
|
@ -34,32 +33,32 @@ where
|
|||
get_widget!(builder, gtk::SearchEntry, search_entry);
|
||||
get_widget!(builder, gtk::ListBox, list);
|
||||
|
||||
let instruments = db.get_instruments();
|
||||
|
||||
for (index, instrument) in instruments.iter().enumerate() {
|
||||
let label = gtk::Label::new(Some(&instrument.name));
|
||||
label.set_halign(gtk::Align::Start);
|
||||
let row = SelectorRow::new(index.try_into().unwrap(), &label);
|
||||
row.show_all();
|
||||
list.insert(&row, -1);
|
||||
}
|
||||
|
||||
let result = Rc::new(InstrumentSelector {
|
||||
db: db,
|
||||
backend: backend,
|
||||
window: window,
|
||||
callback: callback,
|
||||
instruments: RefCell::new(instruments),
|
||||
search_entry: search_entry,
|
||||
list: list,
|
||||
});
|
||||
|
||||
result
|
||||
.backend
|
||||
.get_instruments(clone!(@strong result => move |instruments| {
|
||||
for (index, instrument) in instruments.iter().enumerate() {
|
||||
let label = gtk::Label::new(Some(&instrument.name));
|
||||
label.set_halign(gtk::Align::Start);
|
||||
let row = SelectorRow::new(index.try_into().unwrap(), &label);
|
||||
row.show_all();
|
||||
result.list.insert(&row, -1);
|
||||
}
|
||||
|
||||
result
|
||||
.list
|
||||
.connect_row_activated(clone!(@strong result => move |_, row| {
|
||||
.connect_row_activated(clone!(@strong result, @strong instruments => move |_, row| {
|
||||
result.window.close();
|
||||
let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap();
|
||||
let index: usize = row.get_index().try_into().unwrap();
|
||||
(result.callback)(result.instruments.borrow()[index].clone());
|
||||
(result.callback)(instruments[index].clone());
|
||||
}));
|
||||
|
||||
result
|
||||
|
|
@ -69,11 +68,12 @@ where
|
|||
let index: usize = row.get_index().try_into().unwrap();
|
||||
let search = result.search_entry.get_text().to_string();
|
||||
|
||||
search.is_empty() || result.instruments.borrow()[index]
|
||||
search.is_empty() || instruments[index]
|
||||
.name
|
||||
.to_lowercase()
|
||||
.contains(&result.search_entry.get_text().to_string().to_lowercase())
|
||||
}))));
|
||||
}));
|
||||
|
||||
result
|
||||
.search_entry
|
||||
|
|
@ -83,7 +83,7 @@ where
|
|||
|
||||
add_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
let editor = InstrumentEditor::new(
|
||||
result.db.clone(),
|
||||
result.backend.clone(),
|
||||
&result.window,
|
||||
None,
|
||||
clone!(@strong result => move |instrument| {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use super::selector_row::SelectorRow;
|
||||
use super::{InstrumentSelector, PersonSelector};
|
||||
use crate::backend::*;
|
||||
use crate::database::*;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
|
|
@ -9,7 +10,7 @@ use std::convert::TryInto;
|
|||
use std::rc::Rc;
|
||||
|
||||
pub struct PartEditor {
|
||||
db: Rc<Database>,
|
||||
backend: Rc<Backend>,
|
||||
window: gtk::Window,
|
||||
title_entry: gtk::Entry,
|
||||
composer: RefCell<Option<Person>>,
|
||||
|
|
@ -20,7 +21,7 @@ pub struct PartEditor {
|
|||
|
||||
impl PartEditor {
|
||||
pub fn new<F: Fn(WorkPartDescription) -> () + 'static, P: IsA<gtk::Window>>(
|
||||
db: Rc<Database>,
|
||||
backend: Rc<Backend>,
|
||||
parent: &P,
|
||||
part: Option<WorkPartDescription>,
|
||||
callback: F,
|
||||
|
|
@ -63,7 +64,7 @@ impl PartEditor {
|
|||
});
|
||||
|
||||
let result = Rc::new(PartEditor {
|
||||
db: db,
|
||||
backend: backend,
|
||||
window: window,
|
||||
title_entry: title_entry,
|
||||
composer: composer,
|
||||
|
|
@ -86,7 +87,7 @@ impl PartEditor {
|
|||
}));
|
||||
|
||||
composer_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
PersonSelector::new(result.db.clone(), &result.window, clone!(@strong result => move |person| {
|
||||
PersonSelector::new(result.backend.clone(), &result.window, clone!(@strong result => move |person| {
|
||||
result.composer.replace(Some(person.clone()));
|
||||
result.composer_label.set_text(&person.name_fl());
|
||||
})).show();
|
||||
|
|
@ -98,7 +99,7 @@ impl PartEditor {
|
|||
}));
|
||||
|
||||
add_instrument_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
InstrumentSelector::new(result.db.clone(), &result.window, clone!(@strong result => move |instrument| {
|
||||
InstrumentSelector::new(result.backend.clone(), &result.window, clone!(@strong result => move |instrument| {
|
||||
{
|
||||
let mut instruments = result.instruments.borrow_mut();
|
||||
instruments.push(instrument);
|
||||
|
|
|
|||
|
|
@ -1,19 +1,27 @@
|
|||
use crate::backend::Backend;
|
||||
use crate::database::*;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct PersonEditor {
|
||||
pub struct PersonEditor<F>
|
||||
where
|
||||
F: Fn(Person) -> () + 'static,
|
||||
{
|
||||
window: gtk::Window,
|
||||
callback: F,
|
||||
id: i64,
|
||||
first_name_entry: gtk::Entry,
|
||||
last_name_entry: gtk::Entry,
|
||||
}
|
||||
|
||||
impl PersonEditor {
|
||||
pub fn new<F: Fn(Person) -> () + 'static, P: IsA<gtk::Window>>(
|
||||
db: Rc<Database>,
|
||||
impl<F> PersonEditor<F>
|
||||
where
|
||||
F: Fn(Person) -> () + 'static,
|
||||
{
|
||||
pub fn new<P: IsA<gtk::Window>>(
|
||||
backend: Rc<Backend>,
|
||||
parent: &P,
|
||||
person: Option<Person>,
|
||||
callback: F,
|
||||
|
|
@ -36,8 +44,9 @@ impl PersonEditor {
|
|||
};
|
||||
|
||||
let result = Rc::new(PersonEditor {
|
||||
id: id,
|
||||
window: window,
|
||||
callback: callback,
|
||||
id: id,
|
||||
first_name_entry: first_name_entry,
|
||||
last_name_entry: last_name_entry,
|
||||
});
|
||||
|
|
@ -47,16 +56,16 @@ impl PersonEditor {
|
|||
}));
|
||||
|
||||
save_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
result.window.close();
|
||||
|
||||
let person = Person {
|
||||
id: result.id,
|
||||
first_name: result.first_name_entry.get_text().to_string(),
|
||||
last_name: result.last_name_entry.get_text().to_string(),
|
||||
};
|
||||
|
||||
db.update_person(person.clone());
|
||||
callback(person);
|
||||
backend.update_person(person.clone(), clone!(@strong result => move |_| {
|
||||
result.window.close();
|
||||
(result.callback)(person.clone());
|
||||
}));
|
||||
}));
|
||||
|
||||
result.window.set_transient_for(Some(parent));
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
use super::selector_row::SelectorRow;
|
||||
use super::PersonEditor;
|
||||
use crate::backend::Backend;
|
||||
use crate::database::*;
|
||||
use gio::prelude::*;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::convert::TryInto;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
|
@ -13,10 +13,9 @@ pub struct PersonSelector<F>
|
|||
where
|
||||
F: Fn(Person) -> () + 'static,
|
||||
{
|
||||
db: Rc<Database>,
|
||||
backend: Rc<Backend>,
|
||||
window: gtk::Window,
|
||||
callback: F,
|
||||
persons: RefCell<Vec<Person>>,
|
||||
list: gtk::ListBox,
|
||||
search_entry: gtk::SearchEntry,
|
||||
}
|
||||
|
|
@ -25,7 +24,7 @@ impl<F> PersonSelector<F>
|
|||
where
|
||||
F: Fn(Person) -> () + 'static,
|
||||
{
|
||||
pub fn new<P: IsA<gtk::Window>>(db: Rc<Database>, parent: &P, callback: F) -> Rc<Self> {
|
||||
pub fn new<P: IsA<gtk::Window>>(backend: Rc<Backend>, parent: &P, callback: F) -> Rc<Self> {
|
||||
let builder =
|
||||
gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/person_selector.ui");
|
||||
|
||||
|
|
@ -34,32 +33,32 @@ where
|
|||
get_widget!(builder, gtk::SearchEntry, search_entry);
|
||||
get_widget!(builder, gtk::ListBox, list);
|
||||
|
||||
let persons = db.get_persons();
|
||||
|
||||
for (index, person) in persons.iter().enumerate() {
|
||||
let label = gtk::Label::new(Some(&person.name_lf()));
|
||||
label.set_halign(gtk::Align::Start);
|
||||
let row = SelectorRow::new(index.try_into().unwrap(), &label);
|
||||
row.show_all();
|
||||
list.insert(&row, -1);
|
||||
}
|
||||
|
||||
let result = Rc::new(PersonSelector {
|
||||
db: db,
|
||||
backend: backend,
|
||||
window: window,
|
||||
callback: callback,
|
||||
persons: RefCell::new(persons),
|
||||
search_entry: search_entry,
|
||||
list: list,
|
||||
});
|
||||
|
||||
result
|
||||
.backend
|
||||
.get_persons(clone!(@strong result => move |persons| {
|
||||
for (index, person) in persons.iter().enumerate() {
|
||||
let label = gtk::Label::new(Some(&person.name_lf()));
|
||||
label.set_halign(gtk::Align::Start);
|
||||
let row = SelectorRow::new(index.try_into().unwrap(), &label);
|
||||
row.show_all();
|
||||
result.list.insert(&row, -1);
|
||||
}
|
||||
|
||||
result
|
||||
.list
|
||||
.connect_row_activated(clone!(@strong result => move |_, row| {
|
||||
.connect_row_activated(clone!(@strong result, @strong persons => move |_, row| {
|
||||
result.window.close();
|
||||
let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap();
|
||||
let index: usize = row.get_index().try_into().unwrap();
|
||||
(result.callback)(result.persons.borrow()[index].clone());
|
||||
(result.callback)(persons[index].clone());
|
||||
}));
|
||||
|
||||
result
|
||||
|
|
@ -68,12 +67,12 @@ where
|
|||
let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap();
|
||||
let index: usize = row.get_index().try_into().unwrap();
|
||||
let search = result.search_entry.get_text().to_string().to_lowercase();
|
||||
|
||||
search.is_empty() || result.persons.borrow()[index]
|
||||
search.is_empty() || persons[index]
|
||||
.name_lf()
|
||||
.to_lowercase()
|
||||
.contains(&search)
|
||||
}))));
|
||||
}));
|
||||
|
||||
result
|
||||
.search_entry
|
||||
|
|
@ -83,7 +82,7 @@ where
|
|||
|
||||
add_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
let editor = PersonEditor::new(
|
||||
result.db.clone(),
|
||||
result.backend.clone(),
|
||||
&result.window,
|
||||
None,
|
||||
clone!(@strong result => move |person| {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use super::selector_row::SelectorRow;
|
||||
use super::{InstrumentSelector, PersonSelector, PartEditor, SectionEditor};
|
||||
use crate::backend::*;
|
||||
use crate::database::*;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
|
|
@ -49,9 +50,12 @@ impl PartOrSection {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct WorkEditor {
|
||||
db: Rc<Database>,
|
||||
pub struct WorkEditor<F>
|
||||
where
|
||||
F: Fn(WorkDescription) -> () + 'static, {
|
||||
backend: Rc<Backend>,
|
||||
window: gtk::Window,
|
||||
callback: F,
|
||||
save_button: gtk::Button,
|
||||
id: i64,
|
||||
title_entry: gtk::Entry,
|
||||
|
|
@ -63,9 +67,11 @@ pub struct WorkEditor {
|
|||
part_list: gtk::ListBox,
|
||||
}
|
||||
|
||||
impl WorkEditor {
|
||||
pub fn new<F: Fn(WorkDescription) -> () + 'static, P: IsA<gtk::Window>>(
|
||||
db: Rc<Database>,
|
||||
impl<F> WorkEditor<F>
|
||||
where
|
||||
F: Fn(WorkDescription) -> () + 'static, {
|
||||
pub fn new<P: IsA<gtk::Window>>(
|
||||
backend: Rc<Backend>,
|
||||
parent: &P,
|
||||
work: Option<WorkDescription>,
|
||||
callback: F,
|
||||
|
|
@ -134,8 +140,9 @@ impl WorkEditor {
|
|||
});
|
||||
|
||||
let result = Rc::new(WorkEditor {
|
||||
db: db,
|
||||
backend: backend,
|
||||
window: window,
|
||||
callback: callback,
|
||||
save_button: save_button,
|
||||
id: id,
|
||||
title_entry: title_entry,
|
||||
|
|
@ -152,8 +159,6 @@ impl WorkEditor {
|
|||
}));
|
||||
|
||||
result.save_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
result.window.close();
|
||||
|
||||
let mut section_count: i64 = 0;
|
||||
let mut parts: Vec<WorkPartDescription> = Vec::new();
|
||||
let mut sections: Vec<WorkSectionDescription> = Vec::new();
|
||||
|
|
@ -179,12 +184,14 @@ impl WorkEditor {
|
|||
sections: sections,
|
||||
};
|
||||
|
||||
result.db.update_work(work.clone().into());
|
||||
callback(work);
|
||||
result.backend.update_work(work.clone().into(), clone!(@strong result => move |_| {
|
||||
result.window.close();
|
||||
(result.callback)(work.clone());
|
||||
}));
|
||||
}));
|
||||
|
||||
composer_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
PersonSelector::new(result.db.clone(), &result.window, clone!(@strong result => move |person| {
|
||||
PersonSelector::new(result.backend.clone(), &result.window, clone!(@strong result => move |person| {
|
||||
result.composer.replace(Some(person.clone()));
|
||||
result.composer_label.set_text(&person.name_fl());
|
||||
result.save_button.set_sensitive(true);
|
||||
|
|
@ -192,7 +199,7 @@ impl WorkEditor {
|
|||
}));
|
||||
|
||||
add_instrument_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
InstrumentSelector::new(result.db.clone(), &result.window, clone!(@strong result => move |instrument| {
|
||||
InstrumentSelector::new(result.backend.clone(), &result.window, clone!(@strong result => move |instrument| {
|
||||
{
|
||||
let mut instruments = result.instruments.borrow_mut();
|
||||
instruments.push(instrument);
|
||||
|
|
@ -216,7 +223,7 @@ impl WorkEditor {
|
|||
}));
|
||||
|
||||
add_part_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
PartEditor::new(result.db.clone(), &result.window, None, clone!(@strong result => move |part| {
|
||||
PartEditor::new(result.backend.clone(), &result.window, None, clone!(@strong result => move |part| {
|
||||
{
|
||||
let mut structure = result.structure.borrow_mut();
|
||||
structure.push(PartOrSection::part(part));
|
||||
|
|
@ -247,7 +254,7 @@ impl WorkEditor {
|
|||
|
||||
if pos.is_part() {
|
||||
let editor =
|
||||
PartEditor::new(result.db.clone(), &result.window, Some(pos.unwrap_part()), clone!(@strong result => move |part| {
|
||||
PartEditor::new(result.backend.clone(), &result.window, Some(pos.unwrap_part()), clone!(@strong result => move |part| {
|
||||
result.structure.borrow_mut()[index] = PartOrSection::part(part);
|
||||
result.show_parts();
|
||||
}));
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use glib::clone;
|
|||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
mod backend;
|
||||
mod database;
|
||||
mod dialogs;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use super::backend::Backend;
|
||||
use super::database::*;
|
||||
use super::dialogs::*;
|
||||
use gio::prelude::*;
|
||||
|
|
@ -12,7 +13,7 @@ use std::rc::Rc;
|
|||
|
||||
pub struct Window {
|
||||
window: libhandy::ApplicationWindow,
|
||||
db: Rc<Database>,
|
||||
backend: Rc<Backend>,
|
||||
leaflet: libhandy::Leaflet,
|
||||
persons: RefCell<Vec<Person>>,
|
||||
works: RefCell<Vec<WorkDescription>>,
|
||||
|
|
@ -46,14 +47,13 @@ impl Window {
|
|||
get_widget!(builder, gtk::Box, recording_box);
|
||||
get_widget!(builder, gtk::ListBox, recording_list);
|
||||
|
||||
let db = Rc::new(Database::new("test.sqlite"));
|
||||
let persons = db.get_persons();
|
||||
let backend = Backend::new("test.sqlite");
|
||||
|
||||
let result = Rc::new(Window {
|
||||
window: window,
|
||||
db: db,
|
||||
backend: Rc::new(backend),
|
||||
leaflet: leaflet,
|
||||
persons: RefCell::new(persons),
|
||||
persons: RefCell::new(Vec::new()),
|
||||
works: RefCell::new(Vec::new()),
|
||||
recordings: RefCell::new(Vec::new()),
|
||||
sidebar_box: sidebar_box,
|
||||
|
|
@ -108,9 +108,11 @@ impl Window {
|
|||
result.window,
|
||||
"add-person",
|
||||
clone!(@strong result => move |_, _| {
|
||||
PersonEditor::new(result.db.clone(), &result.window, None, clone!(@strong result => move |_| {
|
||||
result.persons.replace(result.db.get_persons());
|
||||
PersonEditor::new(result.backend.clone(), &result.window, None, clone!(@strong result => move |_| {
|
||||
result.backend.get_persons(clone!(@strong result => move |persons| {
|
||||
result.persons.replace(persons);
|
||||
result.show_persons();
|
||||
}));
|
||||
})).show();
|
||||
})
|
||||
);
|
||||
|
|
@ -119,7 +121,7 @@ impl Window {
|
|||
result.window,
|
||||
"add-instrument",
|
||||
clone!(@strong result => move |_, _| {
|
||||
InstrumentEditor::new(result.db.clone(), &result.window, None, |instrument| {
|
||||
InstrumentEditor::new(result.backend.clone(), &result.window, None, |instrument| {
|
||||
println!("{:?}", instrument);
|
||||
}).show();
|
||||
})
|
||||
|
|
@ -129,9 +131,11 @@ impl Window {
|
|||
result.window,
|
||||
"add-work",
|
||||
clone!(@strong result => move |_, _| {
|
||||
WorkEditor::new(result.db.clone(), &result.window, None, clone!(@strong result => move |_| {
|
||||
result.persons.replace(result.db.get_persons());
|
||||
WorkEditor::new(result.backend.clone(), &result.window, None, clone!(@strong result => move |_| {
|
||||
result.backend.get_persons(clone!(@strong result => move |persons| {
|
||||
result.persons.replace(persons);
|
||||
result.show_persons();
|
||||
}));
|
||||
})).show();
|
||||
})
|
||||
);
|
||||
|
|
@ -140,7 +144,7 @@ impl Window {
|
|||
result.window,
|
||||
"add-ensemble",
|
||||
clone!(@strong result => move |_, _| {
|
||||
EnsembleEditor::new(result.db.clone(), &result.window, None, |ensemble| {
|
||||
EnsembleEditor::new(result.backend.clone(), &result.window, None, |ensemble| {
|
||||
println!("{:?}", ensemble);
|
||||
}).show();
|
||||
})
|
||||
|
|
@ -155,12 +159,15 @@ impl Window {
|
|||
"edit-person",
|
||||
Some(glib::VariantTy::new("x").unwrap()),
|
||||
clone!(@strong result => move |_, id| {
|
||||
let person = result.db.get_person(id.unwrap().get().unwrap()).unwrap();
|
||||
PersonEditor::new(result.db.clone(), &result.window, Some(person), clone!(@strong result => move |person| {
|
||||
result.persons.replace(result.db.get_persons());
|
||||
result.backend.get_person(id.unwrap().get().unwrap(), clone!(@strong result => move |person| {
|
||||
let person = person.unwrap();
|
||||
PersonEditor::new(result.backend.clone(), &result.window, Some(person), clone!(@strong result => move |person| {
|
||||
result.backend.get_persons(clone!(@strong result => move |persons| {
|
||||
result.persons.replace(persons);
|
||||
result.show_persons();
|
||||
result.show_person(person);
|
||||
}));
|
||||
})).show();
|
||||
}));
|
||||
})
|
||||
);
|
||||
|
||||
|
|
@ -169,10 +176,13 @@ impl Window {
|
|||
"delete-person",
|
||||
Some(glib::VariantTy::new("x").unwrap()),
|
||||
clone!(@strong result => move |_, id| {
|
||||
result.db.delete_person(id.unwrap().get().unwrap());
|
||||
result.backend.delete_person(id.unwrap().get().unwrap(), clone!(@strong result => move |_| {
|
||||
result.back();
|
||||
result.persons.replace(result.db.get_persons());
|
||||
result.backend.get_persons(clone!(@strong result => move |persons| {
|
||||
result.persons.replace(persons);
|
||||
result.show_persons();
|
||||
}));
|
||||
}));
|
||||
})
|
||||
);
|
||||
|
||||
|
|
@ -218,13 +228,13 @@ impl Window {
|
|||
|
||||
self.header_menu_button.set_menu_model(Some(&menu));
|
||||
|
||||
self.works.replace(self.db.get_work_descriptions(person.id));
|
||||
self.show_works();
|
||||
|
||||
self.show_recordings();
|
||||
|
||||
self.stack.set_visible_child_name("person_screen");
|
||||
self.leaflet.set_visible_child(&self.stack);
|
||||
// let result = self.clone();
|
||||
// self.backend.get_work_descriptions(person.id, |works| {
|
||||
// result.show_works();
|
||||
// result.show_recordings();
|
||||
// result.stack.set_visible_child_name("person_screen");
|
||||
// result.leaflet.set_visible_child(&result.stack);
|
||||
// });
|
||||
}
|
||||
|
||||
fn show_works(&self) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue