97a1f46e3f
CI / flutter-gates (push) Successful in 1m31s
- 档案 Tab 替换为真实宠物列表(05 号规范 P1):PetsController 四态 驱动(loading/empty/error+重试/ready),空态插画 + 建档 CTA, 下拉刷新,虚线添加卡;页面一律走 T2-11 数据层、不直连 ApiClient - 新增 PetDetailPage(P2 档案信息部分):内存副本首屏 + getPet 刷新, 40401 防枚举 → 不存在态返回列表并刷新;仅 owner 显示编辑入口 (40300 语义);体重/疫苗/时间线区块留待 T2-13/14 接摘要与记录接口 - 新增 PetFormPage(建档/编辑):品种目录接口 + 自定义品种互斥 (目录失败回落自定义 + 重试)、性别契约必填、生日+估算、芯片号; 失焦+提交双校验、错误三层模型沿用登录纵切 - 40902 版本冲突:明确提示 + 自动拉取最新 version 重提路径(有测试); 40903 芯片号冲突字段级报错(有测试) - 埋点挂接:pet_create_started(表单首次输入、每次进入一次)/ succeeded / failed 三事件接线;建宠表单路由名 pet_form、详情页 pet_detail 进入既有 RouteObserver 采集;档案 Tab 页名 pet_archive 改报 pet_list(字典 v2);编辑表单不带路由名(漏斗到达段不掺编辑) - app.dart 装配:pet 服务 ApiClient(:8083)共享 TokenRefresher; 登出 reset 控制器防跨账号泄漏;PetsController.loadBreeds 按物种缓存 - AppState 清理:pets 页原 demo 消费方移除,vaccines 及其模型/持久化 删除;pet demo 仅存首页问候/创作页/主壳头像(后续工单收敛) - 头像按 ADR-010 本地占位,不做上传 - 测试 145 → 177 全绿(列表/详情/表单四态与冲突路径 widget 测试、 控制器 loadBreeds/reset、展示纯函数);analyze 0;format 无 diff Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
98 lines
3.2 KiB
Dart
98 lines
3.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:patbond_flutter/data/demo_data.dart';
|
|
import 'package:patbond_flutter/models/models.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class AppState extends ChangeNotifier {
|
|
static const _petKey = 'patbond_pet';
|
|
static const _postsKey = 'patbond_posts';
|
|
static const _locationWeatherKey = 'patbond_location_weather';
|
|
|
|
/// 首页问候卡 / 创作页 / 主壳头像仍消费的 demo 宠物(T2-12 起档案
|
|
/// Tab 已切独立 pets feature 真实数据;此 demo 随后续工单收敛)。
|
|
PetProfile pet = initialPet;
|
|
List<PostModel> posts = List<PostModel>.from(initialPosts);
|
|
LocationWeather locationWeather = initialLocationWeather;
|
|
bool isReady = false;
|
|
|
|
Future<void> load() async {
|
|
try {
|
|
final preferences = await SharedPreferences.getInstance();
|
|
final savedPet = preferences.getString(_petKey);
|
|
final savedPosts = preferences.getString(_postsKey);
|
|
final savedLocationWeather = preferences.getString(_locationWeatherKey);
|
|
|
|
if (savedPet != null) {
|
|
pet = PetProfile.fromJson(jsonDecode(savedPet) as Map<String, dynamic>);
|
|
}
|
|
if (savedPosts != null) {
|
|
posts = (jsonDecode(savedPosts) as List)
|
|
.map((item) => PostModel.fromJson(item as Map<String, dynamic>))
|
|
.toList();
|
|
}
|
|
if (savedLocationWeather != null) {
|
|
locationWeather = LocationWeather.fromJson(
|
|
jsonDecode(savedLocationWeather) as Map<String, dynamic>,
|
|
);
|
|
}
|
|
} catch (error, stackTrace) {
|
|
debugPrint('读取本地数据失败,已使用默认数据:$error\n$stackTrace');
|
|
pet = initialPet;
|
|
posts = List<PostModel>.from(initialPosts);
|
|
locationWeather = initialLocationWeather;
|
|
} finally {
|
|
isReady = true;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Future<void> updatePost(PostModel value) async {
|
|
final index = posts.indexWhere((post) => post.id == value.id);
|
|
if (index == -1) return;
|
|
posts[index] = value;
|
|
posts = List<PostModel>.from(posts);
|
|
notifyListeners();
|
|
await _savePosts();
|
|
}
|
|
|
|
Future<void> publishPost(PostModel value) async {
|
|
posts = [value, ...posts];
|
|
notifyListeners();
|
|
await _savePosts();
|
|
}
|
|
|
|
Future<void> updateLocationWeather(LocationWeather value) async {
|
|
locationWeather = value;
|
|
notifyListeners();
|
|
await _save(_locationWeatherKey, value.toJson());
|
|
}
|
|
|
|
Future<void> resetDemoData() async {
|
|
pet = initialPet;
|
|
posts = List<PostModel>.from(initialPosts);
|
|
locationWeather = initialLocationWeather;
|
|
notifyListeners();
|
|
final preferences = await SharedPreferences.getInstance();
|
|
await Future.wait([
|
|
preferences.remove(_petKey),
|
|
preferences.remove(_postsKey),
|
|
preferences.remove(_locationWeatherKey),
|
|
]);
|
|
}
|
|
|
|
Future<void> _savePosts() {
|
|
return _save(_postsKey, posts.map((post) => post.toJson()).toList());
|
|
}
|
|
|
|
Future<void> _save(String key, Object value) async {
|
|
try {
|
|
final preferences = await SharedPreferences.getInstance();
|
|
await preferences.setString(key, jsonEncode(value));
|
|
} catch (error, stackTrace) {
|
|
debugPrint('保存本地数据失败:$error\n$stackTrace');
|
|
}
|
|
}
|
|
}
|