feat(profile): 实现个人资料编辑与策略本地缓存
- 将资料编辑页重构为可交互表单,支持昵称、签名编辑与头像更换 - 新增 DioClient 本地缓存读取与写入方法,支持离线读取策略快照 - 设备策略切换后自动使本地缓存失效,避免数据不一致 - Home Tab 切换时选中态跟随可见页面,滑动过程中实时更新底栏高亮 - 系统设置页引入 loading 遮罩层,防止切换开关时重复操作 - 个人资料页接入 profileController 展示真实用户信息与加载状态 - 适配深色模式,修正 home_tab 中头像与文字颜色硬编码问题
This commit is contained in:
1 parent
5f84a64c3e
commit
e06e12fbbc
30 files changed
+2059
-336
No files matched your search
@@ -0,0 +1,82 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../../../core/network/dio_client.dart';
|
||||
import '../../../core/storage/token_storage.dart';
|
||||
import '../domain/profile_models.dart';
|
||||
import '../domain/profile_repository.dart';
|
||||
|
||||
part 'profile_repository_impl.g.dart';
|
||||
|
||||
@riverpod
|
||||
ProfileRepository profileRepository(ProfileRepositoryRef ref) =>
|
||||
ProfileRepositoryImpl();
|
||||
|
||||
class ProfileRepositoryImpl implements ProfileRepository {
|
||||
static const _path = 'v1/client/account/profile';
|
||||
final _picker = ImagePicker();
|
||||
|
||||
UserProfile _parse(dynamic data) {
|
||||
final profile = UserProfile.fromJson(
|
||||
Map<String, dynamic>.from(data as Map),
|
||||
);
|
||||
final avatar = profile.avatar;
|
||||
if (avatar == null || avatar.isEmpty) return profile;
|
||||
return profile.copyWith(
|
||||
avatar: Uri.parse(
|
||||
DioClient.instance.options.baseUrl,
|
||||
).resolve(avatar).toString(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<UserProfile> load() async =>
|
||||
_parse(await DioClient.get(_path, allowStale: true));
|
||||
|
||||
@override
|
||||
Future<UserProfile> save(String nickname, String signature) async {
|
||||
final profile = _parse(
|
||||
await DioClient.put(
|
||||
_path,
|
||||
data: {'nickname': nickname, 'signature': signature},
|
||||
),
|
||||
);
|
||||
await DioClient.storeReadCache(_path, profile.toJson());
|
||||
return profile;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<UserProfile?> chooseAvatar() async {
|
||||
final version = TokenStorage.sessionVersion;
|
||||
final image = await _picker.pickImage(
|
||||
source: ImageSource.gallery,
|
||||
maxWidth: 1024,
|
||||
maxHeight: 1024,
|
||||
imageQuality: 85,
|
||||
);
|
||||
if (image == null || version != TokenStorage.sessionVersion) return null;
|
||||
if (await image.length() > 5 * 1024 * 1024) {
|
||||
throw const ProfileInputException(ProfileIssue.avatarTooLarge);
|
||||
}
|
||||
final name = image.name.toLowerCase();
|
||||
final png = name.endsWith('.png');
|
||||
if (!png && !name.endsWith('.jpg') && !name.endsWith('.jpeg')) {
|
||||
throw const ProfileInputException(ProfileIssue.avatarUnsupported);
|
||||
}
|
||||
final bytes = await image.readAsBytes();
|
||||
if (version != TokenStorage.sessionVersion) return null;
|
||||
final data = FormData.fromMap({
|
||||
'file': MultipartFile.fromBytes(
|
||||
bytes,
|
||||
filename: png ? 'avatar.png' : 'avatar.jpg',
|
||||
contentType: DioMediaType('image', png ? 'png' : 'jpeg'),
|
||||
),
|
||||
});
|
||||
final profile = _parse(
|
||||
await DioClient.postMultipart('$_path/avatar', formData: data),
|
||||
);
|
||||
await DioClient.storeReadCache(_path, profile.toJson());
|
||||
return profile;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'profile_repository_impl.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$profileRepositoryHash() => r'53edb40b05a538aa85e7ff4d1d82072e4c31e5d0';
|
||||
|
||||
/// See also [profileRepository].
|
||||
@ProviderFor(profileRepository)
|
||||
final profileRepositoryProvider =
|
||||
AutoDisposeProvider<ProfileRepository>.internal(
|
||||
profileRepository,
|
||||
name: r'profileRepositoryProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$profileRepositoryHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
typedef ProfileRepositoryRef = AutoDisposeProviderRef<ProfileRepository>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
|
||||
Reference in new issue
Block a user