@@ -3,33 +3,106 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
33import 'package:flutter_screenutil/flutter_screenutil.dart' ;
44import 'package:go_router/go_router.dart' ;
55import 'package:whitenoise/config/providers/active_account_provider.dart' ;
6+ import 'package:whitenoise/config/providers/user_profile_data_provider.dart' ;
7+ import 'package:whitenoise/domain/models/contact_model.dart' ;
68import 'package:whitenoise/routing/routes.dart' ;
79import 'package:whitenoise/ui/core/ui/wn_avatar.dart' ;
810
9- class ChatListActiveAccountAvatar extends ConsumerWidget {
10- const ChatListActiveAccountAvatar ({super .key, this .onTap});
11+ class ChatListActiveAccountAvatar extends ConsumerStatefulWidget {
12+ const ChatListActiveAccountAvatar ({
13+ super .key,
14+ this .onTap,
15+ });
1116
1217 final VoidCallback ? onTap;
1318
1419 @override
15- Widget build (BuildContext context, WidgetRef ref) {
16- final activeAccountState = ref.watch (activeAccountProvider);
17- final metadata = activeAccountState.value? .metadata;
18- final currentDisplayName = metadata? .displayName ?? '' ;
19- final profileImagePath = metadata? .picture ?? '' ;
20+ ConsumerState <ChatListActiveAccountAvatar > createState () => _ChatListActiveAccountAvatarState ();
21+ }
22+
23+ class _ChatListActiveAccountAvatarState extends ConsumerState <ChatListActiveAccountAvatar > {
24+ ProviderSubscription <AsyncValue <ActiveAccountState >>? _activeAccountSubscription;
25+ ContactModel ? _profileData;
26+
27+ @override
28+ void initState () {
29+ super .initState ();
30+ WidgetsBinding .instance.addPostFrameCallback ((_) {
31+ _loadProfileData ();
32+ _activeAccountSubscription = ref.listenManual (
33+ activeAccountProvider,
34+ (previous, next) {
35+ if (next is AsyncData ) {
36+ _loadProfileData ();
37+ }
38+ },
39+ );
40+ });
41+ }
42+
43+ @override
44+ void dispose () {
45+ _activeAccountSubscription? .close ();
46+ super .dispose ();
47+ }
2048
49+ Future <void > _loadProfileData () async {
50+ try {
51+ final AsyncValue <ActiveAccountState > activeAccountState = ref.read (activeAccountProvider);
52+ final String ? pubkey = activeAccountState.valueOrNull? .account? .pubkey;
53+ if (pubkey == null || pubkey.isEmpty) return ;
54+ final ContactModel profileData = await ref
55+ .read (userProfileDataProvider.notifier)
56+ .getUserProfileData (pubkey);
57+ if (! mounted) return ;
58+ final String ? currentPubkey = ref.read (activeAccountProvider).valueOrNull? .account? .pubkey;
59+ if (currentPubkey != pubkey) return ;
60+ setState (() {
61+ _profileData = profileData;
62+ });
63+ } catch (_) {
64+ if (! mounted) return ;
65+ setState (() {
66+ _profileData = null ;
67+ });
68+ }
69+ }
70+
71+ Widget _buildAvatar ({
72+ String imageUrl = '' ,
73+ String ? displayName,
74+ }) {
2175 return InkWell (
2276 borderRadius: BorderRadius .circular (16. r),
2377 onTap:
24- onTap ??
78+ widget. onTap ??
2579 () {
2680 context.push (Routes .settings);
2781 },
2882 child: WnAvatar (
29- imageUrl: profileImagePath ,
30- displayName: currentDisplayName ,
83+ imageUrl: imageUrl ,
84+ displayName: displayName ,
3185 size: 36. r,
3286 ),
3387 );
3488 }
89+
90+ @override
91+ Widget build (BuildContext context) {
92+ final activeAccountState = ref.watch (activeAccountProvider);
93+
94+ return activeAccountState.when (
95+ data: (state) {
96+ if (state.account != null && _profileData != null ) {
97+ return _buildAvatar (
98+ imageUrl: _profileData? .imagePath ?? '' ,
99+ displayName: _profileData? .displayName,
100+ );
101+ }
102+ return _buildAvatar ();
103+ },
104+ loading: () => _buildAvatar (),
105+ error: (error, stack) => _buildAvatar (),
106+ );
107+ }
35108}
0 commit comments