| 1 | import 'dart:async'; |
| 2 | import 'dart:io'; |
| 3 | |
| 4 | import 'package:cake_wallet/generated/i18n.dart'; |
| 5 | import 'package:cake_wallet/new-ui/widgets/coins_page/top_bar_widget/chain_icon.dart'; |
| 6 | import 'package:cake_wallet/new-ui/widgets/coins_page/top_bar_widget/lightning_switcher.dart'; |
| 7 | import 'package:cake_wallet/new-ui/widgets/coins_page/top_bar_widget/sync_bar.dart'; |
| 8 | import 'package:cake_wallet/new-ui/widgets/modern_button.dart'; |
| 9 | import 'package:cake_wallet/view_model/dashboard/dashboard_view_model.dart'; |
| 10 | import 'package:cw_core/sync_status.dart'; |
| 11 | import 'package:flutter/material.dart'; |
| 12 | import 'package:flutter/services.dart'; |
| 13 | import 'package:flutter_mobx/flutter_mobx.dart'; |
| 14 | import 'package:mobx/mobx.dart'; |
| 15 | |
| 16 | class TopBar extends StatefulWidget { |
| 17 | const TopBar({ |
| 18 | super.key, |
| 19 | required this.lightningMode, |
| 20 | required this.onLightningSwitchPress, |
| 21 | required this.dashboardViewModel, |
| 22 | required this.onSettingsButtonPress, |
| 23 | }); |
| 24 | |
| 25 | final bool lightningMode; |
| 26 | final VoidCallback onLightningSwitchPress; |
| 27 | final VoidCallback onSettingsButtonPress; |
| 28 | final DashboardViewModel dashboardViewModel; |
| 29 | |
| 30 | @override |
| 31 | State<TopBar> createState() => _TopBarState(); |
| 32 | } |
| 33 | |
| 34 | class _TopBarState extends State<TopBar> { |
| 35 | static const syncedMessageDuration = Duration(seconds: 3); |
| 36 | |
| 37 | bool showSyncedMessage = false; |
| 38 | Timer? syncedMessageTimer; |
| 39 | late final ReactionDisposer? _statusReactionDisposer; |
| 40 | |
| 41 | @override |
| 42 | void initState() { |
| 43 | super.initState(); |
| 44 | |
| 45 | _statusReactionDisposer = reaction( |
| 46 | (_) => widget.dashboardViewModel.status.runtimeType, |
| 47 | (status) { |
| 48 | syncedMessageTimer?.cancel(); |
| 49 | |
| 50 | if (status == SyncedSyncStatus) { |
| 51 | if(mounted) { |
| 52 | setState(() => showSyncedMessage = true); |
| 53 | } |
| 54 | syncedMessageTimer = |
| 55 | Timer(syncedMessageDuration, () => setState(() => showSyncedMessage = false)); |
| 56 | } else { |
| 57 | if(mounted) { |
| 58 | setState(() => showSyncedMessage = false); |
| 59 | } |
| 60 | } |
| 61 | }, |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | @override |
| 66 | void dispose() { |
| 67 | syncedMessageTimer?.cancel(); |
| 68 | _statusReactionDisposer?.reaction.dispose(); |
| 69 | super.dispose(); |
| 70 | } |
| 71 | |
| 72 | @override |
| 73 | Widget build(BuildContext context) => Padding( |
| 74 | padding: EdgeInsets.only( |
| 75 | bottom: 10, left: 18, right: 18, top: 10 + _additionalTopPadding(context)), |
| 76 | child: Observer( |
| 77 | builder: (_) => Row( |
| 78 | spacing: 12, |
| 79 | children: [ |
| 80 | (widget.dashboardViewModel.hasLightning) |
| 81 | ? LightningSwitcher( |
| 82 | lightningMode: widget.lightningMode, |
| 83 | onLightningSwitchPress: widget.onLightningSwitchPress, |
| 84 | ) |
| 85 | : ChainIcon( |
| 86 | iconPath: widget.dashboardViewModel.wallet.currency.flatIconPath ?? "", |
| 87 | dashboardViewModel: widget.dashboardViewModel, |
| 88 | isSyncHeavy: widget.dashboardViewModel.isSyncHeavy, |
| 89 | showSyncedMessage: showSyncedMessage), |
| 90 | SyncBar( |
| 91 | dashboardViewModel: widget.dashboardViewModel, |
| 92 | isSyncHeavy: widget.dashboardViewModel.isSyncHeavy, |
| 93 | showSyncedMessage: showSyncedMessage, |
| 94 | ), |
| 95 | ModernButton.svg( |
| 96 | iconColor: Theme.of(context).colorScheme.primary, |
| 97 | size: 36, |
| 98 | onPressed: () { |
| 99 | HapticFeedback.mediumImpact(); |
| 100 | widget.onSettingsButtonPress(); |
| 101 | }, |
| 102 | svgPath: "assets/new-ui/top-settings.svg", |
| 103 | semanticLabel: S.of(context).settings_title, |
| 104 | ), |
| 105 | ], |
| 106 | ), |
| 107 | ), |
| 108 | ); |
| 109 | |
| 110 | //FIXME remove after this gets fixed flutter-side |
| 111 | double _additionalTopPadding(BuildContext context) { |
| 112 | if (Platform.isIOS && MediaQuery.of(context).viewPadding.top < 12) return 24; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | } |