1
+ import 'dart:async' ;
1
2
import 'dart:math' ;
2
3
3
4
import 'package:ew_test_keys/ew_test_keys.dart' ;
4
5
import 'package:flutter/cupertino.dart' ;
5
6
import 'package:flutter/material.dart' ;
6
7
import 'package:flutter_mobx/flutter_mobx.dart' ;
7
8
import 'package:flutter_svg/flutter_svg.dart' ;
8
- import 'package:focus_detector/focus_detector.dart' ;
9
9
import 'package:iconsax/iconsax.dart' ;
10
- import 'package:pausable_timer/pausable_timer.dart' ;
11
10
import 'package:provider/provider.dart' ;
12
11
import 'package:sliding_up_panel/sliding_up_panel.dart' ;
13
12
import 'package:upgrader/upgrader.dart' ;
@@ -33,8 +32,6 @@ import 'package:encointer_wallet/page/assets/account_or_community/account_or_com
33
32
import 'package:encointer_wallet/page/assets/account_or_community/switch_account_or_community.dart' ;
34
33
import 'package:encointer_wallet/page/assets/receive/receive_page.dart' ;
35
34
import 'package:encointer_wallet/page/assets/transfer/transfer_page.dart' ;
36
- import 'package:encointer_wallet/service/log/log_service.dart' ;
37
- import 'package:encointer_wallet/service/notification/lib/notification.dart' ;
38
35
import 'package:encointer_wallet/service/substrate_api/api.dart' ;
39
36
import 'package:encointer_wallet/service/tx/lib/tx.dart' ;
40
37
import 'package:encointer_wallet/store/account/types/account_data.dart' ;
@@ -57,7 +54,6 @@ class _AssetsViewState extends State<AssetsView> {
57
54
static const double fractionOfScreenHeight = .7 ;
58
55
static const double avatarSize = 70 ;
59
56
late PanelController _panelController;
60
- late PausableTimer _balanceWatchdog;
61
57
late AppSettings _appSettingsStore;
62
58
late double _panelHeightOpen;
63
59
final double _panelHeightClosed = 0 ;
@@ -75,7 +71,6 @@ class _AssetsViewState extends State<AssetsView> {
75
71
@override
76
72
void didChangeDependencies () {
77
73
_appSettingsStore = context.read <AppSettings >();
78
- _startBalanceWatchdog ();
79
74
l10n = context.l10n;
80
75
// Should typically not be higher than panelHeight, but on really small devices
81
76
// it should not exceed fractionOfScreenHeight x the screen height.
@@ -88,32 +83,16 @@ class _AssetsViewState extends State<AssetsView> {
88
83
89
84
@override
90
85
void dispose () {
91
- _balanceWatchdog.cancel ();
92
86
super .dispose ();
93
87
}
94
88
95
89
@override
96
90
Widget build (BuildContext context) {
97
- return FocusDetector (
98
- onFocusLost: () {
99
- Log .d ('[home:FocusDetector] Focus Lost.' );
100
- _balanceWatchdog.pause ();
101
- },
102
- onFocusGained: () {
103
- Log .d ('[home:FocusDetector] Focus Gained.' );
104
- if (! widget.store.settings.loading) {
105
- _refreshBalanceAndNotify ();
106
- }
107
- _balanceWatchdog
108
- ..reset ()
109
- ..start ();
110
- },
111
- child: Scaffold (
112
- appBar: _appBar (),
113
- body: RepositoryProvider .of <AppConfig >(context).isIntegrationTest
114
- ? _slidingUpPanel (_appBar ())
115
- : _upgradeAlert (_appBar ()),
116
- ),
91
+ return Scaffold (
92
+ appBar: _appBar (),
93
+ body: RepositoryProvider .of <AppConfig >(context).isIntegrationTest
94
+ ? _slidingUpPanel (_appBar ())
95
+ : _upgradeAlert (_appBar ()),
117
96
);
118
97
}
119
98
@@ -337,7 +316,7 @@ class _AssetsViewState extends State<AssetsView> {
337
316
},
338
317
onAddIconPressed: () {
339
318
Navigator .pushNamed (context, CommunityChooserOnMap .route).then ((_) {
340
- _refreshBalanceAndNotify ();
319
+ _refreshEncointerState ();
341
320
});
342
321
},
343
322
addIconButtonKey: const Key (EWTestKeys .addCommunity),
@@ -349,7 +328,7 @@ class _AssetsViewState extends State<AssetsView> {
349
328
accountOrCommunityData: initAllAccounts (),
350
329
onTap: (int index) async {
351
330
await switchAccount (widget.store.account.accountListAll[index]);
352
- _refreshBalanceAndNotify ( );
331
+ unawaited ( _refreshEncointerState () );
353
332
},
354
333
onAddIconPressed: () {
355
334
Navigator .of (context).pushNamed (AddAccountView .route);
@@ -434,59 +413,10 @@ class _AssetsViewState extends State<AssetsView> {
434
413
});
435
414
}
436
415
437
- void _refreshBalanceAndNotify () {
438
- final currentAddress = widget.store.account.currentAddress;
439
- final chosenCid = widget.store.encointer.chosenCid;
440
-
441
- if (currentAddress.isEmpty || chosenCid == null ) {
442
- Log .d ('[home:refreshBalanceAndNotify] address empty or chosenCid == null' , 'Assets' );
443
- }
444
-
445
- webApi.encointer.getEncointerBalance (currentAddress, chosenCid! ).then ((balanceEntry) {
446
- final cidStr = chosenCid.toFmtString ();
447
- Log .d ('[home:refreshBalanceAndNotify] getEncointerBalance' , 'Assets' );
448
-
449
- final community = widget.store.encointer.community! ;
450
- final demurrageRate = widget.store.encointer.community! .demurrage! ;
451
-
452
- final delta = widget.store.encointer.account!
453
- .addBalanceEntryAndReturnDelta (chosenCid, balanceEntry, community.applyDemurrage! );
454
-
455
- Log .d ('[home:refreshBalanceAndNotify] getEncointerBalance balance delta: $delta ' , 'Assets' );
456
-
457
- if (delta.abs () > demurrageRate) {
458
- if (delta > demurrageRate) {
459
- final msg = l10n.incomingConfirmed (
460
- delta,
461
- community.metadata! .symbol,
462
- widget.store.account.currentAccount.name,
463
- );
464
- Log .d ('[home:balanceWatchdog] $msg ' , 'Assets' );
465
- NotificationPlugin .showNotification (45 , l10n.fundsReceived, msg, cid: cidStr);
466
- }
467
- }
468
- }).catchError ((Object ? e, StackTrace ? s) {
469
- Log .e ('[home:refreshBalanceAndNotify] WARNING: could not update balance: $e ' , 'Assets' , s);
470
- });
471
- }
472
-
473
- void _startBalanceWatchdog () {
474
- _balanceWatchdog = PausableTimer (
475
- const Duration (seconds: 12 ),
476
- () {
477
- Log .d ('[balanceWatchdog] triggered' , 'Assets' );
478
-
479
- _refreshBalanceAndNotify ();
480
- _balanceWatchdog
481
- ..reset ()
482
- ..start ();
483
- },
484
- )..start ();
485
- }
486
-
487
416
Future <void > _refreshEncointerState () async {
488
417
// getCurrentPhase is the root of all state updates.
489
418
await webApi.encointer.getCurrentPhase ();
419
+ await widget.store.encointer.getEncointerBalance ();
490
420
}
491
421
}
492
422
0 commit comments