Add coins to cache
This commit is contained in:
parent
f32b4d52d2
commit
157d9b3933
@ -1,3 +1,15 @@
|
||||
use crate::daemon::model::Coin;
|
||||
|
||||
pub struct Cache {
|
||||
pub blockheight: i32,
|
||||
pub coins: Vec<Coin>,
|
||||
}
|
||||
|
||||
impl Default for Cache {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
blockheight: 0,
|
||||
coins: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
use minisafe::config::Config as DaemonConfig;
|
||||
|
||||
use crate::app::{error::Error, view};
|
||||
use crate::{
|
||||
app::{error::Error, view},
|
||||
daemon::model::*,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Message {
|
||||
@ -11,4 +14,5 @@ pub enum Message {
|
||||
DaemonConfigLoaded(Result<(), Error>),
|
||||
BlockHeight(Result<i32, Error>),
|
||||
ReceiveAddress(Result<bitcoin::Address, Error>),
|
||||
Coins(Result<Vec<Coin>, Error>),
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ impl App {
|
||||
config: Config,
|
||||
daemon: Arc<dyn Daemon + Sync + Send>,
|
||||
) -> (App, Command<Message>) {
|
||||
let state: Box<dyn State> = Home {}.into();
|
||||
let state: Box<dyn State> = Home::new(&cache.coins).into();
|
||||
let cmd = state.load(daemon.clone());
|
||||
(
|
||||
Self {
|
||||
@ -62,7 +62,7 @@ impl App {
|
||||
state::SettingsState::new(self.daemon.config().clone(), self.daemon.is_external())
|
||||
.into()
|
||||
}
|
||||
menu::Menu::Home => Home {}.into(),
|
||||
menu::Menu::Home => Home::new(&self.cache.coins).into(),
|
||||
menu::Menu::Receive => ReceivePanel::default().into(),
|
||||
};
|
||||
self.state.load(self.daemon.clone())
|
||||
@ -95,6 +95,18 @@ impl App {
|
||||
}
|
||||
|
||||
pub fn update(&mut self, message: Message) -> Command<Message> {
|
||||
// Update cache when values are passing by.
|
||||
// State will handle the error case.
|
||||
match &message {
|
||||
Message::Coins(Ok(coins)) => {
|
||||
self.cache.coins = coins.clone();
|
||||
}
|
||||
Message::BlockHeight(Ok(blockheight)) => {
|
||||
self.cache.blockheight = blockheight.clone();
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
|
||||
match message {
|
||||
Message::Tick => {
|
||||
let daemon = self.daemon.clone();
|
||||
@ -108,12 +120,6 @@ impl App {
|
||||
Message::BlockHeight,
|
||||
)
|
||||
}
|
||||
Message::BlockHeight(res) => {
|
||||
if let Ok(blockheight) = res {
|
||||
self.cache.blockheight = blockheight;
|
||||
}
|
||||
Command::none()
|
||||
}
|
||||
Message::LoadDaemonConfig(cfg) => {
|
||||
let res = self.load_daemon_config(*cfg);
|
||||
self.update(Message::DaemonConfigLoaded(res))
|
||||
|
||||
@ -1 +1,3 @@
|
||||
pub use minisafe::commands::{GetAddressResult, GetInfoResult, ListCoinsResult};
|
||||
pub use minisafe::commands::{GetAddressResult, GetInfoResult, ListCoinsEntry, ListCoinsResult};
|
||||
|
||||
pub type Coin = ListCoinsEntry;
|
||||
|
||||
@ -39,7 +39,7 @@ enum Step {
|
||||
pub enum Message {
|
||||
Event(iced_native::Event),
|
||||
Syncing(Result<GetInfoResult, DaemonError>),
|
||||
Synced(GetInfoResult, Arc<dyn Daemon + Sync + Send>),
|
||||
Synced(GetInfoResult, Vec<Coin>, Arc<dyn Daemon + Sync + Send>),
|
||||
Started(Result<Arc<dyn Daemon + Sync + Send>, Error>),
|
||||
Loaded(Result<Arc<dyn Daemon + Sync + Send>, Error>),
|
||||
Failure(DaemonError),
|
||||
@ -118,9 +118,16 @@ impl Loader {
|
||||
Ok(info) => {
|
||||
if (info.sync - 1.0_f64).abs() < f64::EPSILON {
|
||||
let daemon = daemon.clone();
|
||||
return Command::perform(async move { (info, daemon) }, |res| {
|
||||
Message::Synced(res.0, res.1)
|
||||
});
|
||||
return Command::perform(
|
||||
async move {
|
||||
let coins = daemon
|
||||
.list_coins()
|
||||
.map(|res| res.coins)
|
||||
.unwrap_or_else(|_| Vec::new());
|
||||
(info, coins, daemon)
|
||||
},
|
||||
|res| Message::Synced(res.0, res.1, res.2),
|
||||
);
|
||||
} else {
|
||||
*progress = info.sync
|
||||
}
|
||||
|
||||
@ -161,9 +161,10 @@ impl Application for GUI {
|
||||
}
|
||||
}
|
||||
(State::Loader(loader), Message::Load(msg)) => {
|
||||
if let loader::Message::Synced(info, minisafed) = *msg {
|
||||
if let loader::Message::Synced(info, coins, minisafed) = *msg {
|
||||
let cache = Cache {
|
||||
blockheight: info.blockheight,
|
||||
coins,
|
||||
};
|
||||
|
||||
let (app, command) = App::new(cache, loader.gui_config.clone(), minisafed);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user