From d93136e285dcef3f533959cdba40a0d80fd96380 Mon Sep 17 00:00:00 2001 From: julian Date: Wed, 8 Nov 2023 15:43:01 -0600 Subject: [PATCH] favourites ui fixes and provider tweaks --- .../manage_favorites_view.dart | 30 +-- .../sub_widgets/favorite_wallets.dart | 2 +- .../desktop_favorite_wallets.dart | 207 +++++++++--------- lib/wallets/isar/models/wallet_info.dart | 3 +- .../providers/favourite_wallets_provider.dart | 21 +- .../favourite_wallets_watcher.dart | 40 ---- 6 files changed, 123 insertions(+), 180 deletions(-) delete mode 100644 lib/widgets/db_watchers/favourite_wallets_watcher.dart diff --git a/lib/pages/manage_favorites_view/manage_favorites_view.dart b/lib/pages/manage_favorites_view/manage_favorites_view.dart index 8cde4a583..d7f297689 100644 --- a/lib/pages/manage_favorites_view/manage_favorites_view.dart +++ b/lib/pages/manage_favorites_view/manage_favorites_view.dart @@ -11,7 +11,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:stackwallet/providers/db/main_db_provider.dart'; -import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/text_styles.dart'; @@ -63,17 +62,8 @@ class ManageFavoritesView extends StatelessWidget { body: isDesktop ? Consumer( builder: (_, ref, __) { - final favorites = ref.watch(pFavouriteWalletInfos); - print( - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); - - // todo [prio=??] do this differently - final nonFavorites = ref - .watch(pWallets) - .wallets - .map((e) => e.info) - .where((e) => !e.isFavourite) - .toList(); + final favorites = ref.watch(pFavouriteWalletInfos(true)); + final nonFavorites = ref.watch(pFavouriteWalletInfos(false)); return Column( children: [ @@ -158,7 +148,7 @@ class ManageFavoritesView extends StatelessWidget { actualIndex - (oldIndex - newIndex), ); } else { - for (int i = oldIndex + 1; i <= newIndex; i++) { + for (int i = oldIndex + 1; i < newIndex; i++) { final next = favorites[i]; next.updateIsFavourite( true, @@ -274,7 +264,8 @@ class ManageFavoritesView extends StatelessWidget { Expanded( child: Consumer( builder: (_, ref, __) { - final favorites = ref.watch(pFavouriteWalletInfos); + final favorites = + ref.watch(pFavouriteWalletInfos(true)); return ReorderableListView.builder( key: key, itemCount: favorites.length, @@ -312,7 +303,7 @@ class ManageFavoritesView extends StatelessWidget { actualIndex - (oldIndex - newIndex), ); } else { - for (int i = oldIndex + 1; i <= newIndex; i++) { + for (int i = oldIndex + 1; i < newIndex; i++) { final next = favorites[i]; next.updateIsFavourite( true, @@ -367,13 +358,8 @@ class ManageFavoritesView extends StatelessWidget { Expanded( child: Consumer( builder: (_, ref, __) { - // todo [prio=??] do this differently - final nonFavorites = ref - .watch(pWallets) - .wallets - .map((e) => e.info) - .where((e) => !e.isFavourite) - .toList(); + final nonFavorites = + ref.watch(pFavouriteWalletInfos(false)); return ListView.builder( itemCount: nonFavorites.length, diff --git a/lib/pages/wallets_view/sub_widgets/favorite_wallets.dart b/lib/pages/wallets_view/sub_widgets/favorite_wallets.dart index 01bb5513f..d43eda2a2 100644 --- a/lib/pages/wallets_view/sub_widgets/favorite_wallets.dart +++ b/lib/pages/wallets_view/sub_widgets/favorite_wallets.dart @@ -73,7 +73,7 @@ class _FavoriteWalletsState extends ConsumerState { Widget build(BuildContext context) { debugPrint("BUILD: $runtimeType"); - final favorites = ref.watch(pFavouriteWalletInfos); + final favorites = ref.watch(pFavouriteWalletInfos(true)); _favLength = favorites.length; bool hasFavorites = favorites.isNotEmpty; diff --git a/lib/pages_desktop_specific/my_stack_view/desktop_favorite_wallets.dart b/lib/pages_desktop_specific/my_stack_view/desktop_favorite_wallets.dart index 82472625e..fa3970f98 100644 --- a/lib/pages_desktop_specific/my_stack_view/desktop_favorite_wallets.dart +++ b/lib/pages_desktop_specific/my_stack_view/desktop_favorite_wallets.dart @@ -17,8 +17,8 @@ import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/text_styles.dart'; +import 'package:stackwallet/wallets/isar/providers/favourite_wallets_provider.dart'; import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart'; -import 'package:stackwallet/widgets/db_watchers/favourite_wallets_watcher.dart'; class DesktopFavoriteWallets extends ConsumerWidget { const DesktopFavoriteWallets({Key? key}) : super(key: key); @@ -30,117 +30,112 @@ class DesktopFavoriteWallets extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { debugPrint("BUILD: $runtimeType"); + final favourites = ref.watch(pFavouriteWalletInfos(true)); - return FavouriteWalletsWatcher( - builder: (context, favourites) { - bool hasFavorites = favourites.isNotEmpty; - return Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.start, + final bool hasFavorites = favourites.isNotEmpty; + return Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Text( - "Favorite wallets", - style: STextStyles.desktopTextExtraSmall(context).copyWith( - color: Theme.of(context) - .extension()! - .textFieldActiveSearchIconRight, - ), - ), - CustomTextButton( - text: "Edit", - onTap: () { - Navigator.of(context) - .pushNamed(ManageFavoritesView.routeName); - }, - ), - ], - ), - const SizedBox( - height: 20, - ), - ConstrainedBox( - constraints: const BoxConstraints( - maxHeight: (cardHeight * 2) + standardPadding, - minHeight: cardHeight, + Text( + "Favorite wallets", + style: STextStyles.desktopTextExtraSmall(context).copyWith( + color: Theme.of(context) + .extension()! + .textFieldActiveSearchIconRight, ), - child: hasFavorites - ? SingleChildScrollView( - primary: false, - child: Wrap( - spacing: 16, - runSpacing: 16, - children: [ - ...favourites.map((e) { - return FavoriteCard( - walletId: e.walletId, - key: Key(e.name), - width: cardWidth, - height: cardHeight, - ); - }) - ], - ), - ) - : Container( - height: cardHeight, - width: cardWidth, - decoration: BoxDecoration( - color: Theme.of(context) - .extension()! - .textFieldDefaultBG, - borderRadius: BorderRadius.circular( - Constants.size.circularBorderRadius, - ), - ), - child: MaterialButton( - splashColor: Theme.of(context) - .extension()! - .highlight, - key: const Key("favoriteWalletsAddFavoriteButtonKey"), - padding: const EdgeInsets.all(12), - materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular( - Constants.size.circularBorderRadius), - ), - onPressed: () { - Navigator.of(context) - .pushNamed(ManageFavoritesView.routeName); - }, - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - SvgPicture.asset( - Assets.svg.plus, - width: 14, - height: 14, - color: Theme.of(context) - .extension()! - .textSubtitle1, - ), - const SizedBox( - width: 4, - ), - Text( - "Add a favorite", - style: STextStyles.itemSubtitle(context).copyWith( - fontSize: 18, - ), - ), - ], - ), - ), - ), ), - const SizedBox( - height: 40, + CustomTextButton( + text: "Edit", + onTap: () { + Navigator.of(context).pushNamed(ManageFavoritesView.routeName); + }, ), ], - ); - }, + ), + const SizedBox( + height: 20, + ), + ConstrainedBox( + constraints: const BoxConstraints( + maxHeight: (cardHeight * 2) + standardPadding, + minHeight: cardHeight, + ), + child: hasFavorites + ? SingleChildScrollView( + primary: false, + child: Wrap( + spacing: 16, + runSpacing: 16, + children: [ + ...favourites.map((e) { + return FavoriteCard( + walletId: e.walletId, + key: Key(e.name), + width: cardWidth, + height: cardHeight, + ); + }) + ], + ), + ) + : Container( + height: cardHeight, + width: cardWidth, + decoration: BoxDecoration( + color: Theme.of(context) + .extension()! + .textFieldDefaultBG, + borderRadius: BorderRadius.circular( + Constants.size.circularBorderRadius, + ), + ), + child: MaterialButton( + splashColor: + Theme.of(context).extension()!.highlight, + key: const Key("favoriteWalletsAddFavoriteButtonKey"), + padding: const EdgeInsets.all(12), + materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular( + Constants.size.circularBorderRadius), + ), + onPressed: () { + Navigator.of(context) + .pushNamed(ManageFavoritesView.routeName); + }, + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + SvgPicture.asset( + Assets.svg.plus, + width: 14, + height: 14, + color: Theme.of(context) + .extension()! + .textSubtitle1, + ), + const SizedBox( + width: 4, + ), + Text( + "Add a favorite", + style: STextStyles.itemSubtitle(context).copyWith( + fontSize: 18, + ), + ), + ], + ), + ), + ), + ), + const SizedBox( + height: 40, + ), + ], ); } } diff --git a/lib/wallets/isar/models/wallet_info.dart b/lib/wallets/isar/models/wallet_info.dart index e330e9565..b7855dfd5 100644 --- a/lib/wallets/isar/models/wallet_info.dart +++ b/lib/wallets/isar/models/wallet_info.dart @@ -162,11 +162,10 @@ class WalletInfo implements IsarId { } else if (flag) { final highest = await isar.walletInfo .where() - .walletIdEqualTo(walletId) .sortByFavouriteOrderIndexDesc() .favouriteOrderIndexProperty() .findFirst(); - index = highest ?? 0; + index = (highest ?? 0) + 1; } else { index = -1; } diff --git a/lib/wallets/isar/providers/favourite_wallets_provider.dart b/lib/wallets/isar/providers/favourite_wallets_provider.dart index 08a3187eb..650b57d64 100644 --- a/lib/wallets/isar/providers/favourite_wallets_provider.dart +++ b/lib/wallets/isar/providers/favourite_wallets_provider.dart @@ -7,17 +7,19 @@ import 'package:stackwallet/providers/db/main_db_provider.dart'; import 'package:stackwallet/wallets/isar/models/wallet_info.dart'; class _Watcher extends ChangeNotifier { + final bool isFavourite; late final StreamSubscription> _streamSubscription; List _value; List get value => _value; - _Watcher(this._value, Isar isar) { + _Watcher(this._value, this.isFavourite, Isar isar) { _streamSubscription = isar.walletInfo .filter() - .isFavouriteEqualTo(true) - .watch() + .isFavouriteEqualTo(isFavourite) + .sortByFavouriteOrderIndex() + .watch(fireImmediately: true) .listen((event) { _value = event; notifyListeners(); @@ -31,16 +33,17 @@ class _Watcher extends ChangeNotifier { } } -final _wiProvider = ChangeNotifierProvider.autoDispose( - (ref) { +final _wiProvider = ChangeNotifierProvider.family<_Watcher, bool>( + (ref, isFavourite) { final isar = ref.watch(mainDBProvider).isar; final watcher = _Watcher( isar.walletInfo .filter() - .isFavouriteEqualTo(true) + .isFavouriteEqualTo(isFavourite) .sortByFavouriteOrderIndex() .findAllSync(), + isFavourite, isar, ); @@ -50,8 +53,8 @@ final _wiProvider = ChangeNotifierProvider.autoDispose( }, ); -final pFavouriteWalletInfos = Provider.autoDispose( - (ref) { - return ref.watch(_wiProvider).value; +final pFavouriteWalletInfos = Provider.family, bool>( + (ref, isFavourite) { + return ref.watch(_wiProvider(isFavourite)).value; }, ); diff --git a/lib/widgets/db_watchers/favourite_wallets_watcher.dart b/lib/widgets/db_watchers/favourite_wallets_watcher.dart deleted file mode 100644 index 3d4d4a9f4..000000000 --- a/lib/widgets/db_watchers/favourite_wallets_watcher.dart +++ /dev/null @@ -1,40 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:isar/isar.dart'; -import 'package:stackwallet/providers/db/main_db_provider.dart'; -import 'package:stackwallet/wallets/isar/models/wallet_info.dart'; - -class FavouriteWalletsWatcher extends ConsumerWidget { - const FavouriteWalletsWatcher({ - super.key, - required this.builder, - }); - - final Widget Function(BuildContext, List) builder; - - @override - Widget build(BuildContext context, WidgetRef ref) { - final initialInfo = ref - .watch(mainDBProvider) - .isar - .walletInfo - .where() - .filter() - .isFavouriteEqualTo(true) - .findAllSync(); - - return StreamBuilder( - stream: ref - .watch(mainDBProvider) - .isar - .walletInfo - .where() - .filter() - .isFavouriteEqualTo(true) - .watch(), - builder: (context, snapshot) { - return builder(context, snapshot.data ?? initialInfo); - }, - ); - } -}