Allow to upload instruments

This commit is contained in:
Elias Projahn 2020-11-28 23:07:31 +01:00
parent 13910a664c
commit 9c255d0cfe
10 changed files with 611 additions and 181 deletions

View file

@ -1,79 +1,123 @@
use crate::backend::*;
use crate::backend::Backend;
use crate::database::*;
use anyhow::Result;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::cell::RefCell;
use std::rc::Rc;
pub struct InstrumentEditor<F>
where
F: Fn(Instrument) -> () + 'static,
{
/// A dialog for creating or editing a instrument.
pub struct InstrumentEditor {
backend: Rc<Backend>,
window: libhandy::Window,
callback: F,
id: String,
window: libhandy::Window,
stack: gtk::Stack,
info_bar: gtk::InfoBar,
name_entry: gtk::Entry,
upload_switch: gtk::Switch,
saved_cb: RefCell<Option<Box<dyn Fn(Instrument) -> ()>>>,
}
impl<F> InstrumentEditor<F>
where
F: Fn(Instrument) -> () + 'static,
{
impl InstrumentEditor {
/// Create a new instrument editor and optionally initialize it.
pub fn new<P: IsA<gtk::Window>>(
backend: Rc<Backend>,
parent: &P,
instrument: Option<Instrument>,
callback: F,
) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/instrument_editor.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::Stack, stack);
get_widget!(builder, gtk::InfoBar, info_bar);
get_widget!(builder, gtk::Entry, name_entry);
get_widget!(builder, gtk::Switch, upload_switch);
let id = match instrument {
Some(instrument) => {
name_entry.set_text(&instrument.name);
instrument.id
}
None => generate_id(),
};
let result = Rc::new(InstrumentEditor {
backend: backend,
window: window,
callback: callback,
id: id,
name_entry: name_entry,
let this = Rc::new(Self {
backend,
id,
window,
stack,
info_bar,
name_entry,
upload_switch,
saved_cb: RefCell::new(None),
});
cancel_button.connect_clicked(clone!(@strong result => move |_| {
result.window.close();
// Connect signals and callbacks
cancel_button.connect_clicked(clone!(@strong this => move |_| {
this.window.close();
}));
save_button.connect_clicked(clone!(@strong result => move |_| {
let instrument = Instrument {
id: result.id.clone(),
name: result.name_entry.get_text().to_string(),
};
save_button.connect_clicked(clone!(@strong this => move |_| {
let context = glib::MainContext::default();
let clone = this.clone();
context.spawn_local(async move {
clone.stack.set_visible_child_name("loading");
match clone.clone().save().await {
Ok(_) => {
clone.window.close();
}
Err(_) => {
clone.info_bar.set_revealed(true);
clone.stack.set_visible_child_name("content");
}
}
let c = glib::MainContext::default();
let clone = result.clone();
c.spawn_local(async move {
clone.backend.db().update_instrument(instrument.clone()).await.unwrap();
clone.window.close();
(clone.callback)(instrument.clone());
});
}));
result.window.set_transient_for(Some(parent));
this.window.set_transient_for(Some(parent));
result
this
}
/// Set the closure to be called if the instrument was saved.
pub fn set_saved_cb<F: Fn(Instrument) -> () + 'static>(&self, cb: F) {
self.saved_cb.replace(Some(Box::new(cb)));
}
/// Show the instrument editor.
pub fn show(&self) {
self.window.show();
}
/// Save the instrument and possibly upload it to the server.
async fn save(self: Rc<Self>) -> Result<()> {
let name = self.name_entry.get_text().to_string();
let instrument = Instrument {
id: self.id.clone(),
name,
};
let upload = self.upload_switch.get_active();
if upload {
self.backend.post_instrument(&instrument).await?;
}
self.backend.db().update_instrument(instrument.clone()).await?;
self.backend.library_changed();
if let Some(cb) = &*self.saved_cb.borrow() {
cb(instrument.clone());
}
Ok(())
}
}