fix(mobile-app): 修复头像更新未同步到账号列表的问题
- AccountService 添加 MultiAccountService 依赖 - updateLocalAvatarSvg() 更新后同步到账号列表 - updateProfile() 更新昵称/头像后同步到账号列表 - uploadAvatar() 上传头像后同步到账号列表 - 新增 _syncProfileToAccountList() 统一处理同步逻辑 - 调整 injection_container 依赖注入顺序 确保切换账号时显示正确的头像和昵称 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
339f95f7ed
commit
2a929fc082
|
|
@ -32,22 +32,13 @@ final apiClientProvider = Provider<ApiClient>((ref) {
|
|||
return ApiClient(secureStorage: secureStorage);
|
||||
});
|
||||
|
||||
// Account Service Provider
|
||||
final accountServiceProvider = Provider<AccountService>((ref) {
|
||||
final apiClient = ref.watch(apiClientProvider);
|
||||
final secureStorage = ref.watch(secureStorageProvider);
|
||||
return AccountService(
|
||||
apiClient: apiClient,
|
||||
secureStorage: secureStorage,
|
||||
);
|
||||
});
|
||||
|
||||
// Telemetry Storage Provider
|
||||
final telemetryStorageProvider = Provider<TelemetryStorage>((ref) {
|
||||
return TelemetryStorage();
|
||||
});
|
||||
|
||||
// Multi Account Service Provider
|
||||
// 注意:必须在 AccountService 之前定义,因为 AccountService 依赖它
|
||||
final multiAccountServiceProvider = Provider<MultiAccountService>((ref) {
|
||||
final secureStorage = ref.watch(secureStorageProvider);
|
||||
final localStorage = ref.watch(localStorageProvider);
|
||||
|
|
@ -55,6 +46,18 @@ final multiAccountServiceProvider = Provider<MultiAccountService>((ref) {
|
|||
return MultiAccountService(secureStorage, localStorage, telemetryStorage);
|
||||
});
|
||||
|
||||
// Account Service Provider
|
||||
final accountServiceProvider = Provider<AccountService>((ref) {
|
||||
final apiClient = ref.watch(apiClientProvider);
|
||||
final secureStorage = ref.watch(secureStorageProvider);
|
||||
final multiAccountService = ref.watch(multiAccountServiceProvider);
|
||||
return AccountService(
|
||||
apiClient: apiClient,
|
||||
secureStorage: secureStorage,
|
||||
multiAccountService: multiAccountService,
|
||||
);
|
||||
});
|
||||
|
||||
// Referral Service Provider
|
||||
final referralServiceProvider = Provider<ReferralService>((ref) {
|
||||
final apiClient = ref.watch(apiClientProvider);
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import '../storage/storage_keys.dart';
|
|||
import '../errors/exceptions.dart';
|
||||
import '../telemetry/telemetry_service.dart';
|
||||
import '../sentry/sentry_service.dart';
|
||||
import 'multi_account_service.dart';
|
||||
|
||||
/// 设备硬件信息 (存储在 deviceName 字段中)
|
||||
class DeviceHardwareInfo {
|
||||
|
|
@ -335,14 +336,17 @@ class WalletAddressInfo {
|
|||
class AccountService {
|
||||
final ApiClient _apiClient;
|
||||
final SecureStorage _secureStorage;
|
||||
final MultiAccountService? _multiAccountService;
|
||||
|
||||
static const String _tag = '[AccountService]';
|
||||
|
||||
AccountService({
|
||||
required ApiClient apiClient,
|
||||
required SecureStorage secureStorage,
|
||||
MultiAccountService? multiAccountService,
|
||||
}) : _apiClient = apiClient,
|
||||
_secureStorage = secureStorage {
|
||||
_secureStorage = secureStorage,
|
||||
_multiAccountService = multiAccountService {
|
||||
debugPrint('$_tag 初始化完成');
|
||||
}
|
||||
|
||||
|
|
@ -889,10 +893,45 @@ class AccountService {
|
|||
|
||||
/// 更新本地存储的头像 SVG
|
||||
/// 当从 API 获取到头像后,同步保存到本地存储
|
||||
/// 同时更新账号列表中的头像信息
|
||||
Future<void> updateLocalAvatarSvg(String avatarSvg) async {
|
||||
debugPrint('$_tag updateLocalAvatarSvg() - 更新本地头像 SVG (长度: ${avatarSvg.length})');
|
||||
await _secureStorage.write(key: StorageKeys.avatarSvg, value: avatarSvg);
|
||||
debugPrint('$_tag updateLocalAvatarSvg() - 保存成功');
|
||||
|
||||
// 同步到账号列表
|
||||
await _syncAvatarToAccountList(avatarSvg: avatarSvg);
|
||||
}
|
||||
|
||||
/// 同步头像信息到账号列表
|
||||
Future<void> _syncAvatarToAccountList({String? avatarSvg, String? avatarUrl}) async {
|
||||
await _syncProfileToAccountList(avatarSvg: avatarSvg, avatarUrl: avatarUrl);
|
||||
}
|
||||
|
||||
/// 同步用户资料到账号列表
|
||||
Future<void> _syncProfileToAccountList({
|
||||
String? username,
|
||||
String? avatarSvg,
|
||||
String? avatarUrl,
|
||||
}) async {
|
||||
if (_multiAccountService == null) {
|
||||
debugPrint('$_tag _syncProfileToAccountList() - MultiAccountService 未注入,跳过同步');
|
||||
return;
|
||||
}
|
||||
|
||||
final userSerialNum = await _secureStorage.read(key: StorageKeys.userSerialNum);
|
||||
if (userSerialNum == null) {
|
||||
debugPrint('$_tag _syncProfileToAccountList() - 未找到用户序列号,跳过同步');
|
||||
return;
|
||||
}
|
||||
|
||||
await _multiAccountService.updateAccountInList(
|
||||
userSerialNum: userSerialNum,
|
||||
username: username,
|
||||
avatarSvg: avatarSvg,
|
||||
avatarUrl: avatarUrl,
|
||||
);
|
||||
debugPrint('$_tag _syncProfileToAccountList() - 已同步资料到账号列表');
|
||||
}
|
||||
|
||||
/// 获取推荐码
|
||||
|
|
@ -1180,6 +1219,11 @@ class AccountService {
|
|||
await _secureStorage.write(key: StorageKeys.avatarSvg, value: avatarUrl);
|
||||
}
|
||||
|
||||
// 同步到账号列表
|
||||
if (nickname != null || avatarUrl != null) {
|
||||
await _syncProfileToAccountList(username: nickname, avatarSvg: avatarUrl);
|
||||
}
|
||||
|
||||
debugPrint('$_tag updateProfile() - 用户资料更新完成');
|
||||
} on ApiException catch (e) {
|
||||
debugPrint('$_tag updateProfile() - API 异常: $e');
|
||||
|
|
@ -1263,6 +1307,9 @@ class AccountService {
|
|||
await _secureStorage.write(key: StorageKeys.avatarUrl, value: newAvatarUrl);
|
||||
debugPrint('$_tag uploadAvatar() - 本地存储已更新');
|
||||
|
||||
// 同步到账号列表
|
||||
await _syncProfileToAccountList(avatarUrl: newAvatarUrl);
|
||||
|
||||
// 将图片复制到本地缓存目录
|
||||
try {
|
||||
final localPath = await _saveAvatarToLocal(imageFile);
|
||||
|
|
|
|||
|
|
@ -147,6 +147,39 @@ class MultiAccountService {
|
|||
await _saveAccountList(accounts);
|
||||
}
|
||||
|
||||
/// 更新账号列表中的账号信息(如头像变更时调用)
|
||||
/// 仅更新指定字段,保留其他字段不变
|
||||
Future<void> updateAccountInList({
|
||||
required String userSerialNum,
|
||||
String? username,
|
||||
String? avatarSvg,
|
||||
String? avatarUrl,
|
||||
}) async {
|
||||
debugPrint('$_tag updateAccountInList() - 更新账号: $userSerialNum');
|
||||
final accounts = await getAccountList();
|
||||
|
||||
final existingIndex = accounts.indexWhere(
|
||||
(a) => a.userSerialNum == userSerialNum,
|
||||
);
|
||||
|
||||
if (existingIndex < 0) {
|
||||
debugPrint('$_tag updateAccountInList() - 账号不存在');
|
||||
return;
|
||||
}
|
||||
|
||||
final existing = accounts[existingIndex];
|
||||
accounts[existingIndex] = AccountSummary(
|
||||
userSerialNum: existing.userSerialNum,
|
||||
username: username ?? existing.username,
|
||||
avatarSvg: avatarSvg ?? existing.avatarSvg,
|
||||
avatarUrl: avatarUrl ?? existing.avatarUrl,
|
||||
createdAt: existing.createdAt,
|
||||
);
|
||||
|
||||
await _saveAccountList(accounts);
|
||||
debugPrint('$_tag updateAccountInList() - 更新成功');
|
||||
}
|
||||
|
||||
/// 从列表中移除账号
|
||||
Future<void> removeAccount(String userSerialNum) async {
|
||||
debugPrint('$_tag removeAccount() - 移除账号: $userSerialNum');
|
||||
|
|
|
|||
Loading…
Reference in New Issue