Add interrupt method to state trait

Before changing panel, the previous state
may need to release a harware connection
for example.
In order to achieve this we introduce an interrupt method
that is used to drop action state.

close #1162
This commit is contained in:
edouardparis 2024-07-01 13:45:30 +02:00
parent b6f49c4735
commit 5c59c4f5de
6 changed files with 22 additions and 0 deletions

View File

@ -157,6 +157,8 @@ impl App {
} }
fn set_current_panel(&mut self, menu: Menu) -> Command<Message> { fn set_current_panel(&mut self, menu: Menu) -> Command<Message> {
self.panels.current_mut().interrupt();
match &menu { match &menu {
menu::Menu::TransactionPreSelected(txid) => { menu::Menu::TransactionPreSelected(txid) => {
if let Ok(Some(tx)) = Handle::current().block_on(async { if let Ok(Some(tx)) = Handle::current().block_on(async {

View File

@ -47,6 +47,7 @@ pub trait State {
fn subscription(&self) -> Subscription<Message> { fn subscription(&self) -> Subscription<Message> {
Subscription::none() Subscription::none()
} }
fn interrupt(&mut self) {}
fn reload( fn reload(
&mut self, &mut self,
_daemon: Arc<dyn Daemon + Sync + Send>, _daemon: Arc<dyn Daemon + Sync + Send>,

View File

@ -107,6 +107,10 @@ impl PsbtState {
} }
} }
pub fn interrupt(&mut self) {
self.action = None;
}
pub fn subscription(&self) -> Subscription<Message> { pub fn subscription(&self) -> Subscription<Message> {
if let Some(action) = &self.action { if let Some(action) = &self.action {
action.as_ref().subscription() action.as_ref().subscription()

View File

@ -67,6 +67,10 @@ impl State for PsbtsPanel {
} }
} }
fn interrupt(&mut self) {
self.selected_tx = None;
}
fn update( fn update(
&mut self, &mut self,
daemon: Arc<dyn Daemon + Sync + Send>, daemon: Arc<dyn Daemon + Sync + Send>,

View File

@ -81,6 +81,10 @@ impl State for CreateSpendPanel {
self.steps.get(self.current).unwrap().subscription() self.steps.get(self.current).unwrap().subscription()
} }
fn interrupt(&mut self) {
self.steps.get_mut(self.current).unwrap().interrupt();
}
fn update( fn update(
&mut self, &mut self,
daemon: Arc<dyn Daemon + Sync + Send>, daemon: Arc<dyn Daemon + Sync + Send>,

View File

@ -61,6 +61,7 @@ pub trait Step {
message: Message, message: Message,
) -> Command<Message>; ) -> Command<Message>;
fn apply(&self, _draft: &mut TransactionDraft) {} fn apply(&self, _draft: &mut TransactionDraft) {}
fn interrupt(&mut self) {}
fn load(&mut self, _draft: &TransactionDraft) {} fn load(&mut self, _draft: &TransactionDraft) {}
fn subscription(&self) -> Subscription<Message> { fn subscription(&self) -> Subscription<Message> {
Subscription::none() Subscription::none()
@ -776,6 +777,12 @@ impl Step for SaveSpend {
)); ));
} }
fn interrupt(&mut self) {
if let Some((psbt_state, _)) = &mut self.spend {
psbt_state.interrupt()
}
}
fn subscription(&self) -> Subscription<Message> { fn subscription(&self) -> Subscription<Message> {
if let Some((psbt_state, _)) = &self.spend { if let Some((psbt_state, _)) = &self.spend {
psbt_state.subscription() psbt_state.subscription()