Skip to content

Commit 6135853

Browse files
committed
installer: move ImportDescriptorModal with some logic to
import_descriptor.rs
1 parent ed59d1b commit 6135853

File tree

3 files changed

+113
-79
lines changed

3 files changed

+113
-79
lines changed

liana-gui/src/installer/step/descriptor/mod.rs

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,12 @@ use crate::{
2323
hw::{HardwareWallet, HardwareWalletMessage, HardwareWallets},
2424
installer::{
2525
message::{self, Message},
26+
step::import_descriptor::{ImportDescriptorModal, BACKUP_NETWORK_NOT_MATCH},
2627
step::{Context, Step},
2728
view, Error,
2829
},
2930
};
3031

31-
const BACKUP_NETWORK_NOT_MATCH: &str = "Backup network do not match the selected network!";
32-
33-
#[derive(Debug)]
34-
pub enum ImportDescriptorModal {
35-
None,
36-
Export(ExportModal),
37-
Decrypt(DecryptModal),
38-
}
39-
4032
pub struct ImportDescriptor {
4133
network: Network,
4234
wrong_network: bool,
@@ -95,23 +87,7 @@ impl Step for ImportDescriptor {
9587
}
9688

9789
fn subscription(&self, hws: &HardwareWallets) -> Subscription<Message> {
98-
if let ImportDescriptorModal::Export(modal) = &self.modal {
99-
if let Some(sub) = modal.subscription() {
100-
sub.map(|m| Message::ImportExport(ImportExportMessage::Progress(m)))
101-
} else {
102-
Subscription::none()
103-
}
104-
} else if let ImportDescriptorModal::Decrypt(modal) = &self.modal {
105-
let mut batch = vec![hws.refresh().map(Message::HardwareWallets)];
106-
if let Some(import_modal) = modal.modal.as_ref() {
107-
if let Some(sub) = import_modal.subscription() {
108-
batch.push(sub.map(|p| Message::ImportExport(ImportExportMessage::Progress(p))))
109-
}
110-
}
111-
Subscription::batch(batch)
112-
} else {
113-
Subscription::none()
114-
}
90+
self.modal.subscriptions(hws)
11591
}
11692

11793
fn update(&mut self, hws: &mut HardwareWallets, message: Message) -> Task<Message> {
@@ -167,29 +143,7 @@ impl Step for ImportDescriptor {
167143
self.modal = ImportDescriptorModal::Decrypt(DecryptModal::new(bytes, self.network));
168144
None
169145
}
170-
Message::ImportExport(ImportExportMessage::Progress(Progress::Xpub(xpub))) => {
171-
if let ImportDescriptorModal::Decrypt(modal) = &mut self.modal {
172-
let _ = modal.update(Decrypt::CloseModal);
173-
Some(modal.update(Decrypt::Xpub(xpub)))
174-
} else {
175-
None
176-
}
177-
}
178-
Message::ImportExport(m) => {
179-
if let ImportDescriptorModal::Export(modal) = &mut self.modal {
180-
let task: Task<Message> = modal.update(m);
181-
Some(task)
182-
} else if let ImportDescriptorModal::Decrypt(modal) = &mut self.modal {
183-
if let Some(mo) = &mut modal.modal {
184-
let task: Task<Message> = mo.update(m);
185-
Some(task)
186-
} else {
187-
None
188-
}
189-
} else {
190-
None
191-
}
192-
}
146+
Message::ImportExport(m) => Some(self.modal.update(Message::ImportExport(m))),
193147
Message::HardwareWallets(HardwareWalletMessage::Update) => {
194148
if let ImportDescriptorModal::Decrypt(modal) = &mut self.modal {
195149
modal.update_devices(hws)
@@ -230,31 +184,7 @@ impl Step for ImportDescriptor {
230184
}
231185
None
232186
}
233-
Message::Decrypt(msg) => {
234-
if let ImportDescriptorModal::Decrypt(modal) = &mut self.modal {
235-
match msg {
236-
Decrypt::Fetched(_, _)
237-
| Decrypt::Xpub(_)
238-
| Decrypt::XpubError(_)
239-
| Decrypt::Mnemonic(_)
240-
| Decrypt::MnemonicStatus(_, _)
241-
| Decrypt::UnexpectedPayload(_)
242-
| Decrypt::InvalidDescriptor
243-
| Decrypt::ContentNotSupported
244-
| Decrypt::PasteXpub
245-
| Decrypt::SelectXpub
246-
| Decrypt::PasteMnemonic
247-
| Decrypt::SelectMnemonic
248-
| Decrypt::SelectImportXpub
249-
| Decrypt::None
250-
| Decrypt::CloseModal
251-
| Decrypt::ShowOptions(_) => Some(modal.update(msg)),
252-
Decrypt::Backup(_) | Decrypt::Close => None,
253-
}
254-
} else {
255-
None
256-
}
257-
}
187+
Message::Decrypt(msg) => Some(self.modal.update(Message::Decrypt(msg))),
258188
_ => None,
259189
};
260190
task.unwrap_or(Task::none())
@@ -306,11 +236,7 @@ impl Step for ImportDescriptor {
306236
self.wrong_network,
307237
self.error.as_ref(),
308238
);
309-
match &self.modal {
310-
ImportDescriptorModal::None => content,
311-
ImportDescriptorModal::Export(modal) => modal.view(content),
312-
ImportDescriptorModal::Decrypt(modal) => modal.view(content),
313-
}
239+
self.modal.view(content)
314240
}
315241
}
316242

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use iced::{Subscription, Task};
2+
use liana_ui::widget::Element;
3+
4+
use crate::{
5+
app::state::export::ExportModal,
6+
decrypt::{Decrypt, DecryptModal},
7+
export::{ImportExportMessage, Progress},
8+
hw::HardwareWallets,
9+
installer,
10+
};
11+
12+
pub const BACKUP_NETWORK_NOT_MATCH: &str = "Backup network do not match the selected network!";
13+
14+
#[derive(Debug)]
15+
pub enum ImportDescriptorModal {
16+
None,
17+
Export(ExportModal),
18+
Decrypt(DecryptModal),
19+
}
20+
21+
impl ImportDescriptorModal {
22+
pub fn subscriptions(&self, hws: &HardwareWallets) -> Subscription<installer::Message> {
23+
if let ImportDescriptorModal::Export(modal) = &self {
24+
if let Some(sub) = modal.subscription() {
25+
sub.map(|m| installer::Message::ImportExport(ImportExportMessage::Progress(m)))
26+
} else {
27+
Subscription::none()
28+
}
29+
} else if let ImportDescriptorModal::Decrypt(modal) = &self {
30+
let mut batch = vec![hws.refresh().map(installer::Message::HardwareWallets)];
31+
if let Some(import_modal) = modal.modal.as_ref() {
32+
if let Some(sub) = import_modal.subscription() {
33+
batch.push(sub.map(|p| {
34+
installer::Message::ImportExport(ImportExportMessage::Progress(p))
35+
}))
36+
}
37+
}
38+
Subscription::batch(batch)
39+
} else {
40+
Subscription::none()
41+
}
42+
}
43+
44+
pub fn view<'a>(
45+
&'a self,
46+
content: Element<'a, installer::Message>,
47+
) -> Element<'a, installer::Message> {
48+
match &self {
49+
ImportDescriptorModal::None => content,
50+
ImportDescriptorModal::Export(modal) => modal.view(content),
51+
ImportDescriptorModal::Decrypt(modal) => modal.view(content),
52+
}
53+
}
54+
55+
pub fn update(&mut self, msg: installer::Message) -> Task<installer::Message> {
56+
match msg {
57+
installer::Message::ImportExport(ImportExportMessage::Progress(Progress::Xpub(
58+
xpub,
59+
))) => {
60+
if let ImportDescriptorModal::Decrypt(modal) = self {
61+
let _ = modal.update(Decrypt::CloseModal);
62+
return modal.update(Decrypt::Xpub(xpub));
63+
}
64+
}
65+
installer::Message::ImportExport(m) => {
66+
if let ImportDescriptorModal::Export(modal) = self {
67+
let task: Task<installer::Message> = modal.update(m);
68+
return task;
69+
} else if let ImportDescriptorModal::Decrypt(modal) = self {
70+
if let Some(mo) = &mut modal.modal {
71+
let task: Task<installer::Message> = mo.update(m);
72+
return task;
73+
}
74+
}
75+
}
76+
installer::Message::Decrypt(msg) => {
77+
if let ImportDescriptorModal::Decrypt(modal) = self {
78+
match msg {
79+
Decrypt::Fetched(_, _)
80+
| Decrypt::Xpub(_)
81+
| Decrypt::XpubError(_)
82+
| Decrypt::Mnemonic(_)
83+
| Decrypt::MnemonicStatus(_, _)
84+
| Decrypt::UnexpectedPayload(_)
85+
| Decrypt::InvalidDescriptor
86+
| Decrypt::ContentNotSupported
87+
| Decrypt::PasteXpub
88+
| Decrypt::SelectXpub
89+
| Decrypt::PasteMnemonic
90+
| Decrypt::SelectMnemonic
91+
| Decrypt::SelectImportXpub
92+
| Decrypt::None
93+
| Decrypt::CloseModal
94+
| Decrypt::ShowOptions(_) => return modal.update(msg),
95+
Decrypt::Backup(_) | Decrypt::Close => {}
96+
}
97+
}
98+
}
99+
_ => {}
100+
}
101+
Task::none()
102+
}
103+
104+
pub fn is_some(&self) -> bool {
105+
!matches!(self, ImportDescriptorModal::None)
106+
}
107+
}

liana-gui/src/installer/step/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod descriptor;
2+
pub mod import_descriptor;
23

34
mod backend;
45
mod mnemonic;

0 commit comments

Comments
 (0)