Skip to content

Commit

Permalink
Merge tag 'v1.13.2' into f-droid
Browse files Browse the repository at this point in the history
  • Loading branch information
clangenb committed Mar 6, 2024
2 parents 0525eac + eac68f1 commit 5ff7984
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 126 deletions.
25 changes: 5 additions & 20 deletions app/lib/common/components/logo/community_icon.dart
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';

import 'package:encointer_wallet/config/consts.dart';
import 'package:encointer_wallet/theme/theme.dart';
import 'package:encointer_wallet/store/app.dart';

class CommunityIconObserver extends StatelessWidget {
const CommunityIconObserver({super.key, double? radius}) : radius = radius ?? 10;
class CommunityCircleAvatar extends StatelessWidget {
const CommunityCircleAvatar(this.icon, {super.key, double? radius}) : radius = radius ?? 10;

final SvgPicture icon;

final double radius;

@override
Widget build(BuildContext context) {
final store = context.watch<AppStore>();
return CircleAvatar(
backgroundColor: context.colorScheme.background,
radius: radius,
child: Observer(
builder: (_) {
if (store.encointer.community != null && store.encointer.community!.assetsCid != null) {
if (store.encointer.community!.communityIcon != null) {
return SvgPicture.string(store.encointer.community!.communityIcon!);
} else {
return SvgPicture.asset(fallBackCommunityIcon);
}
} else {
return SvgPicture.asset(fallBackCommunityIcon);
}
},
),
child: icon,
);
}
}
16 changes: 13 additions & 3 deletions app/lib/page-encointer/common/community_chooser_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:encointer_wallet/common/components/address_icon.dart';
import 'package:encointer_wallet/common/components/logo/community_icon.dart';
import 'package:encointer_wallet/store/app.dart';
import 'package:encointer_wallet/utils/format.dart';
import 'package:flutter_svg/svg.dart';

/// the CombinedCommunityAndAccountAvatar should be wrapped in an InkWell to provide the callback on a click
class CombinedCommunityAndAccountAvatar extends StatefulWidget {
Expand Down Expand Up @@ -42,7 +43,10 @@ class _CombinedCommunityAndAccountAvatarState extends State<CombinedCommunityAnd
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(widget.communityAvatarSize),
),
child: CommunityAvatar(avatarSize: widget.communityAvatarSize),
child: CommunityAvatar(
widget.store.encointer.communityIconOrDefault,
avatarSize: widget.communityAvatarSize,
),
),
Positioned(
bottom: 0,
Expand Down Expand Up @@ -72,7 +76,13 @@ class _CombinedCommunityAndAccountAvatarState extends State<CombinedCommunityAnd
}

