From bd2d7baee047cd7ddc9db3f633cf22b734d92596 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Thu, 8 Apr 2021 14:26:25 +0200 Subject: [PATCH] Listen for state changes in medium preview --- musicus/src/import/medium_preview.rs | 34 +++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/musicus/src/import/medium_preview.rs b/musicus/src/import/medium_preview.rs index 481c0b7..69c817f 100644 --- a/musicus/src/import/medium_preview.rs +++ b/musicus/src/import/medium_preview.rs @@ -5,7 +5,7 @@ use glib::clone; use gtk::prelude::*; use gtk_macros::get_widget; use musicus_backend::db::Medium; -use musicus_backend::import::ImportSession; +use musicus_backend::import::{ImportSession, State}; use std::rc::Rc; use std::sync::Arc; @@ -14,6 +14,8 @@ pub struct MediumPreview { handle: NavigationHandle<()>, session: Arc, widget: gtk::Box, + import_button: gtk::Button, + done_stack: gtk::Stack, } impl Screen<(Arc, Medium), ()> for MediumPreview { @@ -34,6 +36,8 @@ impl Screen<(Arc, Medium), ()> for MediumPreview { handle, session, widget, + import_button, + done_stack, }); // Connect signals and callbacks @@ -111,10 +115,38 @@ impl Screen<(Arc, Medium), ()> for MediumPreview { medium_box.append(&frame); } + this.handle_state(&this.session.state()); + spawn!(@clone this, async move { + loop { + let state = this.session.state_change().await; + this.handle_state(&state); + + match state { + State::Ready | State::Error => break, + _ => (), + } + } + }); + this } } +impl MediumPreview { + /// Handle a state change of the import process. + fn handle_state(&self, state: &State) { + match state { + State::Waiting => todo!("This shouldn't happen."), + State::Copying => self.done_stack.set_visible_child_name("loading"), + State::Ready => { + self.done_stack.set_visible_child_name("ready"); + self.import_button.set_sensitive(true); + } + State::Error => todo!("Import error!"), + } + } +} + impl Widget for MediumPreview { fn get_widget(&self) -> gtk::Widget { self.widget.clone().upcast()