Skip to content

Commit 5985682

Browse files
committed
[assets] remove balance watchdog that was very slow and kind of unreliable and update encointer balance with the rest of the state periodically
1 parent 9e3b39e commit 5985682

File tree

2 files changed

+21
-79
lines changed

2 files changed

+21
-79
lines changed

app/lib/page/assets/index.dart

+9-79
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
import 'dart:async';
12
import 'dart:math';
23

34
import 'package:ew_test_keys/ew_test_keys.dart';
45
import 'package:flutter/cupertino.dart';
56
import 'package:flutter/material.dart';
67
import 'package:flutter_mobx/flutter_mobx.dart';
78
import 'package:flutter_svg/flutter_svg.dart';
8-
import 'package:focus_detector/focus_detector.dart';
99
import 'package:iconsax/iconsax.dart';
10-
import 'package:pausable_timer/pausable_timer.dart';
1110
import 'package:provider/provider.dart';
1211
import 'package:sliding_up_panel/sliding_up_panel.dart';
1312
import 'package:upgrader/upgrader.dart';
@@ -33,8 +32,6 @@ import 'package:encointer_wallet/page/assets/account_or_community/account_or_com
3332
import 'package:encointer_wallet/page/assets/account_or_community/switch_account_or_community.dart';
3433
import 'package:encointer_wallet/page/assets/receive/receive_page.dart';
3534
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';
3835
import 'package:encointer_wallet/service/substrate_api/api.dart';
3936
import 'package:encointer_wallet/service/tx/lib/tx.dart';
4037
import 'package:encointer_wallet/store/account/types/account_data.dart';
@@ -57,7 +54,6 @@ class _AssetsViewState extends State<AssetsView> {
5754
static const double fractionOfScreenHeight = .7;
5855
static const double avatarSize = 70;
5956
late PanelController _panelController;
60-
late PausableTimer _balanceWatchdog;
6157
late AppSettings _appSettingsStore;
6258
late double _panelHeightOpen;
6359
final double _panelHeightClosed = 0;
@@ -75,7 +71,6 @@ class _AssetsViewState extends State<AssetsView> {
7571
@override
7672
void didChangeDependencies() {
7773
_appSettingsStore = context.read<AppSettings>();
78-
_startBalanceWatchdog();
7974
l10n = context.l10n;
8075
// Should typically not be higher than panelHeight, but on really small devices
8176
// it should not exceed fractionOfScreenHeight x the screen height.
@@ -88,32 +83,16 @@ class _AssetsViewState extends State<AssetsView> {
8883

8984
@override
9085
void dispose() {
91-
_balanceWatchdog.cancel();
9286
super.dispose();
9387
}
9488

9589
@override
9690
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()),
11796
);
11897
}
11998

@@ -337,7 +316,7 @@ class _AssetsViewState extends State<AssetsView> {
337316
},
338317
onAddIconPressed: () {
339318
Navigator.pushNamed(context, CommunityChooserOnMap.route).then((_) {
340-
_refreshBalanceAndNotify();
319+
_refreshEncointerState();
341320
});
342321
},
343322
addIconButtonKey: const Key(EWTestKeys.addCommunity),
@@ -349,7 +328,7 @@ class _AssetsViewState extends State<AssetsView> {
349328
accountOrCommunityData: initAllAccounts(),
350329
onTap: (int index) async {
351330
await switchAccount(widget.store.account.accountListAll[index]);
352-
_refreshBalanceAndNotify();
331+
unawaited(_refreshEncointerState());
353332
},
354333
onAddIconPressed: () {
355334
Navigator.of(context).pushNamed(AddAccountView.route);
@@ -434,59 +413,10 @@ class _AssetsViewState extends State<AssetsView> {
434413
});
435414
}
436415

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-
487416
Future<void> _refreshEncointerState() async {
488417
// getCurrentPhase is the root of all state updates.
489418
await webApi.encointer.getCurrentPhase();
419+
await widget.store.encointer.getEncointerBalance();
490420
}
491421
}
492422

app/lib/store/encointer/encointer.dart

+12
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ abstract class _EncointerStore with Store {
348348
webApi.encointer.getReputations(),
349349
webApi.encointer.getMeetupTime(),
350350
webApi.encointer.getMeetupTimeOverride(),
351+
getEncointerBalance(),
351352
updateAggregatedAccountData(),
352353
]).timeout(const Duration(seconds: 15)).catchError((Object? e, s) {
353354
Log.e('Error executing update state: $e', 'EncointerStore');
@@ -360,6 +361,17 @@ abstract class _EncointerStore with Store {
360361
await _updateStateFuture!;
361362
}
362363

364+
Future<void> getEncointerBalance() async {
365+
final currentAddress = _rootStore.account.currentAddress;
366+
367+
if (currentAddress.isEmpty || chosenCid == null) {
368+
Log.d('[getEncointerBalance] address empty or chosenCid == null', 'EncointerStore');
369+
}
370+
371+
final balanceEntry = await webApi.encointer.getEncointerBalance(currentAddress, chosenCid!);
372+
_rootStore.encointer.account?.addBalanceEntry(chosenCid!, balanceEntry);
373+
}
374+
363375
Future<void> updateAggregatedAccountData() async {
364376
try {
365377
final currentPubKey = _rootStore.account.currentAccountPubKey;

0 commit comments

Comments
 (0)