Files
ttstd_family_care/lib/features/home/presentation/system_settings_controller.dart
T
tongtong e06e12fbbc feat(profile): 实现个人资料编辑与策略本地缓存
- 将资料编辑页重构为可交互表单,支持昵称、签名编辑与头像更换
- 新增 DioClient 本地缓存读取与写入方法,支持离线读取策略快照
- 设备策略切换后自动使本地缓存失效,避免数据不一致
- Home Tab 切换时选中态跟随可见页面,滑动过程中实时更新底栏高亮
- 系统设置页引入 loading 遮罩层,防止切换开关时重复操作
- 个人资料页接入 profileController 展示真实用户信息与加载状态
- 适配深色模式,修正 home_tab 中头像与文字颜色硬编码问题
2026-11-09 09:24:51 +08:00

73 lines
2.3 KiB
Dart

import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/network/api_exception.dart';
import '../../device/data/device_providers.dart';
import '../../device/domain/device_models.dart';
// Keep this declaration explicit, as in device_providers.dart: the pinned
// Riverpod generator cannot parse the current Flutter SDK's dot shorthand.
final systemSettingsControllerProvider = NotifierProvider.autoDispose
.family<SystemSettingsController, AsyncValue<DevicePolicy?>, String>(
SystemSettingsController.new,
);
/// 页面级策略状态:先展示缓存,请求期间保留数据并锁定操作。
class SystemSettingsController
extends AutoDisposeFamilyNotifier<AsyncValue<DevicePolicy?>, String> {
late String _sn;
bool _active = true;
@override
AsyncValue<DevicePolicy?> build(String sn) {
_sn = sn;
_active = true;
ref.onDispose(() => _active = false);
unawaited(Future<void>.microtask(_load));
return const AsyncLoading();
}
Future<void> _load() async {
final repo = ref.read(deviceRepositoryProvider);
try {
final cached = await repo.getCachedPolicy(_sn);
if (!_active) return;
if (cached != null) {
state = const AsyncLoading<DevicePolicy?>().copyWithPrevious(
AsyncData(cached),
);
}
final policy = await repo.getPolicy(_sn);
if (_active) state = AsyncData(policy);
} catch (error, stack) {
if (_active) state = AsyncError(error, stack);
}
}
Future<void> toggle(String field, bool enabled) async {
if (state.isLoading || state.valueOrNull == null) return;
final previous = state;
state = const AsyncLoading<DevicePolicy?>().copyWithPrevious(previous);
try {
final repo = ref.read(deviceRepositoryProvider);
final error = await repo.togglePolicy(_sn, field, enabled ? 1 : 0);
if (!_active) return;
if (error != null) {
throw ApiException(code: 'policy_toggle', message: error);
}
final policy = await repo.getPolicy(_sn);
if (!_active) return;
state = AsyncData(policy);
ref.invalidate(devicePolicyProvider(_sn));
} catch (error, stack) {
if (_active) {
state = AsyncError<DevicePolicy?>(
error,
stack,
).copyWithPrevious(previous);
}
}
}
}