From 20ca372028b668d6a671f93d7f29bbcde49caa49 Mon Sep 17 00:00:00 2001 From: ice-orion <102020833+ice-orion@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:34:14 +0300 Subject: [PATCH] refactor: return UserMetadata from followersDataSource --- .../components/follow_list_item.dart | 44 ++++++++++--------- .../follow_list_modal/followers_list.dart | 26 ++--------- .../follow_list_modal/following_list.dart | 19 ++------ .../followers_data_source_provider.c.dart | 2 +- 4 files changed, 31 insertions(+), 60 deletions(-) diff --git a/lib/app/features/user/pages/profile_page/pages/follow_list_modal/components/follow_list_item.dart b/lib/app/features/user/pages/profile_page/pages/follow_list_modal/components/follow_list_item.dart index d04847fe3..3af7deeeb 100644 --- a/lib/app/features/user/pages/profile_page/pages/follow_list_modal/components/follow_list_item.dart +++ b/lib/app/features/user/pages/profile_page/pages/follow_list_modal/components/follow_list_item.dart @@ -6,40 +6,44 @@ import 'package:ion/app/components/list_item/list_item.dart'; import 'package:ion/app/components/list_items_loading_state/item_loading_state.dart'; import 'package:ion/app/extensions/extensions.dart'; import 'package:ion/app/features/components/user/follow_user_button/follow_user_button.dart'; -import 'package:ion/app/features/user/model/user_metadata.c.dart'; +import 'package:ion/app/features/user/providers/user_metadata_provider.c.dart'; import 'package:ion/app/utils/username.dart'; class FollowListItem extends ConsumerWidget { const FollowListItem({ - required this.userMetadata, + required this.pubkey, super.key, }); - final UserMetadataEntity? userMetadata; + final String pubkey; static double get itemHeight => 35.0.s; @override Widget build(BuildContext context, WidgetRef ref) { - if (userMetadata == null) { - return ItemLoadingState(itemHeight: itemHeight); - } + final userMetadataResult = ref.watch(userMetadataProvider(pubkey)); - final UserMetadataEntity(:masterPubkey, :data) = userMetadata!; - - return ListItem.user( - title: Text(data.name), - trailing: FollowUserButton(pubkey: masterPubkey), - subtitle: Text( - prefixUsername( - username: data.displayName, - context: context, - ), - ), - profilePicture: data.picture, - onTap: () { - Navigator.of(context).pop(masterPubkey); + return userMetadataResult.maybeWhen( + data: (userMetadata) { + if (userMetadata == null) { + return const SizedBox.shrink(); + } + return ListItem.user( + title: Text(userMetadata.data.name), + trailing: FollowUserButton(pubkey: pubkey), + subtitle: Text( + prefixUsername( + username: userMetadata.data.displayName, + context: context, + ), + ), + profilePicture: userMetadata.data.picture, + onTap: () { + Navigator.of(context).pop(userMetadata.masterPubkey); + }, + ); }, + orElse: () => ItemLoadingState(itemHeight: itemHeight), ); } } diff --git a/lib/app/features/user/pages/profile_page/pages/follow_list_modal/followers_list.dart b/lib/app/features/user/pages/profile_page/pages/follow_list_modal/followers_list.dart index 946fe0713..d0d8fb710 100644 --- a/lib/app/features/user/pages/profile_page/pages/follow_list_modal/followers_list.dart +++ b/lib/app/features/user/pages/profile_page/pages/follow_list_modal/followers_list.dart @@ -6,9 +6,7 @@ import 'package:ion/app/components/screen_offset/screen_side_offset.dart'; import 'package:ion/app/components/scroll_view/load_more_builder.dart'; import 'package:ion/app/extensions/extensions.dart'; import 'package:ion/app/features/nostr/providers/entities_paged_data_provider.c.dart'; -import 'package:ion/app/features/nostr/providers/nostr_cache.c.dart'; import 'package:ion/app/features/user/model/follow_type.dart'; -import 'package:ion/app/features/user/model/user_metadata.c.dart'; import 'package:ion/app/features/user/pages/profile_page/pages/follow_list_modal/components/follow_app_bar.dart'; import 'package:ion/app/features/user/pages/profile_page/pages/follow_list_modal/components/follow_list_item.dart'; import 'package:ion/app/features/user/pages/profile_page/pages/follow_list_modal/components/follow_list_loading.dart'; @@ -35,7 +33,9 @@ class FollowersList extends ConsumerWidget { SliverList.separated( separatorBuilder: (_, __) => SizedBox(height: 16.0.s), itemCount: entities.length, - itemBuilder: (context, index) => _ListItem(pubkey: entities[index].masterPubkey), + itemBuilder: (context, index) => ScreenSideOffset.small( + child: FollowListItem(pubkey: entities[index].masterPubkey), + ), ) else const FollowListLoading(), @@ -50,23 +50,3 @@ class FollowersList extends ConsumerWidget { ); } } - -class _ListItem extends ConsumerWidget { - const _ListItem({required this.pubkey}); - - final String pubkey; - - @override - Widget build(BuildContext context, WidgetRef ref) { - final userMetadata = ref.watch( - nostrCacheProvider.select( - cacheSelector( - UserMetadataEntity.cacheKeyBuilder(pubkey: pubkey), - ), - ), - ); - return ScreenSideOffset.small( - child: FollowListItem(userMetadata: userMetadata), - ); - } -} diff --git a/lib/app/features/user/pages/profile_page/pages/follow_list_modal/following_list.dart b/lib/app/features/user/pages/profile_page/pages/follow_list_modal/following_list.dart index baef4227c..9ca940884 100644 --- a/lib/app/features/user/pages/profile_page/pages/follow_list_modal/following_list.dart +++ b/lib/app/features/user/pages/profile_page/pages/follow_list_modal/following_list.dart @@ -10,7 +10,6 @@ import 'package:ion/app/features/user/pages/profile_page/pages/follow_list_modal import 'package:ion/app/features/user/pages/profile_page/pages/follow_list_modal/components/follow_list_loading.dart'; import 'package:ion/app/features/user/pages/profile_page/pages/follow_list_modal/components/follow_search_bar.dart'; import 'package:ion/app/features/user/providers/follow_list_provider.c.dart'; -import 'package:ion/app/features/user/providers/user_metadata_provider.c.dart'; class FollowingList extends ConsumerWidget { const FollowingList({required this.pubkey, super.key}); @@ -31,7 +30,9 @@ class FollowingList extends ConsumerWidget { SliverList.separated( separatorBuilder: (_, __) => SizedBox(height: 16.0.s), itemCount: pubkeys.length, - itemBuilder: (context, index) => _ListItem(pubkey: pubkeys[index]), + itemBuilder: (context, index) => ScreenSideOffset.small( + child: FollowListItem(pubkey: pubkeys[index]), + ), ) else const FollowListLoading(), @@ -40,17 +41,3 @@ class FollowingList extends ConsumerWidget { ); } } - -class _ListItem extends ConsumerWidget { - const _ListItem({required this.pubkey}); - - final String pubkey; - - @override - Widget build(BuildContext context, WidgetRef ref) { - final userMetadata = ref.watch(userMetadataProvider(pubkey)).valueOrNull; - return ScreenSideOffset.small( - child: FollowListItem(userMetadata: userMetadata), - ); - } -} diff --git a/lib/app/features/user/providers/followers_data_source_provider.c.dart b/lib/app/features/user/providers/followers_data_source_provider.c.dart index 2e47cac60..3eb780c93 100644 --- a/lib/app/features/user/providers/followers_data_source_provider.c.dart +++ b/lib/app/features/user/providers/followers_data_source_provider.c.dart @@ -16,7 +16,7 @@ List? followersDataSource(Ref ref, String pubkey) { return [ EntitiesDataSource( actionSource: ActionSourceUser(pubkey), - entityFilter: (entity) => entity is FollowListEntity, + entityFilter: (entity) => entity is UserMetadataEntity, requestFilters: [ RequestFilter( kinds: const [FollowListEntity.kind],