Skip to content

Commit c16ac6d

Browse files
fix: show loading on switch instead of no accounts (#676)
1 parent 34cdf96 commit c16ac6d

File tree

4 files changed

+145
-176
lines changed

4 files changed

+145
-176
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3636
- Fixed profile image not showing up chatlist after login
3737
- Fixes auto-scroll to bottom when keyboard opens
3838
- Fixed time shown in messages to be in local time instead of UTC
39+
- Fixed account switcher error
3940

4041
### Security
4142

ios/Podfile.lock

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,21 @@ EXTERNAL SOURCES:
8484
:path: ".symlinks/plugins/sqflite_darwin/darwin"
8585

8686
SPEC CHECKSUMS:
87-
audio_session: 19e9480dbdd4e5f6c4543826b2e8b0e4ab6145fe
88-
emoji_picker_flutter: 8e50ec5caac456a23a78637e02c6293ea0ac8771
87+
audio_session: 9bb7f6c970f21241b19f5a3658097ae459681ba0
88+
emoji_picker_flutter: ece213fc274bdddefb77d502d33080dc54e616cc
8989
Flutter: cabc95a1d2626b1b06e7179b784ebcf0c0cde467
90-
flutter_native_splash: df59bb2e1421aa0282cb2e95618af4dcb0c56c29
91-
flutter_secure_storage: d33dac7ae2ea08509be337e775f6b59f1ff45f12
92-
image_picker_ios: c560581cceedb403a6ff17f2f816d7fea1421fc1
93-
integration_test: 252f60fa39af5e17c3aa9899d35d908a0721b573
94-
just_audio: a42c63806f16995daf5b219ae1d679deb76e6a79
95-
mobile_scanner: 77265f3dc8d580810e91849d4a0811a90467ed5e
96-
package_info_plus: c0502532a26c7662a62a356cebe2692ec5fe4ec4
97-
path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46
98-
rust_lib_whitenoise: 69ef24b69b2aba78a7ebabc09a504b5a39177d21
99-
share_plus: 8b6f8b3447e494cca5317c8c3073de39b3600d1f
100-
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
101-
sqflite_darwin: 5a7236e3b501866c1c9befc6771dfd73ffb8702d
90+
flutter_native_splash: c32d145d68aeda5502d5f543ee38c192065986cf
91+
flutter_secure_storage: 1ed9476fba7e7a782b22888f956cce43e2c62f13
92+
image_picker_ios: 7fe1ff8e34c1790d6fff70a32484959f563a928a
93+
integration_test: 4a889634ef21a45d28d50d622cf412dc6d9f586e
94+
just_audio: 4e391f57b79cad2b0674030a00453ca5ce817eed
95+
mobile_scanner: 9157936403f5a0644ca3779a38ff8404c5434a93
96+
package_info_plus: af8e2ca6888548050f16fa2f1938db7b5a5df499
97+
path_provider_foundation: 080d55be775b7414fd5a5ef3ac137b97b097e564
98+
rust_lib_whitenoise: 22de658398f8e36a1a396d35b6b6547a0732e6bb
99+
share_plus: 50da8cb520a8f0f65671c6c6a99b3617ed10a58a
100+
shared_preferences_foundation: 9e1978ff2562383bd5676f64ec4e9aa8fa06a6f7
101+
sqflite_darwin: 20b2a3a3b70e43edae938624ce550a3cbf66a3d0
102102

103103
PODFILE CHECKSUM: 251cb053df7158f337c0712f2ab29f4e0fa474ce
104104

lib/ui/settings/general_settings_screen.dart

Lines changed: 12 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import 'package:gap/gap.dart';
55
import 'package:go_router/go_router.dart';
66
import 'package:package_info_plus/package_info_plus.dart';
77
import 'package:whitenoise/config/extensions/toast_extension.dart';
8-
import 'package:whitenoise/config/providers/active_account_provider.dart';
98
import 'package:whitenoise/config/providers/active_pubkey_provider.dart';
109
import 'package:whitenoise/config/providers/auth_provider.dart';
11-
import 'package:whitenoise/config/providers/user_profile_data_provider.dart';
1210
import 'package:whitenoise/domain/models/contact_model.dart';
1311
import 'package:whitenoise/domain/services/draft_message_service.dart';
1412
import 'package:whitenoise/routing/routes.dart';
@@ -31,88 +29,17 @@ class GeneralSettingsScreen extends ConsumerStatefulWidget {
3129
}
3230

3331
class _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

Comments
 (0)