stack_wallet/lib/pages_desktop_specific/desktop_home_view.dart

182 lines
7.1 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2022-12-01 14:48:23 +00:00
import 'package:stackwallet/pages_desktop_specific/address_book_view/desktop_address_book.dart';
2022-11-18 18:45:42 +00:00
import 'package:stackwallet/pages_desktop_specific/desktop_exchange/desktop_exchange_view.dart';
2022-12-01 14:48:23 +00:00
import 'package:stackwallet/pages_desktop_specific/desktop_menu.dart';
import 'package:stackwallet/pages_desktop_specific/my_stack_view/my_stack_view.dart';
import 'package:stackwallet/pages_desktop_specific/notifications/desktop_notifications_view.dart';
import 'package:stackwallet/pages_desktop_specific/settings/desktop_settings_view.dart';
2022-12-01 16:10:18 +00:00
import 'package:stackwallet/pages_desktop_specific/settings/settings_menu/desktop_about_view.dart';
import 'package:stackwallet/pages_desktop_specific/settings/settings_menu/desktop_support_view.dart';
2022-11-14 16:40:31 +00:00
import 'package:stackwallet/providers/desktop/current_desktop_menu_item.dart';
2022-11-26 19:39:52 +00:00
import 'package:stackwallet/providers/global/auto_swb_service_provider.dart';
2022-11-14 16:40:31 +00:00
import 'package:stackwallet/providers/global/notifications_provider.dart';
2022-11-26 19:39:52 +00:00
import 'package:stackwallet/providers/global/prefs_provider.dart';
import 'package:stackwallet/providers/global/wallets_provider.dart';
import 'package:stackwallet/providers/ui/transaction_filter_provider.dart';
2022-11-14 16:40:31 +00:00
import 'package:stackwallet/providers/ui/unread_notifications_provider.dart';
import 'package:stackwallet/route_generator.dart';
2022-11-26 19:39:52 +00:00
import 'package:stackwallet/utilities/enums/backup_frequency_type.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/widgets/background.dart';
2022-11-26 19:39:52 +00:00
final currentWalletIdProvider = StateProvider<String?>((_) => null);
class DesktopHomeView extends ConsumerStatefulWidget {
const DesktopHomeView({Key? key}) : super(key: key);
static const String routeName = "/desktopHome";
@override
ConsumerState<DesktopHomeView> createState() => _DesktopHomeViewState();
}
class _DesktopHomeViewState extends ConsumerState<DesktopHomeView> {
2022-12-01 16:10:18 +00:00
final GlobalKey myStackViewNavKey = GlobalKey<NavigatorState>();
2022-11-26 19:39:52 +00:00
late final Navigator myStackViewNav;
@override
void initState() {
myStackViewNav = Navigator(
2022-12-01 16:10:18 +00:00
key: myStackViewNavKey,
onGenerateRoute: RouteGenerator.generateRoute,
initialRoute: MyStackView.routeName,
2022-11-26 19:39:52 +00:00
);
super.initState();
}
final Map<DesktopMenuItemId, Widget> contentViews = {
DesktopMenuItemId.myStack: Container(
// key: Key("desktopStackHomeKey"),
// onGenerateRoute: RouteGenerator.generateRoute,
// initialRoute: MyStackView.routeName,
),
2022-11-18 18:45:42 +00:00
DesktopMenuItemId.exchange: const Navigator(
key: Key("desktopExchangeHomeKey"),
onGenerateRoute: RouteGenerator.generateRoute,
initialRoute: DesktopExchangeView.routeName,
),
DesktopMenuItemId.notifications: const Navigator(
key: Key("desktopNotificationsHomeKey"),
onGenerateRoute: RouteGenerator.generateRoute,
initialRoute: DesktopNotificationsView.routeName,
2022-09-16 17:26:12 +00:00
),
DesktopMenuItemId.addressBook: const Navigator(
2022-11-09 15:57:41 +00:00
key: Key("desktopAddressBookHomeKey"),
onGenerateRoute: RouteGenerator.generateRoute,
initialRoute: DesktopAddressBook.routeName,
2022-09-16 17:26:12 +00:00
),
DesktopMenuItemId.settings: const Navigator(
2022-10-13 17:55:04 +00:00
key: Key("desktopSettingHomeKey"),
2022-10-05 17:41:41 +00:00
onGenerateRoute: RouteGenerator.generateRoute,
2022-10-17 16:50:37 +00:00
initialRoute: DesktopSettingsView.routeName,
2022-09-16 17:26:12 +00:00
),
DesktopMenuItemId.support: const Navigator(
2022-11-07 15:15:44 +00:00
key: Key("desktopSupportHomeKey"),
onGenerateRoute: RouteGenerator.generateRoute,
initialRoute: DesktopSupportView.routeName,
2022-09-16 17:26:12 +00:00
),
DesktopMenuItemId.about: const Navigator(
2022-11-07 17:42:55 +00:00
key: Key("desktopAboutHomeKey"),
onGenerateRoute: RouteGenerator.generateRoute,
initialRoute: DesktopAboutView.routeName,
2022-09-16 17:26:12 +00:00
),
};
2022-09-16 17:26:12 +00:00
2022-11-26 19:39:52 +00:00
DesktopMenuItemId prev = DesktopMenuItemId.myStack;
2022-11-14 16:40:31 +00:00
void onMenuSelectionWillChange(DesktopMenuItemId newKey) {
2022-11-26 19:39:52 +00:00
if (prev == DesktopMenuItemId.myStack && prev == newKey) {
2022-12-01 16:10:18 +00:00
Navigator.of(myStackViewNavKey.currentContext!)
2022-11-26 19:39:52 +00:00
.popUntil(ModalRoute.withName(MyStackView.routeName));
if (ref.read(currentWalletIdProvider.state).state != null) {
final managerProvider = ref
.read(walletsChangeNotifierProvider)
.getManagerProvider(ref.read(currentWalletIdProvider.state).state!);
if (ref.read(managerProvider).shouldAutoSync) {
ref.read(managerProvider).shouldAutoSync = false;
}
ref.read(transactionFilterProvider.state).state = null;
if (ref.read(prefsChangeNotifierProvider).isAutoBackupEnabled &&
ref.read(prefsChangeNotifierProvider).backupFrequencyType ==
BackupFrequencyType.afterClosingAWallet) {
ref.read(autoSWBServiceProvider).doBackup();
}
ref.read(managerProvider.notifier).isActiveWallet = false;
}
}
prev = newKey;
2022-11-14 16:40:31 +00:00
// check for unread notifications and refresh provider before
// showing notifications view
if (newKey == DesktopMenuItemId.notifications) {
ref.refresh(unreadNotificationsStateProvider);
}
// mark notifications as read if leaving notifications view
if (ref.read(currentDesktopMenuItemProvider.state).state ==
DesktopMenuItemId.notifications &&
newKey != DesktopMenuItemId.notifications) {
final Set<int> unreadNotificationIds =
ref.read(unreadNotificationsStateProvider.state).state;
if (unreadNotificationIds.isNotEmpty) {
List<Future<void>> futures = [];
for (int i = 0; i < unreadNotificationIds.length - 1; i++) {
futures.add(ref
.read(notificationsProvider)
.markAsRead(unreadNotificationIds.elementAt(i), false));
}
// wait for multiple to update if any
Future.wait(futures).then((_) {
// only notify listeners once
ref
.read(notificationsProvider)
.markAsRead(unreadNotificationIds.last, true);
});
}
}
2022-09-16 17:26:12 +00:00
}
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).extension<StackColors>()!.background,
child: Background(
child: Row(
children: [
DesktopMenu(
// onSelectionChanged: onMenuSelectionChanged,
onSelectionWillChange: onMenuSelectionWillChange,
),
Container(
width: 1,
color: Theme.of(context).extension<StackColors>()!.background,
),
Expanded(
2022-11-26 19:39:52 +00:00
child: IndexedStack(
index: ref
.watch(currentDesktopMenuItemProvider.state)
.state
.index >
0
? 1
: 0,
children: [
myStackViewNav,
contentViews[
ref.watch(currentDesktopMenuItemProvider.state).state]!,
],
),
),
2022-11-26 19:39:52 +00:00
// Expanded(
// child: contentViews[
// ref.watch(currentDesktopMenuItemProvider.state).state]!,
// ),
],
),
),
);
}
}