11use std:: convert:: TryInto ;
22use std:: time:: { SystemTime , UNIX_EPOCH } ;
33
4+ use bitcoin:: Txid ;
45use iced:: { Command , Element } ;
56
67use super :: State ;
78
9+ use revault_ui:: chart:: { FlowChart , FlowChartMessage } ;
10+
811use crate :: {
912 app:: {
1013 context:: Context ,
@@ -13,7 +16,10 @@ use crate::{
1316 view:: LoadingDashboard ,
1417 view:: { HistoryEventListItemView , HistoryEventView , HistoryView } ,
1518 } ,
16- daemon:: model:: { HistoryEvent , HistoryEventKind , VaultTransactions , ALL_HISTORY_EVENTS } ,
19+ daemon:: model:: {
20+ HistoryEvent , HistoryEventKind , HistoryEventTransaction , TransactionKind ,
21+ ALL_HISTORY_EVENTS ,
22+ } ,
1723} ;
1824
1925pub const HISTORY_EVENT_PAGE_SIZE : u64 = 20 ;
@@ -93,7 +99,7 @@ impl State for HistoryState {
9399 }
94100 Message :: HistoryEvent ( msg) => {
95101 if let Some ( event) = selected_event {
96- event. update ( msg)
102+ event. update ( ctx , msg)
97103 }
98104 }
99105 Message :: Close => {
@@ -283,7 +289,9 @@ impl HistoryEventListItemState {
283289#[ derive( Debug ) ]
284290pub struct HistoryEventState {
285291 event : HistoryEvent ,
286- txs : Vec < VaultTransactions > ,
292+ txs : Vec < HistoryEventTransaction > ,
293+ selected_tx : Option < Txid > ,
294+ flowchart : Option < FlowChart > ,
287295 loading_fail : Option < Error > ,
288296 view : HistoryEventView ,
289297}
@@ -293,22 +301,99 @@ impl HistoryEventState {
293301 Self {
294302 event,
295303 txs : Vec :: new ( ) ,
304+ flowchart : None ,
305+ selected_tx : None ,
296306 loading_fail : None ,
297307 view : HistoryEventView :: new ( ) ,
298308 }
299309 }
300310
301- pub fn update ( & mut self , message : HistoryEventMessage ) {
302- let HistoryEventMessage :: OnChainTransactions ( res) = message;
303- match res {
304- Ok ( txs) => self . txs = txs,
305- Err ( e) => self . loading_fail = Some ( e. into ( ) ) ,
311+ pub fn update ( & mut self , ctx : & Context , message : HistoryEventMessage ) {
312+ match message {
313+ HistoryEventMessage :: ToggleFlowChart ( toggle) => {
314+ if toggle {
315+ self . flowchart = Some ( FlowChart :: new (
316+ ctx. network ( ) ,
317+ self . txs
318+ . iter ( )
319+ . map ( |event_tx| event_tx. tx . clone ( ) )
320+ . collect ( ) ,
321+ ) ) ;
322+ } else {
323+ self . flowchart = None ;
324+ }
325+ }
326+ HistoryEventMessage :: FlowChart ( FlowChartMessage :: TxSelected ( txid) ) => {
327+ if self . selected_tx . is_none ( ) {
328+ self . selected_tx = txid;
329+ } else {
330+ self . selected_tx = None ;
331+ }
332+ }
333+ HistoryEventMessage :: OnChainTransactions ( res) => match res {
334+ Ok ( vault_txs) => {
335+ let mut list: Vec < HistoryEventTransaction > = Vec :: new ( ) ;
336+ for txs in vault_txs {
337+ list. push ( HistoryEventTransaction :: new (
338+ & txs. deposit ,
339+ TransactionKind :: Deposit ,
340+ ) ) ;
341+
342+ if let Some ( unvault) = txs. unvault {
343+ list. push ( HistoryEventTransaction :: new (
344+ & unvault,
345+ TransactionKind :: Unvault ,
346+ ) ) ;
347+ }
348+ if let Some ( cancel) = txs. cancel {
349+ list. push ( HistoryEventTransaction :: new (
350+ & cancel,
351+ TransactionKind :: Cancel ,
352+ ) ) ;
353+ }
354+ if let Some ( spend) = txs. spend {
355+ list. push ( HistoryEventTransaction :: new ( & spend, TransactionKind :: Spend ) ) ;
356+ }
357+ if let Some ( unvault_emergency) = txs. unvault_emergency {
358+ list. push ( HistoryEventTransaction :: new (
359+ & unvault_emergency,
360+ TransactionKind :: UnvaultEmergency ,
361+ ) ) ;
362+ }
363+ if let Some ( emergency) = txs. emergency {
364+ list. push ( HistoryEventTransaction :: new (
365+ & emergency,
366+ TransactionKind :: Emergency ,
367+ ) ) ;
368+ }
369+ }
370+
371+ list. sort_by ( |a, b| a. blockheight . cmp ( & b. blockheight ) ) ;
372+ self . txs = list;
373+ }
374+ Err ( e) => self . loading_fail = Some ( e. into ( ) ) ,
375+ } ,
306376 }
307377 }
308378
309379 pub fn view ( & mut self , ctx : & Context ) -> Element < Message > {
310- self . view
311- . view ( ctx, & self . event , & self . txs , self . loading_fail . as_ref ( ) )
380+ let selected = if let Some ( txid) = self . selected_tx {
381+ self . txs . iter ( ) . find ( |vault_tx| vault_tx. tx . txid ( ) == txid)
382+ } else {
383+ None
384+ } ;
385+ self . view . view (
386+ ctx,
387+ & self . event ,
388+ & self . txs ,
389+ selected,
390+ self . flowchart . as_mut ( ) . map ( |chart| {
391+ chart
392+ . view ( )
393+ . map ( |msg| Message :: HistoryEvent ( HistoryEventMessage :: FlowChart ( msg) ) )
394+ } ) ,
395+ self . loading_fail . as_ref ( ) ,
396+ )
312397 }
313398
314399 pub fn load ( & self , ctx : & Context ) -> Command < Message > {
0 commit comments