feat(profile): 实现个人资料编辑与策略本地缓存

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

No files matched your search

@@ -0,0 +1,83 @@
import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ttstd_family_care/core/network/api_exception.dart';
import 'package:ttstd_family_care/features/profile/data/profile_repository_impl.dart';
import 'package:ttstd_family_care/features/profile/domain/profile_models.dart';
import 'package:ttstd_family_care/features/profile/domain/profile_repository.dart';
import 'package:ttstd_family_care/features/profile/presentation/profile_controller.dart';
class FakeProfileRepository implements ProfileRepository {
final pendingLoad = Completer<UserProfile>();
bool failSave = false;
int saves = 0;
@override
Future<UserProfile> load() => pendingLoad.future;
@override
Future<UserProfile> save(String nickname, String signature) async {
saves++;
if (failSave) throw const ApiException(code: '400', message: 'Save failed');
return UserProfile(id: '7', nickname: nickname, signature: signature);
}
@override
Future<UserProfile?> chooseAvatar() async => null;
}
void main() {
test(
'late loads cannot overwrite saved profile, failed saves retain data',
() async {
final repository = FakeProfileRepository();
final container = ProviderContainer(
overrides: [profileRepositoryProvider.overrideWithValue(repository)],
);
addTearDown(container.dispose);
container.listen(profileControllerProvider, (_, _) {});
await Future<void>.delayed(Duration.zero);
final controller = container.read(profileControllerProvider.notifier);
expect(await controller.save(' 家人 ', ' 你好 '), isTrue);
repository.pendingLoad.complete(
const UserProfile(id: '7', nickname: 'old'),
);
await Future<void>.delayed(Duration.zero);
expect(container.read(profileControllerProvider).profile?.nickname, '家人');
expect(
container.read(profileControllerProvider).profile?.signature,
'你好',
);
await controller.changeAvatar();
expect(container.read(profileControllerProvider).profile?.nickname, '家人');
expect(container.read(profileControllerProvider).saving, isFalse);
repository.failSave = true;
expect(await controller.save('new', ''), isFalse);
expect(container.read(profileControllerProvider).profile?.nickname, '家人');
expect(container.read(profileControllerProvider).alert, 'Save failed');
final calls = repository.saves;
expect(await controller.save(' ', ''), isFalse);
expect(repository.saves, calls);
expect(
container.read(profileControllerProvider).issue,
ProfileIssue.nicknameInvalid,
);
},
);
test('numeric user IDs and absent optional profile fields decode', () {
final profile = UserProfile.fromJson({
'id': 7,
'nickname': null,
'signature': null,
});
expect(profile.id, '7');
expect(profile.nickname, '');
expect(profile.signature, '');
});
}
@@ -0,0 +1,135 @@
import 'dart:async';
import 'package:cupertino_ui/cupertino_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';
import 'package:ttstd_family_care/app/app.dart';
import 'package:ttstd_family_care/features/device/data/device_providers.dart';
import 'package:ttstd_family_care/features/device/domain/device_models.dart';
import 'package:ttstd_family_care/features/profile/data/profile_repository_impl.dart';
import 'package:ttstd_family_care/features/profile/domain/profile_models.dart';
import 'package:ttstd_family_care/features/profile/domain/profile_repository.dart';
import 'package:ttstd_family_care/features/profile/presentation/profile_edit_page.dart';
import 'package:ttstd_family_care/features/profile/presentation/profile_tab.dart';
import 'package:ttstd_family_care/l10n/app_localizations.dart';
class _ProfileRepository implements ProfileRepository {
Completer<UserProfile>? pendingLoad;
UserProfile profile = const UserProfile(id: '7', nickname: 'Old name');
@override
Future<UserProfile> load() async =>
pendingLoad == null ? profile : await pendingLoad!.future;
@override
Future<UserProfile> save(String nickname, String signature) async {
profile = profile.copyWith(nickname: nickname, signature: signature);
return profile;
}
@override
Future<UserProfile?> chooseAvatar() async => null;
}
void main() {
for (final signature in ['', 'First line\nSecond line\nMore text']) {
testWidgets(
'profile and devices load without shifting sections ($signature)',
(tester) async {
final repository = _ProfileRepository()
..pendingLoad = Completer<UserProfile>();
final devices = Completer<List<DeviceBrief>>();
final router = GoRouter(
routes: [GoRoute(path: '/', builder: (_, _) => const ProfileTab())],
);
addTearDown(router.dispose);
await tester.pumpWidget(
ProviderScope(
overrides: [
profileRepositoryProvider.overrideWithValue(repository),
myDevicesProvider.overrideWith((_) => devices.future),
],
child: App(router: router),
),
);
await tester.pump();
final l10n = AppLocalizations.of(
tester.element(find.byType(ProfileTab)),
);
final boundBefore = tester.getTopLeft(find.text(l10n.deviceBound));
final settingsBefore = tester.getTopLeft(find.text(l10n.settingsTitle));
repository.pendingLoad!.complete(
UserProfile(
id: '123456789012345678901234567890',
nickname: 'A very long nickname that must stay on one line',
signature: signature,
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
expect(tester.getTopLeft(find.text(l10n.deviceBound)), boundBefore);
expect(
tester.getTopLeft(find.text(l10n.settingsTitle)),
settingsBefore,
);
devices.complete([
const DeviceBrief(
serialno: 'one',
snName: 'A very long device name that stays on one line',
snModel: 'Tablet model',
online: 1,
),
]);
await tester.pumpAndSettle();
expect(
tester.getTopLeft(find.text(l10n.settingsTitle)),
settingsBefore,
);
expect(tester.takeException(), isNull);
},
);
}
testWidgets('editing from profile updates the header after saving', (
tester,
) async {
final repository = _ProfileRepository();
final router = GoRouter(
initialLocation: '/profile',
routes: [
GoRoute(path: '/profile', builder: (_, _) => const ProfileTab()),
GoRoute(
path: '/profile/edit',
builder: (_, _) => const ProfileEditPage(),
),
],
);
addTearDown(router.dispose);
await tester.pumpWidget(
ProviderScope(
overrides: [
profileRepositoryProvider.overrideWithValue(repository),
myDevicesProvider.overrideWith((_) async => []),
],
child: App(router: router),
),
);
await tester.pumpAndSettle();
expect(find.text('Old name'), findsOneWidget);
final l10n = AppLocalizations.of(tester.element(find.byType(ProfileTab)));
await tester.tap(find.text(l10n.profileEdit));
await tester.pumpAndSettle();
final fields = find.byType(CupertinoTextField);
await tester.enterText(fields.at(0), 'New name');
await tester.enterText(fields.at(1), 'My signature');
await tester.tap(find.text(l10n.commonSave));
await tester.pumpAndSettle();
expect(router.routeInformationProvider.value.uri.path, '/profile');
expect(find.text('New name'), findsOneWidget);
expect(find.text('My signature'), findsOneWidget);
expect(repository.profile.nickname, 'New name');
});
}