/
wallet_sync_info.go
651 lines (589 loc) · 22.1 KB
/
wallet_sync_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package components
import (
"fmt"
"strings"
"time"
"gioui.org/font"
"gioui.org/layout"
"gioui.org/unit"
"github.com/crypto-power/cryptopower/libwallet/assets/dcr"
sharedW "github.com/crypto-power/cryptopower/libwallet/assets/wallet"
libutils "github.com/crypto-power/cryptopower/libwallet/utils"
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
"github.com/crypto-power/cryptopower/ui/load"
"github.com/crypto-power/cryptopower/ui/values"
)
const WalletSyncInfoID = "WalletSyncInfo"
type WalletSyncInfo struct {
*load.Load
wallet sharedW.Asset
rescanUpdate *sharedW.HeadersRescanProgressReport
walletStatusIcon *cryptomaterial.Icon
syncSwitch *cryptomaterial.Switch
toBackup cryptomaterial.Button
isStatusConnected bool
reload Reload
backup func(sharedW.Asset)
ForwardButton cryptomaterial.IconButton
IsSlider bool
}
type ProgressInfo struct {
remainingSyncTime string
HeadersToFetchOrScan int32
stepFetchProgress int32
syncProgress int
}
type Reload func()
// SyncProgressInfo is made independent of the walletInfo struct so that once
// set with a value, it always persists till unset. This will help address the
// progress bar issue where, changing UI pages alters the progress on the sync
// status progress percentage.
var syncProgressInfo = map[sharedW.Asset]ProgressInfo{}
func NewWalletSyncInfo(l *load.Load, wallet sharedW.Asset, reload Reload, backup func(sharedW.Asset)) *WalletSyncInfo {
wsi := &WalletSyncInfo{
Load: l,
wallet: wallet,
reload: reload,
walletStatusIcon: cryptomaterial.NewIcon(l.Theme.Icons.DotIcon),
syncSwitch: l.Theme.Switch(),
backup: backup,
}
wsi.toBackup = l.Theme.Button(values.String(values.StrBackupNow))
wsi.toBackup.Font.Weight = font.Medium
wsi.toBackup.TextSize = l.ConvertTextSize(values.TextSize14)
wsi.ForwardButton, _ = SubpageHeaderButtons(l)
wsi.ForwardButton.Icon = wsi.Theme.Icons.NavigationArrowForward
wsi.ForwardButton.Size = values.MarginPadding20
return wsi
}
func (wsi *WalletSyncInfo) Init() {
autoSync := wsi.wallet.ReadBoolConfigValueForKey(sharedW.AutoSyncConfigKey, false)
wsi.syncSwitch.SetChecked(autoSync)
go func() {
wsi.isStatusConnected = libutils.IsOnline()
}()
}
func (wsi *WalletSyncInfo) GetWallet() sharedW.Asset {
return wsi.wallet
}
func (wsi *WalletSyncInfo) WalletInfoLayout(gtx C) D {
wsi.handle()
return wsi.pageContentWrapper(gtx, "", nil, func(gtx C) D {
items := []layout.FlexChild{
layout.Rigid(wsi.walletNameAndBackupInfo),
layout.Rigid(wsi.syncStatusSection),
}
if len(wsi.wallet.GetEncryptedSeed()) > 0 {
items = append(items, layout.Rigid(func(gtx C) D {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return layout.E.Layout(gtx, wsi.toBackup.Layout)
}))
if wsi.IsSlider {
items = append(items, layout.Rigid(layout.Spacer{Height: values.MarginPadding24}.Layout))
}
}
return layout.Flex{Axis: layout.Vertical}.Layout(gtx, items...)
})
}
func (wsi *WalletSyncInfo) pageContentWrapper(gtx C, sectionTitle string, redirectBtn, body layout.Widget) D {
return wsi.Theme.Card().Layout(gtx, func(gtx C) D {
return layout.UniformInset(values.MarginPadding16).Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Inset{
Bottom: values.MarginPadding16,
}.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
if sectionTitle == "" {
return D{}
}
txt := wsi.Theme.Body1(sectionTitle)
txt.Font.Weight = font.SemiBold
return txt.Layout(gtx)
}),
layout.Flexed(1, func(gtx C) D {
if redirectBtn != nil {
return layout.E.Layout(gtx, redirectBtn)
}
return D{}
}),
)
})
}),
layout.Rigid(body),
)
})
})
}
func (wsi *WalletSyncInfo) walletNameAndBackupInfo(gtx C) D {
items := make([]layout.FlexChild, 0)
if wsi.IsSlider {
items = append(items, layout.Rigid(func(gtx C) D {
return layout.Inset{
Right: values.MarginPadding10,
}.Layout(gtx, func(gtx C) D {
icon := wsi.Theme.AssetIcon(wsi.wallet.GetAssetType())
if wsi.wallet.IsWatchingOnlyWallet() {
icon = wsi.Theme.WatchOnlyAssetIcon(wsi.wallet.GetAssetType())
}
return icon.Layout16dp(gtx)
})
}))
}
items = append(items, layout.Rigid(func(gtx C) D {
return layout.Inset{
Right: values.MarginPadding10,
}.Layout(gtx, func(gtx C) D {
txt := wsi.Theme.Body1(strings.ToUpper(wsi.wallet.GetWalletName()))
txt.Font.Weight = font.SemiBold
return txt.Layout(gtx)
})
}))
if len(wsi.wallet.GetEncryptedSeed()) > 0 {
items = append(items, layout.Rigid(func(gtx C) D {
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(wsi.Theme.Icons.RedAlert.Layout20dp),
layout.Rigid(func(gtx C) D {
return layout.Inset{
Left: values.MarginPadding9,
Right: values.MarginPadding16,
}.Layout(gtx, wsi.Theme.Body2(values.String(values.StrBackupWarning)).Layout)
}),
)
}))
}
if wsi.IsSlider {
items = append(items, layout.Flexed(1, func(gtx C) D {
return layout.E.Layout(gtx, wsi.ForwardButton.Layout)
}))
}
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx, items...)
}
// syncStatusSection lays out content for displaying sync status.
func (wsi *WalletSyncInfo) syncStatusSection(gtx C) D {
isBtcAsset := wsi.wallet.GetAssetType() == libutils.BTCWalletAsset
isLtcAsset := wsi.wallet.GetAssetType() == libutils.LTCWalletAsset
syncing := wsi.wallet.IsSyncing()
// btcwallet does not export implementation to track address discovery.
// During btc address discovery, show the normal synced info page with an
// extra label showing the address discovery is in progress.
rescanning := wsi.wallet.IsRescanning() && !isLtcAsset && !isBtcAsset && !syncing
uniform := layout.Inset{Top: values.MarginPadding5, Bottom: values.MarginPadding5}
return wsi.Theme.Card().Layout(gtx, func(gtx C) D {
return Container{Padding: layout.Inset{
Top: values.MarginPadding15,
Bottom: values.MarginPadding16,
}}.Layout(gtx, func(gtx C) D {
items := []layout.FlexChild{layout.Rigid(func(gtx C) D {
return layout.Inset{Bottom: values.MarginPadding20}.Layout(gtx, wsi.syncBoxTitleRow)
})}
if syncing || rescanning {
items = append(items, layout.Rigid(func(gtx C) D {
return layout.Inset{Bottom: values.MarginPadding20}.Layout(gtx, func(gtx C) D {
return layout.Flex{}.Layout(gtx,
layout.Flexed(.93, wsi.progressBarRow),
layout.Rigid(wsi.syncStatusIcon),
)
})
}))
}
if rescanning {
items = append(items, layout.Rigid(func(gtx C) D {
return wsi.rescanDetailsLayout(gtx, uniform)
}))
} else {
items = append(items, layout.Rigid(func(gtx C) D {
return wsi.syncContent(gtx, uniform)
}))
}
return layout.Flex{Axis: layout.Vertical}.Layout(gtx, items...)
})
})
}
func (wsi *WalletSyncInfo) rescanDetailsLayout(gtx C, inset layout.Inset) D {
if !wsi.wallet.IsRescanning() || wsi.rescanUpdate == nil {
return D{}
}
return layout.Inset{Top: values.MarginPadding10}.Layout(gtx, func(gtx C) D {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
card := wsi.Theme.Card()
card.Color = wsi.Theme.Color.Gray4
return card.Layout(gtx, func(gtx C) D {
return Container{Padding: layout.UniformInset(values.MarginPadding16)}.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return inset.Layout(gtx, func(gtx C) D {
return wsi.Theme.Body1(wsi.wallet.GetWalletName()).Layout(gtx)
})
}),
layout.Rigid(func(gtx C) D {
return inset.Layout(gtx, func(gtx C) D {
headersFetchedTitleLabel := wsi.labelTexSize16Layout(values.String(values.StrBlocksScanned), 0, true)
blocksScannedLabel := wsi.labelTexSize16Layout(fmt.Sprint(wsi.rescanUpdate.CurrentRescanHeight), 0, false)
return EndToEndRow(gtx, headersFetchedTitleLabel, blocksScannedLabel)
})
}),
layout.Rigid(func(gtx C) D {
return inset.Layout(gtx, func(gtx C) D {
progressTitleLabel := wsi.labelTexSize16Layout(values.String(values.StrSyncingProgress), 0, true)
rescanProgress := values.StringF(values.StrBlocksLeft, wsi.rescanUpdate.TotalHeadersToScan-wsi.rescanUpdate.CurrentRescanHeight)
blocksScannedLabel := wsi.labelTexSize16Layout(rescanProgress, 0, false)
return EndToEndRow(gtx, progressTitleLabel, blocksScannedLabel)
})
}),
)
})
})
})
}
// syncContent lays out sync status content when the wallet is syncing, synced, not connected
func (wsi *WalletSyncInfo) syncContent(gtx C, uniform layout.Inset) D {
isBtcAsset := wsi.wallet.GetAssetType() == libutils.BTCWalletAsset
isLtcAsset := wsi.wallet.GetAssetType() == libutils.LTCWalletAsset
isSyncing := wsi.wallet.IsSyncing()
isBtcORLtcAsset := isBtcAsset || isLtcAsset
// Rescanning should happen on a synced chain.
isRescanning := wsi.wallet.IsRescanning() && !isSyncing
isInProgress := isSyncing || isRescanning
bestBlock := wsi.wallet.GetBestBlock()
isAddDiscovering := false
syncIsScanning := false
if !isBtcORLtcAsset {
isAddDiscovering = wsi.wallet.(*dcr.Asset).IsAddressDiscovering()
syncIsScanning = wsi.wallet.(*dcr.Asset).IsSycnRescanning()
}
dp8 := values.MarginPadding8
return uniform.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(wsi.labelTexSize16Layout(values.String(values.StrLatestBlock), dp8, true)),
layout.Rigid(func(gtx C) D {
if !isInProgress {
return D{}
}
return wsi.labelTexSize16Layout(values.String(values.StrBlockHeaderFetched), dp8, true)(gtx)
}),
layout.Rigid(func(gtx C) D {
if isRescanning && (isBtcORLtcAsset) {
return D{}
}
return wsi.labelTexSize16Layout(values.String(values.StrSyncingProgress), dp8, true)(gtx)
}),
layout.Rigid(func(gtx C) D {
if !isInProgress || (isRescanning && isBtcORLtcAsset) {
return D{}
}
return wsi.labelTexSize16Layout(values.String(values.StrSyncCompTime), dp8, true)(gtx)
}),
layout.Rigid(func(gtx C) D {
if !(isRescanning && isBtcORLtcAsset) {
return D{}
}
return wsi.labelTexSize16Layout(values.String(values.StrAddressDiscoveryInProgress), dp8, true)(gtx)
}),
)
}),
layout.Flexed(1, func(gtx C) D {
return layout.E.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical, Alignment: layout.End}.Layout(gtx,
layout.Rigid(func(gtx C) D {
latestBlockTitle := fmt.Sprintf("%d (%s)", bestBlock.Height, TimeAgo(bestBlock.Timestamp))
return wsi.labelTexSize16Layout(latestBlockTitle, dp8, false)(gtx)
}),
layout.Rigid(func(gtx C) D {
if !isInProgress || (isRescanning && (isBtcORLtcAsset)) {
return D{}
}
header := wsi.FetchSyncProgress().HeadersToFetchOrScan
// When progress's state is rescan header is a header of rescan and not fetch
// this is a workaround display block for user
if header < bestBlock.Height {
header = bestBlock.Height
}
blockHeightFetched := values.StringF(values.StrBlockHeaderFetchedCount, bestBlock.Height, header)
return wsi.labelTexSize16Layout(blockHeightFetched, dp8, false)(gtx)
}),
layout.Rigid(func(gtx C) D {
currentSeconds := time.Now().Unix()
w := wsi.wallet
daysBehind := TimeFormat(int(currentSeconds-w.GetBestBlockTimeStamp()), true)
syncProgress := values.String(values.StrWalletNotSynced)
if wsi.wallet.IsSyncing() {
syncProgress = values.StringF(values.StrSyncingProgressStat, daysBehind)
if !isBtcORLtcAsset {
if isAddDiscovering {
syncProgress = values.String(values.StrAddressDiscovering)
} else if syncIsScanning {
syncProgress = values.String(values.StrRescanningBlocks)
}
}
} else if wsi.wallet.IsRescanning() {
syncProgress = values.String(values.StrRescanningBlocks)
} else if wsi.wallet.IsSynced() {
syncProgress = values.String(values.StrComplete)
}
return wsi.labelTexSize16Layout(syncProgress, dp8, false)(gtx)
}),
layout.Rigid(func(gtx C) D {
if !isInProgress || (isRescanning && isBtcORLtcAsset) {
return D{}
}
_, timeLeft := wsi.progressStatusDetails()
return wsi.labelTexSize16Layout(timeLeft, dp8, false)(gtx)
}),
)
})
}),
)
})
}
// syncBoxTitleRow lays out widgets in the title row inside the sync status box.
func (wsi *WalletSyncInfo) syncBoxTitleRow(gtx C) D {
textSize14 := values.TextSize14
statusLabel := wsi.Theme.Label(wsi.ConvertTextSize(textSize14), values.String(values.StrOffline))
wsi.walletStatusIcon.Color = wsi.Theme.Color.Danger
if wsi.wallet.IsConnectedToNetwork() {
statusLabel.Text = values.String(values.StrOnline)
wsi.walletStatusIcon.Color = wsi.Theme.Color.Success
}
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.End}.Layout(gtx,
layout.Rigid(func(gtx C) D {
if wsi.IsMobileView() {
return D{} // not enough space
}
return wsi.labelSize(textSize14, values.String(values.StrWalletStatus)).Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
if wsi.wallet.IsSyncShuttingDown() {
return layout.Inset{
Left: values.MarginPadding4,
}.Layout(gtx, wsi.labelSize(textSize14, values.String(values.StrCanceling)).Layout)
}
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
inset := layout.Inset{Right: values.MarginPadding4}
if !wsi.IsMobileView() {
inset.Left = values.MarginPadding4
}
return inset.Layout(gtx, func(gtx C) D {
return wsi.walletStatusIcon.Layout(gtx, values.MarginPadding15)
})
}),
layout.Rigid(statusLabel.Layout),
layout.Rigid(func(gtx C) D {
if wsi.wallet.IsConnectedToNetwork() {
connectedPeers := fmt.Sprintf("%d", wsi.wallet.ConnectedPeers())
return wsi.labelSize(textSize14, values.StringF(values.StrConnectedTo, connectedPeers)).Layout(gtx)
}
if !wsi.isStatusConnected {
return wsi.labelSize(textSize14, values.String(values.StrNoInternet)).Layout(gtx)
}
return wsi.labelSize(textSize14, values.String(values.StrNoConnectedPeer)).Layout(gtx)
}),
)
}),
layout.Flexed(1, func(gtx C) D {
return layout.E.Layout(gtx, wsi.layoutAutoSyncSection)
}),
)
}
func (wsi *WalletSyncInfo) labelSize(size unit.Sp, txt string) cryptomaterial.Label {
return wsi.Theme.Label(wsi.ConvertTextSize(size), txt)
}
func (wsi *WalletSyncInfo) labelTexSize16Layout(txt string, bottomInset unit.Dp, colorGrey bool) func(gtx C) D {
return func(gtx C) D {
lbl := wsi.Theme.Body1(txt)
if colorGrey {
lbl.Color = wsi.Theme.Color.GrayText2
}
lbl.TextSize = wsi.ConvertTextSize(values.TextSize16)
return layout.Inset{Bottom: bottomInset}.Layout(gtx, lbl.Layout)
}
}
// progressBarRow lays out the progress bar.
func (wsi *WalletSyncInfo) progressBarRow(gtx C) D {
return layout.Inset{Right: values.MarginPadding5}.Layout(gtx, func(gtx C) D {
progress, _ := wsi.progressStatusDetails()
p := wsi.Theme.ProgressBar(progress)
p.Height = values.MarginPadding16
p.Radius = cryptomaterial.Radius(4)
p.Color = wsi.Theme.Color.Success
p.TrackColor = wsi.Theme.Color.Gray2
progressTitleLabel := wsi.labelSize(values.TextSize14, fmt.Sprintf("%v%%", progress))
progressTitleLabel.Color = wsi.Theme.Color.Text
return p.TextLayout(gtx, progressTitleLabel.Layout)
})
}
// progressStatusRow lays out the progress status when the wallet is syncing.
func (wsi *WalletSyncInfo) progressStatusDetails() (int, string) {
timeLeftLabel := ""
pgrss := wsi.FetchSyncProgress()
timeLeft := pgrss.remainingSyncTime
progress := pgrss.syncProgress
walletIsRescanning := wsi.wallet.IsRescanning()
if walletIsRescanning && wsi.rescanUpdate != nil {
progress = int(wsi.rescanUpdate.RescanProgress)
timeLeft = TimeFormat(int(wsi.rescanUpdate.RescanTimeRemaining), true)
}
if wsi.wallet.IsSyncing() || walletIsRescanning {
timeLeftLabel = values.StringF(values.StrTimeLeftFmt, timeLeft)
if progress == 0 {
timeLeftLabel = values.String(values.StrLoading)
}
}
return progress, timeLeftLabel
}
func (wsi *WalletSyncInfo) layoutAutoSyncSection(gtx C) D {
return layout.Flex{}.Layout(gtx,
layout.Rigid(func(gtx C) D {
wsi.syncSwitch.SetChecked(wsi.wallet.IsSyncing() || wsi.wallet.IsSynced())
return layout.Inset{Right: values.MarginPadding10}.Layout(gtx, wsi.syncSwitch.Layout)
}),
layout.Rigid(wsi.Theme.Body2(values.String(values.StrSync)).Layout),
)
}
func (wsi *WalletSyncInfo) FetchSyncProgress() ProgressInfo {
pgrss, ok := syncProgressInfo[wsi.wallet]
if !ok {
pgrss = ProgressInfo{}
}
// remove the unnecessary sync progress data if already synced.
wsi.deleteSyncProgress()
return pgrss
}
// deleteSyncProgress removes the map entry after the data persisted is no longer necessary.
func (wsi *WalletSyncInfo) deleteSyncProgress() {
wal := wsi.wallet
if wal.IsSynced() {
delete(syncProgressInfo, wal)
}
}
func (wsi *WalletSyncInfo) syncStatusIcon(gtx C) D {
icon := wsi.Theme.Icons.SyncingIcon
if wsi.wallet.IsSynced() {
icon = wsi.Theme.Icons.SuccessIcon
} else if wsi.wallet.IsSyncing() {
icon = wsi.Theme.Icons.SyncingIcon
}
i := layout.Inset{Left: values.MarginPadding16}
return i.Layout(gtx, func(gtx C) D {
return icon.LayoutSize(gtx, wsi.ConvertIconSize(values.MarginPadding20))
})
}
// ListenForNotifications starts a goroutine to watch for sync updates and
// update the UI accordingly. To prevent UI lags, this method does not refresh
// the window display every time a sync update is received. During active blocks
// sync, rescan or proposals sync, the Layout method auto refreshes the display
// every set interval. Other sync updates that affect the UI but occur outside
// of an active sync requires a display refresh. The caller of this method must
// ensure that the StopListeningForNotifications() method is called whenever the
// the page or modal using these notifications is closed.
func (wsi *WalletSyncInfo) ListenForNotifications() {
updateSyncProgress := func(progress ProgressInfo) {
// Update sync progress fields which will be displayed
// when the next UI invalidation occurs.
previousProgress := wsi.FetchSyncProgress()
// headers to fetch cannot be less than the previously fetched.
// Page refresh only needed if there is new data to update the UI.
if progress.HeadersToFetchOrScan >= previousProgress.HeadersToFetchOrScan {
// set the new progress against the associated asset.
syncProgressInfo[wsi.wallet] = progress
// We only care about sync state changes here, to
// refresh the window display.
wsi.reload()
}
}
syncProgressListener := &sharedW.SyncProgressListener{
OnHeadersFetchProgress: func(t *sharedW.HeadersFetchProgressReport) {
progress := ProgressInfo{}
progress.stepFetchProgress = t.HeadersFetchProgress
progress.HeadersToFetchOrScan = t.TotalHeadersToFetch
progress.syncProgress = int(t.TotalSyncProgress)
progress.remainingSyncTime = TimeFormat(int(t.TotalTimeRemainingSeconds), true)
updateSyncProgress(progress)
},
OnAddressDiscoveryProgress: func(t *sharedW.AddressDiscoveryProgressReport) {
progress := ProgressInfo{}
progress.syncProgress = int(t.TotalSyncProgress)
progress.remainingSyncTime = TimeFormat(int(t.TotalTimeRemainingSeconds), true)
progress.stepFetchProgress = t.AddressDiscoveryProgress
updateSyncProgress(progress)
},
OnHeadersRescanProgress: func(t *sharedW.HeadersRescanProgressReport) {
progress := ProgressInfo{}
progress.HeadersToFetchOrScan = t.TotalHeadersToScan
progress.syncProgress = int(t.TotalSyncProgress)
progress.remainingSyncTime = TimeFormat(int(t.TotalTimeRemainingSeconds), true)
progress.stepFetchProgress = t.RescanProgress
updateSyncProgress(progress)
},
OnSyncCompleted: func() {
wsi.reload()
},
}
err := wsi.wallet.AddSyncProgressListener(syncProgressListener, WalletSyncInfoID)
if err != nil {
log.Errorf("Error adding sync progress listener: %v", err)
return
}
txAndBlockNotificationListener := &sharedW.TxAndBlockNotificationListener{
OnTransaction: func(walletID int, transaction *sharedW.Transaction) {
wsi.reload()
},
OnBlockAttached: func(walletID int, blockHeight int32) {
wsi.reload()
},
}
err = wsi.wallet.AddTxAndBlockNotificationListener(txAndBlockNotificationListener, WalletSyncInfoID)
if err != nil {
log.Errorf("Error adding tx and block notification listener: %v", err)
return
}
blocksRescanProgressListener := &sharedW.BlocksRescanProgressListener{
OnBlocksRescanStarted: func(walletID int) {
wsi.rescanUpdate = nil
},
OnBlocksRescanProgress: func(progress *sharedW.HeadersRescanProgressReport) {
wsi.rescanUpdate = progress
},
OnBlocksRescanEnded: func(walletID int, err error) {
wsi.rescanUpdate = nil
wsi.reload()
},
}
wsi.wallet.SetBlocksRescanProgressListener(blocksRescanProgressListener)
}
// StopListeningForNotifications stops listening for sync progress, tx and block
// notifications.
func (wsi *WalletSyncInfo) StopListeningForNotifications() {
wsi.wallet.RemoveSyncProgressListener(WalletSyncInfoID)
wsi.wallet.RemoveTxAndBlockNotificationListener(WalletSyncInfoID)
wsi.wallet.SetBlocksRescanProgressListener(nil)
}
func (wsi *WalletSyncInfo) handle() {
// As long as the internet connection hasn't been established keep checking.
if !wsi.isStatusConnected {
go func() {
wsi.isStatusConnected = libutils.IsOnline()
}()
}
isSyncShutting := wsi.wallet.IsSyncShuttingDown()
wsi.syncSwitch.SetEnabled(!isSyncShutting)
if wsi.syncSwitch.Changed() {
if wsi.wallet.IsRescanning() {
wsi.wallet.CancelRescan()
}
go func() {
wsi.ToggleSync(wsi.wallet, func(b bool) {
wsi.syncSwitch.SetChecked(b)
wsi.wallet.SaveUserConfigValue(sharedW.AutoSyncConfigKey, b)
})
}()
}
if wsi.toBackup.Button.Clicked() {
wsi.backup(wsi.wallet)
}
}