class CommunityAvatar extends StatelessWidget {
const CommunityAvatar({super.key, this.avatarSize = 120});
const CommunityAvatar(
this.icon, {
super.key,
this.avatarSize = 120,
});

final SvgPicture icon;

final double avatarSize;

Expand All @@ -81,7 +91,7 @@ class CommunityAvatar extends StatelessWidget {
return SizedBox(
width: avatarSize,
height: avatarSize,
child: const CommunityIconObserver(),
child: CommunityCircleAvatar(icon),
);
}
}
5 changes: 4 additions & 1 deletion app/lib/page-encointer/meetup/ceremony_step3_finish.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class CeremonyStep3Finish extends StatelessWidget {
children: [
const CeremonyProgressBar(progress: 3),
const SizedBox(height: 48),
const CommunityAvatar(avatarSize: 96),
CommunityAvatar(
store.encointer.communityIconOrDefault,
avatarSize: 96,
),
Center(
child: Text(
l10n.thankYou,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:encointer_wallet/page/assets/announcement/logic/announcement_car
import 'package:encointer_wallet/models/announcement/announcement.dart';
import 'package:encointer_wallet/config/consts.dart';
import 'package:encointer_wallet/common/components/logo/community_icon.dart';
import 'package:encointer_wallet/store/app.dart';
import 'package:encointer_wallet/gen/assets.gen.dart';
import 'package:encointer_wallet/theme/theme.dart';

Expand All @@ -19,6 +20,8 @@ class AnnouncementCard extends StatelessWidget {

@override
Widget build(BuildContext context) {
final store = context.read<AppStore>();

final local = Localizations.localeOf(context);
final cardStore = context.watch<AnnouncementCardStore>();
return Padding(
Expand All @@ -39,7 +42,10 @@ class AnnouncementCard extends StatelessWidget {
radius: 8,
backgroundImage: Assets.images.public.app.provider(),
)
: const CommunityIconObserver(radius: 8),
: CommunityCircleAvatar(
store.encointer.communityIconOrDefault,
radius: 8,
),
),
title: Align(
alignment: Alignment.centerRight,
Expand Down
106 changes: 9 additions & 97 deletions app/lib/page/assets/index.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import 'dart:async';
import 'dart:math';

import 'package:ew_test_keys/ew_test_keys.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:focus_detector/focus_detector.dart';
import 'package:iconsax/iconsax.dart';
import 'package:pausable_timer/pausable_timer.dart';
import 'package:provider/provider.dart';
import 'package:sliding_up_panel/sliding_up_panel.dart';
import 'package:upgrader/upgrader.dart';
Expand All @@ -26,16 +25,13 @@ import 'package:encointer_wallet/utils/repository_provider.dart';
import 'package:encointer_wallet/config/consts.dart';
import 'package:encointer_wallet/models/index.dart';
import 'package:encointer_wallet/modules/modules.dart';
import 'package:encointer_wallet/models/encointer_balance_data/balance_entry.dart';
import 'package:encointer_wallet/page-encointer/ceremony_box/ceremony_box.dart';
import 'package:encointer_wallet/page-encointer/common/community_chooser_on_map.dart';
import 'package:encointer_wallet/page-encointer/common/community_chooser_panel.dart';
import 'package:encointer_wallet/page/assets/account_or_community/account_or_community_data.dart';
import 'package:encointer_wallet/page/assets/account_or_community/switch_account_or_community.dart';
import 'package:encointer_wallet/page/assets/receive/receive_page.dart';
import 'package:encointer_wallet/page/assets/transfer/transfer_page.dart';
import 'package:encointer_wallet/service/log/log_service.dart';
import 'package:encointer_wallet/service/notification/lib/notification.dart';
import 'package:encointer_wallet/service/substrate_api/api.dart';
import 'package:encointer_wallet/service/tx/lib/tx.dart';
import 'package:encointer_wallet/store/account/types/account_data.dart';
Expand All @@ -58,7 +54,6 @@ class _AssetsViewState extends State<AssetsView> {
static const double fractionOfScreenHeight = .7;
static const double avatarSize = 70;
late PanelController _panelController;
late PausableTimer _balanceWatchdog;
late AppSettings _appSettingsStore;
late double _panelHeightOpen;
final double _panelHeightClosed = 0;
Expand All @@ -76,7 +71,6 @@ class _AssetsViewState extends State<AssetsView> {
@override
void didChangeDependencies() {
_appSettingsStore = context.read<AppSettings>();
_startBalanceWatchdog();
l10n = context.l10n;
// Should typically not be higher than panelHeight, but on really small devices
// it should not exceed fractionOfScreenHeight x the screen height.
Expand All @@ -89,32 +83,16 @@ class _AssetsViewState extends State<AssetsView> {

@override
void dispose() {
_balanceWatchdog.cancel();
super.dispose();
}

@override
Widget build(BuildContext context) {
return FocusDetector(
onFocusLost: () {
Log.d('[home:FocusDetector] Focus Lost.');
_balanceWatchdog.pause();
},
onFocusGained: () {
Log.d('[home:FocusDetector] Focus Gained.');
if (!widget.store.settings.loading) {
_refreshBalanceAndNotify();
}
_balanceWatchdog
..reset()
..start();
},
child: Scaffold(
appBar: _appBar(),
body: RepositoryProvider.of<AppConfig>(context).isIntegrationTest
? _slidingUpPanel(_appBar())
: _upgradeAlert(_appBar()),
),
return Scaffold(
appBar: _appBar(),
body: RepositoryProvider.of<AppConfig>(context).isIntegrationTest
? _slidingUpPanel(_appBar())
: _upgradeAlert(_appBar()),
);
}

Expand Down Expand Up @@ -338,7 +316,7 @@ class _AssetsViewState extends State<AssetsView> {
},
onAddIconPressed: () {
Navigator.pushNamed(context, CommunityChooserOnMap.route).then((_) {
_refreshBalanceAndNotify();
_refreshEncointerState();
});
},
addIconButtonKey: const Key(EWTestKeys.addCommunity),
Expand All @@ -350,7 +328,7 @@ class _AssetsViewState extends State<AssetsView> {
accountOrCommunityData: initAllAccounts(),
onTap: (int index) async {
await switchAccount(widget.store.account.accountListAll[index]);
_refreshBalanceAndNotify();
unawaited(_refreshEncointerState());
},
onAddIconPressed: () {
Navigator.of(context).pushNamed(AddAccountView.route);
Expand Down Expand Up @@ -435,76 +413,10 @@ class _AssetsViewState extends State<AssetsView> {
});
}

void _refreshBalanceAndNotify() {
webApi.encointer.getAllBalances(widget.store.account.currentAddress).then((balances) {
Log.d('[home:refreshBalanceAndNotify] get all balances', 'Assets');
if (widget.store.encointer.chosenCid == null) {
Log.d('[home:refreshBalanceAndNotify] no community selected', 'Assets');
return;
}
var activeAccountHasBalance = false;
balances.forEach((cid, balanceEntry) {
final cidStr = cid.toFmtString();
if (widget.store.encointer.communityStores!.containsKey(cidStr)) {
final community = widget.store.encointer.communityStores![cidStr]!;
final oldBalanceEntry =
widget.store.encointer.accountStores?[widget.store.account.currentAddress]?.balanceEntries[cidStr];
final demurrageRate = community.demurrage!;
final newBalance = community.applyDemurrage != null ? community.applyDemurrage!(balanceEntry) ?? 0 : 0;
final oldBalance = (community.applyDemurrage != null && oldBalanceEntry != null)
? community.applyDemurrage!(oldBalanceEntry) ?? 0
: 0;

final delta = newBalance - oldBalance;
Log.d('[home:refreshBalanceAndNotify] balance for $cidStr was $oldBalance, changed by $delta', 'Assets');
if (delta.abs() > demurrageRate) {
widget.store.encointer.accountStores![widget.store.account.currentAddress]
?.addBalanceEntry(cid, balances[cid]!);
if (delta > demurrageRate) {
final msg = l10n.incomingConfirmed(
delta,
community.metadata!.symbol,
widget.store.account.currentAccount.name,
);
Log.d('[home:balanceWatchdog] $msg', 'Assets');
NotificationPlugin.showNotification(45, l10n.fundsReceived, msg, cid: cidStr);
}
}
if (cid == widget.store.encointer.chosenCid) {
activeAccountHasBalance = true;
}
}
});
if (!activeAccountHasBalance) {
Log.d(
"[home:refreshBalanceAndNotify] didn't get any balance for active account. initialize store balance to zero",
'Assets',
);
widget.store.encointer.accountStores![widget.store.account.currentAddress]
?.addBalanceEntry(widget.store.encointer.chosenCid!, BalanceEntry(0, 0));
}
}).catchError((Object? e, StackTrace? s) {
Log.e('[home:refreshBalanceAndNotify] WARNING: could not update balance: $e', 'Assets', s);
});
}

void _startBalanceWatchdog() {
_balanceWatchdog = PausableTimer(
const Duration(seconds: 12),
() {
Log.d('[balanceWatchdog] triggered', 'Assets');

_refreshBalanceAndNotify();
_balanceWatchdog
..reset()
..start();
},
)..start();
}

Future<void> _refreshEncointerState() async {
// getCurrentPhase is the root of all state updates.
await webApi.encointer.getCurrentPhase();
await widget.store.encointer.getEncointerBalance();
}
}

Expand Down
9 changes: 7 additions & 2 deletions app/lib/page/profile/account/account_manage_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,17 @@ class _AccountManagePageState extends State<AccountManagePage> {

final community = _appStore.encointer.communityStores?[cidFmt];

if (community == null) {
if (community == null || community.applyDemurrage == null) {
// Never happened, but we want to be defensive here to prevent a red screen.
Log.e('[AccountManagePage] Communities is null, even though we have a balance entry for it. Fatal app error.');
return Container();
}

if ((community.applyDemurrage!(entry) ?? 0) <= 0.0001 && cidFmt != _appStore.encointer.chosenCid!.toFmtString()) {
Log.p("[AccountManagePage] Don't display community with 0 balance");
return Container();
}

final isBootstrapper = _appStore.encointer.community!.bootstrappers != null &&
_appStore.encointer.community!.bootstrappers!.contains(address);

Expand All @@ -120,7 +125,7 @@ class _AccountManagePageState extends State<AccountManagePage> {
leading: CommunityIcon(
store: _appStore,
isBootstrapper: isBootstrapper,
icon: const CommunityIconObserver(),
icon: CommunityCircleAvatar(community.icon),
),
title: Text(community.name!, style: h3),
subtitle: Text(community.symbol!, style: h3),
Expand Down
23 changes: 23 additions & 0 deletions app/lib/store/encointer/encointer.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:flutter_svg/svg.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:mobx/mobx.dart';

Expand All @@ -15,6 +16,7 @@ import 'package:encointer_wallet/store/encointer/sub_stores/bazaar_store/bazaar_
import 'package:encointer_wallet/store/encointer/sub_stores/community_store/community_account_store/community_account_store.dart';
import 'package:encointer_wallet/store/encointer/sub_stores/community_store/community_store.dart';
import 'package:encointer_wallet/store/encointer/sub_stores/encointer_account_store/encointer_account_store.dart';
import 'package:encointer_wallet/config/consts.dart';

part 'encointer.g.dart';

Expand Down Expand Up @@ -176,6 +178,15 @@ abstract class _EncointerStore with Store {
return applyDemurrage(communityBalanceEntry);
}

@computed
SvgPicture get communityIconOrDefault {
if (community != null) {
return community!.icon;
} else {
return SvgPicture.asset(fallBackCommunityIcon);
}
}

double? applyDemurrage(BalanceEntry? entry) {
if (_rootStore.chain.latestHeaderNumber != null &&
entry != null &&
Expand Down Expand Up @@ -337,6 +348,7 @@ abstract class _EncointerStore with Store {
webApi.encointer.getReputations(),
webApi.encointer.getMeetupTime(),
webApi.encointer.getMeetupTimeOverride(),
getEncointerBalance(),
updateAggregatedAccountData(),
]).timeout(const Duration(seconds: 15)).catchError((Object? e, s) {
Log.e('Error executing update state: $e', 'EncointerStore');
Expand All @@ -349,6 +361,17 @@ abstract class _EncointerStore with Store {
await _updateStateFuture!;
}

Future<void> getEncointerBalance() async {
final currentAddress = _rootStore.account.currentAddress;

if (currentAddress.isEmpty || chosenCid == null) {
Log.d('[getEncointerBalance] address empty or chosenCid == null', 'EncointerStore');
}

final balanceEntry = await webApi.encointer.getEncointerBalance(currentAddress, chosenCid!);
_rootStore.encointer.account?.addBalanceEntry(chosenCid!, balanceEntry);
}

Future<void> updateAggregatedAccountData() async {
try {
final currentPubKey = _rootStore.account.currentAccountPubKey;
Expand Down
Loading

0 comments on commit 5ff7984

Please sign in to comment.