@@ -5,10 +5,8 @@ import 'package:gap/gap.dart';
55import 'package:go_router/go_router.dart' ;
66import 'package:package_info_plus/package_info_plus.dart' ;
77import 'package:whitenoise/config/extensions/toast_extension.dart' ;
8- import 'package:whitenoise/config/providers/active_account_provider.dart' ;
98import 'package:whitenoise/config/providers/active_pubkey_provider.dart' ;
109import 'package:whitenoise/config/providers/auth_provider.dart' ;
11- import 'package:whitenoise/config/providers/user_profile_data_provider.dart' ;
1210import 'package:whitenoise/domain/models/contact_model.dart' ;
1311import 'package:whitenoise/domain/services/draft_message_service.dart' ;
1412import 'package:whitenoise/routing/routes.dart' ;
@@ -31,88 +29,17 @@ class GeneralSettingsScreen extends ConsumerStatefulWidget {
3129}
3230
3331class _GeneralSettingsScreenState extends ConsumerState <GeneralSettingsScreen > {
34- List <Account > _accounts = [];
35- List <ContactModel > _accountsProfileData = [];
36- ProviderSubscription <AsyncValue <ActiveAccountState >>? _activeAccountSubscription;
3732 PackageInfo ? _packageInfo;
38-
39- bool _isLoadingAccounts = false ;
4033 bool _isLoadingPackageInfo = false ;
41- DateTime ? _lastAccountsLoadTime;
42-
43- static const Duration _accountsCacheDuration = Duration (minutes: 2 );
4434
4535 @override
4636 void initState () {
4737 super .initState ();
4838 WidgetsBinding .instance.addPostFrameCallback ((_) {
49- _loadAccountsProfileData ();
5039 _loadPackageInfo ();
51- _activeAccountSubscription = ref.listenManual (
52- activeAccountProvider,
53- (previous, next) {
54- if (next is AsyncData ) {
55- // This ensures profile updates are reflected immediately
56- _invalidateAccountsCache ();
57- _loadAccountsProfileData ();
58- }
59- },
60- );
6140 });
6241 }
6342
64- @override
65- void dispose () {
66- _activeAccountSubscription? .close ();
67- super .dispose ();
68- }
69-
70- Future <void > _loadAccountsProfileDataIfNeeded () async {
71- if (_lastAccountsLoadTime != null ) {
72- final cacheAge = DateTime .now ().difference (_lastAccountsLoadTime! );
73- if (cacheAge < _accountsCacheDuration &&
74- _accounts.isNotEmpty &&
75- _accountsProfileData.isNotEmpty) {
76- return ; // Use cached data
77- }
78- }
79- await _loadAccountsProfileData ();
80- }
81-
82- void _invalidateAccountsCache () {
83- _lastAccountsLoadTime = null ;
84- }
85-
86- Future <void > _loadAccountsProfileData () async {
87- if (_isLoadingAccounts) return ;
88- _isLoadingAccounts = true ;
89-
90- try {
91- final List <Account > accounts = await getAccounts ();
92- final UserProfileDataNotifier userProfileDataNotifier = ref.read (
93- userProfileDataProvider.notifier,
94- );
95- final List <Future <ContactModel >> accountsProfileDataFutures =
96- accounts
97- .map ((account) => userProfileDataNotifier.getUserProfileData (account.pubkey))
98- .toList ();
99- final List <ContactModel > accountsProfileData = await Future .wait (accountsProfileDataFutures);
100-
101- if (! mounted) return ;
102- setState (() {
103- _accounts = accounts;
104- _accountsProfileData = accountsProfileData;
105- _lastAccountsLoadTime = DateTime .now ();
106- });
107- } catch (e) {
108- if (mounted) {
109- ref.showErrorToast ('Failed to load accounts' );
110- }
111- } finally {
112- _isLoadingAccounts = false ;
113- }
114- }
115-
11643 Future <void > _loadPackageInfo () async {
11744 if (_isLoadingPackageInfo || _packageInfo != null ) return ;
11845 _isLoadingPackageInfo = true ;
@@ -149,18 +76,13 @@ class _GeneralSettingsScreenState extends ConsumerState<GeneralSettingsScreen> {
14976 bool isDismissible = true ,
15077 bool showSuccessToast = false ,
15178 }) async {
152- if (_accounts.isEmpty) {
153- await _loadAccountsProfileDataIfNeeded ();
154- }
155-
15679 if (! mounted) return ;
15780
15881 SwitchProfileBottomSheet .show (
15982 context: context,
160- profiles: _accountsProfileData,
16183 isDismissible: isDismissible,
16284 showSuccessToast: showSuccessToast,
163- onProfileSelected: (selectedProfile) async {
85+ onProfileSelected: (ContactModel selectedProfile) async {
16486 await _switchAccount (selectedProfile.publicKey);
16587 },
16688 );
@@ -210,8 +132,6 @@ class _GeneralSettingsScreenState extends ConsumerState<GeneralSettingsScreen> {
210132
211133 final authNotifier = ref.read (authProvider.notifier);
212134
213- final hasMultipleAccounts = _accounts.length > 2 ;
214-
215135 if (! mounted) return ;
216136
217137 // Clear all draft messages before logout
@@ -230,15 +150,19 @@ class _GeneralSettingsScreenState extends ConsumerState<GeneralSettingsScreen> {
230150 }
231151
232152 if (finalAuthState.isAuthenticated) {
233- if (hasMultipleAccounts) {
234- await _loadAccountsProfileData ();
153+ try {
154+ final List <Account > accounts = await getAccounts ();
155+ final hasMultipleAccounts = accounts.length > 1 ;
235156
236- if (mounted) {
237- await _showAccountSwitcher (isDismissible: false , showSuccessToast: true );
157+ if (hasMultipleAccounts) {
158+ if (mounted) {
159+ await _showAccountSwitcher (isDismissible: false , showSuccessToast: true );
160+ }
161+ } else {
162+ ref.showSuccessToast ('Account signed out. Switched to the other available account.' );
238163 }
239- } else {
240- ref.showSuccessToast ('Account signed out. Switched to the other available account.' );
241- await _loadAccountsProfileData ();
164+ } catch (e) {
165+ ref.showErrorToast ('Failed to check accounts after logout' );
242166 }
243167 } else {
244168 ref.showSuccessToast ('Signed out successfully.' );
0 commit comments