| 1 | import 'dart:async'; |
| 2 | import 'package:cake_wallet/entities/preferences_key.dart'; |
| 3 | import 'package:cake_wallet/routes.dart'; |
| 4 | import 'package:cake_wallet/src/screens/release_notes/release_notes_screen.dart'; |
| 5 | import 'package:cake_wallet/src/screens/yat_emoji_id.dart'; |
| 6 | import 'package:cake_wallet/src/widgets/vulnerable_seeds_popup.dart'; |
| 7 | import 'package:cake_wallet/utils/show_pop_up.dart'; |
| 8 | import 'package:cake_wallet/utils/version_comparator.dart'; |
| 9 | import 'package:flutter/material.dart'; |
| 10 | import 'package:cake_wallet/view_model/dashboard/dashboard_view_model.dart'; |
| 11 | import 'package:cake_wallet/src/screens/dashboard/pages/balance/balance_page.dart'; |
| 12 | import 'package:cake_wallet/view_model/wallet_address_list/wallet_address_list_view_model.dart'; |
| 13 | import 'package:cake_wallet/main.dart'; |
| 14 | import 'package:cake_wallet/router.dart' as Router; |
| 15 | import 'package:shared_preferences/shared_preferences.dart'; |
| 16 | |
| 17 | class DesktopDashboardPage extends StatelessWidget { |
| 18 | DesktopDashboardPage({ |
| 19 | required this.balancePage, |
| 20 | required this.dashboardViewModel, |
| 21 | required this.addressListViewModel, |
| 22 | required this.desktopKey, |
| 23 | }); |
| 24 | |
| 25 | final BalancePage balancePage; |
| 26 | final DashboardViewModel dashboardViewModel; |
| 27 | final WalletAddressListViewModel addressListViewModel; |
| 28 | final GlobalKey<NavigatorState> desktopKey; |
| 29 | |
| 30 | bool _isEffectsInstalled = false; |
| 31 | StreamSubscription<bool>? _onInactiveSub; |
| 32 | |
| 33 | @override |
| 34 | Widget build(BuildContext context) { |
| 35 | _setEffects(context); |
| 36 | |
| 37 | return Container( |
| 38 | color: Theme.of(context).colorScheme.surface, |
| 39 | child: Row( |
| 40 | crossAxisAlignment: CrossAxisAlignment.start, |
| 41 | children: [ |
| 42 | Container( |
| 43 | width: 400, |
| 44 | child: balancePage, |
| 45 | ), |
| 46 | Flexible( |
| 47 | child: ConstrainedBox( |
| 48 | constraints: BoxConstraints(maxWidth: 500), |
| 49 | child: Navigator( |
| 50 | key: desktopKey, |
| 51 | initialRoute: Routes.desktop_actions, |
| 52 | onGenerateRoute: (settings) => Router.createRoute(settings), |
| 53 | onGenerateInitialRoutes: (NavigatorState navigator, String initialRouteName) { |
| 54 | return [ |
| 55 | navigator.widget.onGenerateRoute!(RouteSettings(name: initialRouteName))! |
| 56 | ]; |
| 57 | }, |
| 58 | ), |
| 59 | ), |
| 60 | ), |
| 61 | ], |
| 62 | ), |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | void _setEffects(BuildContext context) async { |
| 67 | if (_isEffectsInstalled) { |
| 68 | return; |
| 69 | } |
| 70 | _isEffectsInstalled = true; |
| 71 | |
| 72 | var needToPresentYat = false; |
| 73 | var isInactive = false; |
| 74 | |
| 75 | _onInactiveSub = rootKey.currentState!.isInactive.listen((inactive) { |
| 76 | isInactive = inactive; |
| 77 | |
| 78 | if (needToPresentYat) { |
| 79 | Future<void>.delayed(Duration(milliseconds: 500)).then((_) { |
| 80 | showPopUp<void>( |
| 81 | context: navigatorKey.currentContext!, |
| 82 | builder: (_) => YatEmojiId(dashboardViewModel.yatStore.emoji)); |
| 83 | needToPresentYat = false; |
| 84 | }); |
| 85 | } |
| 86 | }); |
| 87 | |
| 88 | dashboardViewModel.yatStore.emojiIncommingStream.listen((String emoji) { |
| 89 | if (!_isEffectsInstalled || emoji.isEmpty) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | needToPresentYat = true; |
| 94 | }); |
| 95 | |
| 96 | final sharedPrefs = await SharedPreferences.getInstance(); |
| 97 | final currentAppVersion = |
| 98 | VersionComparator.getExtendedVersionNumber(dashboardViewModel.settingsStore.appVersion); |
| 99 | final lastSeenAppVersion = sharedPrefs.getInt(PreferencesKey.lastSeenAppVersion); |
| 100 | final isNewInstall = sharedPrefs.getBool(PreferencesKey.isNewInstall); |
| 101 | |
| 102 | if (currentAppVersion != lastSeenAppVersion && !isNewInstall!) { |
| 103 | await Future<void>.delayed(Duration(seconds: 1)); |
| 104 | await showPopUp<void>( |
| 105 | context: context, |
| 106 | builder: (BuildContext context) { |
| 107 | return ReleaseNotesScreen( |
| 108 | title: 'Version ${dashboardViewModel.settingsStore.appVersion}'); |
| 109 | }); |
| 110 | sharedPrefs.setInt(PreferencesKey.lastSeenAppVersion, currentAppVersion); |
| 111 | } else if (isNewInstall!) { |
| 112 | sharedPrefs.setInt(PreferencesKey.lastSeenAppVersion, currentAppVersion); |
| 113 | } |
| 114 | |
| 115 | _showVulnerableSeedsPopup(context); |
| 116 | } |
| 117 | |
| 118 | void _showVulnerableSeedsPopup(BuildContext context) async { |
| 119 | final List<String> affectedWalletNames = await dashboardViewModel.checkAffectedWallets(); |
| 120 | |
| 121 | if (affectedWalletNames.isNotEmpty) { |
| 122 | Future<void>.delayed( |
| 123 | Duration(seconds: 1), |
| 124 | () { |
| 125 | showPopUp<void>( |
| 126 | context: context, |
| 127 | builder: (BuildContext context) { |
| 128 | return VulnerableSeedsPopup(affectedWalletNames); |
| 129 | }, |
| 130 | ); |
| 131 | }, |
| 132 | ); |
| 133 | } |
| 134 | } |
| 135 | } |