- 将资料编辑页重构为可交互表单,支持昵称、签名编辑与头像更换 - 新增 DioClient 本地缓存读取与写入方法,支持离线读取策略快照 - 设备策略切换后自动使本地缓存失效,避免数据不一致 - Home Tab 切换时选中态跟随可见页面,滑动过程中实时更新底栏高亮 - 系统设置页引入 loading 遮罩层,防止切换开关时重复操作 - 个人资料页接入 profileController 展示真实用户信息与加载状态 - 适配深色模式,修正 home_tab 中头像与文字颜色硬编码问题
87 lines
2.5 KiB
Dart
87 lines
2.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import '../../../core/network/error_message.dart';
|
|
import '../data/profile_repository_impl.dart';
|
|
import '../domain/profile_models.dart';
|
|
|
|
part 'profile_controller.g.dart';
|
|
|
|
@riverpod
|
|
class ProfileController extends _$ProfileController {
|
|
bool _active = true;
|
|
int _generation = 0;
|
|
|
|
@override
|
|
ProfileState build() {
|
|
_active = true;
|
|
ref.onDispose(() => _active = false);
|
|
unawaited(Future<void>.microtask(reload));
|
|
return const ProfileState();
|
|
}
|
|
|
|
Future<void> reload() async {
|
|
if (!_active || state.saving) return;
|
|
final generation = ++_generation;
|
|
state = state.copyWith(loading: true, alert: null, issue: null);
|
|
try {
|
|
final profile = await ref.read(profileRepositoryProvider).load();
|
|
if (_active && generation == _generation) {
|
|
state = state.copyWith(profile: profile, loading: false);
|
|
}
|
|
} catch (_) {
|
|
if (_active && generation == _generation) {
|
|
state = state.copyWith(loading: false);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<bool> save(String nickname, String signature) async {
|
|
if (state.saving) return false;
|
|
final name = nickname.trim();
|
|
if (name.isEmpty || name.length > 64) {
|
|
state = state.copyWith(issue: ProfileIssue.nicknameInvalid);
|
|
return false;
|
|
}
|
|
if (signature.trim().length > 200) {
|
|
state = state.copyWith(issue: ProfileIssue.signatureInvalid);
|
|
return false;
|
|
}
|
|
return _write(
|
|
() => ref.read(profileRepositoryProvider).save(name, signature.trim()),
|
|
);
|
|
}
|
|
|
|
Future<void> changeAvatar() async {
|
|
await _write(() => ref.read(profileRepositoryProvider).chooseAvatar());
|
|
}
|
|
|
|
Future<bool> _write(Future<UserProfile?> Function() action) async {
|
|
if (state.saving) return false;
|
|
++_generation;
|
|
state = state.copyWith(
|
|
saving: true,
|
|
loading: false,
|
|
alert: null,
|
|
issue: null,
|
|
);
|
|
try {
|
|
final profile = await action();
|
|
if (!_active) return false;
|
|
state = state.copyWith(profile: profile ?? state.profile, saving: false);
|
|
return profile != null;
|
|
} on ProfileInputException catch (error) {
|
|
if (_active) state = state.copyWith(saving: false, issue: error.issue);
|
|
return false;
|
|
} catch (error) {
|
|
if (_active) {
|
|
state = state.copyWith(saving: false, alert: userMessageOf(error));
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void consumeAlert() => state = state.copyWith(alert: null, issue: null);
|
|
}
|