Compare commits
10 Commits
8aac8c52cc
..
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 694543647f | |||
| eff3526840 | |||
| a4a97c03c2 | |||
| 7d5c84d06d | |||
| 294fc4a781 | |||
| 6038901099 | |||
| 0e87413360 | |||
| 9892b65a19 | |||
| f873acf9a3 | |||
| 92524da8e2 |
@@ -0,0 +1,243 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'dart:ui' show ImageByteFormat;
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/rendering.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:integration_test/integration_test.dart';
|
||||||
|
import 'package:patbond_flutter/app/app.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/session_manager.dart';
|
||||||
|
import 'package:patbond_flutter/features/pets/pet_detail_page.dart';
|
||||||
|
import 'package:patbond_flutter/features/pets/pet_form_page.dart';
|
||||||
|
|
||||||
|
/// 内存 token 存储(桌面实测环境无 keyring;不落任何持久化)。
|
||||||
|
class _InMemoryTokenStore implements TokenStore {
|
||||||
|
final Map<String, String> _values = {};
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<String?> read(String key) async => _values[key];
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> write(String key, String value) async => _values[key] = value;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> delete(String key) async => _values.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// M3.5-01/02/03 compose 真链路桌面实测(默认跳过,不计入常规测试套件):
|
||||||
|
///
|
||||||
|
/// ```bash
|
||||||
|
/// # 先起后端六容器(patbond-api 仓库根 docker compose up -d --build),再:
|
||||||
|
/// PATBOND_UX_LIVE=1 flutter test integration_test/client_ux_live_test.dart -d linux
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// 驱动**真实 App**(Linux 桌面 GTK 渲染管线 + 真实 HTTP)走用户实测那条路:
|
||||||
|
/// 注册 → UI 登录 → 档案页建档 → 打开生日选择器(校验全中文 + 品牌配色 +
|
||||||
|
/// 手输可用 + 一键今天)→ 保存进详情页(校验花费卡展示实际月份 + chevron)。
|
||||||
|
/// 逐步截图落到 `build/ux-live/`:本机是 Wayland 会话,X11 的
|
||||||
|
/// `import -window root` 取不到根窗口,改为把整棵 App 包一层
|
||||||
|
/// [RepaintBoundary] 后 `toImage()` 直出真实渲染像素(含 Overlay 里的弹窗)。
|
||||||
|
void main() {
|
||||||
|
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.fullyLive;
|
||||||
|
|
||||||
|
final enabled = Platform.environment['PATBOND_UX_LIVE'] == '1';
|
||||||
|
const authBase = 'http://127.0.0.1:8081';
|
||||||
|
const petBase = 'http://127.0.0.1:8083';
|
||||||
|
final shotDir = Directory('build/ux-live');
|
||||||
|
const rootKey = ValueKey<String>('ux-live-root');
|
||||||
|
|
||||||
|
Future<void> shot(WidgetTester tester, String name) async {
|
||||||
|
final boundary = tester.renderObject<RenderRepaintBoundary>(
|
||||||
|
find.byKey(rootKey),
|
||||||
|
);
|
||||||
|
await tester.runAsync(() async {
|
||||||
|
if (!shotDir.existsSync()) shotDir.createSync(recursive: true);
|
||||||
|
final image = await boundary.toImage(pixelRatio: 1.5);
|
||||||
|
final bytes = await image.toByteData(format: ImageByteFormat.png);
|
||||||
|
final path = '${shotDir.path}/$name.png';
|
||||||
|
File(path).writeAsBytesSync(bytes!.buffer.asUint8List());
|
||||||
|
debugPrint('[shot] $name → $path (${bytes.lengthInBytes} bytes)');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> pumpUntil(
|
||||||
|
WidgetTester tester,
|
||||||
|
Finder finder, {
|
||||||
|
Duration timeout = const Duration(seconds: 25),
|
||||||
|
}) async {
|
||||||
|
final deadline = DateTime.now().add(timeout);
|
||||||
|
while (DateTime.now().isBefore(deadline)) {
|
||||||
|
await tester.pump(const Duration(milliseconds: 250));
|
||||||
|
if (finder.evaluate().isNotEmpty) return;
|
||||||
|
}
|
||||||
|
fail('等待超时:$finder');
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> pumpUntilGone(
|
||||||
|
WidgetTester tester,
|
||||||
|
Finder finder, {
|
||||||
|
Duration timeout = const Duration(seconds: 25),
|
||||||
|
}) async {
|
||||||
|
final deadline = DateTime.now().add(timeout);
|
||||||
|
while (DateTime.now().isBefore(deadline)) {
|
||||||
|
await tester.pump(const Duration(milliseconds: 250));
|
||||||
|
if (finder.evaluate().isEmpty) return;
|
||||||
|
}
|
||||||
|
fail('等待消失超时:$finder');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 直连服务端下单个 JSON 请求(种子数据走真实 HTTP,不绕 DB)。
|
||||||
|
Future<Map<String, dynamic>> postJson(
|
||||||
|
String url,
|
||||||
|
Map<String, Object?> body, {
|
||||||
|
String? bearer,
|
||||||
|
}) async {
|
||||||
|
final client = HttpClient();
|
||||||
|
final request = await client.postUrl(Uri.parse(url));
|
||||||
|
request.headers.contentType = ContentType.json;
|
||||||
|
if (bearer != null) {
|
||||||
|
request.headers.set(HttpHeaders.authorizationHeader, 'Bearer $bearer');
|
||||||
|
}
|
||||||
|
request.add(utf8.encode(jsonEncode(body)));
|
||||||
|
final response = await request.close();
|
||||||
|
final text = await response.transform(utf8.decoder).join();
|
||||||
|
client.close();
|
||||||
|
expect(response.statusCode, anyOf(200, 201), reason: 'POST $url 失败:$text');
|
||||||
|
final envelope = jsonDecode(text) as Map<String, dynamic>;
|
||||||
|
expect(envelope['code'], 0, reason: 'POST $url 业务码非 0:$text');
|
||||||
|
return envelope['data'] as Map<String, dynamic>;
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgets('桌面真链路:日期选择器中文/品牌色/手输/今天 + 花费卡实际月份', (tester) async {
|
||||||
|
// ---- 注册一次性账号 + 种子数据(宠物 + 当月一笔就医支出)----
|
||||||
|
final seed = DateTime.now().millisecondsSinceEpoch;
|
||||||
|
final username = 'uxlive$seed';
|
||||||
|
final password = 'Live1234!$seed';
|
||||||
|
await postJson('$authBase/api/v1/auth/register', {
|
||||||
|
'username': username,
|
||||||
|
'phone': '+86137${(seed % 100000000).toString().padLeft(8, '0')}',
|
||||||
|
'password': password,
|
||||||
|
});
|
||||||
|
final tokens = await postJson('$authBase/api/v1/auth/login', {
|
||||||
|
'username': username,
|
||||||
|
'password': password,
|
||||||
|
});
|
||||||
|
final accessToken = tokens['accessToken'] as String;
|
||||||
|
final pet = await postJson('$petBase/api/v1/pets', {
|
||||||
|
'name': '实测豆豆',
|
||||||
|
'species': 'dog',
|
||||||
|
'sex': 'male',
|
||||||
|
'customBreedName': '中华田园犬',
|
||||||
|
}, bearer: accessToken);
|
||||||
|
final petId = pet['id'] as String;
|
||||||
|
// 当月一笔 128.50 元就医支出:花费卡应显示「N 月花费 ¥128.50」——
|
||||||
|
// 用户那次误录(记到 4 月)看到 ¥0 正是因为记录不在当月窗口内。
|
||||||
|
await postJson('$petBase/api/v1/pets/$petId/health-events', {
|
||||||
|
'eventType': 'medical',
|
||||||
|
'occurredAt': DateTime.now().toUtc().toIso8601String(),
|
||||||
|
'title': '皮肤检查',
|
||||||
|
'amountCents': 12850,
|
||||||
|
}, bearer: accessToken);
|
||||||
|
|
||||||
|
// ---- 启动真实 App ----
|
||||||
|
await tester.pumpWidget(
|
||||||
|
RepaintBoundary(
|
||||||
|
key: rootKey,
|
||||||
|
child: App(
|
||||||
|
sessionManager: SessionManager(store: _InMemoryTokenStore()),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await pumpUntil(tester, find.text('登录'));
|
||||||
|
|
||||||
|
await tester.enterText(find.byType(TextField).at(0), username);
|
||||||
|
await tester.enterText(find.byType(TextField).at(1), password);
|
||||||
|
await tester.tap(find.text('登录'));
|
||||||
|
|
||||||
|
// ---- 档案 Tab → 建档表单 ----
|
||||||
|
await pumpUntil(tester, find.text('档案'));
|
||||||
|
await tester.tap(find.text('档案'));
|
||||||
|
// 列表已有种子宠物 → 走虚线「+ 添加宠物」卡(空态 CTA 文案不同)。
|
||||||
|
await pumpUntil(tester, find.textContaining('添加宠物'));
|
||||||
|
await tester.tap(find.textContaining('添加宠物').first);
|
||||||
|
await pumpUntil(tester, find.byType(PetFormPage));
|
||||||
|
await shot(tester, '01-pet-form');
|
||||||
|
|
||||||
|
// ---- 生日日期选择器:全中文 + 品牌配色 ----
|
||||||
|
await tester.tap(find.widgetWithText(ListTile, '生日(可选)'));
|
||||||
|
await pumpUntil(tester, find.byType(DatePickerDialog));
|
||||||
|
await tester.pump(const Duration(milliseconds: 600));
|
||||||
|
await shot(tester, '02-date-picker-zh');
|
||||||
|
|
||||||
|
expect(find.text('选择日期'), findsOneWidget, reason: '标题非中文');
|
||||||
|
expect(find.text('确定'), findsOneWidget);
|
||||||
|
expect(find.text('取消'), findsOneWidget);
|
||||||
|
expect(find.text('Select date'), findsNothing);
|
||||||
|
expect(find.text('OK'), findsNothing);
|
||||||
|
|
||||||
|
final inkColors = tester
|
||||||
|
.widgetList<Ink>(find.byType(Ink))
|
||||||
|
.map((ink) => ink.decoration)
|
||||||
|
.whereType<ShapeDecoration>()
|
||||||
|
.map((d) => d.color)
|
||||||
|
.toList();
|
||||||
|
expect(
|
||||||
|
inkColors,
|
||||||
|
contains(AppColors.primaryStrong),
|
||||||
|
reason: '选中日仍是 fromSeed 派生的暗红棕,未取品牌 primaryStrong',
|
||||||
|
);
|
||||||
|
|
||||||
|
// ---- 手输模式:中文标签 + 敲入已知日期 ----
|
||||||
|
await tester.tap(find.byIcon(Icons.edit_outlined));
|
||||||
|
await pumpUntil(tester, find.text('输入日期'));
|
||||||
|
await shot(tester, '03-date-input-zh');
|
||||||
|
final field = find.descendant(
|
||||||
|
of: find.byType(DatePickerDialog),
|
||||||
|
matching: find.byType(TextField),
|
||||||
|
);
|
||||||
|
await tester.enterText(field, '2024/03/15');
|
||||||
|
await tester.pump(const Duration(milliseconds: 300));
|
||||||
|
await tester.tap(find.text('确定'));
|
||||||
|
await pumpUntil(tester, find.text('2024-03-15'));
|
||||||
|
await shot(tester, '04-date-typed');
|
||||||
|
|
||||||
|
// ---- 「今天」快捷键:一键落今天,不开弹窗 ----
|
||||||
|
final todayText =
|
||||||
|
'${DateTime.now().year}-'
|
||||||
|
'${DateTime.now().month.toString().padLeft(2, '0')}-'
|
||||||
|
'${DateTime.now().day.toString().padLeft(2, '0')}';
|
||||||
|
await tester.tap(find.widgetWithText(TextButton, '今天'));
|
||||||
|
await pumpUntil(tester, find.text(todayText));
|
||||||
|
expect(find.byType(DatePickerDialog), findsNothing, reason: '「今天」不该开弹窗');
|
||||||
|
await shot(tester, '05-today-shortcut');
|
||||||
|
|
||||||
|
// ---- 退出建档表单(本单不验建档链路,宠物与支出已由 API 种下)----
|
||||||
|
await tester.tap(find.byIcon(Icons.arrow_back));
|
||||||
|
await pumpUntilGone(tester, find.byType(PetFormPage));
|
||||||
|
|
||||||
|
// ---- 详情页:花费卡展示实际月份 + chevron 可点提示 ----
|
||||||
|
await pumpUntil(tester, find.text('实测豆豆'));
|
||||||
|
await shot(tester, '06-pets-list');
|
||||||
|
await tester.tap(find.text('实测豆豆'));
|
||||||
|
await pumpUntil(tester, find.byType(PetDetailPage));
|
||||||
|
final monthLabel = '${DateTime.now().month} 月花费';
|
||||||
|
await pumpUntil(tester, find.text(monthLabel));
|
||||||
|
await tester.pump(const Duration(milliseconds: 600));
|
||||||
|
await shot(tester, '07-pet-detail-expense-card');
|
||||||
|
|
||||||
|
expect(find.text('本月花费'), findsNothing, reason: '仍是硬编码「本月花费」');
|
||||||
|
// 当月真有支出 → 卡片给出金额(月份口径自证:记录落在当月才计入)。
|
||||||
|
expect(find.text('¥128.50'), findsOneWidget);
|
||||||
|
expect(
|
||||||
|
find.ancestor(
|
||||||
|
of: find.text(monthLabel),
|
||||||
|
matching: find.widgetWithIcon(Card, Icons.chevron_right),
|
||||||
|
),
|
||||||
|
findsOneWidget,
|
||||||
|
reason: '花费卡缺 chevron 可点提示',
|
||||||
|
);
|
||||||
|
}, skip: !enabled);
|
||||||
|
}
|
||||||
@@ -0,0 +1,471 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
import 'dart:ui' show ImageByteFormat;
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/rendering.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:integration_test/integration_test.dart';
|
||||||
|
import 'package:patbond_flutter/app/app.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/pet_avatar.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/post_card.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/session_manager.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_compression.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_picking.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
import 'package:patbond_flutter/features/pets/pet_detail_page.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_edit_page.dart';
|
||||||
|
|
||||||
|
/// 内存 token 存储(桌面实测环境无 keyring;不落任何持久化)。
|
||||||
|
class _InMemoryTokenStore implements TokenStore {
|
||||||
|
final Map<String, String> _values = {};
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<String?> read(String key) async => _values[key];
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> write(String key, String value) async => _values[key] = value;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> delete(String key) async => _values.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 固定测试图:16×16 棋盘 PNG(87 字节)。
|
||||||
|
///
|
||||||
|
/// **刻意不用 1×1 的极小图**:1×1 的 PNG/JPEG 服务端照收、curl 也能原样取回,
|
||||||
|
/// 但走 `NetworkImage` 的解码路径会抛「Codec failed to produce an image」,
|
||||||
|
/// UI 于是落到 `RemoteImage` 的兜底占位——本机实测踩过这一格,很容易被误判
|
||||||
|
/// 成「头像根本没传上去」。用一张最小的**多像素**图即可走通到真实像素。
|
||||||
|
final Uint8List _pngBytes = base64Decode(
|
||||||
|
'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAHklEQVR42mOo0RV6Fc'
|
||||||
|
'FDPMlAkmogyTBqw6gNQ8YGAKJxBJBXv4+TAAAAAElFTkSuQmCC',
|
||||||
|
);
|
||||||
|
|
||||||
|
/// 桌面替身:选图直接给上面那张 JPEG(Linux 无 image_picker 原生实现)。
|
||||||
|
class _DesktopPicker implements MediaImagePicker {
|
||||||
|
int calls = 0;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<PickedMediaImage>> pickImages({required int limit}) async {
|
||||||
|
calls += 1;
|
||||||
|
return [PickedMediaImage(bytes: _pngBytes, name: 'avatar.png')];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 桌面替身:压缩原样透传(Linux 无 flutter_image_compress 原生实现)。
|
||||||
|
class _PassthroughCompressor implements MediaImageCompressor {
|
||||||
|
const _PassthroughCompressor();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<CompressedMediaImage> compress(
|
||||||
|
PickedMediaImage source, {
|
||||||
|
required int quality,
|
||||||
|
}) async => CompressedMediaImage(bytes: source.bytes, mimeType: 'image/png');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// M3.5-08/09/10 compose 真链路桌面实测(默认跳过,不计入常规测试套件):
|
||||||
|
///
|
||||||
|
/// ```bash
|
||||||
|
/// # 先起后端六容器(patbond-api 仓库根 docker compose up -d --build),再:
|
||||||
|
/// PATBOND_PROFILE_LIVE=1 \
|
||||||
|
/// flutter test integration_test/profile_avatar_live_test.dart -d linux
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// 驱动**真实 App**(Linux 桌面 GTK 渲染 + 真实 HTTP + 真实 MinIO)走一遍:
|
||||||
|
/// UI 注册 → 首页问候语显示用户名 → 资料页显示真实用户名与零值统计 →
|
||||||
|
/// 设昵称 → 首页问候语与资料页同步变昵称 → Feed 作者名同步(验证服务端
|
||||||
|
/// `/internal/users/profiles` 的 SQL 回退链)→ 传用户头像 → 宠物详情传头像。
|
||||||
|
///
|
||||||
|
/// 只有**选图与压缩两层**是桌面替身;createUpload / 预签名 PUT 直传 /
|
||||||
|
/// confirm / PATCH 全为生产实现。逐步截图落到 `build/profile-live/`。
|
||||||
|
void main() {
|
||||||
|
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.fullyLive;
|
||||||
|
|
||||||
|
final enabled = Platform.environment['PATBOND_PROFILE_LIVE'] == '1';
|
||||||
|
const authBase = 'http://127.0.0.1:8081';
|
||||||
|
const petBase = 'http://127.0.0.1:8083';
|
||||||
|
const communityBase = 'http://127.0.0.1:8084';
|
||||||
|
final shotDir = Directory('build/profile-live');
|
||||||
|
const rootKey = ValueKey<String>('profile-live-root');
|
||||||
|
|
||||||
|
Future<void> shot(WidgetTester tester, String name) async {
|
||||||
|
final boundary = tester.renderObject<RenderRepaintBoundary>(
|
||||||
|
find.byKey(rootKey),
|
||||||
|
);
|
||||||
|
await tester.runAsync(() async {
|
||||||
|
if (!shotDir.existsSync()) shotDir.createSync(recursive: true);
|
||||||
|
final image = await boundary.toImage(pixelRatio: 1.5);
|
||||||
|
final bytes = await image.toByteData(format: ImageByteFormat.png);
|
||||||
|
File(
|
||||||
|
'${shotDir.path}/$name.png',
|
||||||
|
).writeAsBytesSync(bytes!.buffer.asUint8List());
|
||||||
|
debugPrint('[shot] $name (${bytes.lengthInBytes} bytes)');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> pumpUntil(
|
||||||
|
WidgetTester tester,
|
||||||
|
Finder finder, {
|
||||||
|
Duration timeout = const Duration(seconds: 30),
|
||||||
|
}) async {
|
||||||
|
final deadline = DateTime.now().add(timeout);
|
||||||
|
while (DateTime.now().isBefore(deadline)) {
|
||||||
|
await tester.pump(const Duration(milliseconds: 250));
|
||||||
|
if (finder.evaluate().isNotEmpty) return;
|
||||||
|
}
|
||||||
|
fail('等待超时:$finder');
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> pumpUntilGone(
|
||||||
|
WidgetTester tester,
|
||||||
|
Finder finder, {
|
||||||
|
Duration timeout = const Duration(seconds: 30),
|
||||||
|
}) async {
|
||||||
|
final deadline = DateTime.now().add(timeout);
|
||||||
|
while (DateTime.now().isBefore(deadline)) {
|
||||||
|
await tester.pump(const Duration(milliseconds: 250));
|
||||||
|
if (finder.evaluate().isEmpty) return;
|
||||||
|
}
|
||||||
|
fail('等待消失超时:$finder');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 让出**真实时间**:`pump(Duration)` 只推进一帧(把动画时钟往前拨,
|
||||||
|
/// 并不真的等 5 秒),等网络/等服务端缓存过期必须走 [WidgetTester.runAsync]。
|
||||||
|
Future<void> settleReal(
|
||||||
|
WidgetTester tester, {
|
||||||
|
Duration wait = const Duration(seconds: 2),
|
||||||
|
}) async {
|
||||||
|
await tester.runAsync(() => Future<void>.delayed(wait));
|
||||||
|
for (var i = 0; i < 6; i++) {
|
||||||
|
await tester.pump(const Duration(milliseconds: 120));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 在指定滚动容器里向下滚到目标可见(资料页/列表页的底部入口都需要)。
|
||||||
|
Future<void> scrollTo(
|
||||||
|
WidgetTester tester,
|
||||||
|
Finder list,
|
||||||
|
Finder target, {
|
||||||
|
int maxDrags = 10,
|
||||||
|
}) async {
|
||||||
|
for (var i = 0; i < maxDrags && target.evaluate().isEmpty; i++) {
|
||||||
|
await tester.drag(list, const Offset(0, -300));
|
||||||
|
await tester.pump(const Duration(milliseconds: 300));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Map<String, dynamic>> postJson(
|
||||||
|
String url,
|
||||||
|
Map<String, Object?> body, {
|
||||||
|
String? bearer,
|
||||||
|
}) async {
|
||||||
|
final client = HttpClient();
|
||||||
|
final request = await client.postUrl(Uri.parse(url));
|
||||||
|
request.headers.contentType = ContentType.json;
|
||||||
|
request.headers.set('Idempotency-Key', DateTime.now().toIso8601String());
|
||||||
|
if (bearer != null) {
|
||||||
|
request.headers.set(HttpHeaders.authorizationHeader, 'Bearer $bearer');
|
||||||
|
}
|
||||||
|
request.add(utf8.encode(jsonEncode(body)));
|
||||||
|
final response = await request.close();
|
||||||
|
final text = await response.transform(utf8.decoder).join();
|
||||||
|
client.close();
|
||||||
|
expect(response.statusCode, anyOf(200, 201), reason: 'POST $url:$text');
|
||||||
|
final envelope = jsonDecode(text) as Map<String, dynamic>;
|
||||||
|
expect(envelope['code'], 0, reason: 'POST $url 业务码非 0:$text');
|
||||||
|
return envelope['data'] as Map<String, dynamic>;
|
||||||
|
}
|
||||||
|
|
||||||
|
String greetingPrefix() {
|
||||||
|
final hour = DateTime.now().hour;
|
||||||
|
if (hour < 6) return '夜深了';
|
||||||
|
if (hour < 11) return '早上好';
|
||||||
|
if (hour < 14) return '中午好';
|
||||||
|
if (hour < 18) return '下午好';
|
||||||
|
return '晚上好';
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgets('桌面真链路:注册 → 资料真实化 → 设昵称 → 传头像 → Feed 作者名 → 宠物头像', (tester) async {
|
||||||
|
final picker = _DesktopPicker();
|
||||||
|
final seed = DateTime.now().millisecondsSinceEpoch;
|
||||||
|
final username = 'plive$seed';
|
||||||
|
final password = 'Live1234!$seed';
|
||||||
|
final phone = '+86137${(seed % 100000000).toString().padLeft(8, '0')}';
|
||||||
|
// 昵称与帖文都带 seed:Feed 是全局的,同名会让断言认错卡片。
|
||||||
|
final nickname = '实测柴${seed % 1000000}';
|
||||||
|
final postContent = '资料页实测帖 $seed';
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
RepaintBoundary(
|
||||||
|
key: rootKey,
|
||||||
|
child: App(
|
||||||
|
sessionManager: SessionManager(store: _InMemoryTokenStore()),
|
||||||
|
avatarUploaderFactory: (repository, purpose) => MediaUploader(
|
||||||
|
repository: repository,
|
||||||
|
purpose: purpose,
|
||||||
|
maxImages: 1,
|
||||||
|
maxConcurrentUploads: 1,
|
||||||
|
picker: picker,
|
||||||
|
compressor: const _PassthroughCompressor(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// ---- 步骤 1:UI 注册(注册面不收昵称,ADR-022 决策 D3.5-5)----
|
||||||
|
await pumpUntil(tester, find.text('立即注册'));
|
||||||
|
await tester.tap(find.text('立即注册'));
|
||||||
|
await pumpUntil(tester, find.text('创建账号'));
|
||||||
|
// live binding 下首个 enterText 常被丢(焦点还没建立),故每格「点入 →
|
||||||
|
// 输入 → 验值 → 必要时重输」,本机实测踩过这一格。
|
||||||
|
for (final entry in [
|
||||||
|
(0, username),
|
||||||
|
(1, phone.substring(3)),
|
||||||
|
(2, password),
|
||||||
|
(3, password),
|
||||||
|
]) {
|
||||||
|
final field = find.byType(TextField).at(entry.$1);
|
||||||
|
for (var attempt = 0; attempt < 3; attempt++) {
|
||||||
|
await tester.tap(field);
|
||||||
|
await tester.pump(const Duration(milliseconds: 200));
|
||||||
|
await tester.enterText(field, entry.$2);
|
||||||
|
await tester.pump(const Duration(milliseconds: 300));
|
||||||
|
if (tester.widget<TextField>(field).controller?.text == entry.$2) break;
|
||||||
|
}
|
||||||
|
expect(
|
||||||
|
tester.widget<TextField>(field).controller?.text,
|
||||||
|
entry.$2,
|
||||||
|
reason: '注册表单第 ${entry.$1} 格未收到输入',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await shot(tester, '01-register');
|
||||||
|
await tester.tap(find.widgetWithText(FilledButton, '注册'));
|
||||||
|
|
||||||
|
// ---- 步骤 2:主壳挂载 → 等 /me 到手(资料页与首页共用这一次请求)----
|
||||||
|
await pumpUntil(tester, find.text('我的'));
|
||||||
|
await pumpUntil(tester, find.text('${greetingPrefix()},$username 👋'));
|
||||||
|
await shot(tester, '02-home-greeting-username');
|
||||||
|
|
||||||
|
// 借真实会话种一篇帖与一只宠物(UI 发帖/建档已有各自实测覆盖)。
|
||||||
|
final tokens = await tester.runAsync(
|
||||||
|
() => postJson('$authBase/api/v1/auth/login', {
|
||||||
|
'username': username,
|
||||||
|
'password': password,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
final accessToken = tokens!['accessToken'] as String;
|
||||||
|
await tester.runAsync(
|
||||||
|
() => postJson('$communityBase/api/v1/posts', {
|
||||||
|
'category': 'general',
|
||||||
|
'content': postContent,
|
||||||
|
'status': 'published',
|
||||||
|
}, bearer: accessToken),
|
||||||
|
);
|
||||||
|
final pet = await tester.runAsync(
|
||||||
|
() => postJson('$petBase/api/v1/pets', {
|
||||||
|
'name': '实测豆豆',
|
||||||
|
'species': 'dog',
|
||||||
|
'sex': 'male',
|
||||||
|
'customBreedName': '中华田园犬',
|
||||||
|
}, bearer: accessToken),
|
||||||
|
);
|
||||||
|
|
||||||
|
// ---- 步骤 2.5:登出 → UI 登录 ----
|
||||||
|
// 覆盖登录链路,同时让三个控制器重新预取:登出即 reset 是「跨账号不
|
||||||
|
// 泄漏」的既有纪律(app.dart),重登后各 Tab 从 initial 重新拉取,
|
||||||
|
// 于是上面用 API 种下的帖与宠物才会进入内存副本。
|
||||||
|
await tester.tap(find.text('我的'));
|
||||||
|
await pumpUntil(tester, find.text('@$username'));
|
||||||
|
final profileList = find
|
||||||
|
.ancestor(of: find.text('@$username'), matching: find.byType(ListView))
|
||||||
|
.first;
|
||||||
|
await scrollTo(tester, profileList, find.text('切换账号或退出登录'));
|
||||||
|
await pumpUntil(tester, find.text('切换账号或退出登录'));
|
||||||
|
await tester.tap(find.text('切换账号或退出登录'));
|
||||||
|
await pumpUntil(tester, find.text('立即注册'));
|
||||||
|
for (final entry in [(0, username), (1, password)]) {
|
||||||
|
final field = find.byType(TextField).at(entry.$1);
|
||||||
|
for (var attempt = 0; attempt < 3; attempt++) {
|
||||||
|
await tester.tap(field);
|
||||||
|
await tester.pump(const Duration(milliseconds: 200));
|
||||||
|
await tester.enterText(field, entry.$2);
|
||||||
|
await tester.pump(const Duration(milliseconds: 300));
|
||||||
|
if (tester.widget<TextField>(field).controller?.text == entry.$2) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await tester.tap(find.widgetWithText(FilledButton, '登录'));
|
||||||
|
await pumpUntil(tester, find.text('我的'));
|
||||||
|
await pumpUntil(tester, find.text('${greetingPrefix()},$username 👋'));
|
||||||
|
|
||||||
|
// ---- 步骤 3:资料页展示真实用户名 + 零值统计(不是 demo 的 24/1.8k/2)----
|
||||||
|
await tester.tap(find.text('我的'));
|
||||||
|
await pumpUntil(tester, find.text('@$username'));
|
||||||
|
await shot(tester, '03-profile-real-username');
|
||||||
|
expect(find.text('萌宠新手(豆豆家长)'), findsNothing);
|
||||||
|
expect(find.text('1.8k'), findsNothing);
|
||||||
|
expect(find.text('获赞'), findsOneWidget);
|
||||||
|
// 统计来自服务端:刚发了 1 帖、没人赞 → 作品 1 / 获赞 0。
|
||||||
|
expect(
|
||||||
|
find.descendant(
|
||||||
|
of: find
|
||||||
|
.ancestor(of: find.text('我的作品'), matching: find.byType(Column))
|
||||||
|
.first,
|
||||||
|
matching: find.text('1'),
|
||||||
|
),
|
||||||
|
findsOneWidget,
|
||||||
|
reason: '作品数应为服务端聚合的 1',
|
||||||
|
);
|
||||||
|
|
||||||
|
// ---- 步骤 4:设昵称 ----
|
||||||
|
await tester.tap(find.widgetWithText(OutlinedButton, '编辑资料'));
|
||||||
|
await pumpUntil(tester, find.byType(ProfileEditPage));
|
||||||
|
expect(
|
||||||
|
find.text('未设置,当前展示为用户名「$username」'),
|
||||||
|
findsOneWidget,
|
||||||
|
reason: '编辑页不该把 username 预填进昵称框',
|
||||||
|
);
|
||||||
|
await shot(tester, '04-profile-edit-empty');
|
||||||
|
await tester.enterText(find.byType(TextField).first, nickname);
|
||||||
|
await tester.pump(const Duration(milliseconds: 300));
|
||||||
|
await tester.tap(find.widgetWithText(FilledButton, '保存'));
|
||||||
|
await pumpUntilGone(tester, find.byType(ProfileEditPage));
|
||||||
|
await pumpUntil(tester, find.text(nickname));
|
||||||
|
await shot(tester, '05-profile-nickname-set');
|
||||||
|
|
||||||
|
// ---- 步骤 5:上传用户头像(真实两步上传 + PATCH)----
|
||||||
|
await tester.tap(find.widgetWithText(OutlinedButton, '编辑资料'));
|
||||||
|
await pumpUntil(tester, find.byType(ProfileEditPage));
|
||||||
|
await tester.tap(find.widgetWithText(TextButton, '更换头像'));
|
||||||
|
await pumpUntil(tester, find.text('使用这张'));
|
||||||
|
await shot(tester, '06-avatar-sheet-ready');
|
||||||
|
await tester.tap(find.text('使用这张'));
|
||||||
|
await pumpUntil(tester, find.text('已选择\n新头像'));
|
||||||
|
await tester.tap(find.widgetWithText(FilledButton, '保存'));
|
||||||
|
await pumpUntilGone(tester, find.byType(ProfileEditPage));
|
||||||
|
await pumpUntil(tester, find.text('资料已更新'));
|
||||||
|
await settleReal(tester);
|
||||||
|
await shot(tester, '07-profile-avatar-uploaded');
|
||||||
|
expect(picker.calls, 1);
|
||||||
|
|
||||||
|
// ---- 步骤 6:首页问候语 + Feed 作者名同步为昵称 ----
|
||||||
|
await tester.tap(find.text('首页'));
|
||||||
|
await pumpUntil(tester, find.text('${greetingPrefix()},$nickname 👋'));
|
||||||
|
await shot(tester, '08-home-greeting-nickname');
|
||||||
|
|
||||||
|
// Feed 首屏是在设昵称之前拉的,故先下拉刷新——作者名要变必须重取,
|
||||||
|
// 这一步同时验证服务端 `/internal/users/profiles` 的 SQL 回退链
|
||||||
|
// (客户端对 FeedCard.author.nickname 不做任何拼装)。
|
||||||
|
final feedList = find.byType(ListView).first;
|
||||||
|
// ⚠️ 服务端有意的滞后:community 的 AuthorProfileGateway 把
|
||||||
|
// `/internal/users/profiles` 的结果放在 60s TTL 的进程内缓存里
|
||||||
|
// (`patbond.author-profile.cache-ttl`,M3 T3-05)。首屏 Feed 是设昵称
|
||||||
|
// 之前拉的,那次已把「作者名 = username」写进缓存,所以立刻下拉刷新
|
||||||
|
// 仍是旧名——这不是客户端 bug,客户端对作者名不做任何拼装。
|
||||||
|
// 故先等过 TTL,再刷新验证同步。
|
||||||
|
await settleReal(tester, wait: const Duration(seconds: 65));
|
||||||
|
var authorSynced = false;
|
||||||
|
for (var round = 0; round < 3 && !authorSynced; round++) {
|
||||||
|
// 逐段 moveBy + 抬手才会触发 RefreshIndicator(fling 的速度语义被当成
|
||||||
|
// 普通滚动,本机实测过);随后必须循环 pump 让真实 HTTP 有时间完成。
|
||||||
|
final pull = await tester.startGesture(
|
||||||
|
tester.getTopLeft(feedList) + const Offset(200, 120),
|
||||||
|
);
|
||||||
|
for (var i = 0; i < 6; i++) {
|
||||||
|
await pull.moveBy(const Offset(0, 60));
|
||||||
|
await tester.pump(const Duration(milliseconds: 80));
|
||||||
|
}
|
||||||
|
await pull.up();
|
||||||
|
await settleReal(tester, wait: const Duration(seconds: 3));
|
||||||
|
for (var i = 0; i < 8 && find.text(postContent).evaluate().isEmpty; i++) {
|
||||||
|
await tester.drag(feedList, const Offset(0, -240));
|
||||||
|
await tester.pump(const Duration(milliseconds: 400));
|
||||||
|
}
|
||||||
|
if (find.text(postContent).evaluate().isNotEmpty) {
|
||||||
|
final card = find.ancestor(
|
||||||
|
of: find.text(postContent),
|
||||||
|
matching: find.byType(PostCard),
|
||||||
|
);
|
||||||
|
debugPrint(
|
||||||
|
'[live] 第 $round 轮我的卡片文本:'
|
||||||
|
'${tester.widgetList<Text>(find.descendant(of: card, matching: find.byType(Text))).map((t) => t.data).join(' | ')}',
|
||||||
|
);
|
||||||
|
authorSynced = find
|
||||||
|
.descendant(of: card, matching: find.text(nickname))
|
||||||
|
.evaluate()
|
||||||
|
.isNotEmpty;
|
||||||
|
}
|
||||||
|
if (!authorSynced) {
|
||||||
|
await tester.drag(feedList, const Offset(0, 2000));
|
||||||
|
await tester.pump(const Duration(milliseconds: 400));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect(
|
||||||
|
authorSynced,
|
||||||
|
isTrue,
|
||||||
|
reason: 'Feed 作者名未随昵称变化(服务端 /internal 回退链或刷新未生效)',
|
||||||
|
);
|
||||||
|
await shot(tester, '09-feed-author-nickname');
|
||||||
|
|
||||||
|
// ---- 步骤 7:宠物详情传头像(WRITE 档,纯头像 PATCH)----
|
||||||
|
await tester.tap(find.text('档案'));
|
||||||
|
await pumpUntil(tester, find.text('实测豆豆'));
|
||||||
|
await shot(tester, '10a-pets-list-avatar-placeholder');
|
||||||
|
await tester.tap(find.text('实测豆豆'));
|
||||||
|
await pumpUntil(tester, find.byType(PetDetailPage));
|
||||||
|
await shot(tester, '10-pet-detail-placeholder-avatar');
|
||||||
|
expect(
|
||||||
|
tester
|
||||||
|
.widgetList<PetAvatar>(find.byType(PetAvatar))
|
||||||
|
.any((a) => a.url == null),
|
||||||
|
isTrue,
|
||||||
|
reason: '上传前应是爪印占位',
|
||||||
|
);
|
||||||
|
await tester.tap(find.byType(PetAvatar).first);
|
||||||
|
await pumpUntil(tester, find.text('使用这张'));
|
||||||
|
await tester.tap(find.text('使用这张'));
|
||||||
|
await pumpUntil(tester, find.text('头像已更新'));
|
||||||
|
await settleReal(tester, wait: const Duration(seconds: 6));
|
||||||
|
await shot(tester, '11-pet-avatar-uploaded');
|
||||||
|
final detailAvatarUrl = tester
|
||||||
|
.widgetList<PetAvatar>(find.byType(PetAvatar))
|
||||||
|
.map((a) => a.url)
|
||||||
|
.whereType<String>()
|
||||||
|
.firstOrNull;
|
||||||
|
debugPrint('[live] 详情头像 URL:$detailAvatarUrl');
|
||||||
|
final imageStatus = await tester.runAsync(() async {
|
||||||
|
final client = HttpClient();
|
||||||
|
final response = await (await client.getUrl(
|
||||||
|
Uri.parse(detailAvatarUrl!),
|
||||||
|
)).close();
|
||||||
|
final bytes = await response.fold<int>(0, (n, chunk) => n + chunk.length);
|
||||||
|
client.close();
|
||||||
|
return '${response.statusCode} / $bytes bytes';
|
||||||
|
});
|
||||||
|
debugPrint('[live] 应用进程内直取头像:$imageStatus');
|
||||||
|
expect(imageStatus, startsWith('200'), reason: '预签名头像 URL 应可直取');
|
||||||
|
// 真的画出了像素:详情头像下必须有 Image,且不是 RemoteImage 的兜底
|
||||||
|
// 占位(占位是 Icon(Icons.pets),画不出图时才出现)。
|
||||||
|
final detailAvatar = find.byWidgetPredicate(
|
||||||
|
(widget) => widget is PetAvatar && widget.showEditBadge,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
find.descendant(of: detailAvatar, matching: find.byType(Image)),
|
||||||
|
findsOneWidget,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
find.descendant(of: detailAvatar, matching: find.byIcon(Icons.pets)),
|
||||||
|
findsNothing,
|
||||||
|
reason: '仍是爪印占位说明图没画出来(后端对了但 UI 没显示)',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
tester
|
||||||
|
.widgetList<PetAvatar>(find.byType(PetAvatar))
|
||||||
|
.any((a) => (a.url ?? '').contains('pet_avatar')),
|
||||||
|
isTrue,
|
||||||
|
reason: '详情页头像应换成 pet_avatar 前缀的预签名 URL',
|
||||||
|
);
|
||||||
|
debugPrint('[live] petId=${pet!['id']} username=$username');
|
||||||
|
}, skip: !enabled);
|
||||||
|
}
|
||||||
@@ -0,0 +1,193 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:integration_test/integration_test.dart';
|
||||||
|
import 'package:patbond_flutter/app/app.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/post_card.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/session_manager.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_compression.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_picking.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
|
||||||
|
/// 内存 token 存储(桌面实测环境无 keyring;不落任何持久化)。
|
||||||
|
class _InMemoryTokenStore implements TokenStore {
|
||||||
|
final Map<String, String> _values = {};
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<String?> read(String key) async => _values[key];
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> write(String key, String value) async => _values[key] = value;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> delete(String key) async => _values.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 1x1 真 PNG(70B):桌面实测的「选中的照片」。
|
||||||
|
final Uint8List _pngBytes = base64Decode(
|
||||||
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAF'
|
||||||
|
'AAH/q842iQAAAABJRU5ErkJggg==',
|
||||||
|
);
|
||||||
|
|
||||||
|
/// 桌面选图替身:Linux 桌面无 image_picker 平台实现,实测注入字节。
|
||||||
|
class _DesktopPicker implements MediaImagePicker {
|
||||||
|
@override
|
||||||
|
Future<List<PickedMediaImage>> pickImages({required int limit}) async => [
|
||||||
|
PickedMediaImage(bytes: _pngBytes, name: 'live.png'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 桌面压缩替身:Linux 桌面无 flutter_image_compress 原生实现,原样透传
|
||||||
|
/// (mime 落 image/png,在服务端白名单内)。其余环节全为生产实现。
|
||||||
|
class _PassthroughCompressor implements MediaImageCompressor {
|
||||||
|
@override
|
||||||
|
Future<CompressedMediaImage> compress(
|
||||||
|
PickedMediaImage source, {
|
||||||
|
required int quality,
|
||||||
|
}) async => CompressedMediaImage(bytes: source.bytes, mimeType: 'image/png');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// T3-17 发布链路桌面真链路实测(默认跳过,不计入常规测试套件):
|
||||||
|
///
|
||||||
|
/// ```bash
|
||||||
|
/// # 先起后端六容器(patbond-api 仓库根:docker compose up -d --build),再:
|
||||||
|
/// PATBOND_PUBLISH_LIVE=1 flutter test integration_test/publish_live_test.dart -d linux
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// 驱动**真实 App**(Linux 桌面渲染 + 真实 HTTP + MinIO 预签名直传):
|
||||||
|
/// 注册两个一次性账号 → A 登录 → 创作 Tab「发布动态」→ 输入正文 → 选图
|
||||||
|
/// 上传(真 createUpload / 真预签名 PUT / 真 confirm)→ 发布(建草稿 →
|
||||||
|
/// PATCH 迁移)→ 回首页 Feed 新帖置顶可见 → **另起一个全新 App 实例
|
||||||
|
/// (新会话/新控制器/新 HTTP 客户端)以 B 账号登录,B 的 Feed 同样看到
|
||||||
|
/// 该帖**(M3 验收「发布后可在另一客户端看到」取证)。
|
||||||
|
///
|
||||||
|
/// 桌面替身仅两处:选图与压缩(Linux 无这两个插件的平台实现)。
|
||||||
|
/// 注意:桌面 `platform` 值为 `linux`,不在契约枚举内 → 埋点批次被服务端
|
||||||
|
/// 400 整批拒绝(既有预期行为,见 analytics_service.dart 注释);埋点落库
|
||||||
|
/// 取证走 curl(26 号报告 §5c)。
|
||||||
|
void main() {
|
||||||
|
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.fullyLive;
|
||||||
|
|
||||||
|
final enabled = Platform.environment['PATBOND_PUBLISH_LIVE'] == '1';
|
||||||
|
const authBase = 'http://127.0.0.1:8081';
|
||||||
|
|
||||||
|
Future<void> pumpUntil(
|
||||||
|
WidgetTester tester,
|
||||||
|
Finder finder, {
|
||||||
|
Duration timeout = const Duration(seconds: 30),
|
||||||
|
}) async {
|
||||||
|
final deadline = DateTime.now().add(timeout);
|
||||||
|
while (DateTime.now().isBefore(deadline)) {
|
||||||
|
await tester.pump(const Duration(milliseconds: 250));
|
||||||
|
if (finder.evaluate().isNotEmpty) return;
|
||||||
|
}
|
||||||
|
fail('等待超时:$finder');
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> register(String username, String password, int seed) async {
|
||||||
|
final client = HttpClient();
|
||||||
|
final request = await client.postUrl(
|
||||||
|
Uri.parse('$authBase/api/v1/auth/register'),
|
||||||
|
);
|
||||||
|
request.headers.contentType = ContentType.json;
|
||||||
|
request.add(
|
||||||
|
utf8.encode(
|
||||||
|
jsonEncode({
|
||||||
|
'username': username,
|
||||||
|
'phone': '+86137${(seed % 100000000).toString().padLeft(8, '0')}',
|
||||||
|
'password': password,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final response = await request.close();
|
||||||
|
expect(response.statusCode, 200, reason: '注册测试账号失败:$username');
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> login(
|
||||||
|
WidgetTester tester,
|
||||||
|
String username,
|
||||||
|
String password,
|
||||||
|
) async {
|
||||||
|
await pumpUntil(tester, find.text('登录'));
|
||||||
|
await tester.enterText(find.byType(TextField).at(0), username);
|
||||||
|
await tester.enterText(find.byType(TextField).at(1), password);
|
||||||
|
await tester.tap(find.text('登录'));
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgets('发布桌面真链路:选图上传 → 发布 → Feed 置顶 → 第二账号可见', (tester) async {
|
||||||
|
final seed = DateTime.now().millisecondsSinceEpoch;
|
||||||
|
final authorName = 'publive$seed';
|
||||||
|
final readerName = 'pubread$seed';
|
||||||
|
const password = 'Live1234!publish';
|
||||||
|
final marker = '真链路发布 $seed';
|
||||||
|
|
||||||
|
await register(authorName, password, seed);
|
||||||
|
await register(readerName, password, seed + 1);
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
App(
|
||||||
|
sessionManager: SessionManager(store: _InMemoryTokenStore()),
|
||||||
|
mediaUploaderFactory: (repository, analytics) => MediaUploader(
|
||||||
|
repository: repository,
|
||||||
|
picker: _DesktopPicker(),
|
||||||
|
compressor: _PassthroughCompressor(),
|
||||||
|
analytics: analytics,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// ---- A 登录并发布(正文 + 一张真上传的图)----
|
||||||
|
await login(tester, authorName, password);
|
||||||
|
await pumpUntil(tester, find.text('创作'));
|
||||||
|
await tester.tap(find.text('创作'));
|
||||||
|
await pumpUntil(tester, find.text('发布动态'));
|
||||||
|
await tester.tap(find.text('发布动态'));
|
||||||
|
await pumpUntil(tester, find.widgetWithText(TextButton, '存草稿'));
|
||||||
|
|
||||||
|
await tester.enterText(find.byType(TextField).first, marker);
|
||||||
|
await tester.pump(const Duration(milliseconds: 300));
|
||||||
|
await tester.tap(find.byIcon(Icons.add_photo_alternate_outlined));
|
||||||
|
// 上传全程(createUpload → 预签名 PUT → confirm)走真实链路。
|
||||||
|
await pumpUntil(tester, find.byType(Image));
|
||||||
|
final publish = find.widgetWithText(FilledButton, '发布');
|
||||||
|
for (var i = 0; i < 120; i++) {
|
||||||
|
await tester.pump(const Duration(milliseconds: 250));
|
||||||
|
if (tester.widget<FilledButton>(publish).onPressed != null) break;
|
||||||
|
}
|
||||||
|
expect(
|
||||||
|
tester.widget<FilledButton>(publish).onPressed,
|
||||||
|
isNotNull,
|
||||||
|
reason: '图片未在 30s 内 ready,发布钮仍禁用',
|
||||||
|
);
|
||||||
|
await tester.tap(publish);
|
||||||
|
|
||||||
|
// ---- 回首页 Feed:新帖置顶可见 ----
|
||||||
|
await pumpUntil(tester, find.byType(PostCard));
|
||||||
|
await pumpUntil(tester, find.textContaining(marker));
|
||||||
|
final cards = tester.widgetList<PostCard>(find.byType(PostCard)).toList();
|
||||||
|
expect(cards.first.card.contentPreview, contains(marker));
|
||||||
|
expect(cards.first.card.mediaCount, 1);
|
||||||
|
|
||||||
|
// ---- 第二客户端(全新 App 实例 + 全新会话):B 登录后同样看到该帖 ----
|
||||||
|
// 换 key 强制重建整棵树:新的 SessionManager / Controller / HTTP 客户端,
|
||||||
|
// 等价于另一台客户端首次登录(不是同一实例内的账号切换)。
|
||||||
|
await tester.pumpWidget(
|
||||||
|
App(
|
||||||
|
key: const ValueKey('client-b'),
|
||||||
|
sessionManager: SessionManager(store: _InMemoryTokenStore()),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await login(tester, readerName, password);
|
||||||
|
await pumpUntil(tester, find.byType(PostCard));
|
||||||
|
await pumpUntil(tester, find.textContaining(marker));
|
||||||
|
final readerCards = tester
|
||||||
|
.widgetList<PostCard>(find.byType(PostCard))
|
||||||
|
.toList();
|
||||||
|
expect(readerCards.first.card.contentPreview, contains(marker));
|
||||||
|
}, skip: !enabled);
|
||||||
|
}
|
||||||
@@ -19,7 +19,10 @@ enum AnalyticsPageName {
|
|||||||
create('create'),
|
create('create'),
|
||||||
petArchive('pet_archive'),
|
petArchive('pet_archive'),
|
||||||
services('services'),
|
services('services'),
|
||||||
postDetail('post_detail');
|
postDetail('post_detail'),
|
||||||
|
// —— 字典 v3 页面族(22 号报告 §2;本枚举登记 M3 已落地页)——
|
||||||
|
/// 发布页(P3,T3-17 push 全屏页)。
|
||||||
|
postForm('post_form');
|
||||||
|
|
||||||
const AnalyticsPageName(this.pageName);
|
const AnalyticsPageName(this.pageName);
|
||||||
|
|
||||||
|
|||||||
+67
-3
@@ -5,21 +5,27 @@ import 'package:patbond_flutter/analytics/analytics_route_observer.dart';
|
|||||||
import 'package:patbond_flutter/analytics/analytics_service.dart';
|
import 'package:patbond_flutter/analytics/analytics_service.dart';
|
||||||
import 'package:patbond_flutter/analytics/page_view_tracker.dart';
|
import 'package:patbond_flutter/analytics/page_view_tracker.dart';
|
||||||
import 'package:patbond_flutter/analytics/session_tracker.dart';
|
import 'package:patbond_flutter/analytics/session_tracker.dart';
|
||||||
|
import 'package:patbond_flutter/app/app_localization.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_client.dart';
|
import 'package:patbond_flutter/core/network/api_client.dart';
|
||||||
import 'package:patbond_flutter/core/network/token_refresher.dart';
|
import 'package:patbond_flutter/core/network/token_refresher.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/avatar_upload_sheet.dart';
|
||||||
import 'package:patbond_flutter/features/auth/auth_repository.dart';
|
import 'package:patbond_flutter/features/auth/auth_repository.dart';
|
||||||
import 'package:patbond_flutter/features/auth/login_page.dart';
|
import 'package:patbond_flutter/features/auth/login_page.dart';
|
||||||
import 'package:patbond_flutter/features/auth/session_manager.dart';
|
import 'package:patbond_flutter/features/auth/session_manager.dart';
|
||||||
import 'package:patbond_flutter/features/auth/splash_page.dart';
|
import 'package:patbond_flutter/features/auth/splash_page.dart';
|
||||||
import 'package:patbond_flutter/features/community/community_controller.dart';
|
import 'package:patbond_flutter/features/community/community_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_interaction_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/community/community_repository.dart';
|
import 'package:patbond_flutter/features/community/community_repository.dart';
|
||||||
import 'package:patbond_flutter/features/community/feed_analytics.dart';
|
import 'package:patbond_flutter/features/community/feed_analytics.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/post_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/main/main_shell_page.dart';
|
import 'package:patbond_flutter/features/main/main_shell_page.dart';
|
||||||
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/pets/pet_analytics.dart';
|
import 'package:patbond_flutter/features/pets/pet_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/pets/pets_controller.dart';
|
import 'package:patbond_flutter/features/pets/pets_controller.dart';
|
||||||
import 'package:patbond_flutter/features/pets/pets_repository.dart';
|
import 'package:patbond_flutter/features/pets/pets_repository.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||||
import 'package:patbond_flutter/state/app_state.dart';
|
import 'package:patbond_flutter/state/app_state.dart';
|
||||||
|
|
||||||
class App extends StatefulWidget {
|
class App extends StatefulWidget {
|
||||||
@@ -29,6 +35,8 @@ class App extends StatefulWidget {
|
|||||||
this.authRepository,
|
this.authRepository,
|
||||||
this.petsRepository,
|
this.petsRepository,
|
||||||
this.communityRepository,
|
this.communityRepository,
|
||||||
|
this.mediaUploaderFactory,
|
||||||
|
this.avatarUploaderFactory,
|
||||||
});
|
});
|
||||||
|
|
||||||
/// 测试注入口;生产默认走安全存储 + 真实 API。
|
/// 测试注入口;生产默认走安全存储 + 真实 API。
|
||||||
@@ -37,6 +45,14 @@ class App extends StatefulWidget {
|
|||||||
final PetsRepository? petsRepository;
|
final PetsRepository? petsRepository;
|
||||||
final CommunityRepository? communityRepository;
|
final CommunityRepository? communityRepository;
|
||||||
|
|
||||||
|
/// 发布页媒体上传器构造口(桌面实测替换选图/压缩层;生产为 null)。
|
||||||
|
final MediaUploaderFactory? mediaUploaderFactory;
|
||||||
|
|
||||||
|
/// 头像上传器构造口(T3.5-08/09)。生产为 null → 走
|
||||||
|
/// [defaultAvatarUploader];桌面实测与集成测试在此替换选图/压缩层
|
||||||
|
/// (Linux 桌面无 image_picker / 压缩原生实现),网络三段仍是生产实现。
|
||||||
|
final AvatarUploaderFactory? avatarUploaderFactory;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<App> createState() => _AppState();
|
State<App> createState() => _AppState();
|
||||||
}
|
}
|
||||||
@@ -47,9 +63,13 @@ class _AppState extends State<App> {
|
|||||||
late final AuthRepository authRepository;
|
late final AuthRepository authRepository;
|
||||||
late final PetsController petsController;
|
late final PetsController petsController;
|
||||||
late final CommunityController communityController;
|
late final CommunityController communityController;
|
||||||
|
late final ProfileController profileController;
|
||||||
|
late final AvatarUploaderBuilder _avatarUploaderBuilder;
|
||||||
late final PetAnalytics petAnalytics;
|
late final PetAnalytics petAnalytics;
|
||||||
late final HealthRecordAnalytics healthRecordAnalytics;
|
late final HealthRecordAnalytics healthRecordAnalytics;
|
||||||
late final FeedAnalytics feedAnalytics;
|
late final FeedAnalytics feedAnalytics;
|
||||||
|
late final CommunityInteractionAnalytics interactionAnalytics;
|
||||||
|
late final PostAnalytics postAnalytics;
|
||||||
late final SessionTracker _sessionTracker;
|
late final SessionTracker _sessionTracker;
|
||||||
late final AnalyticsService _analytics;
|
late final AnalyticsService _analytics;
|
||||||
late final PageViewTracker _pageViewTracker;
|
late final PageViewTracker _pageViewTracker;
|
||||||
@@ -95,13 +115,32 @@ class _AppState extends State<App> {
|
|||||||
petsController = PetsController(
|
petsController = PetsController(
|
||||||
repository: widget.petsRepository ?? _buildPetsRepository(),
|
repository: widget.petsRepository ?? _buildPetsRepository(),
|
||||||
);
|
);
|
||||||
// T3-14:Feed segment 接线主壳(数据层 T3-12 就位)。
|
// T3-14:Feed segment 接线主壳(数据层 T3-12 就位);T3-16 起
|
||||||
|
// 点赞/收藏成功埋点经 interactionAnalytics 在 controller 内上报。
|
||||||
|
interactionAnalytics = CommunityInteractionAnalytics(_analytics.trackEvent);
|
||||||
|
final communityRepository =
|
||||||
|
widget.communityRepository ?? _buildCommunityRepository();
|
||||||
communityController = CommunityController(
|
communityController = CommunityController(
|
||||||
repository: widget.communityRepository ?? _buildCommunityRepository(),
|
repository: communityRepository,
|
||||||
|
interactionAnalytics: interactionAnalytics,
|
||||||
);
|
);
|
||||||
|
// T3.5-08/10:资料与统计单例,资料 Tab 与首页问候语共用(一次 /me
|
||||||
|
// 供两个消费点,改昵称后两处同时变)。
|
||||||
|
profileController = ProfileController(
|
||||||
|
authRepository: authRepository,
|
||||||
|
communityRepository: communityRepository,
|
||||||
|
);
|
||||||
|
// 头像上传:purpose 由调用页给定(user_avatar / pet_avatar)。仓库复用
|
||||||
|
// community 仓库——media 两步上传端点由 user 服务提供,已在
|
||||||
|
// _buildCommunityRepository 里单独接线(mediaApi)。
|
||||||
|
final avatarFactory = widget.avatarUploaderFactory ?? defaultAvatarUploader;
|
||||||
|
_avatarUploaderBuilder = (purpose) =>
|
||||||
|
avatarFactory(communityRepository, purpose);
|
||||||
petAnalytics = PetAnalytics(_analytics.trackEvent);
|
petAnalytics = PetAnalytics(_analytics.trackEvent);
|
||||||
healthRecordAnalytics = HealthRecordAnalytics(_analytics.trackEvent);
|
healthRecordAnalytics = HealthRecordAnalytics(_analytics.trackEvent);
|
||||||
feedAnalytics = FeedAnalytics(_analytics.trackEvent);
|
feedAnalytics = FeedAnalytics(_analytics.trackEvent);
|
||||||
|
// T3-17:发布漏斗五事件 + 媒体上传三段(发布页与 MediaUploader 消费)。
|
||||||
|
postAnalytics = PostAnalytics(_analytics.trackEvent);
|
||||||
|
|
||||||
// 认证状态切换补点(根路由 AnimatedSwitcher 无路由事件)
|
// 认证状态切换补点(根路由 AnimatedSwitcher 无路由事件)
|
||||||
sessionManager.addListener(_reportAuthStateChange);
|
sessionManager.addListener(_reportAuthStateChange);
|
||||||
@@ -134,8 +173,19 @@ class _AppState extends State<App> {
|
|||||||
session: sessionManager,
|
session: sessionManager,
|
||||||
refresher: refresher,
|
refresher: refresher,
|
||||||
);
|
);
|
||||||
|
// `/api/v1/me` 由 user 服务(:8082)提供,auth(:8081)上没有该路由,
|
||||||
|
// 故单独接一条 user 线路(与 community 仓库的 mediaApi 同构)。
|
||||||
|
final userApi = ApiClient(
|
||||||
|
dio: buildPatbondDio(
|
||||||
|
session: sessionManager,
|
||||||
|
baseUrl: patbondUserApiBaseUrl,
|
||||||
|
),
|
||||||
|
session: sessionManager,
|
||||||
|
refresher: refresher,
|
||||||
|
);
|
||||||
return ApiAuthRepository(
|
return ApiAuthRepository(
|
||||||
api: api,
|
api: api,
|
||||||
|
userApi: userApi,
|
||||||
session: sessionManager,
|
session: sessionManager,
|
||||||
refresher: refresher,
|
refresher: refresher,
|
||||||
analytics: _analytics,
|
analytics: _analytics,
|
||||||
@@ -190,10 +240,12 @@ class _AppState extends State<App> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _reportAuthStateChange() {
|
void _reportAuthStateChange() {
|
||||||
// 登出即清宠物档案与社区 Feed 内存副本(跨账号不泄漏;重登后重新拉取)。
|
// 登出即清宠物档案、社区 Feed 与本人资料内存副本(跨账号不泄漏;
|
||||||
|
// 重登后重新拉取)。
|
||||||
if (sessionManager.status == AuthStatus.unauthenticated) {
|
if (sessionManager.status == AuthStatus.unauthenticated) {
|
||||||
petsController.reset();
|
petsController.reset();
|
||||||
communityController.reset();
|
communityController.reset();
|
||||||
|
profileController.reset();
|
||||||
}
|
}
|
||||||
// 认证状态机切页补点(03 §3.2 非路由曝光 1/2)
|
// 认证状态机切页补点(03 §3.2 非路由曝光 1/2)
|
||||||
final page = switch (sessionManager.status) {
|
final page = switch (sessionManager.status) {
|
||||||
@@ -212,6 +264,7 @@ class _AppState extends State<App> {
|
|||||||
appState.dispose();
|
appState.dispose();
|
||||||
petsController.dispose();
|
petsController.dispose();
|
||||||
communityController.dispose();
|
communityController.dispose();
|
||||||
|
profileController.dispose();
|
||||||
if (widget.sessionManager == null) sessionManager.dispose();
|
if (widget.sessionManager == null) sessionManager.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
@@ -235,9 +288,15 @@ class _AppState extends State<App> {
|
|||||||
appState: appState,
|
appState: appState,
|
||||||
petsController: petsController,
|
petsController: petsController,
|
||||||
communityController: communityController,
|
communityController: communityController,
|
||||||
|
profileController: profileController,
|
||||||
|
currentUserId: sessionManager.userId,
|
||||||
petAnalytics: petAnalytics,
|
petAnalytics: petAnalytics,
|
||||||
healthRecordAnalytics: healthRecordAnalytics,
|
healthRecordAnalytics: healthRecordAnalytics,
|
||||||
feedAnalytics: feedAnalytics,
|
feedAnalytics: feedAnalytics,
|
||||||
|
interactionAnalytics: interactionAnalytics,
|
||||||
|
postAnalytics: postAnalytics,
|
||||||
|
mediaUploaderFactory: widget.mediaUploaderFactory,
|
||||||
|
avatarUploaderBuilder: _avatarUploaderBuilder,
|
||||||
pageViewTracker: _pageViewTracker,
|
pageViewTracker: _pageViewTracker,
|
||||||
onLogout: authRepository.logout,
|
onLogout: authRepository.logout,
|
||||||
);
|
);
|
||||||
@@ -250,6 +309,11 @@ class _AppState extends State<App> {
|
|||||||
title: 'Patbond',
|
title: 'Patbond',
|
||||||
debugShowCheckedModeBanner: false,
|
debugShowCheckedModeBanner: false,
|
||||||
theme: buildAppTheme(),
|
theme: buildAppTheme(),
|
||||||
|
// M3.5-01:Material 内置组件中文化(此前未配 delegate,日期选择器等
|
||||||
|
// 全部回退英文兜底)。单语言 zh-CN,不随设备语言回退英文。
|
||||||
|
localizationsDelegates: appLocalizationsDelegates,
|
||||||
|
supportedLocales: appSupportedLocales,
|
||||||
|
locale: appLocale,
|
||||||
navigatorObservers: [_routeObserver],
|
navigatorObservers: [_routeObserver],
|
||||||
home: ListenableBuilder(
|
home: ListenableBuilder(
|
||||||
listenable: sessionManager,
|
listenable: sessionManager,
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/// 应用本地化配置(M3.5-01)。
|
||||||
|
///
|
||||||
|
/// 根因备忘:M3 之前 [MaterialApp] 未配任何 `localizationsDelegates`,
|
||||||
|
/// Flutter 回退到内置的 `DefaultMaterialLocalizations`(仅英文),导致
|
||||||
|
/// 一切 Material 内置组件(日期选择器标题「Select date」、确定/取消
|
||||||
|
/// 「OK」/「Cancel」、模式切换 tooltip「Switch to input」、格式报错
|
||||||
|
/// 「Invalid format.」)全英文,而业务自绘文案全中文——同一弹窗内中英混排。
|
||||||
|
/// 挂上三件套后分别为「选择日期 / 确定 / 取消 / 切换到输入模式 / 格式无效。」。
|
||||||
|
///
|
||||||
|
/// 这里把三件套 delegate 与 locale 收在一处,供 `App` 与 widget 测试共用:
|
||||||
|
/// 测试若只 `pumpWidget(MaterialApp(home: ...))` 而不带 delegate,看到的仍是
|
||||||
|
/// 英文兜底,与真机不一致;需要断言中文渲染的测试请一并挂上本文件的常量。
|
||||||
|
library;
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||||
|
|
||||||
|
/// Material / Cupertino / Widgets 三件套本地化 delegate。
|
||||||
|
const List<LocalizationsDelegate<Object>> appLocalizationsDelegates =
|
||||||
|
<LocalizationsDelegate<Object>>[
|
||||||
|
GlobalMaterialLocalizations.delegate,
|
||||||
|
GlobalCupertinoLocalizations.delegate,
|
||||||
|
GlobalWidgetsLocalizations.delegate,
|
||||||
|
];
|
||||||
|
|
||||||
|
/// 简体中文(中国大陆)。M3.5 仅单语言,暂不做语言切换。
|
||||||
|
const Locale appLocale = Locale('zh', 'CN');
|
||||||
|
|
||||||
|
/// 支持语言列表。仅 zh-CN——避免设备语言为英文时回退到英文,
|
||||||
|
/// 造成同一界面内业务中文 + 组件英文的混排。
|
||||||
|
const List<Locale> appSupportedLocales = <Locale>[appLocale];
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
|
||||||
|
/// PATCH 请求的**三态字段**:契约 v1.4.0 对「昵称 / 头像」两类天生可选的
|
||||||
|
/// 字段定型为三态语义(`UpdateMeRequest` 与 `UpdatePetRequest.avatarAssetId`):
|
||||||
|
///
|
||||||
|
/// | 态 | JSON 表现 | 服务端语义 |
|
||||||
|
/// | --- | --- | --- |
|
||||||
|
/// | [PatchField.absent] | **键不出现** | 不改 |
|
||||||
|
/// | [PatchField.clear] | 键出现且值为 `null` | 清空 |
|
||||||
|
/// | [PatchField.value] | 键出现且有值 | 设置 |
|
||||||
|
///
|
||||||
|
/// Dart 的 `String?` 只有两态(有值 / null),无法区分「不改」与「清空」——
|
||||||
|
/// 若把「不改」也编码为 `null`,未改动的字段会被服务端当成清空指令执行
|
||||||
|
/// (用户只改昵称,头像就被顺手删了)。故三态必须由类型承载,
|
||||||
|
/// 不能靠 `T?` 加约定。
|
||||||
|
///
|
||||||
|
/// 序列化一律经 [writeTo]:它是「absent 不落键」这条纪律的唯一实现处,
|
||||||
|
/// 各请求 DTO 不自行拼 map,避免某处漏写 `isPresent` 判断。
|
||||||
|
@immutable
|
||||||
|
class PatchField<T extends Object> {
|
||||||
|
/// 不改:键不出现在 JSON 里。
|
||||||
|
const PatchField.absent() : _present = false, _value = null;
|
||||||
|
|
||||||
|
/// 清空:键出现且值为 `null`。
|
||||||
|
const PatchField.clear() : _present = true, _value = null;
|
||||||
|
|
||||||
|
/// 设置为 [value]。
|
||||||
|
const PatchField.value(T value) : _present = true, _value = value;
|
||||||
|
|
||||||
|
final bool _present;
|
||||||
|
final T? _value;
|
||||||
|
|
||||||
|
/// 本次 PATCH 是否触及该字段(决定键是否落进 JSON)。
|
||||||
|
bool get isPresent => _present;
|
||||||
|
|
||||||
|
/// 是否为「显式清空」(present 且值为 null)。
|
||||||
|
bool get isClear => _present && _value == null;
|
||||||
|
|
||||||
|
/// present 且有值时的值;absent 与 clear 均为 null(两者不可由此区分)。
|
||||||
|
T? get valueOrNull => _value;
|
||||||
|
|
||||||
|
/// 按三态把自己写进 [json]:absent 不落键;clear 落 `null`;
|
||||||
|
/// 有值时落 [encode] 的产物(缺省原样写入)。
|
||||||
|
void writeTo(
|
||||||
|
Map<String, Object?> json,
|
||||||
|
String key, {
|
||||||
|
Object? Function(T value)? encode,
|
||||||
|
}) {
|
||||||
|
if (!_present) return;
|
||||||
|
final value = _value;
|
||||||
|
json[key] = value == null ? null : (encode?.call(value) ?? value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(Object other) =>
|
||||||
|
other is PatchField<T> &&
|
||||||
|
other._present == _present &&
|
||||||
|
other._value == _value;
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => Object.hash(_present, _value);
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() => _present ? 'PatchField($_value)' : 'PatchField.absent()';
|
||||||
|
}
|
||||||
@@ -176,6 +176,7 @@ ThemeData buildAppTheme() {
|
|||||||
borderRadius: BorderRadius.all(Radius.circular(AppRadius.md)),
|
borderRadius: BorderRadius.all(Radius.circular(AppRadius.md)),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
datePickerTheme: _datePickerTheme,
|
||||||
navigationBarTheme: NavigationBarThemeData(
|
navigationBarTheme: NavigationBarThemeData(
|
||||||
backgroundColor: AppColors.surface,
|
backgroundColor: AppColors.surface,
|
||||||
indicatorColor: AppColors.surfaceTint,
|
indicatorColor: AppColors.surfaceTint,
|
||||||
@@ -194,3 +195,102 @@ ThemeData buildAppTheme() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 日期选择器主题(M3.5-01)。
|
||||||
|
///
|
||||||
|
/// 根因备忘:此前未定制,`showDatePicker` 完全走 `ColorScheme.fromSeed`
|
||||||
|
/// 由珊瑚橙 `#FF6F4C` 派生出的 M3 调和色(选中日为暗红棕实底),
|
||||||
|
/// 与全 app 品牌色脱节。这里只复用 05 号规范(iteration-2/05、iteration-3/05)
|
||||||
|
/// 已审计过的色对,不新造色值:
|
||||||
|
///
|
||||||
|
/// | 位置 | 色对 | 对比度 | 来源 |
|
||||||
|
/// | --- | --- | --- | --- |
|
||||||
|
/// | 头部(帮助文字 + 标题 + 模式切换图标) | `surfaceTint` 底 + `primaryDark` | 7.98:1 | 选中 chip 同款(iteration-2/05 §3 D7) |
|
||||||
|
/// | 选中日 / 选中年 | `primaryStrong` 底 + 白字 | 4.49:1 | FAB / 头像徽标同款(同 §2) |
|
||||||
|
/// | 今日(未选中)描边与文字 | 白底 + `primaryStrong` | 4.49:1 | 同上 |
|
||||||
|
/// | 未选中日 / 年 | 白底 + `ink` | ≥12:1 | 正文主色 |
|
||||||
|
/// | 星期表头 | 白底 + `inkSoft` | 6.59:1 | 承载信息的次级文字(§3 DEBT-2) |
|
||||||
|
/// | 越界不可选日 | 白底 + `muted` | 3.36:1 | 禁用态,DEBT-2 允许的 `muted` 用途 |
|
||||||
|
/// | 确定 / 取消按钮 | 白底 + `primaryStrong` 文字 | 4.49:1 | 可点击文字链接(§1.1 token 注释) |
|
||||||
|
final DatePickerThemeData _datePickerTheme = DatePickerThemeData(
|
||||||
|
backgroundColor: AppColors.surface,
|
||||||
|
elevation: 0,
|
||||||
|
surfaceTintColor: Colors.transparent,
|
||||||
|
shadowColor: Colors.transparent,
|
||||||
|
shape: const RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.all(Radius.circular(AppRadius.xl)),
|
||||||
|
side: BorderSide(color: AppColors.border),
|
||||||
|
),
|
||||||
|
dividerColor: AppColors.border,
|
||||||
|
headerBackgroundColor: AppColors.surfaceTint,
|
||||||
|
headerForegroundColor: AppColors.primaryDark,
|
||||||
|
headerHelpStyle: const TextStyle(
|
||||||
|
color: AppColors.primaryDark,
|
||||||
|
fontSize: 12,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
),
|
||||||
|
headerHeadlineStyle: const TextStyle(
|
||||||
|
color: AppColors.primaryDark,
|
||||||
|
// 22(默认 32 偏大):中文 `formatMediumDate` 是「9月10日周四」5~6 字,
|
||||||
|
// 横屏侧栏头部宽度下 26 起就会折行,22 一行放得下。
|
||||||
|
fontSize: 22,
|
||||||
|
fontWeight: FontWeight.w800,
|
||||||
|
),
|
||||||
|
// 年份下拉 / 上下月箭头一行:与头部同族深色,白底 8.74:1。
|
||||||
|
subHeaderForegroundColor: AppColors.primaryDark,
|
||||||
|
weekdayStyle: const TextStyle(
|
||||||
|
color: AppColors.inkSoft,
|
||||||
|
fontSize: 12,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
),
|
||||||
|
dayStyle: const TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
|
||||||
|
dayForegroundColor: WidgetStateProperty.resolveWith((states) {
|
||||||
|
if (states.contains(WidgetState.disabled)) return AppColors.muted;
|
||||||
|
if (states.contains(WidgetState.selected)) return Colors.white;
|
||||||
|
return AppColors.ink;
|
||||||
|
}),
|
||||||
|
dayBackgroundColor: WidgetStateProperty.resolveWith((states) {
|
||||||
|
if (states.contains(WidgetState.selected)) return AppColors.primaryStrong;
|
||||||
|
return null;
|
||||||
|
}),
|
||||||
|
dayOverlayColor: WidgetStateProperty.resolveWith((states) {
|
||||||
|
if (states.contains(WidgetState.selected)) return Colors.white24;
|
||||||
|
return AppColors.primary.withAlpha(31);
|
||||||
|
}),
|
||||||
|
todayForegroundColor: WidgetStateProperty.resolveWith((states) {
|
||||||
|
if (states.contains(WidgetState.disabled)) return AppColors.muted;
|
||||||
|
if (states.contains(WidgetState.selected)) return Colors.white;
|
||||||
|
return AppColors.primaryStrong;
|
||||||
|
}),
|
||||||
|
todayBackgroundColor: WidgetStateProperty.resolveWith((states) {
|
||||||
|
if (states.contains(WidgetState.selected)) return AppColors.primaryStrong;
|
||||||
|
return null;
|
||||||
|
}),
|
||||||
|
// 今日始终带描边(选中态由实底承载,未选中态靠 1.5px 描边定位):
|
||||||
|
// `primaryStrong` 白底 4.49:1,超过非文字元素 3:1 门槛。
|
||||||
|
todayBorder: const BorderSide(color: AppColors.primaryStrong, width: 1.5),
|
||||||
|
yearStyle: const TextStyle(fontSize: 15, fontWeight: FontWeight.w600),
|
||||||
|
yearForegroundColor: WidgetStateProperty.resolveWith((states) {
|
||||||
|
if (states.contains(WidgetState.disabled)) return AppColors.muted;
|
||||||
|
if (states.contains(WidgetState.selected)) return Colors.white;
|
||||||
|
return AppColors.ink;
|
||||||
|
}),
|
||||||
|
yearBackgroundColor: WidgetStateProperty.resolveWith((states) {
|
||||||
|
if (states.contains(WidgetState.selected)) return AppColors.primaryStrong;
|
||||||
|
return null;
|
||||||
|
}),
|
||||||
|
yearOverlayColor: WidgetStateProperty.resolveWith((states) {
|
||||||
|
if (states.contains(WidgetState.selected)) return Colors.white24;
|
||||||
|
return AppColors.primary.withAlpha(31);
|
||||||
|
}),
|
||||||
|
cancelButtonStyle: TextButton.styleFrom(
|
||||||
|
foregroundColor: AppColors.inkSoft,
|
||||||
|
minimumSize: const Size(64, 44),
|
||||||
|
textStyle: const TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
|
||||||
|
),
|
||||||
|
confirmButtonStyle: TextButton.styleFrom(
|
||||||
|
foregroundColor: AppColors.primaryStrong,
|
||||||
|
minimumSize: const Size(64, 44),
|
||||||
|
textStyle: const TextStyle(fontSize: 14, fontWeight: FontWeight.w700),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|||||||
@@ -0,0 +1,144 @@
|
|||||||
|
/// 日期录入共享层(M3.5-02)。
|
||||||
|
///
|
||||||
|
/// 根因备忘:全仓 7 处 `showDatePicker` 各写一遍裸调用,用户实测暴露两个问题:
|
||||||
|
///
|
||||||
|
/// 1. **月份只能靠 `< >` 逐月切**。Flutter 原生日历有年份网格、没有月份网格,
|
||||||
|
/// 从 9 月回到 4 月要点 5 次箭头。已实际导致误录——用户把当月(2026-09)
|
||||||
|
/// 的就医记录记成了 2026-04-09,进而误判「本月花费 ¥0」是统计坏了。
|
||||||
|
/// 2. **键盘输入模式在英文兜底下不可用**。原生 calendar 模式本就带一枚
|
||||||
|
/// 切换到手输的铅笔按钮,但未配本地化时按钮 tooltip 是
|
||||||
|
/// `Switch to input`、报错是 `Invalid format.`,中文用户看不懂也不敢用。
|
||||||
|
/// M3.5-01 挂上 zh-CN delegate 后变成「切换到输入模式」/「格式无效。」/
|
||||||
|
/// 「超出范围。」,输入框提示也从 `mm/dd/yyyy` 变成中文习惯的
|
||||||
|
/// `yyyy/mm/dd`,这条路才真正走通。
|
||||||
|
///
|
||||||
|
/// 本文件的处置:
|
||||||
|
///
|
||||||
|
/// - [pickAppDate] 收口 7 处调用:统一 calendar 首屏 + **保留**手输切换
|
||||||
|
/// (明确不用 `calendarOnly`——那恰好会砍掉手输这条快路),并把
|
||||||
|
/// `initialDate` 夹进 `[firstDate, lastDate]` 防越界断言。各调用点原有的
|
||||||
|
/// 业务约束(如健康事件 `lastDate: now` 不许未来)由调用方原样传入,不改。
|
||||||
|
/// - [AppDateFieldTrailing] 给 7 处日期行统一挂「今天」快捷键:绝大多数录入
|
||||||
|
/// 就是「记今天的事」,一键落值比开弹窗再找今天更快,也把「月份导航」
|
||||||
|
/// 这条容易走错的路整段绕开。原生 `showDatePicker` 无法注入自定义动作
|
||||||
|
/// (`builder` 只能包裹整个 Dialog,拿不到它的内部选中态),所以快捷键
|
||||||
|
/// 放在调用方表单行而非弹窗内。
|
||||||
|
library;
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
|
||||||
|
/// 抹掉时分秒,只留年月日(日期选择器返回值与业务日期字段的统一口径)。
|
||||||
|
DateTime dateOnly(DateTime value) =>
|
||||||
|
DateTime(value.year, value.month, value.day);
|
||||||
|
|
||||||
|
/// 今天(本地时区,仅日期)。
|
||||||
|
DateTime today() => dateOnly(DateTime.now());
|
||||||
|
|
||||||
|
/// 判断 [date] 是否落在 `[firstDate, lastDate]` 闭区间内(按日粒度)。
|
||||||
|
bool isDateSelectable({
|
||||||
|
required DateTime date,
|
||||||
|
required DateTime firstDate,
|
||||||
|
required DateTime lastDate,
|
||||||
|
}) {
|
||||||
|
final day = dateOnly(date);
|
||||||
|
return !day.isBefore(dateOnly(firstDate)) && !day.isAfter(dateOnly(lastDate));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 全仓统一的日期选择入口。
|
||||||
|
///
|
||||||
|
/// [initialDate] 会被夹进 `[firstDate, lastDate]`——调用方常传「当前值 ?? 今天」,
|
||||||
|
/// 而部分表单的 `firstDate` 就是今天(如提醒到期日),历史值可能已越界。
|
||||||
|
///
|
||||||
|
/// 返回 `null` 表示用户取消;返回值已抹掉时分秒。
|
||||||
|
Future<DateTime?> pickAppDate({
|
||||||
|
required BuildContext context,
|
||||||
|
required DateTime initialDate,
|
||||||
|
required DateTime firstDate,
|
||||||
|
required DateTime lastDate,
|
||||||
|
}) async {
|
||||||
|
final first = dateOnly(firstDate);
|
||||||
|
final last = dateOnly(lastDate);
|
||||||
|
var initial = dateOnly(initialDate);
|
||||||
|
if (initial.isBefore(first)) initial = first;
|
||||||
|
if (initial.isAfter(last)) initial = last;
|
||||||
|
|
||||||
|
final picked = await showDatePicker(
|
||||||
|
context: context,
|
||||||
|
initialDate: initial,
|
||||||
|
firstDate: first,
|
||||||
|
lastDate: last,
|
||||||
|
// calendar 首屏 + 头部铅笔按钮切手输:日期不确定时翻日历,日期已知时
|
||||||
|
// 一行敲完。不用 calendarOnly(砍掉手输)、不用 input 首屏(多数录入
|
||||||
|
// 是「今天/最近几天」,日历一眼可点,手输反而更慢)。
|
||||||
|
initialEntryMode: DatePickerEntryMode.calendar,
|
||||||
|
// 帮助文字/确定/取消/手输提示与格式报错全部交给 zh-CN 本地化
|
||||||
|
// (appLocalizationsDelegates),不在此硬编码中文,避免两处文案漂移。
|
||||||
|
);
|
||||||
|
return picked == null ? null : dateOnly(picked);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 日期行尾部(7 处日期 [ListTile] 统一形态):「今天」快捷键 + 日历图标。
|
||||||
|
///
|
||||||
|
/// - 「今天」直接把字段落到今天,不开弹窗;今天越界(业务不允许)时按钮
|
||||||
|
/// 自动隐藏,只留日历图标。
|
||||||
|
/// - 触控目标 44×44(项目最小触控口径),文字 `primaryStrong` 白底 4.49:1。
|
||||||
|
class AppDateFieldTrailing extends StatelessWidget {
|
||||||
|
const AppDateFieldTrailing({
|
||||||
|
required this.firstDate,
|
||||||
|
required this.lastDate,
|
||||||
|
required this.onToday,
|
||||||
|
super.key,
|
||||||
|
this.enabled = true,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// 业务允许的最早日期(与 [pickAppDate] 同一约束)。
|
||||||
|
final DateTime firstDate;
|
||||||
|
|
||||||
|
/// 业务允许的最晚日期。
|
||||||
|
final DateTime lastDate;
|
||||||
|
|
||||||
|
/// 点「今天」的回调,入参为今天(仅日期)。
|
||||||
|
final ValueChanged<DateTime> onToday;
|
||||||
|
|
||||||
|
/// 表单提交中等禁用态。
|
||||||
|
final bool enabled;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final now = today();
|
||||||
|
final showToday = isDateSelectable(
|
||||||
|
date: now,
|
||||||
|
firstDate: firstDate,
|
||||||
|
lastDate: lastDate,
|
||||||
|
);
|
||||||
|
return Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
if (showToday)
|
||||||
|
Tooltip(
|
||||||
|
message: '设为今天',
|
||||||
|
child: TextButton(
|
||||||
|
onPressed: enabled ? () => onToday(now) : null,
|
||||||
|
style: TextButton.styleFrom(
|
||||||
|
foregroundColor: AppColors.primaryStrong,
|
||||||
|
disabledForegroundColor: AppColors.muted,
|
||||||
|
minimumSize: const Size(44, 44),
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||||
|
textStyle: const TextStyle(
|
||||||
|
fontSize: 13,
|
||||||
|
fontWeight: FontWeight.w700,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: const Text('今天'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const Icon(
|
||||||
|
Icons.calendar_month_outlined,
|
||||||
|
size: 20,
|
||||||
|
color: AppColors.muted,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,271 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_repository.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
|
||||||
|
/// 页面侧的构造口:调用页只知道「用途」,仓库与选图/压缩层的装配由
|
||||||
|
/// `app.dart` 在装配处完成。
|
||||||
|
///
|
||||||
|
/// 缺省为 null 时页面**不渲染头像上传入口**:意为「本次构建未装配上传能力」,
|
||||||
|
/// 而不是「有入口但点了没反应」。生产装配恒注入(见 `app.dart`)。
|
||||||
|
typedef AvatarUploaderBuilder = MediaUploader Function(MediaPurpose purpose);
|
||||||
|
|
||||||
|
/// App 级注入口(沿 `MediaUploaderFactory` 先例:工厂拿到已装配的仓库)。
|
||||||
|
///
|
||||||
|
/// 生产缺省 [defaultAvatarUploader];**桌面实测与集成测试**在此替换选图与
|
||||||
|
/// 压缩两层——Linux 桌面既无 image_picker 也无 flutter_image_compress 的
|
||||||
|
/// 原生实现,而 createUpload / 预签名 PUT 直传 / confirm 三段仍走生产实现。
|
||||||
|
typedef AvatarUploaderFactory =
|
||||||
|
MediaUploader Function(
|
||||||
|
CommunityRepository repository,
|
||||||
|
MediaPurpose purpose,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// 生产缺省头像上传器:单图、单并发(头像没有批量语义)。
|
||||||
|
MediaUploader defaultAvatarUploader(
|
||||||
|
CommunityRepository repository,
|
||||||
|
MediaPurpose purpose,
|
||||||
|
) => MediaUploader(
|
||||||
|
repository: repository,
|
||||||
|
purpose: purpose,
|
||||||
|
maxImages: 1,
|
||||||
|
maxConcurrentUploads: 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// 弹出头像上传流程,返回**ready 的 assetId**;用户取消或未走到 ready
|
||||||
|
/// 即 null(孤儿防护:未 confirm 的 asset 绝不外露,见 [MediaUploader])。
|
||||||
|
Future<String?> showAvatarUploadSheet(
|
||||||
|
BuildContext context, {
|
||||||
|
required AvatarUploaderBuilder builder,
|
||||||
|
required MediaPurpose purpose,
|
||||||
|
String title = '更换头像',
|
||||||
|
}) {
|
||||||
|
return showModalBottomSheet<String>(
|
||||||
|
context: context,
|
||||||
|
useSafeArea: true,
|
||||||
|
isScrollControlled: true,
|
||||||
|
showDragHandle: true,
|
||||||
|
builder: (context) =>
|
||||||
|
AvatarUploadSheet(uploader: builder(purpose), title: title),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 头像上传 sheet:复用发布页的 [MediaUploader] 六态编排(queued /
|
||||||
|
/// compressing / uploading / confirming / ready / failed),单图上限。
|
||||||
|
///
|
||||||
|
/// 与九宫格的差异只在呈现:这里一次只有一张图,故用整幅预览 + 线性进度
|
||||||
|
/// 条,而不是格内 [UploadProgressOverlay];状态机、凭据过期换新、失败可
|
||||||
|
/// 重试、孤儿防护全部沿用编排器,本组件不复制任何上传逻辑。
|
||||||
|
///
|
||||||
|
/// **ready 后仍需用户点「使用这张」**:上传成功不等于用户满意这张图,
|
||||||
|
/// 自动关闭会剥夺预览确认的机会(头像是长期可见的身份标识)。
|
||||||
|
class AvatarUploadSheet extends StatefulWidget {
|
||||||
|
const AvatarUploadSheet({
|
||||||
|
required this.uploader,
|
||||||
|
required this.title,
|
||||||
|
super.key,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// 本次会话专属的上传器(purpose 与 maxImages=1 由构造口设定);
|
||||||
|
/// 关闭 sheet 即 dispose,在途请求经 [MediaUploader.reset] 作废。
|
||||||
|
final MediaUploader uploader;
|
||||||
|
|
||||||
|
final String title;
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<AvatarUploadSheet> createState() => _AvatarUploadSheetState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _AvatarUploadSheetState extends State<AvatarUploadSheet> {
|
||||||
|
MediaUploader get _uploader => widget.uploader;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_uploader.addListener(_onChanged);
|
||||||
|
// 打开即拉起选择器:sheet 的唯一目的就是选一张图,多一次「选择图片」
|
||||||
|
// 点击是纯摩擦。用户在系统选择器里取消后回落到空态(可再次选择)。
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (mounted) _uploader.pickAndAdd();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onChanged() {
|
||||||
|
if (mounted) setState(() {});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_uploader.removeListener(_onChanged);
|
||||||
|
// 在途任务按 cancelled 收敛(未 confirm 的服务端 asset 弃引用,
|
||||||
|
// 由服务端超时清理兜底)。
|
||||||
|
_uploader
|
||||||
|
..reset()
|
||||||
|
..dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _reselect() async {
|
||||||
|
// 换图前先清空:maxImages=1 时不清空则 remainingSlots=0,选择器不会拉起。
|
||||||
|
_uploader.reset();
|
||||||
|
await _uploader.pickAndAdd();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final items = _uploader.items;
|
||||||
|
final item = items.isEmpty ? null : items.first;
|
||||||
|
return Padding(
|
||||||
|
padding: const EdgeInsets.fromLTRB(20, 0, 20, 24),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
Text(widget.title, style: Theme.of(context).textTheme.titleLarge),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
if (item != null) _preview(item),
|
||||||
|
if (item != null) const SizedBox(height: 14),
|
||||||
|
..._body(item),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 预览:原图字节直接解码(不落盘),圆形裁切以贴合头像最终形态。
|
||||||
|
Widget _preview(MediaUploadItem item) {
|
||||||
|
return Center(
|
||||||
|
child: ClipOval(
|
||||||
|
child: Image.memory(
|
||||||
|
item.previewBytes,
|
||||||
|
width: 132,
|
||||||
|
height: 132,
|
||||||
|
fit: BoxFit.cover,
|
||||||
|
errorBuilder: (context, error, stack) => Container(
|
||||||
|
width: 132,
|
||||||
|
height: 132,
|
||||||
|
color: AppColors.surfaceTint,
|
||||||
|
child: const Icon(Icons.image_outlined, color: AppColors.muted),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Widget> _body(MediaUploadItem? item) {
|
||||||
|
if (item == null) {
|
||||||
|
// 选择器拉起中 / 用户取消后的空态。
|
||||||
|
return _uploader.isPicking
|
||||||
|
? const [_BusyLine(label: '正在打开相册…')]
|
||||||
|
: [
|
||||||
|
const Text(
|
||||||
|
'还没有选择图片',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: TextStyle(color: AppColors.inkSoft, fontSize: 13),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 14),
|
||||||
|
FilledButton.icon(
|
||||||
|
onPressed: _reselect,
|
||||||
|
icon: const Icon(Icons.photo_library_outlined, size: 18),
|
||||||
|
label: const Text('选择图片'),
|
||||||
|
),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
switch (item.phase) {
|
||||||
|
case MediaItemPhase.queued:
|
||||||
|
case MediaItemPhase.compressing:
|
||||||
|
return const [_BusyLine(label: '正在处理图片…')];
|
||||||
|
case MediaItemPhase.uploading:
|
||||||
|
return [
|
||||||
|
_ProgressLine(progress: item.progress),
|
||||||
|
const SizedBox(height: 10),
|
||||||
|
Text(
|
||||||
|
'上传中 ${(item.progress * 100).round()}%',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: const TextStyle(color: AppColors.inkSoft, fontSize: 13),
|
||||||
|
),
|
||||||
|
];
|
||||||
|
case MediaItemPhase.confirming:
|
||||||
|
return const [
|
||||||
|
_ProgressLine(progress: 1),
|
||||||
|
SizedBox(height: 10),
|
||||||
|
Text(
|
||||||
|
'正在确认…',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: TextStyle(color: AppColors.inkSoft, fontSize: 13),
|
||||||
|
),
|
||||||
|
];
|
||||||
|
case MediaItemPhase.ready:
|
||||||
|
return [
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () => Navigator.of(context).pop(item.assetId),
|
||||||
|
child: const Text('使用这张'),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
TextButton(onPressed: _reselect, child: const Text('重新选择')),
|
||||||
|
];
|
||||||
|
case MediaItemPhase.failed:
|
||||||
|
return [
|
||||||
|
Text(
|
||||||
|
item.errorMessage ?? '上传失败',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: const TextStyle(color: AppColors.errorDark, fontSize: 13),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
// 不可重试(如压缩后仍超 10 MB)只给「重新选择」——重试同一张
|
||||||
|
// 必然再失败,给重试钮是误导。
|
||||||
|
if (item.retryable)
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () => _uploader.retry(item.localId),
|
||||||
|
child: const Text('重试'),
|
||||||
|
),
|
||||||
|
if (item.retryable) const SizedBox(height: 8),
|
||||||
|
TextButton(onPressed: _reselect, child: const Text('重新选择')),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _BusyLine extends StatelessWidget {
|
||||||
|
const _BusyLine({required this.label});
|
||||||
|
|
||||||
|
final String label;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
const SizedBox(
|
||||||
|
width: 18,
|
||||||
|
height: 18,
|
||||||
|
child: CircularProgressIndicator(strokeWidth: 2),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 10),
|
||||||
|
Text(
|
||||||
|
label,
|
||||||
|
style: const TextStyle(color: AppColors.inkSoft, fontSize: 13),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ProgressLine extends StatelessWidget {
|
||||||
|
const _ProgressLine({required this.progress});
|
||||||
|
|
||||||
|
final double progress;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return ClipRRect(
|
||||||
|
borderRadius: const BorderRadius.all(Radius.circular(AppRadius.pill)),
|
||||||
|
child: LinearProgressIndicator(
|
||||||
|
value: progress,
|
||||||
|
minHeight: 6,
|
||||||
|
backgroundColor: AppColors.surfaceTint,
|
||||||
|
color: AppColors.primary,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,141 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/pet_avatar.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_display.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
|
||||||
|
/// 评论条目(05 号规范 §3.4,demo 气泡形态升共享):
|
||||||
|
/// `PetAvatar sm32` + 10 + 气泡(`surface` 底、`border` 1px、圆角 16、
|
||||||
|
/// padding 12)——作者名 13/w700 → 4 → 内容 bodyMedium → 6 → 底行
|
||||||
|
/// (时间 11 `inkSoft` + 仅本人评论的「删除」入口)。
|
||||||
|
///
|
||||||
|
/// - @ 回复(单层平铺,契约 replyToUser)以「回复 @昵称:」前缀呈现;
|
||||||
|
/// 降级作者统一「宠友」占位(isDegraded 一个判定口)。
|
||||||
|
/// - [onDelete] 非 null 才渲染删除入口——**权限判定在调用方**(17 号
|
||||||
|
/// 后端语义:仅评论作者可删,他人可见评论 403/40301);[deleting]
|
||||||
|
/// 期间入口替换为 14 转圈防重复提交。
|
||||||
|
/// - 评论点赞(§3.4 底行右端)无契约端点,M3 不渲染。
|
||||||
|
class CommentTile extends StatelessWidget {
|
||||||
|
const CommentTile({
|
||||||
|
required this.comment,
|
||||||
|
super.key,
|
||||||
|
this.onDelete,
|
||||||
|
this.deleting = false,
|
||||||
|
this.now,
|
||||||
|
});
|
||||||
|
|
||||||
|
final PostComment comment;
|
||||||
|
|
||||||
|
/// 删除回调;null = 非本人评论,不渲染删除入口。
|
||||||
|
final VoidCallback? onDelete;
|
||||||
|
|
||||||
|
/// 删除请求在途(入口转圈锁定)。
|
||||||
|
final bool deleting;
|
||||||
|
|
||||||
|
/// 相对时间的参考时钟(测试注入;缺省取当前时间)。
|
||||||
|
final DateTime? now;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final replyTo = comment.replyToUser;
|
||||||
|
return Row(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
PetAvatar(size: PetAvatarSize.sm, url: comment.author.avatarUrl),
|
||||||
|
const SizedBox(width: 10),
|
||||||
|
Expanded(
|
||||||
|
child: Container(
|
||||||
|
padding: const EdgeInsets.all(12),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: AppColors.surface,
|
||||||
|
border: Border.all(color: AppColors.border),
|
||||||
|
borderRadius: BorderRadius.circular(16),
|
||||||
|
),
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
authorDisplayName(comment.author),
|
||||||
|
style: const TextStyle(
|
||||||
|
color: AppColors.ink,
|
||||||
|
fontSize: 13,
|
||||||
|
fontWeight: FontWeight.w700,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Text.rich(
|
||||||
|
TextSpan(
|
||||||
|
children: [
|
||||||
|
if (replyTo != null)
|
||||||
|
TextSpan(
|
||||||
|
text: '回复 @${authorDisplayName(replyTo)}:',
|
||||||
|
style: const TextStyle(
|
||||||
|
color: AppColors.inkSoft,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
TextSpan(text: comment.content),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
style: Theme.of(context).textTheme.bodyMedium,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 6),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
feedRelativeTime(comment.createdAt, now: now),
|
||||||
|
style: const TextStyle(
|
||||||
|
color: AppColors.inkSoft,
|
||||||
|
fontSize: 11,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const Spacer(),
|
||||||
|
if (onDelete != null)
|
||||||
|
deleting
|
||||||
|
? const Padding(
|
||||||
|
padding: EdgeInsets.all(4),
|
||||||
|
child: SizedBox(
|
||||||
|
width: 14,
|
||||||
|
height: 14,
|
||||||
|
child: CircularProgressIndicator(
|
||||||
|
strokeWidth: 2,
|
||||||
|
color: AppColors.inkSoft,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: Semantics(
|
||||||
|
label: '删除评论',
|
||||||
|
button: true,
|
||||||
|
child: InkWell(
|
||||||
|
onTap: onDelete,
|
||||||
|
borderRadius: BorderRadius.circular(
|
||||||
|
AppRadius.pill,
|
||||||
|
),
|
||||||
|
// 视觉 11 字,触控由 padding 撑到 ≥32
|
||||||
|
//(气泡内行高受限,不足 44 以热区扩展补偿)。
|
||||||
|
child: const Padding(
|
||||||
|
padding: EdgeInsets.symmetric(
|
||||||
|
horizontal: 10,
|
||||||
|
vertical: 8,
|
||||||
|
),
|
||||||
|
child: Text(
|
||||||
|
'删除',
|
||||||
|
style: TextStyle(
|
||||||
|
color: AppColors.inkSoft,
|
||||||
|
fontSize: 11,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,12 +31,21 @@ enum LikeButtonVariant {
|
|||||||
final Color activeCountColor;
|
final Color activeCountColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 点赞/收藏交互钮(05 号规范 §3.5 静态规格):图标 20 + 计数 13/w600,
|
/// 点赞/收藏交互钮(05 号规范 §3.5):图标 20 + 计数 13/w600,未激活
|
||||||
/// 未激活一律 `inkSoft`(6.59:1)。触控 44×44 由 padding 撑足。
|
/// 一律 `inkSoft`(6.59:1)。触控 44×44 由 padding 撑足。
|
||||||
///
|
///
|
||||||
/// T3-14 只做展示([onPressed] 传 null 即禁用态,仍按正常色渲染计数与
|
/// 乐观更新视觉(§3.5/§4 三层闪烁抑制的 UI 半边,状态本体由持有方经
|
||||||
/// 状态);乐观更新动画与 ToggleSync 接线属 T3-15/16。
|
/// ToggleSync 驱动):
|
||||||
class LikeButton extends StatelessWidget {
|
///
|
||||||
|
/// - **点按驱动**的状态变化:激活播 240ms 弹性缩放(1→1.25→1)+ 图标
|
||||||
|
/// 120ms 淡入;取消仅 120ms 颜色渐出、无缩放。
|
||||||
|
/// - **非点按驱动**的状态变化(失败回滚 / 服务端对账):零动画直接跳变;
|
||||||
|
/// 若激活动画未播完,等播完再跳(避免动画中途反转的抖动,§4.3a)。
|
||||||
|
/// - 计数变化一律直接替换,不做滚动动画(回滚时无二次滚动)。
|
||||||
|
/// - 系统「减弱动态效果」开启时全部降级为瞬变。
|
||||||
|
///
|
||||||
|
/// [onPressed] 传 null 即纯展示禁用态(仍按正常色渲染计数与状态)。
|
||||||
|
class LikeButton extends StatefulWidget {
|
||||||
const LikeButton({
|
const LikeButton({
|
||||||
required this.variant,
|
required this.variant,
|
||||||
required this.active,
|
required this.active,
|
||||||
@@ -52,15 +61,106 @@ class LikeButton extends StatelessWidget {
|
|||||||
final VoidCallback? onPressed;
|
final VoidCallback? onPressed;
|
||||||
final String? semanticLabel;
|
final String? semanticLabel;
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<LikeButton> createState() => _LikeButtonState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _LikeButtonState extends State<LikeButton>
|
||||||
|
with SingleTickerProviderStateMixin {
|
||||||
|
late final AnimationController _scaleController = AnimationController(
|
||||||
|
vsync: this,
|
||||||
|
duration: const Duration(milliseconds: 240),
|
||||||
|
);
|
||||||
|
late final Animation<double> _scale = TweenSequence<double>([
|
||||||
|
TweenSequenceItem(
|
||||||
|
tween: Tween<double>(
|
||||||
|
begin: 1,
|
||||||
|
end: 1.25,
|
||||||
|
).chain(CurveTween(curve: Curves.easeOut)),
|
||||||
|
weight: 40,
|
||||||
|
),
|
||||||
|
TweenSequenceItem(
|
||||||
|
tween: Tween<double>(
|
||||||
|
begin: 1.25,
|
||||||
|
end: 1,
|
||||||
|
).chain(CurveTween(curve: Curves.easeOutBack)),
|
||||||
|
weight: 60,
|
||||||
|
),
|
||||||
|
]).animate(_scaleController);
|
||||||
|
|
||||||
|
/// 当前展示态(回滚等待动画播完期间可短暂落后于 widget.active)。
|
||||||
|
late bool _displayActive = widget.active;
|
||||||
|
|
||||||
|
/// 展示计数与状态成对更新(§4.3c:回滚不出现「心已灭计数未减」中间帧)。
|
||||||
|
late int _displayCount = widget.count;
|
||||||
|
|
||||||
|
/// 最近一次点按的期望目标态;didUpdateWidget 以此区分「点按驱动」
|
||||||
|
/// (播动画)与「回滚/对账」(零动画跳变)。
|
||||||
|
bool? _expectedTarget;
|
||||||
|
|
||||||
|
/// 图标切换是否走 120ms 淡入淡出(点按驱动);回滚跳变置 false。
|
||||||
|
bool _fadeSwap = false;
|
||||||
|
|
||||||
|
bool get _reduceMotion =>
|
||||||
|
MediaQuery.maybeOf(context)?.disableAnimations ?? false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didUpdateWidget(LikeButton oldWidget) {
|
||||||
|
super.didUpdateWidget(oldWidget);
|
||||||
|
if (oldWidget.active == widget.active) {
|
||||||
|
// 状态未变的计数变化 = 服务端对账:静默替换、不播动画(§4.4)。
|
||||||
|
_displayCount = widget.count;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final tapDriven = _expectedTarget == widget.active;
|
||||||
|
_expectedTarget = null;
|
||||||
|
if (tapDriven && !_reduceMotion) {
|
||||||
|
_fadeSwap = true;
|
||||||
|
_displayActive = widget.active;
|
||||||
|
_displayCount = widget.count;
|
||||||
|
if (widget.active) _scaleController.forward(from: 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 回滚 / 对账:零动画、计数与状态成对跳变。激活动画未播完则等
|
||||||
|
// 播完再跳(§4.3a,避免动画中途反转的抖动)。
|
||||||
|
_fadeSwap = false;
|
||||||
|
if (_scaleController.isAnimating) {
|
||||||
|
_scaleController.forward().whenComplete(() {
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {
|
||||||
|
_displayActive = widget.active;
|
||||||
|
_displayCount = widget.count;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
_displayActive = widget.active;
|
||||||
|
_displayCount = widget.count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _handleTap() {
|
||||||
|
_expectedTarget = !widget.active;
|
||||||
|
widget.onPressed!();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_scaleController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final variant = widget.variant;
|
||||||
|
final active = _displayActive;
|
||||||
final iconColor = active ? variant.activeIconColor : AppColors.inkSoft;
|
final iconColor = active ? variant.activeIconColor : AppColors.inkSoft;
|
||||||
final countColor = active ? variant.activeCountColor : AppColors.inkSoft;
|
final countColor = active ? variant.activeCountColor : AppColors.inkSoft;
|
||||||
return Semantics(
|
return Semantics(
|
||||||
label: semanticLabel,
|
label: widget.semanticLabel,
|
||||||
button: onPressed != null,
|
button: widget.onPressed != null,
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
onTap: onPressed,
|
onTap: widget.onPressed == null ? null : _handleTap,
|
||||||
borderRadius: BorderRadius.circular(AppRadius.pill),
|
borderRadius: BorderRadius.circular(AppRadius.pill),
|
||||||
child: ConstrainedBox(
|
child: ConstrainedBox(
|
||||||
constraints: const BoxConstraints(minWidth: 44, minHeight: 44),
|
constraints: const BoxConstraints(minWidth: 44, minHeight: 44),
|
||||||
@@ -69,14 +169,24 @@ class LikeButton extends StatelessWidget {
|
|||||||
child: Row(
|
child: Row(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
Icon(
|
ScaleTransition(
|
||||||
active ? variant.activeIcon : variant.inactiveIcon,
|
scale: _scale,
|
||||||
size: 20,
|
child: AnimatedSwitcher(
|
||||||
color: iconColor,
|
duration: _fadeSwap
|
||||||
|
? const Duration(milliseconds: 120)
|
||||||
|
: Duration.zero,
|
||||||
|
child: Icon(
|
||||||
|
active ? variant.activeIcon : variant.inactiveIcon,
|
||||||
|
key: ValueKey(active),
|
||||||
|
size: 20,
|
||||||
|
color: iconColor,
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(width: 4),
|
const SizedBox(width: 4),
|
||||||
|
// 计数直接替换(§3.5:不做滚动动画,避免回滚二次滚动)。
|
||||||
Text(
|
Text(
|
||||||
'$count',
|
'$_displayCount',
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
color: countColor,
|
color: countColor,
|
||||||
fontSize: 13,
|
fontSize: 13,
|
||||||
|
|||||||
@@ -28,9 +28,12 @@ enum PetAvatarSize {
|
|||||||
/// 头像实现。圆形裁切 + 3px `surface` 白描边 + 轻投影(正典
|
/// 头像实现。圆形裁切 + 3px `surface` 白描边 + 轻投影(正典
|
||||||
/// `.patbond-avatar` 规格),可选右下编辑徽标与品牌渐变环。
|
/// `.patbond-avatar` 规格),可选右下编辑徽标与品牌渐变环。
|
||||||
///
|
///
|
||||||
/// M2 首版不做头像上传(ADR-010 / D2-1 裁决),[url] 为 null 时渲染
|
/// M2 首版不做头像上传(ADR-010 / D2-1 裁决);T3.5-09 起 [url] 由服务端
|
||||||
/// 本地占位形态(`surfaceTint` 底 + `Icons.pets`);有图时经
|
/// `Pet.avatarUrl`(预签名 GET)驱动,[url] 为 null 时仍渲染本地占位形态
|
||||||
/// [RemoteImage] 加载(loading / 失败兜底由其内置)。
|
/// (`surfaceTint` 底 + `Icons.pets`)——签一个必然 404 的 URL 比返回 null
|
||||||
|
/// 更糟,故服务端对非 ready 的 asset 直接给 null,客户端只有一种占位逻辑。
|
||||||
|
/// 有图时经 [RemoteImage] 加载(缓存 key 已剥签名参数;loading / 失败兜底
|
||||||
|
/// 由其内置)。
|
||||||
class PetAvatar extends StatelessWidget {
|
class PetAvatar extends StatelessWidget {
|
||||||
const PetAvatar({
|
const PetAvatar({
|
||||||
required this.size,
|
required this.size,
|
||||||
@@ -45,7 +48,7 @@ class PetAvatar extends StatelessWidget {
|
|||||||
|
|
||||||
final PetAvatarSize size;
|
final PetAvatarSize size;
|
||||||
|
|
||||||
/// 头像图 URL;null 为本地占位形态(M2 默认)。
|
/// 头像图 URL;null 为本地占位形态(无头像 / asset 非 ready / 存储未配置)。
|
||||||
final String? url;
|
final String? url;
|
||||||
|
|
||||||
/// 可点击时整体 [InkWell] 圆形 ripple。
|
/// 可点击时整体 [InkWell] 圆形 ripple。
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/upload_progress_overlay.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
import 'package:patbond_flutter/widgets/common.dart';
|
import 'package:patbond_flutter/widgets/common.dart';
|
||||||
|
|
||||||
/// 图片九宫格展示态(05 号规范 §3.2)。
|
/// 图片九宫格展示态(05 号规范 §3.2)。
|
||||||
@@ -14,7 +16,8 @@ import 'package:patbond_flutter/widgets/common.dart';
|
|||||||
/// [urls] 单元素而 [totalCount] > 1 时渲染 4:3 单格 + 右下「+N」角标
|
/// [urls] 单元素而 [totalCount] > 1 时渲染 4:3 单格 + 右下「+N」角标
|
||||||
/// 胶囊(同 80% scrim 精算)。
|
/// 胶囊(同 80% scrim 精算)。
|
||||||
///
|
///
|
||||||
/// 编辑态(「+」格 / 删除角标)随发布页工单(T3-17)扩展。
|
/// 编辑态(「+」格 / 删除角标 / 进度覆盖层)见同文件 [PostMediaEditGrid]
|
||||||
|
/// (T3-17 发布页选图区)。
|
||||||
class PostMediaGrid extends StatelessWidget {
|
class PostMediaGrid extends StatelessWidget {
|
||||||
const PostMediaGrid({
|
const PostMediaGrid({
|
||||||
required this.urls,
|
required this.urls,
|
||||||
@@ -106,6 +109,206 @@ class PostMediaGrid extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 编辑态九宫格(05 号规范 §3.2 编辑态;T3-17 发布页选图区)。
|
||||||
|
///
|
||||||
|
/// 与展示态 [PostMediaGrid] 同文件成组,但**独立成类**:展示态以
|
||||||
|
/// 「至少一张图 + URL 列表」为前提(构造断言),编辑态的常态却是
|
||||||
|
/// 「零张图 + 一个『+』格」,共用一个构造签名只会让两边都别扭。
|
||||||
|
///
|
||||||
|
/// - 固定 3 列(九宫格语义),格间距 4、圆角 `sm`(12)、1:1 `cover`;
|
||||||
|
/// 缩略图直接渲染选图原始字节([MediaUploadItem.previewBytes],
|
||||||
|
/// 不落磁盘、不走网络;解码失败回落 surfaceTint 块 + pets 图标)。
|
||||||
|
/// - 每格叠 [UploadProgressOverlay](六态映射四视觉态);可重试失败格
|
||||||
|
/// 整格点按重试,终态失败不给重试通栏。
|
||||||
|
/// - 删除角标:右上 22 圆 `ink` 80% 实底 + 白 close 14,触控热区 32。
|
||||||
|
/// - 「+」格:虚线 1.5px 圆角 12 + `add_photo_alternate_outlined` 24
|
||||||
|
/// `inkSoft`;[canAdd] 为 false(满 9 张)时隐藏。
|
||||||
|
/// - 长按拖拽排序未做(05 §6 D9 可选项):删格即整组重排,position 由
|
||||||
|
/// [MediaUploader.buildAttachRequests] 按当前列表序重发号。
|
||||||
|
class PostMediaEditGrid extends StatelessWidget {
|
||||||
|
const PostMediaEditGrid({
|
||||||
|
required this.items,
|
||||||
|
super.key,
|
||||||
|
this.canAdd = true,
|
||||||
|
this.onAdd,
|
||||||
|
this.onRemove,
|
||||||
|
this.onRetry,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// 当前上传项快照(顺序 = position 语义)。
|
||||||
|
final List<MediaUploadItem> items;
|
||||||
|
|
||||||
|
/// 是否渲染「+」格(剩余槽位 > 0)。
|
||||||
|
final bool canAdd;
|
||||||
|
|
||||||
|
final VoidCallback? onAdd;
|
||||||
|
|
||||||
|
/// 删格回调,参数为 [MediaUploadItem.localId]。
|
||||||
|
final ValueChanged<int>? onRemove;
|
||||||
|
|
||||||
|
/// 重试回调(仅可重试失败格触发),参数为 localId。
|
||||||
|
final ValueChanged<int>? onRetry;
|
||||||
|
|
||||||
|
static const _spacing = 4.0;
|
||||||
|
static const _columns = 3;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final cellCount = items.length + (canAdd ? 1 : 0);
|
||||||
|
if (cellCount == 0) return const SizedBox.shrink();
|
||||||
|
return GridView.builder(
|
||||||
|
shrinkWrap: true,
|
||||||
|
physics: const NeverScrollableScrollPhysics(),
|
||||||
|
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||||
|
crossAxisCount: _columns,
|
||||||
|
mainAxisSpacing: _spacing,
|
||||||
|
crossAxisSpacing: _spacing,
|
||||||
|
),
|
||||||
|
itemCount: cellCount,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
if (index == items.length) return _AddCell(onTap: onAdd);
|
||||||
|
return _EditCell(
|
||||||
|
item: items[index],
|
||||||
|
onRemove: onRemove == null
|
||||||
|
? null
|
||||||
|
: () => onRemove!(items[index].localId),
|
||||||
|
onRetry: onRetry == null || !items[index].retryable
|
||||||
|
? null
|
||||||
|
: () => onRetry!(items[index].localId),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _EditCell extends StatelessWidget {
|
||||||
|
const _EditCell({required this.item, this.onRemove, this.onRetry});
|
||||||
|
|
||||||
|
final MediaUploadItem item;
|
||||||
|
final VoidCallback? onRemove;
|
||||||
|
final VoidCallback? onRetry;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Stack(
|
||||||
|
fit: StackFit.expand,
|
||||||
|
children: [
|
||||||
|
ClipRRect(
|
||||||
|
borderRadius: BorderRadius.circular(AppRadius.sm),
|
||||||
|
child: Image.memory(
|
||||||
|
item.previewBytes,
|
||||||
|
fit: BoxFit.cover,
|
||||||
|
gaplessPlayback: true,
|
||||||
|
// 选图字节无法解码时的兜底(RemoteImage 同款形态)。
|
||||||
|
errorBuilder: (context, _, _) => const ColoredBox(
|
||||||
|
color: AppColors.surfaceTint,
|
||||||
|
child: Center(
|
||||||
|
child: Icon(Icons.pets, color: AppColors.muted, size: 20),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
ClipRRect(
|
||||||
|
borderRadius: BorderRadius.circular(AppRadius.sm),
|
||||||
|
child: UploadProgressOverlay(
|
||||||
|
phase: item.phase,
|
||||||
|
progress: item.progress,
|
||||||
|
onRetry: onRetry,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Positioned(
|
||||||
|
right: 0,
|
||||||
|
top: 0,
|
||||||
|
child: GestureDetector(
|
||||||
|
onTap: onRemove,
|
||||||
|
behavior: HitTestBehavior.opaque,
|
||||||
|
child: const Padding(
|
||||||
|
// 22 圆角标 + padding 撑到 32 触控热区。
|
||||||
|
padding: EdgeInsets.all(5),
|
||||||
|
child: DecoratedBox(
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
shape: BoxShape.circle,
|
||||||
|
color: Color(0xCC3E2A1F),
|
||||||
|
),
|
||||||
|
child: SizedBox(
|
||||||
|
width: 22,
|
||||||
|
height: 22,
|
||||||
|
child: Center(
|
||||||
|
child: Icon(Icons.close, size: 14, color: Colors.white),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _AddCell extends StatelessWidget {
|
||||||
|
const _AddCell({this.onTap});
|
||||||
|
|
||||||
|
final VoidCallback? onTap;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return InkWell(
|
||||||
|
onTap: onTap,
|
||||||
|
borderRadius: BorderRadius.circular(AppRadius.sm),
|
||||||
|
child: CustomPaint(
|
||||||
|
painter: const _DashedBorderPainter(),
|
||||||
|
child: const Center(
|
||||||
|
child: Icon(
|
||||||
|
Icons.add_photo_alternate_outlined,
|
||||||
|
size: 24,
|
||||||
|
color: AppColors.inkSoft,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 「+」格的虚线圆角边框(Flutter 无内置虚线边框,按规范 1.5px 自绘)。
|
||||||
|
class _DashedBorderPainter extends CustomPainter {
|
||||||
|
const _DashedBorderPainter();
|
||||||
|
|
||||||
|
@override
|
||||||
|
void paint(Canvas canvas, Size size) {
|
||||||
|
final paint = Paint()
|
||||||
|
..color = AppColors.border
|
||||||
|
..strokeWidth = 1.5
|
||||||
|
..style = PaintingStyle.stroke;
|
||||||
|
final path = Path()
|
||||||
|
..addRRect(
|
||||||
|
RRect.fromRectAndRadius(
|
||||||
|
Offset.zero & size,
|
||||||
|
const Radius.circular(AppRadius.sm),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
const dash = 5.0;
|
||||||
|
const gap = 4.0;
|
||||||
|
for (final metric in path.computeMetrics()) {
|
||||||
|
var distance = 0.0;
|
||||||
|
while (distance < metric.length) {
|
||||||
|
final next = distance + dash;
|
||||||
|
canvas.drawPath(
|
||||||
|
metric.extractPath(
|
||||||
|
distance,
|
||||||
|
next > metric.length ? metric.length : next,
|
||||||
|
),
|
||||||
|
paint,
|
||||||
|
);
|
||||||
|
distance = next + gap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool shouldRepaint(_DashedBorderPainter oldDelegate) => false;
|
||||||
|
}
|
||||||
|
|
||||||
/// 单图折叠形态:4:3 圆角封面 + 右下「+N」胶囊角标。
|
/// 单图折叠形态:4:3 圆角封面 + 右下「+N」胶囊角标。
|
||||||
class _CollapsedCover extends StatelessWidget {
|
class _CollapsedCover extends StatelessWidget {
|
||||||
const _CollapsedCover({
|
const _CollapsedCover({
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
/// 认证接口的响应模型(接口契约冻结稿,字段名与后端一致)。
|
/// 认证接口的响应模型(接口契约冻结稿,字段名与后端一致)。
|
||||||
|
library;
|
||||||
|
|
||||||
|
import 'package:patbond_flutter/core/models/patch_field.dart';
|
||||||
|
|
||||||
|
export 'package:patbond_flutter/core/models/patch_field.dart';
|
||||||
|
|
||||||
class AuthTokens {
|
class AuthTokens {
|
||||||
const AuthTokens({
|
const AuthTokens({
|
||||||
required this.userId,
|
required this.userId,
|
||||||
@@ -32,11 +38,16 @@ class AuthTokens {
|
|||||||
final DateTime refreshTokenExpiresAt;
|
final DateTime refreshTokenExpiresAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `GET /api/v1/me` 的用户资料。
|
/// `GET` / `PATCH /api/v1/me` 的用户资料(契约 v1.4.0 恰好这 6 个字段)。
|
||||||
|
///
|
||||||
|
/// **不含 `avatarAssetId`**:客户端对头像 asset 只写不读,「有头像」等价于
|
||||||
|
/// [avatarUrl] 非 null。
|
||||||
class UserProfile {
|
class UserProfile {
|
||||||
const UserProfile({
|
const UserProfile({
|
||||||
required this.userId,
|
required this.userId,
|
||||||
required this.username,
|
required this.username,
|
||||||
|
required this.nickname,
|
||||||
|
required this.avatarUrl,
|
||||||
required this.phone,
|
required this.phone,
|
||||||
required this.createdAt,
|
required this.createdAt,
|
||||||
});
|
});
|
||||||
@@ -45,6 +56,11 @@ class UserProfile {
|
|||||||
return UserProfile(
|
return UserProfile(
|
||||||
userId: json['userId'] as String,
|
userId: json['userId'] as String,
|
||||||
username: json['username'] as String,
|
username: json['username'] as String,
|
||||||
|
// DB 原值:服务端**刻意不做 username 回退**(v1.4.0 定型,理由见
|
||||||
|
// [displayName]),未设置即 null。
|
||||||
|
nickname: json['nickname'] as String?,
|
||||||
|
// 时效性预签名 GET URL,每次响应现签;不得持久化、过期即重取。
|
||||||
|
avatarUrl: json['avatarUrl'] as String?,
|
||||||
// 服务端可返回 null(历史数据或未来第三方注册),不得非空强转。
|
// 服务端可返回 null(历史数据或未来第三方注册),不得非空强转。
|
||||||
phone: json['phone'] as String?,
|
phone: json['phone'] as String?,
|
||||||
createdAt: DateTime.parse(json['createdAt'] as String),
|
createdAt: DateTime.parse(json['createdAt'] as String),
|
||||||
@@ -53,6 +69,57 @@ class UserProfile {
|
|||||||
|
|
||||||
final String userId;
|
final String userId;
|
||||||
final String username;
|
final String username;
|
||||||
|
|
||||||
|
/// 昵称 DB 原值,未设置为 null。**编辑态预填只能用它**,不能用
|
||||||
|
/// [displayName]——否则保存时会把展示回退值固化成真实昵称。
|
||||||
|
final String? nickname;
|
||||||
|
|
||||||
|
/// 头像预签名 GET URL(会过期,禁止入本地存储);无头像 / asset 非
|
||||||
|
/// ready / 对象存储未配置三种情况均为 null。
|
||||||
|
final String? avatarUrl;
|
||||||
|
|
||||||
final String? phone;
|
final String? phone;
|
||||||
final DateTime createdAt;
|
final DateTime createdAt;
|
||||||
|
|
||||||
|
/// 本人视角的展示名:`nickname ?? username`。
|
||||||
|
///
|
||||||
|
/// **回退刻意做在客户端展示层**:服务端 `/me` 返回 DB 原值不回退(契约
|
||||||
|
/// v1.4.0 §Me),因为 `/me` 是本人编辑态——若服务端回退,编辑页会把
|
||||||
|
/// `llx` 预填进昵称框,用户误以为设过昵称,下次保存即把这个展示约定
|
||||||
|
/// **固化成真实数据**,`/internal/users/profiles` 的 SQL 回退链从此再不
|
||||||
|
/// 触发。他人视角(Feed 作者名)的回退由服务端 SQL 层承担,客户端不重复。
|
||||||
|
String get displayName => nickname ?? username;
|
||||||
|
|
||||||
|
/// 是否已有头像(响应不回显 avatarAssetId,判定只看 URL 是否现签成功)。
|
||||||
|
bool get hasAvatar => avatarUrl != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `PATCH /api/v1/me` 请求体(契约 v1.4.0,**三态语义**:
|
||||||
|
/// 键缺省 = 不改 / 显式 `null` = 清空 / 给值 = 设置)。
|
||||||
|
///
|
||||||
|
/// 两字段都 absent 即**空 patch**——服务端答 400/40000 而非静默 200,
|
||||||
|
/// 故调用前应以 [isEmpty] 短路(无变更不发请求)。
|
||||||
|
///
|
||||||
|
/// 昵称的服务端校验:btrim 后长度按**码点**计 1~32;纯空白或空串是
|
||||||
|
/// 400/40000 而**不是**隐式清空(清空只走 [PatchField.clear] 一条路)。
|
||||||
|
class UpdateMeRequest {
|
||||||
|
const UpdateMeRequest({
|
||||||
|
this.nickname = const PatchField<String>.absent(),
|
||||||
|
this.avatarAssetId = const PatchField<String>.absent(),
|
||||||
|
});
|
||||||
|
|
||||||
|
final PatchField<String> nickname;
|
||||||
|
|
||||||
|
/// 头像 asset(两步上传产物,`purpose` 须为 `user_avatar`)。
|
||||||
|
final PatchField<String> avatarAssetId;
|
||||||
|
|
||||||
|
/// 本次 patch 未触及任何字段(发出去必得 400/40000)。
|
||||||
|
bool get isEmpty => !nickname.isPresent && !avatarAssetId.isPresent;
|
||||||
|
|
||||||
|
Map<String, Object?> toJson() {
|
||||||
|
final json = <String, Object?>{};
|
||||||
|
nickname.writeTo(json, 'nickname');
|
||||||
|
avatarAssetId.writeTo(json, 'avatarAssetId');
|
||||||
|
return json;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,18 +28,33 @@ abstract class AuthRepository {
|
|||||||
Future<SessionRestoreResult> restoreSession();
|
Future<SessionRestoreResult> restoreSession();
|
||||||
|
|
||||||
Future<UserProfile> me();
|
Future<UserProfile> me();
|
||||||
|
|
||||||
|
/// 本人资料部分更新(昵称 / 头像,三态语义见 [UpdateMeRequest])。
|
||||||
|
/// 成功返回**更新后的全量 [UserProfile]**(服务端回显,含现签 avatarUrl)。
|
||||||
|
Future<UserProfile> updateMe(UpdateMeRequest request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 基于 [ApiClient] 的实现。
|
||||||
|
///
|
||||||
|
/// **端口线路**(ADR-002 无网关,分端口直连):注册/登录/登出/刷新走 auth
|
||||||
|
/// 服务(:8081);`GET` 与 `PATCH /api/v1/me` 由 **user 服务**(:8082
|
||||||
|
/// `MeController`)提供,故经 [userApi] 直连——auth 上没有 `/api/v1/me`
|
||||||
|
/// 路由,走主客户端会得到 404。未提供 [userApi] 时回落主客户端(既有测试
|
||||||
|
/// 桩场景)。同一线路分离手法与 `ApiCommunityRepository` 的 `mediaApi` 一致。
|
||||||
class ApiAuthRepository implements AuthRepository {
|
class ApiAuthRepository implements AuthRepository {
|
||||||
ApiAuthRepository({
|
ApiAuthRepository({
|
||||||
required this._api,
|
required ApiClient api,
|
||||||
required this._session,
|
required this._session,
|
||||||
required this._refresher,
|
required this._refresher,
|
||||||
|
ApiClient? userApi,
|
||||||
this._analytics,
|
this._analytics,
|
||||||
this._uuid = const Uuid(),
|
this._uuid = const Uuid(),
|
||||||
});
|
}) : _api = api,
|
||||||
|
// 缺省回落主客户端:既有测试桩只注入一个 ApiClient。
|
||||||
|
_userApi = userApi ?? api;
|
||||||
|
|
||||||
final ApiClient _api;
|
final ApiClient _api;
|
||||||
|
final ApiClient _userApi;
|
||||||
final SessionManager _session;
|
final SessionManager _session;
|
||||||
final TokenRefresher _refresher;
|
final TokenRefresher _refresher;
|
||||||
final AnalyticsService? _analytics;
|
final AnalyticsService? _analytics;
|
||||||
@@ -132,11 +147,25 @@ class ApiAuthRepository implements AuthRepository {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<UserProfile> me() async {
|
Future<UserProfile> me() async {
|
||||||
final data = await _api.request(
|
final data = await _userApi.request(
|
||||||
'/api/v1/me',
|
'/api/v1/me',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
requiresAuth: true,
|
requiresAuth: true,
|
||||||
);
|
);
|
||||||
return UserProfile.fromJson(data! as Map<String, dynamic>);
|
return UserProfile.fromJson(data! as Map<String, dynamic>);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<UserProfile> updateMe(UpdateMeRequest request) async {
|
||||||
|
// 无乐观锁、无幂等键(契约定型:唯一合法写者 + 列级选择性 UPDATE,
|
||||||
|
// 同 body 重放天然幂等)。三态由 request.toJson 表达——「不改」的字段
|
||||||
|
// 根本不出现在 body 里,绝不发成 null。
|
||||||
|
final data = await _userApi.request(
|
||||||
|
'/api/v1/me',
|
||||||
|
method: 'PATCH',
|
||||||
|
body: request.toJson(),
|
||||||
|
requiresAuth: true,
|
||||||
|
);
|
||||||
|
return UserProfile.fromJson(data! as Map<String, dynamic>);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_interaction_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/community/community_models.dart';
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
import 'package:patbond_flutter/features/community/community_repository.dart';
|
import 'package:patbond_flutter/features/community/community_repository.dart';
|
||||||
import 'package:patbond_flutter/features/community/toggle_sync.dart';
|
import 'package:patbond_flutter/features/community/toggle_sync.dart';
|
||||||
@@ -18,7 +19,7 @@ enum LoadMorePhase { idle, loading, error }
|
|||||||
/// 评论列表只属详情页,按「页面级状态按页自建」纪律经 [repository]
|
/// 评论列表只属详情页,按「页面级状态按页自建」纪律经 [repository]
|
||||||
/// 自取,不膨胀本控制器。服务端是唯一事实来源,内存副本仅作展示缓存。
|
/// 自取,不膨胀本控制器。服务端是唯一事实来源,内存副本仅作展示缓存。
|
||||||
class CommunityController extends ChangeNotifier {
|
class CommunityController extends ChangeNotifier {
|
||||||
CommunityController({required this._repository}) {
|
CommunityController({required this._repository, this._interactionAnalytics}) {
|
||||||
_likeSync = ToggleSync(
|
_likeSync = ToggleSync(
|
||||||
read: (id) {
|
read: (id) {
|
||||||
final post = _postCache[id];
|
final post = _postCache[id];
|
||||||
@@ -35,6 +36,11 @@ class CommunityController extends ChangeNotifier {
|
|||||||
final state = target
|
final state = target
|
||||||
? await _repository.likePost(id)
|
? await _repository.likePost(id)
|
||||||
: await _repository.unlikePost(id);
|
: await _repository.unlikePost(id);
|
||||||
|
// 成功响应后上报(06 §1.4 口径;乐观翻转与失败均不报)。
|
||||||
|
final source = _likeSources[id] ?? InteractionSource.feed;
|
||||||
|
target
|
||||||
|
? _interactionAnalytics?.postLiked(source: source)
|
||||||
|
: _interactionAnalytics?.postUnliked(source: source);
|
||||||
return ToggleOutcome(active: state.liked, count: state.likeCount);
|
return ToggleOutcome(active: state.liked, count: state.likeCount);
|
||||||
},
|
},
|
||||||
generation: () => _generation,
|
generation: () => _generation,
|
||||||
@@ -62,6 +68,10 @@ class CommunityController extends ChangeNotifier {
|
|||||||
final state = target
|
final state = target
|
||||||
? await _repository.bookmarkPost(id)
|
? await _repository.bookmarkPost(id)
|
||||||
: await _repository.unbookmarkPost(id);
|
: await _repository.unbookmarkPost(id);
|
||||||
|
final source = _bookmarkSources[id] ?? InteractionSource.feed;
|
||||||
|
target
|
||||||
|
? _interactionAnalytics?.postFavorited(source: source)
|
||||||
|
: _interactionAnalytics?.postUnfavorited(source: source);
|
||||||
return ToggleOutcome(
|
return ToggleOutcome(
|
||||||
active: state.bookmarked,
|
active: state.bookmarked,
|
||||||
count: state.bookmarkCount,
|
count: state.bookmarkCount,
|
||||||
@@ -73,6 +83,12 @@ class CommunityController extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final CommunityRepository _repository;
|
final CommunityRepository _repository;
|
||||||
|
final CommunityInteractionAnalytics? _interactionAnalytics;
|
||||||
|
|
||||||
|
/// 各帖最近一次 toggle 的触点来源(Feed 卡片 / 详情页共享同一实例,
|
||||||
|
/// 成功响应上报时按发起触点归因)。
|
||||||
|
final Map<String, InteractionSource> _likeSources = {};
|
||||||
|
final Map<String, InteractionSource> _bookmarkSources = {};
|
||||||
|
|
||||||
/// 页面级状态(评论列表、我的帖子、收藏页等)按页直接经仓库取数。
|
/// 页面级状态(评论列表、我的帖子、收藏页等)按页直接经仓库取数。
|
||||||
CommunityRepository get repository => _repository;
|
CommunityRepository get repository => _repository;
|
||||||
@@ -193,10 +209,34 @@ class CommunityController extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 点赞/取消点赞(乐观翻转,终态由 [ToggleSync] 对账收敛,不外抛)。
|
/// 点赞/取消点赞(乐观翻转,终态由 [ToggleSync] 对账收敛,不外抛)。
|
||||||
void toggleLike(String postId) => _likeSync.toggle(postId);
|
/// [source] 为触点来源(埋点归因),Feed 卡片缺省 feed、详情页传
|
||||||
|
/// post_detail。
|
||||||
|
void toggleLike(
|
||||||
|
String postId, {
|
||||||
|
InteractionSource source = InteractionSource.feed,
|
||||||
|
}) {
|
||||||
|
_likeSources[postId] = source;
|
||||||
|
_likeSync.toggle(postId);
|
||||||
|
}
|
||||||
|
|
||||||
/// 收藏/取消收藏(与点赞同构)。
|
/// 收藏/取消收藏(与点赞同构)。
|
||||||
void toggleBookmark(String postId) => _bookmarkSync.toggle(postId);
|
void toggleBookmark(
|
||||||
|
String postId, {
|
||||||
|
InteractionSource source = InteractionSource.feed,
|
||||||
|
}) {
|
||||||
|
_bookmarkSources[postId] = source;
|
||||||
|
_bookmarkSync.toggle(postId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 评论创建/删除后的计数调整(详情页与 Feed 卡片同源写入,
|
||||||
|
/// 服务端 comment_count 由写侧同事务维护,本地只做展示对齐)。
|
||||||
|
void adjustCommentCount(String postId, int delta) {
|
||||||
|
final current =
|
||||||
|
_postCache[postId]?.commentCount ?? _cardOrNull(postId)?.commentCount;
|
||||||
|
if (current == null) return;
|
||||||
|
final next = current + delta;
|
||||||
|
_writeInteraction(postId, commentCount: next < 0 ? 0 : next);
|
||||||
|
}
|
||||||
|
|
||||||
/// 消费一次性 toggle 错误(SnackBar 展示后清除)。
|
/// 消费一次性 toggle 错误(SnackBar 展示后清除)。
|
||||||
void clearToggleError() => _toggleError = null;
|
void clearToggleError() => _toggleError = null;
|
||||||
@@ -215,6 +255,8 @@ class CommunityController extends ChangeNotifier {
|
|||||||
_loadMoreError = null;
|
_loadMoreError = null;
|
||||||
_toggleError = null;
|
_toggleError = null;
|
||||||
_postCache.clear();
|
_postCache.clear();
|
||||||
|
_likeSources.clear();
|
||||||
|
_bookmarkSources.clear();
|
||||||
_likeSync.reset();
|
_likeSync.reset();
|
||||||
_bookmarkSync.reset();
|
_bookmarkSync.reset();
|
||||||
_notify();
|
_notify();
|
||||||
@@ -234,6 +276,7 @@ class CommunityController extends ChangeNotifier {
|
|||||||
int? likeCount,
|
int? likeCount,
|
||||||
bool? bookmarkedByMe,
|
bool? bookmarkedByMe,
|
||||||
int? bookmarkCount,
|
int? bookmarkCount,
|
||||||
|
int? commentCount,
|
||||||
}) {
|
}) {
|
||||||
final post = _postCache[postId];
|
final post = _postCache[postId];
|
||||||
if (post != null) {
|
if (post != null) {
|
||||||
@@ -242,6 +285,7 @@ class CommunityController extends ChangeNotifier {
|
|||||||
likeCount: likeCount,
|
likeCount: likeCount,
|
||||||
bookmarkedByMe: bookmarkedByMe,
|
bookmarkedByMe: bookmarkedByMe,
|
||||||
bookmarkCount: bookmarkCount,
|
bookmarkCount: bookmarkCount,
|
||||||
|
commentCount: commentCount,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
final index = _feed.indexWhere((card) => card.id == postId);
|
final index = _feed.indexWhere((card) => card.id == postId);
|
||||||
@@ -252,6 +296,7 @@ class CommunityController extends ChangeNotifier {
|
|||||||
likeCount: likeCount,
|
likeCount: likeCount,
|
||||||
bookmarkedByMe: bookmarkedByMe,
|
bookmarkedByMe: bookmarkedByMe,
|
||||||
bookmarkCount: bookmarkCount,
|
bookmarkCount: bookmarkCount,
|
||||||
|
commentCount: commentCount,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (post != null || index != -1) _notify();
|
if (post != null || index != -1) _notify();
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_exceptions.dart';
|
||||||
import 'package:patbond_flutter/features/community/community_models.dart';
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
|
||||||
/// 降级作者([AuthorSummary.isDegraded],资料暂不可得或已注销)的
|
/// 降级作者([AuthorSummary.isDegraded],资料暂不可得或已注销)的
|
||||||
@@ -31,3 +32,28 @@ String feedLoadErrorMessage(ApiException? error) => switch (error) {
|
|||||||
ApiRateLimitException _ => '请求过于频繁,请稍后再试',
|
ApiRateLimitException _ => '请求过于频繁,请稍后再试',
|
||||||
_ => '动态加载失败,请稍后重试',
|
_ => '动态加载失败,请稍后重试',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// 发布失败的用户话术(T3-17,三条关键语义各自可辨;服务端原始
|
||||||
|
/// message 不上屏):
|
||||||
|
///
|
||||||
|
/// - 40905 同键异 payload:提交标识已被页面重置,再点一次即可;
|
||||||
|
/// - 42203 asset 未 ready:等图片传完再发;
|
||||||
|
/// - 网络失败:可重试(同键重放,服务端不会重复建帖)。
|
||||||
|
String postPublishErrorMessage(ApiException? error) => switch (error) {
|
||||||
|
IdempotencyMismatchException _ => '提交内容与上次重试不一致,已重置提交标识,请再点一次「发布」',
|
||||||
|
MediaNotReadyException _ => '有图片还没上传完成,请等图片就绪后再发布',
|
||||||
|
PostNotFoundException _ => '草稿已不存在(可能已在别处删除),请重新发布',
|
||||||
|
PostVersionConflictException _ => '草稿在别处被修改过,请重试发布',
|
||||||
|
ApiNetworkException _ => '网络异常,请检查网络后重试',
|
||||||
|
ApiRateLimitException _ => '请求过于频繁,请稍后再试',
|
||||||
|
ApiBusinessException(:final code) when code == ApiCodes.paramError =>
|
||||||
|
'内容不符合发布要求,请修改后重试',
|
||||||
|
_ => '发布失败,请稍后重试',
|
||||||
|
};
|
||||||
|
|
||||||
|
/// 草稿保存失败的用户话术(发布页 SnackBar)。
|
||||||
|
String draftSaveErrorMessage(ApiException? error) => switch (error) {
|
||||||
|
ApiNetworkException _ => '网络异常,草稿未保存,请重试',
|
||||||
|
ApiRateLimitException _ => '请求过于频繁,请稍后再试',
|
||||||
|
_ => '草稿保存失败,请重试',
|
||||||
|
};
|
||||||
|
|||||||
@@ -0,0 +1,137 @@
|
|||||||
|
import 'package:patbond_flutter/analytics/page_view_tracker.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
|
||||||
|
/// 互动域埋点强类型封装(06 号规划 §1.4 字典 v3 互动八事件;后端白名单
|
||||||
|
/// 已随 api dev@8089c06 就绪,22 号报告 §1)。沿 pet/feed 域惯例:枚举
|
||||||
|
/// 编译期锁死,业务代码禁止手拼事件名与属性;只记行为不记内容
|
||||||
|
/// (隐私红线:postId/commentId 等内容 ID 一律不进 props)。
|
||||||
|
///
|
||||||
|
/// 点赞/收藏/关注**不埋失败**(06 §1.4 取舍:幂等写入单点交互,失败率
|
||||||
|
/// 靠服务端错误率观测);`comment_create_started` 被字典锁死 unknown,
|
||||||
|
/// 不得上报(22 号 §1 末段)。
|
||||||
|
|
||||||
|
/// 互动触点来源(06 §1.4 source 枚举)。M3 接 feed / post_detail;
|
||||||
|
/// user_profile / follow_list 随后续页面启用。
|
||||||
|
enum InteractionSource {
|
||||||
|
feed('feed'),
|
||||||
|
postDetail('post_detail'),
|
||||||
|
userProfile('user_profile'),
|
||||||
|
followList('follow_list');
|
||||||
|
|
||||||
|
const InteractionSource(this.value);
|
||||||
|
|
||||||
|
final String value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 评论创建失败原因(06 §1.4 失败枚举基底)。网络归并口径同 pet/feed 域:
|
||||||
|
/// 断网/超时/5xx 均并入 network_error,server_error 保留兜底。
|
||||||
|
enum CommentCreateFailureReason {
|
||||||
|
validationError('validation_error'),
|
||||||
|
notFound('not_found'),
|
||||||
|
rateLimited('rate_limited'),
|
||||||
|
networkError('network_error'),
|
||||||
|
serverError('server_error');
|
||||||
|
|
||||||
|
const CommentCreateFailureReason(this.value);
|
||||||
|
|
||||||
|
final String value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 类型化异常 → 评论失败原因;会话失效返回 null(应用即将回登录页,
|
||||||
|
/// 不作为评论失败上报,feed 域同款口径)。
|
||||||
|
CommentCreateFailureReason? commentCreateFailureReasonOf(ApiException error) =>
|
||||||
|
switch (error) {
|
||||||
|
ApiNetworkException _ => CommentCreateFailureReason.networkError,
|
||||||
|
ApiRateLimitException _ => CommentCreateFailureReason.rateLimited,
|
||||||
|
SessionExpiredException _ => null,
|
||||||
|
ApiBusinessException(:final code) => switch (code) {
|
||||||
|
ApiCodes.paramError => CommentCreateFailureReason.validationError,
|
||||||
|
ApiCodes.postNotFound ||
|
||||||
|
ApiCodes.commentNotFound ||
|
||||||
|
ApiCodes.communityUserNotFound => CommentCreateFailureReason.notFound,
|
||||||
|
_ => CommentCreateFailureReason.serverError,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// 正文规模分桶(06 §1.3 隐私红线 1:不报精确字数)。
|
||||||
|
/// empty / short(≤50) / medium(51–500) / long(>500)。
|
||||||
|
String textLengthBucketOf(int length) {
|
||||||
|
if (length <= 0) return 'empty';
|
||||||
|
if (length <= 50) return 'short';
|
||||||
|
if (length <= 500) return 'medium';
|
||||||
|
return 'long';
|
||||||
|
}
|
||||||
|
|
||||||
|
class CommunityInteractionAnalytics {
|
||||||
|
CommunityInteractionAnalytics(this._track);
|
||||||
|
|
||||||
|
/// 生产传 `AnalyticsService.trackEvent`,测试传录制桩。
|
||||||
|
final TrackEventFn _track;
|
||||||
|
|
||||||
|
/// 点赞成功响应后(乐观翻转本身不报;单飞合并链每个实际抵达服务端
|
||||||
|
/// 并成功的状态变更各报一条,与「成功响应后」的字典口径一致)。
|
||||||
|
void postLiked({required InteractionSource source}) {
|
||||||
|
_track('post_liked', {'source': source.value});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 取消点赞成功响应后。
|
||||||
|
void postUnliked({required InteractionSource source}) {
|
||||||
|
_track('post_unliked', {'source': source.value});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 收藏成功响应后。
|
||||||
|
void postFavorited({required InteractionSource source}) {
|
||||||
|
_track('post_favorited', {'source': source.value});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 取消收藏成功响应后。
|
||||||
|
void postUnfavorited({required InteractionSource source}) {
|
||||||
|
_track('post_unfavorited', {'source': source.value});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 评论提交成功响应后。
|
||||||
|
///
|
||||||
|
/// [durationMs]:本次输入会话(首个字符输入)→ 成功响应的耗时
|
||||||
|
/// (评论不设 started 事件,时长随成功事件带出);
|
||||||
|
/// [textLength] 经 [textLengthBucketOf] 分桶后上报,精确字数不出端。
|
||||||
|
void commentCreateSucceeded({
|
||||||
|
required int durationMs,
|
||||||
|
required bool isReply,
|
||||||
|
required int textLength,
|
||||||
|
}) {
|
||||||
|
_track('comment_create_succeeded', {
|
||||||
|
'durationMs': durationMs,
|
||||||
|
'isReply': isReply,
|
||||||
|
'textLengthBucket': textLengthBucketOf(textLength),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 评论提交失败。
|
||||||
|
///
|
||||||
|
/// [errorCode] 为业务错误码(网络错误时缺席);[httpStatus] 由五位
|
||||||
|
/// 业务码推导(`code ~/ 100`,pet 域同款口径);[attemptSeq] 为本次
|
||||||
|
/// 输入会话内第几次提交尝试(从 1 起,成功或清空输入后重置)。
|
||||||
|
void commentCreateFailed({
|
||||||
|
required CommentCreateFailureReason reason,
|
||||||
|
required int attemptSeq,
|
||||||
|
int? errorCode,
|
||||||
|
}) {
|
||||||
|
_track('comment_create_failed', {
|
||||||
|
'failureReason': reason.value,
|
||||||
|
'attemptSeq': attemptSeq,
|
||||||
|
'errorCode': ?errorCode,
|
||||||
|
if (errorCode != null && errorCode >= 10000)
|
||||||
|
'httpStatus': errorCode ~/ 100,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 关注成功响应后。
|
||||||
|
void userFollowed({required InteractionSource source}) {
|
||||||
|
_track('user_followed', {'source': source.value});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 取关成功响应后。
|
||||||
|
void userUnfollowed({required InteractionSource source}) {
|
||||||
|
_track('user_unfollowed', {'source': source.value});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -60,9 +60,14 @@ enum MediaKind {
|
|||||||
_enumFromJson(values, value, 'kind');
|
_enumFromJson(values, value, 'kind');
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 上传用途白名单(M3 定型仅 post_image,决定 objectKey 前缀)。
|
/// 上传用途白名单(契约 v1.4.0 三值;决定 objectKey 前缀)。
|
||||||
|
///
|
||||||
|
/// **用途即引用侧的类型检查**:引用时服务端校验 purpose 相符,故帖图不能
|
||||||
|
/// 当头像、两种头像也互不通用(不符者 404/40405)。
|
||||||
enum MediaPurpose {
|
enum MediaPurpose {
|
||||||
postImage('post_image');
|
postImage('post_image'),
|
||||||
|
userAvatar('user_avatar'),
|
||||||
|
petAvatar('pet_avatar');
|
||||||
|
|
||||||
const MediaPurpose(this.wire);
|
const MediaPurpose(this.wire);
|
||||||
|
|
||||||
@@ -628,3 +633,25 @@ class FollowStats {
|
|||||||
final int followingCount;
|
final int followingCount;
|
||||||
final bool followedByMe;
|
final bool followedByMe;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 我的社区数字(`GET /api/v1/me/community-stats`,契约 v1.4.0 新增)。
|
||||||
|
///
|
||||||
|
/// 两数同一集合:本人的、已发布的、未软删的帖(草稿 / 软删 / hidden /
|
||||||
|
/// archived 均不计;自己赞自己**计入**,与帖子详情的 likeCount 同口径)。
|
||||||
|
/// 空数据为 `0` 而非 null,且该端点**永不 404**——任何已认证用户都有 stats。
|
||||||
|
class CommunityStats {
|
||||||
|
const CommunityStats({
|
||||||
|
required this.receivedLikeCount,
|
||||||
|
required this.publishedPostCount,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory CommunityStats.fromJson(Map<String, dynamic> json) {
|
||||||
|
return CommunityStats(
|
||||||
|
receivedLikeCount: json['receivedLikeCount'] as int,
|
||||||
|
publishedPostCount: json['publishedPostCount'] as int,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final int receivedLikeCount;
|
||||||
|
final int publishedPostCount;
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ abstract class CommunityRepository {
|
|||||||
Future<MediaAsset> completeMediaUpload(String assetId);
|
Future<MediaAsset> completeMediaUpload(String assetId);
|
||||||
|
|
||||||
// ---- 帖子 CRUD / 发布 ----
|
// ---- 帖子 CRUD / 发布 ----
|
||||||
Future<Post> createPost(CreatePostRequest request);
|
/// [idempotencyKey]:调用方持键(T3-17 发布页「同键重放」——网络失败重试
|
||||||
|
/// 沿用同键命中服务端首帖,不重复建帖;缺省则本层每次调用换新键)。
|
||||||
|
Future<Post> createPost(CreatePostRequest request, {String? idempotencyKey});
|
||||||
Future<Post> getPost(String postId);
|
Future<Post> getPost(String postId);
|
||||||
Future<Post> updatePost(String postId, UpdatePostRequest request);
|
Future<Post> updatePost(String postId, UpdatePostRequest request);
|
||||||
Future<void> deletePost(String postId);
|
Future<void> deletePost(String postId);
|
||||||
@@ -53,6 +55,9 @@ abstract class CommunityRepository {
|
|||||||
Future<FollowState> followUser(String userId);
|
Future<FollowState> followUser(String userId);
|
||||||
Future<FollowState> unfollowUser(String userId);
|
Future<FollowState> unfollowUser(String userId);
|
||||||
Future<FollowStats> getFollowStats(String userId);
|
Future<FollowStats> getFollowStats(String userId);
|
||||||
|
|
||||||
|
// ---- 我的社区数字(主体恒为调用者,路径上无 userId)----
|
||||||
|
Future<CommunityStats> getMyCommunityStats();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 基于 [ApiClient] 的实现。全部端点走 Bearer 鉴权(复用既有 token
|
/// 基于 [ApiClient] 的实现。全部端点走 Bearer 鉴权(复用既有 token
|
||||||
@@ -85,6 +90,7 @@ class ApiCommunityRepository implements CommunityRepository {
|
|||||||
Object? body,
|
Object? body,
|
||||||
Map<String, Object?>? query,
|
Map<String, Object?>? query,
|
||||||
bool idempotent = false,
|
bool idempotent = false,
|
||||||
|
String? idempotencyKey,
|
||||||
bool media = false,
|
bool media = false,
|
||||||
}) async {
|
}) async {
|
||||||
try {
|
try {
|
||||||
@@ -93,7 +99,9 @@ class ApiCommunityRepository implements CommunityRepository {
|
|||||||
method: method,
|
method: method,
|
||||||
body: body,
|
body: body,
|
||||||
query: query,
|
query: query,
|
||||||
headers: idempotent ? {'Idempotency-Key': _uuid.v4()} : null,
|
headers: idempotent
|
||||||
|
? {'Idempotency-Key': idempotencyKey ?? _uuid.v4()}
|
||||||
|
: null,
|
||||||
requiresAuth: true,
|
requiresAuth: true,
|
||||||
);
|
);
|
||||||
} on ApiBusinessException catch (error) {
|
} on ApiBusinessException catch (error) {
|
||||||
@@ -132,12 +140,16 @@ class ApiCommunityRepository implements CommunityRepository {
|
|||||||
// ---- posts ----
|
// ---- posts ----
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Post> createPost(CreatePostRequest request) async {
|
Future<Post> createPost(
|
||||||
|
CreatePostRequest request, {
|
||||||
|
String? idempotencyKey,
|
||||||
|
}) async {
|
||||||
final data = await _request(
|
final data = await _request(
|
||||||
'/api/v1/posts',
|
'/api/v1/posts',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: request.toJson(),
|
body: request.toJson(),
|
||||||
idempotent: true,
|
idempotent: true,
|
||||||
|
idempotencyKey: idempotencyKey,
|
||||||
);
|
);
|
||||||
return Post.fromJson(_asMap(data));
|
return Post.fromJson(_asMap(data));
|
||||||
}
|
}
|
||||||
@@ -292,4 +304,12 @@ class ApiCommunityRepository implements CommunityRepository {
|
|||||||
final data = await _request('/api/v1/users/$userId/follow-stats');
|
final data = await _request('/api/v1/users/$userId/follow-stats');
|
||||||
return FollowStats.fromJson(_asMap(data));
|
return FollowStats.fromJson(_asMap(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- 我的社区数字 ----
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<CommunityStats> getMyCommunityStats() async {
|
||||||
|
final data = await _request('/api/v1/me/community-stats');
|
||||||
|
return CommunityStats.fromJson(_asMap(data));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import 'package:patbond_flutter/features/community/community_repository.dart';
|
|||||||
import 'package:patbond_flutter/features/community/media_compression.dart';
|
import 'package:patbond_flutter/features/community/media_compression.dart';
|
||||||
import 'package:patbond_flutter/features/community/media_direct_upload.dart';
|
import 'package:patbond_flutter/features/community/media_direct_upload.dart';
|
||||||
import 'package:patbond_flutter/features/community/media_picking.dart';
|
import 'package:patbond_flutter/features/community/media_picking.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/post_analytics.dart';
|
||||||
|
|
||||||
/// 单张图的上传阶段(05 号规范 §3.3 四视觉态的底层状态模型)。
|
/// 单张图的上传阶段(05 号规范 §3.3 四视觉态的底层状态模型)。
|
||||||
///
|
///
|
||||||
@@ -76,6 +77,12 @@ class _UploadTask {
|
|||||||
bool retryable = false;
|
bool retryable = false;
|
||||||
bool cancelled = false;
|
bool cancelled = false;
|
||||||
|
|
||||||
|
/// 本图第几次上传尝试(媒体三段埋点 attemptSeq,从 1 起;retry 递增)。
|
||||||
|
int attemptSeq = 1;
|
||||||
|
|
||||||
|
/// 本次尝试的 started 时刻(succeeded 的 durationMs 口径)。
|
||||||
|
DateTime? attemptStartedAt;
|
||||||
|
|
||||||
/// 压缩产物缓存(重试跳过重压缩)。
|
/// 压缩产物缓存(重试跳过重压缩)。
|
||||||
CompressedMediaImage? compressed;
|
CompressedMediaImage? compressed;
|
||||||
|
|
||||||
@@ -115,12 +122,16 @@ class _UploadTask {
|
|||||||
/// 对外可见的 [MediaUploadItem.assetId] 与 ready 态严格绑定(断言),
|
/// 对外可见的 [MediaUploadItem.assetId] 与 ready 态严格绑定(断言),
|
||||||
/// [buildAttachRequests] 仅在全员 ready 时可用。
|
/// [buildAttachRequests] 仅在全员 ready 时可用。
|
||||||
/// - 预签名凭据只存内存、用完即弃,不持久化(既有纪律)。
|
/// - 预签名凭据只存内存、用完即弃,不持久化(既有纪律)。
|
||||||
|
/// - **媒体三段埋点**(T3-17):每次尝试恰一条 started,收敛为
|
||||||
|
/// succeeded / failed 各一条;`sizeBucket` 统一取原图字节数。
|
||||||
class MediaUploader extends ChangeNotifier {
|
class MediaUploader extends ChangeNotifier {
|
||||||
MediaUploader({
|
MediaUploader({
|
||||||
required this._repository,
|
required this._repository,
|
||||||
MediaImagePicker? picker,
|
MediaImagePicker? picker,
|
||||||
MediaImageCompressor? compressor,
|
MediaImageCompressor? compressor,
|
||||||
MediaDirectUploadClient? directUpload,
|
MediaDirectUploadClient? directUpload,
|
||||||
|
this._analytics,
|
||||||
|
this.purpose = MediaPurpose.postImage,
|
||||||
this.maxImages = 9,
|
this.maxImages = 9,
|
||||||
this.maxConcurrentUploads = 2,
|
this.maxConcurrentUploads = 2,
|
||||||
this.maxByteSize = 10 * 1024 * 1024,
|
this.maxByteSize = 10 * 1024 * 1024,
|
||||||
@@ -135,9 +146,19 @@ class MediaUploader extends ChangeNotifier {
|
|||||||
final MediaImagePicker _picker;
|
final MediaImagePicker _picker;
|
||||||
final MediaImageCompressor _compressor;
|
final MediaImageCompressor _compressor;
|
||||||
final MediaDirectUploadClient _directUpload;
|
final MediaDirectUploadClient _directUpload;
|
||||||
|
|
||||||
|
/// 媒体上传三段埋点(T3-17 接入;未注入即不上报)。
|
||||||
|
final PostAnalytics? _analytics;
|
||||||
|
|
||||||
final DateTime Function() _now;
|
final DateTime Function() _now;
|
||||||
|
|
||||||
/// 九宫格上限(05 号规范 §3.2)。
|
/// 上传用途(决定服务端 objectKey 前缀,也是引用侧的类型检查依据):
|
||||||
|
/// 发布页 `post_image`、资料页头像 `user_avatar`、宠物头像 `pet_avatar`。
|
||||||
|
/// 用途不符的 asset 在引用时被答 404/40405,所以这个值必须由调用方
|
||||||
|
/// 按场景显式给对(M3.5-08/09 起本类不再只服务发布页)。
|
||||||
|
final MediaPurpose purpose;
|
||||||
|
|
||||||
|
/// 九宫格上限(05 号规范 §3.2);头像场景传 1。
|
||||||
final int maxImages;
|
final int maxImages;
|
||||||
|
|
||||||
final int maxConcurrentUploads;
|
final int maxConcurrentUploads;
|
||||||
@@ -244,24 +265,28 @@ class MediaUploader extends ChangeNotifier {
|
|||||||
task.errorMessage = null;
|
task.errorMessage = null;
|
||||||
task.retryable = false;
|
task.retryable = false;
|
||||||
task.progress = 0;
|
task.progress = 0;
|
||||||
|
task.attemptSeq += 1;
|
||||||
task.phase = MediaItemPhase.queued;
|
task.phase = MediaItemPhase.queued;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
unawaited(_run(task));
|
unawaited(_run(task));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 移除一张图(任意态可移除);在途请求结果一律作废,未 confirm 的
|
/// 移除一张图(任意态可移除);在途请求结果一律作废,未 confirm 的
|
||||||
/// 服务端 asset 弃引用(服务端超时清理兜底)。
|
/// 服务端 asset 弃引用(服务端超时清理兜底)。在途任务被移除按
|
||||||
|
/// `cancelled` 上报一条上传失败(06 §1.4「用户取消」口径)。
|
||||||
void remove(int localId) {
|
void remove(int localId) {
|
||||||
final task = _taskOrNull(localId);
|
final task = _taskOrNull(localId);
|
||||||
if (task == null) return;
|
if (task == null) return;
|
||||||
|
_reportCancelled(task);
|
||||||
task.cancelled = true;
|
task.cancelled = true;
|
||||||
_tasks.remove(task);
|
_tasks.remove(task);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 清空全部(发布成功/离开页面时调用)。
|
/// 清空全部(发布成功/离开页面时调用);在途任务同 [remove] 记 cancelled。
|
||||||
void reset() {
|
void reset() {
|
||||||
for (final task in _tasks) {
|
for (final task in _tasks) {
|
||||||
|
_reportCancelled(task);
|
||||||
task.cancelled = true;
|
task.cancelled = true;
|
||||||
}
|
}
|
||||||
_tasks.clear();
|
_tasks.clear();
|
||||||
@@ -282,6 +307,12 @@ class MediaUploader extends ChangeNotifier {
|
|||||||
await _acquireSlot();
|
await _acquireSlot();
|
||||||
try {
|
try {
|
||||||
if (task.cancelled) return;
|
if (task.cancelled) return;
|
||||||
|
// 一次尝试恰一条 started(含压缩段:压缩失败也在漏斗内可见)。
|
||||||
|
task.attemptStartedAt = _now();
|
||||||
|
_analytics?.mediaUploadStarted(
|
||||||
|
mediaType: MediaType.image,
|
||||||
|
byteSize: task.source.bytes.length,
|
||||||
|
);
|
||||||
final compressed = await _compress(task);
|
final compressed = await _compress(task);
|
||||||
if (compressed == null || task.cancelled) return;
|
if (compressed == null || task.cancelled) return;
|
||||||
await _uploadAndConfirm(task, compressed);
|
await _uploadAndConfirm(task, compressed);
|
||||||
@@ -308,10 +339,20 @@ class MediaUploader extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
_fail(task, message: '图片处理失败', retryable: true);
|
_fail(
|
||||||
|
task,
|
||||||
|
message: '图片处理失败',
|
||||||
|
retryable: true,
|
||||||
|
reason: MediaUploadFailureReason.unsupportedFormat,
|
||||||
|
);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
_fail(task, message: '图片过大,压缩后仍超过 10 MB', retryable: false);
|
_fail(
|
||||||
|
task,
|
||||||
|
message: '图片过大,压缩后仍超过 10 MB',
|
||||||
|
retryable: false,
|
||||||
|
reason: MediaUploadFailureReason.mediaTooLarge,
|
||||||
|
);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,7 +376,12 @@ class MediaUploader extends ChangeNotifier {
|
|||||||
while (true) {
|
while (true) {
|
||||||
if (_credentialsExpired(credentials)) {
|
if (_credentialsExpired(credentials)) {
|
||||||
if (renewed) {
|
if (renewed) {
|
||||||
_fail(task, message: '上传凭据已过期', retryable: true);
|
_fail(
|
||||||
|
task,
|
||||||
|
message: '上传凭据已过期',
|
||||||
|
retryable: true,
|
||||||
|
reason: MediaUploadFailureReason.serverError,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
renewed = true;
|
renewed = true;
|
||||||
@@ -380,6 +426,9 @@ class MediaUploader extends ChangeNotifier {
|
|||||||
task,
|
task,
|
||||||
message: error.statusCode == null ? '网络中断,上传失败' : '上传被存储服务拒绝',
|
message: error.statusCode == null ? '网络中断,上传失败' : '上传被存储服务拒绝',
|
||||||
retryable: true,
|
retryable: true,
|
||||||
|
reason: error.statusCode == null
|
||||||
|
? MediaUploadFailureReason.networkError
|
||||||
|
: MediaUploadFailureReason.serverError,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -398,12 +447,25 @@ class MediaUploader extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
if (task.cancelled) return;
|
if (task.cancelled) return;
|
||||||
if (asset.status != MediaAssetStatus.ready) {
|
if (asset.status != MediaAssetStatus.ready) {
|
||||||
_fail(task, message: '上传确认未通过', retryable: true);
|
_fail(
|
||||||
|
task,
|
||||||
|
message: '上传确认未通过',
|
||||||
|
retryable: true,
|
||||||
|
reason: MediaUploadFailureReason.serverError,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
task.readyAssetId = asset.id;
|
task.readyAssetId = asset.id;
|
||||||
task.progress = 1;
|
task.progress = 1;
|
||||||
_transition(task, MediaItemPhase.ready);
|
_transition(task, MediaItemPhase.ready);
|
||||||
|
final startedAt = task.attemptStartedAt;
|
||||||
|
_analytics?.mediaUploadSucceeded(
|
||||||
|
mediaType: MediaType.image,
|
||||||
|
byteSize: task.source.bytes.length,
|
||||||
|
durationMs: startedAt == null
|
||||||
|
? 0
|
||||||
|
: _now().difference(startedAt).inMilliseconds,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<MediaUploadCredentials> _createUpload(
|
Future<MediaUploadCredentials> _createUpload(
|
||||||
@@ -412,7 +474,7 @@ class MediaUploader extends ChangeNotifier {
|
|||||||
return _repository.createMediaUpload(
|
return _repository.createMediaUpload(
|
||||||
CreateMediaUploadRequest(
|
CreateMediaUploadRequest(
|
||||||
kind: MediaKind.image,
|
kind: MediaKind.image,
|
||||||
purpose: MediaPurpose.postImage,
|
purpose: purpose,
|
||||||
mimeType: compressed.mimeType,
|
mimeType: compressed.mimeType,
|
||||||
byteSize: compressed.byteSize,
|
byteSize: compressed.byteSize,
|
||||||
),
|
),
|
||||||
@@ -424,12 +486,22 @@ class MediaUploader extends ChangeNotifier {
|
|||||||
|
|
||||||
void _failFromApi(_UploadTask task, Exception error) {
|
void _failFromApi(_UploadTask task, Exception error) {
|
||||||
// 参数被服务端拒绝(40000:mime/byteSize 白名单外)重试无意义,终态。
|
// 参数被服务端拒绝(40000:mime/byteSize 白名单外)重试无意义,终态。
|
||||||
final retryable =
|
final isParamError =
|
||||||
error is! ApiBusinessException || error.code != ApiCodes.paramError;
|
error is ApiBusinessException && error.code == ApiCodes.paramError;
|
||||||
_fail(
|
_fail(
|
||||||
task,
|
task,
|
||||||
message: error is ApiBusinessException ? error.message : '网络异常,请重试',
|
message: error is ApiBusinessException ? error.message : '网络异常,请重试',
|
||||||
retryable: retryable,
|
retryable: !isParamError,
|
||||||
|
// 客户端已本地保证 ≤10 MiB,故 40000 归因为格式白名单外;
|
||||||
|
// 会话失效不上报(reason 传 null),其余业务/限流并入 server_error。
|
||||||
|
reason: switch (error) {
|
||||||
|
ApiBusinessException _ when isParamError =>
|
||||||
|
MediaUploadFailureReason.unsupportedFormat,
|
||||||
|
SessionExpiredException _ => null,
|
||||||
|
ApiNetworkException _ => MediaUploadFailureReason.networkError,
|
||||||
|
_ => MediaUploadFailureReason.serverError,
|
||||||
|
},
|
||||||
|
errorCode: error is ApiBusinessException ? error.code : null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -437,14 +509,36 @@ class MediaUploader extends ChangeNotifier {
|
|||||||
_UploadTask task, {
|
_UploadTask task, {
|
||||||
required String message,
|
required String message,
|
||||||
required bool retryable,
|
required bool retryable,
|
||||||
|
required MediaUploadFailureReason? reason,
|
||||||
|
int? errorCode,
|
||||||
}) {
|
}) {
|
||||||
if (task.cancelled) return;
|
if (task.cancelled) return;
|
||||||
task.phase = MediaItemPhase.failed;
|
task.phase = MediaItemPhase.failed;
|
||||||
task.errorMessage = message;
|
task.errorMessage = message;
|
||||||
task.retryable = retryable;
|
task.retryable = retryable;
|
||||||
|
if (reason != null) {
|
||||||
|
_analytics?.mediaUploadFailed(
|
||||||
|
mediaType: MediaType.image,
|
||||||
|
byteSize: task.source.bytes.length,
|
||||||
|
reason: reason,
|
||||||
|
attemptSeq: task.attemptSeq,
|
||||||
|
errorCode: errorCode,
|
||||||
|
);
|
||||||
|
}
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 在途任务被删格/清空作废 → `cancelled`(已 ready / 已 failed 不报)。
|
||||||
|
void _reportCancelled(_UploadTask task) {
|
||||||
|
if (task.cancelled || !task.snapshot().isBusy) return;
|
||||||
|
_analytics?.mediaUploadFailed(
|
||||||
|
mediaType: MediaType.image,
|
||||||
|
byteSize: task.source.bytes.length,
|
||||||
|
reason: MediaUploadFailureReason.cancelled,
|
||||||
|
attemptSeq: task.attemptSeq,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
void _transition(_UploadTask task, MediaItemPhase phase) {
|
void _transition(_UploadTask task, MediaItemPhase phase) {
|
||||||
if (task.cancelled) return;
|
if (task.cancelled) return;
|
||||||
task.phase = phase;
|
task.phase = phase;
|
||||||
@@ -469,3 +563,15 @@ class MediaUploader extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// [MediaUploader] 的构造口(发布页每次进入建一个,退出即 dispose)。
|
||||||
|
///
|
||||||
|
/// 生产缺省即 `MediaUploader(repository: ..., analytics: ...)`;注入点为
|
||||||
|
/// **测试与桌面实测专用**——Linux 桌面既无 image_picker 也无
|
||||||
|
/// flutter_image_compress 的原生实现,桌面真链路只替换选图与压缩两层,
|
||||||
|
/// 其余(createUpload / 直传 PUT / confirm)全为生产实现。
|
||||||
|
typedef MediaUploaderFactory =
|
||||||
|
MediaUploader Function(
|
||||||
|
CommunityRepository repository,
|
||||||
|
PostAnalytics? analytics,
|
||||||
|
);
|
||||||
|
|||||||
@@ -0,0 +1,239 @@
|
|||||||
|
import 'package:patbond_flutter/analytics/page_view_tracker.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_interaction_analytics.dart'
|
||||||
|
show textLengthBucketOf;
|
||||||
|
|
||||||
|
/// post 域埋点强类型封装(06 号规划 §1.4 发布漏斗五事件 + 媒体上传三段;
|
||||||
|
/// 后端白名单随 api dev@`8089c06` 就绪,22 号报告 §1 键集逐一对齐)。
|
||||||
|
/// 沿 pet/feed/互动域惯例:枚举编译期锁死,业务代码禁止手拼事件名与属性。
|
||||||
|
///
|
||||||
|
/// 隐私纪律(06 §1.3 红线):正文只出分桶不出字数(红线 1);postId /
|
||||||
|
/// assetId 等内容 ID 一律不进 props(红线 2);媒体只报 [MediaType] 与
|
||||||
|
/// [mediaSizeBucketOf] 分桶,文件名/路径/URL 禁止(红线 4)。
|
||||||
|
///
|
||||||
|
/// **不得上报**(22 号 §1 末段锁死为 unknown):`post_impression`、
|
||||||
|
/// `post_viewed`、`post_like_failed` 等——本文件不提供其封装。
|
||||||
|
|
||||||
|
/// 发帖入口(06 §1.4 `post_create_started.entryPoint`)。
|
||||||
|
/// M3 接 create_tab / feed;topic_detail / pet_detail 随后续页面启用。
|
||||||
|
enum PostEntryPoint {
|
||||||
|
createTab('create_tab'),
|
||||||
|
feed('feed'),
|
||||||
|
topicDetail('topic_detail'),
|
||||||
|
petDetail('pet_detail');
|
||||||
|
|
||||||
|
const PostEntryPoint(this.value);
|
||||||
|
|
||||||
|
final String value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 草稿保存触发方式(06 §1.4:**自动保存不埋**,防高频)。
|
||||||
|
enum DraftSaveTrigger {
|
||||||
|
/// 「存草稿」按钮显式保存。
|
||||||
|
manual('manual'),
|
||||||
|
|
||||||
|
/// 离开发布页时经「保留草稿?」确认保存。
|
||||||
|
onExit('on_exit');
|
||||||
|
|
||||||
|
const DraftSaveTrigger(this.value);
|
||||||
|
|
||||||
|
final String value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 发布失败原因(06 §1.4 枚举;`content_rejected` 待拍板未启用,
|
||||||
|
/// `not_found` 取 §1.4「失败枚举基底」的复用条——草稿已被别处删除)。
|
||||||
|
enum PostPublishFailureReason {
|
||||||
|
validationError('validation_error'),
|
||||||
|
mediaUploadIncomplete('media_upload_incomplete'),
|
||||||
|
notFound('not_found'),
|
||||||
|
rateLimited('rate_limited'),
|
||||||
|
networkError('network_error'),
|
||||||
|
serverError('server_error');
|
||||||
|
|
||||||
|
const PostPublishFailureReason(this.value);
|
||||||
|
|
||||||
|
final String value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 媒体类型(M3 仅 image;video 随视频能力启用)。
|
||||||
|
enum MediaType {
|
||||||
|
image('image'),
|
||||||
|
video('video');
|
||||||
|
|
||||||
|
const MediaType(this.value);
|
||||||
|
|
||||||
|
final String value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 单文件上传失败原因(06 §1.4 媒体漏斗枚举)。
|
||||||
|
enum MediaUploadFailureReason {
|
||||||
|
mediaTooLarge('media_too_large'),
|
||||||
|
unsupportedFormat('unsupported_format'),
|
||||||
|
networkError('network_error'),
|
||||||
|
serverError('server_error'),
|
||||||
|
|
||||||
|
/// 用户在上传途中删格 / 离开发布页作废在途任务。
|
||||||
|
cancelled('cancelled');
|
||||||
|
|
||||||
|
const MediaUploadFailureReason(this.value);
|
||||||
|
|
||||||
|
final String value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 类型化异常 → 发布失败原因;会话失效返回 null(应用即将回登录页,
|
||||||
|
/// 不作为发布失败上报,feed / 互动域同款口径)。
|
||||||
|
///
|
||||||
|
/// 映射取舍(26 号报告 §3 有对照表):42203 恰为
|
||||||
|
/// [PostPublishFailureReason.mediaUploadIncomplete];40905(同键异
|
||||||
|
/// payload)归 validation_error——提交内容与幂等键不一致属提交侧问题,
|
||||||
|
/// 非服务端故障;40902(乐观锁,自动刷新 version 重提仍失败)归
|
||||||
|
/// server_error 兜底。
|
||||||
|
PostPublishFailureReason? postPublishFailureReasonOf(ApiException error) =>
|
||||||
|
switch (error) {
|
||||||
|
ApiNetworkException _ => PostPublishFailureReason.networkError,
|
||||||
|
ApiRateLimitException _ => PostPublishFailureReason.rateLimited,
|
||||||
|
SessionExpiredException _ => null,
|
||||||
|
ApiBusinessException(:final code) => switch (code) {
|
||||||
|
ApiCodes.paramError || ApiCodes.idempotencyKeyMismatch =>
|
||||||
|
PostPublishFailureReason.validationError,
|
||||||
|
ApiCodes.mediaNotReady =>
|
||||||
|
PostPublishFailureReason.mediaUploadIncomplete,
|
||||||
|
ApiCodes.postNotFound ||
|
||||||
|
ApiCodes.mediaNotFound => PostPublishFailureReason.notFound,
|
||||||
|
_ => PostPublishFailureReason.serverError,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// 媒体大小分桶(06 §1.3 红线 4:不报精确字节数)。
|
||||||
|
/// `lt_1mb` / `mb_1_5` / `mb_5_20` / `gte_20mb`,以 MiB 为界
|
||||||
|
/// (与 MediaUploader 的 10 MiB 上限同一进制)。
|
||||||
|
String mediaSizeBucketOf(int byteSize) {
|
||||||
|
const mib = 1024 * 1024;
|
||||||
|
if (byteSize < mib) return 'lt_1mb';
|
||||||
|
if (byteSize < 5 * mib) return 'mb_1_5';
|
||||||
|
if (byteSize < 20 * mib) return 'mb_5_20';
|
||||||
|
return 'gte_20mb';
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 发布漏斗 + 媒体上传三段埋点(22 号白名单 v3 事件 22~29)。
|
||||||
|
class PostAnalytics {
|
||||||
|
PostAnalytics(this._track);
|
||||||
|
|
||||||
|
/// 生产传 `AnalyticsService.trackEvent`,测试传录制桩。
|
||||||
|
final TrackEventFn _track;
|
||||||
|
|
||||||
|
// ---- 发布漏斗 ----
|
||||||
|
|
||||||
|
/// 进入发布页并产生**首次输入**(首个字符或首次选媒体),每次进入记一次。
|
||||||
|
/// 草稿恢复不算输入(非用户动作,不上报)。
|
||||||
|
void postCreateStarted({required PostEntryPoint entryPoint}) {
|
||||||
|
_track('post_create_started', {'entryPoint': entryPoint.value});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 草稿保存**成功响应后**;仅显式保存与离开时保存(自动保存不埋)。
|
||||||
|
void postDraftSaved({
|
||||||
|
required DraftSaveTrigger trigger,
|
||||||
|
required int mediaCount,
|
||||||
|
}) {
|
||||||
|
_track('post_draft_saved', {
|
||||||
|
'trigger': trigger.value,
|
||||||
|
'mediaCount': mediaCount,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 发布成功响应后(漏斗事件,H5/H6 核心数据源)。
|
||||||
|
///
|
||||||
|
/// [durationMs]:`post_create_started` → 发布成功;[textLength] 经
|
||||||
|
/// [textLengthBucketOf] 分桶后上报,精确字数不出端;[fromDraft] 指
|
||||||
|
/// 「本次发布基于先前保存/恢复的草稿」(发布内部的建草稿→迁移两步
|
||||||
|
/// 不算,见 26 号报告 §3)。
|
||||||
|
void postPublishSucceeded({
|
||||||
|
required int durationMs,
|
||||||
|
required int mediaCount,
|
||||||
|
required int topicCount,
|
||||||
|
required int textLength,
|
||||||
|
required bool fromDraft,
|
||||||
|
}) {
|
||||||
|
_track('post_publish_succeeded', {
|
||||||
|
'durationMs': durationMs,
|
||||||
|
'mediaCount': mediaCount,
|
||||||
|
'topicCount': topicCount,
|
||||||
|
'textLengthBucket': textLengthBucketOf(textLength),
|
||||||
|
'fromDraft': fromDraft,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 发布失败 / 超时 / 本地校验拦截。
|
||||||
|
///
|
||||||
|
/// [errorCode] 为业务错误码(网络错误时缺席);[httpStatus] 由五位业务码
|
||||||
|
/// 推导(`code ~/ 100`,pet 域同款);[attemptSeq] 为本次发布会话内第几次
|
||||||
|
/// 尝试(从 1 起,发布成功或离开发布页后重置)。
|
||||||
|
void postPublishFailed({
|
||||||
|
required PostPublishFailureReason reason,
|
||||||
|
required int attemptSeq,
|
||||||
|
int? errorCode,
|
||||||
|
}) {
|
||||||
|
_track('post_publish_failed', {
|
||||||
|
'failureReason': reason.value,
|
||||||
|
'attemptSeq': attemptSeq,
|
||||||
|
'errorCode': ?errorCode,
|
||||||
|
if (errorCode != null && errorCode >= 10000)
|
||||||
|
'httpStatus': errorCode ~/ 100,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 删帖成功响应后(单事件风格,无专有属性;失败靠服务端错误率观测)。
|
||||||
|
/// M3 触点:发布页「不保留草稿」删除服务端草稿。
|
||||||
|
void postDeleted() {
|
||||||
|
_track('post_deleted', const {});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- 媒体上传三段(逐文件)----
|
||||||
|
|
||||||
|
/// 单个文件开始上传(一次尝试恰一条;重试各记一条)。
|
||||||
|
///
|
||||||
|
/// [byteSize] 取**选图原文件**字节数,保证同一次尝试三段事件的
|
||||||
|
/// `sizeBucket` 一致(压缩产物大小不另开一套桶)。
|
||||||
|
void mediaUploadStarted({
|
||||||
|
required MediaType mediaType,
|
||||||
|
required int byteSize,
|
||||||
|
}) {
|
||||||
|
_track('post_media_upload_started', {
|
||||||
|
'mediaType': mediaType.value,
|
||||||
|
'sizeBucket': mediaSizeBucketOf(byteSize),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 单文件上传成功(confirm 返回 ready 后)。
|
||||||
|
/// [durationMs]:本次尝试 started → ready。
|
||||||
|
void mediaUploadSucceeded({
|
||||||
|
required MediaType mediaType,
|
||||||
|
required int byteSize,
|
||||||
|
required int durationMs,
|
||||||
|
}) {
|
||||||
|
_track('post_media_upload_succeeded', {
|
||||||
|
'mediaType': mediaType.value,
|
||||||
|
'sizeBucket': mediaSizeBucketOf(byteSize),
|
||||||
|
'durationMs': durationMs,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 单文件失败 / 超时 / 用户取消。
|
||||||
|
void mediaUploadFailed({
|
||||||
|
required MediaType mediaType,
|
||||||
|
required int byteSize,
|
||||||
|
required MediaUploadFailureReason reason,
|
||||||
|
required int attemptSeq,
|
||||||
|
int? errorCode,
|
||||||
|
}) {
|
||||||
|
_track('post_media_upload_failed', {
|
||||||
|
'mediaType': mediaType.value,
|
||||||
|
'sizeBucket': mediaSizeBucketOf(byteSize),
|
||||||
|
'failureReason': reason.value,
|
||||||
|
'attemptSeq': attemptSeq,
|
||||||
|
'errorCode': ?errorCode,
|
||||||
|
if (errorCode != null && errorCode >= 10000)
|
||||||
|
'httpStatus': errorCode ~/ 100,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,704 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/post_media_grid.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_display.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_exceptions.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_repository.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/post_analytics.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
/// 发布页(P3,05 号规范 §2.3;T3-17 真实数据整页落地)。
|
||||||
|
///
|
||||||
|
/// 结构:媒体选择区([PostMediaEditGrid] 组装 [MediaUploader])→ 正文
|
||||||
|
/// → 类目(general / help)→ 位置占位;AppBar 三件套「取消 / 发布动态 /
|
||||||
|
/// 发布」,另置「存草稿」。
|
||||||
|
///
|
||||||
|
/// 两条提交路径(15 号后端语义 §2.4):
|
||||||
|
///
|
||||||
|
/// - **直接发布** = `createPost(status: draft)` 建草稿 → `PATCH
|
||||||
|
/// {status: published}` 迁移发布。两步而非「一步建 published」是为了让
|
||||||
|
/// 「发布失败但草稿已保存」成为事实而不是话术:迁移这一步失败时草稿
|
||||||
|
/// 已在服务端,用户内容不会丢。
|
||||||
|
/// - **存草稿退出** = 同一个 `createPost(status: draft)`(或对已有草稿
|
||||||
|
/// `PATCH`),随后离页。
|
||||||
|
///
|
||||||
|
/// 幂等纪律:建草稿的 `Idempotency-Key` 由**本页持有**——网络失败重试
|
||||||
|
/// 沿用同键(服务端命中首帖,不重复建帖);表单一经改动即换新键
|
||||||
|
/// (避免「同键异 payload」的 40905 常态化)。
|
||||||
|
///
|
||||||
|
/// 草稿管理最小实现:进页拉取「我的草稿」最新一条并恢复(05 §2.3 的
|
||||||
|
/// 「已恢复上次草稿」提示条),完整草稿列表页留待(26 号报告 §7)。
|
||||||
|
class PostComposePage extends StatefulWidget {
|
||||||
|
const PostComposePage({
|
||||||
|
required this.controller,
|
||||||
|
super.key,
|
||||||
|
this.entryPoint = PostEntryPoint.createTab,
|
||||||
|
this.analytics,
|
||||||
|
this.uploaderFactory,
|
||||||
|
this.now,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Tab 级单例(app.dart 装配):发布成功后由调用方触发 Feed 刷新。
|
||||||
|
final CommunityController controller;
|
||||||
|
|
||||||
|
/// 入口归因(`post_create_started.entryPoint`)。
|
||||||
|
final PostEntryPoint entryPoint;
|
||||||
|
|
||||||
|
/// post 域埋点(发布漏斗五事件 + 媒体三段,媒体段经 [MediaUploader])。
|
||||||
|
final PostAnalytics? analytics;
|
||||||
|
|
||||||
|
/// [MediaUploader] 构造口(测试 / 桌面实测替换选图与压缩层)。
|
||||||
|
final MediaUploaderFactory? uploaderFactory;
|
||||||
|
|
||||||
|
/// 时钟注入口(durationMs 断言用;缺省取当前时间)。
|
||||||
|
final DateTime Function()? now;
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<PostComposePage> createState() => _PostComposePageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _PostComposePageState extends State<PostComposePage> {
|
||||||
|
static const _maxContentLength = 1000;
|
||||||
|
|
||||||
|
final _contentController = TextEditingController();
|
||||||
|
final _uuid = const Uuid();
|
||||||
|
|
||||||
|
late final MediaUploader _uploader;
|
||||||
|
|
||||||
|
PostCategory _category = PostCategory.general;
|
||||||
|
|
||||||
|
/// 服务端草稿标识与乐观锁版本(建草稿成功或恢复草稿后非空)。
|
||||||
|
String? _draftPostId;
|
||||||
|
int? _draftVersion;
|
||||||
|
|
||||||
|
/// 恢复来的草稿既有媒体(uploader 只持本地选图,服务端媒体只读呈现)。
|
||||||
|
List<PostMediaItem> _draftMedia = const [];
|
||||||
|
|
||||||
|
/// 「已恢复上次草稿」提示条可见性。
|
||||||
|
bool _restoredBannerVisible = false;
|
||||||
|
|
||||||
|
/// 草稿恢复期间的输入监听抑制(恢复不是「首次输入」,不发 started)。
|
||||||
|
bool _restoring = false;
|
||||||
|
|
||||||
|
/// 本页是否基于先前保存/恢复的草稿发布(`fromDraft` 口径)。
|
||||||
|
bool _fromDraft = false;
|
||||||
|
|
||||||
|
/// 上一次同步到服务端的 ready assetId 签名(media 三态判定:
|
||||||
|
/// 与当前一致即 PATCH 缺席不动,不一致才整组替换)。
|
||||||
|
String? _syncedMediaSignature;
|
||||||
|
|
||||||
|
/// 建草稿幂等键(同键重放;表单改动即置 null 换新键)。
|
||||||
|
String? _idempotencyKey;
|
||||||
|
|
||||||
|
bool _publishing = false;
|
||||||
|
bool _savingDraft = false;
|
||||||
|
|
||||||
|
/// 发布失败横幅(页内停留供对照,不用 SnackBar)。
|
||||||
|
String? _publishErrorMessage;
|
||||||
|
|
||||||
|
/// 「草稿已保存」的伴随提示(发布失败时告知内容未丢)。
|
||||||
|
bool _draftPreservedHint = false;
|
||||||
|
|
||||||
|
/// 「已保存草稿 ✓」提示(保存动作后显示)。
|
||||||
|
bool _draftSavedHint = false;
|
||||||
|
|
||||||
|
/// 首次输入已上报 started。
|
||||||
|
bool _started = false;
|
||||||
|
DateTime? _startedAt;
|
||||||
|
|
||||||
|
/// 本页发布尝试序号(attemptSeq,从 1 起)。
|
||||||
|
int _publishAttemptSeq = 0;
|
||||||
|
|
||||||
|
CommunityController get _controller => widget.controller;
|
||||||
|
|
||||||
|
DateTime _nowValue() => (widget.now ?? DateTime.now)();
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_uploader = (widget.uploaderFactory ?? _defaultUploaderFactory)(
|
||||||
|
_controller.repository,
|
||||||
|
widget.analytics,
|
||||||
|
);
|
||||||
|
_uploader.addListener(_onUploaderChanged);
|
||||||
|
_contentController.addListener(_onContentChanged);
|
||||||
|
unawaited(_restoreLatestDraft());
|
||||||
|
}
|
||||||
|
|
||||||
|
static MediaUploader _defaultUploaderFactory(
|
||||||
|
CommunityRepository repository,
|
||||||
|
PostAnalytics? analytics,
|
||||||
|
) => MediaUploader(repository: repository, analytics: analytics);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_uploader.removeListener(_onUploaderChanged);
|
||||||
|
// 在途上传作废(未 confirm 的 asset 弃引用,服务端超时清理兜底)。
|
||||||
|
_uploader.reset();
|
||||||
|
_uploader.dispose();
|
||||||
|
_contentController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- 输入与状态 ----
|
||||||
|
|
||||||
|
String get _content => _contentController.text.trim();
|
||||||
|
|
||||||
|
bool get _isEmptyForm =>
|
||||||
|
_content.isEmpty && _uploader.isEmpty && _draftMedia.isEmpty;
|
||||||
|
|
||||||
|
/// 发布 gating(05 §2.2/§2.3 + 后端 content 必填):正文非空、
|
||||||
|
/// 在场媒体全部 ready、无在途提交。
|
||||||
|
bool get _canPublish =>
|
||||||
|
_content.isNotEmpty &&
|
||||||
|
(_uploader.isEmpty || _uploader.allReady) &&
|
||||||
|
!_publishing &&
|
||||||
|
!_savingDraft;
|
||||||
|
|
||||||
|
void _onContentChanged() {
|
||||||
|
if (_restoring) return;
|
||||||
|
_markDirty();
|
||||||
|
_reportStartedOnce();
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onUploaderChanged() {
|
||||||
|
if (_uploader.items.isNotEmpty) _reportStartedOnce();
|
||||||
|
_markDirty();
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 表单一经改动即弃用旧幂等键(下次提交换新键,杜绝 40905 常态化)。
|
||||||
|
void _markDirty() {
|
||||||
|
_idempotencyKey = null;
|
||||||
|
_draftSavedHint = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _reportStartedOnce() {
|
||||||
|
if (_started) return;
|
||||||
|
if (_content.isEmpty && _uploader.isEmpty) return;
|
||||||
|
_started = true;
|
||||||
|
_startedAt = _nowValue();
|
||||||
|
widget.analytics?.postCreateStarted(entryPoint: widget.entryPoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- 草稿恢复(最小实现:最新一条)----
|
||||||
|
|
||||||
|
Future<void> _restoreLatestDraft() async {
|
||||||
|
try {
|
||||||
|
final page = await _controller.repository.listMyPosts(
|
||||||
|
limit: 1,
|
||||||
|
status: PostStatus.draft,
|
||||||
|
);
|
||||||
|
if (!mounted || page.items.isEmpty) return;
|
||||||
|
final draft = page.items.first;
|
||||||
|
_restoring = true;
|
||||||
|
_contentController.text = draft.content;
|
||||||
|
_restoring = false;
|
||||||
|
setState(() {
|
||||||
|
_draftPostId = draft.id;
|
||||||
|
_draftVersion = draft.version;
|
||||||
|
_draftMedia = draft.media;
|
||||||
|
// ai_creation(M4 预留读侧值)不在发布页可选集内,回落 general。
|
||||||
|
_category = draft.category == PostCategory.aiCreation
|
||||||
|
? PostCategory.general
|
||||||
|
: draft.category;
|
||||||
|
_restoredBannerVisible = true;
|
||||||
|
_fromDraft = true;
|
||||||
|
_syncedMediaSignature = _mediaSignature();
|
||||||
|
});
|
||||||
|
} on ApiException {
|
||||||
|
// 草稿恢复失败静默降级为「新建」,不阻塞发布(不打扰)。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _clearRestoredDraft() {
|
||||||
|
_restoring = true;
|
||||||
|
_contentController.clear();
|
||||||
|
_restoring = false;
|
||||||
|
setState(() {
|
||||||
|
_draftMedia = const [];
|
||||||
|
_restoredBannerVisible = false;
|
||||||
|
_idempotencyKey = null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- 媒体 ----
|
||||||
|
|
||||||
|
Future<void> _pickImages() async {
|
||||||
|
await _uploader.pickAndAdd();
|
||||||
|
if (!mounted) return;
|
||||||
|
if (_uploader.remainingSlots <= 0) {
|
||||||
|
_showSnackBar('最多可选 ${_uploader.maxImages} 张图片');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String _mediaSignature() => _uploader.items
|
||||||
|
.where((item) => item.isReady)
|
||||||
|
.map((item) => item.assetId)
|
||||||
|
.join(',');
|
||||||
|
|
||||||
|
/// 当前选图的挂接请求(全 ready 才可取;封面取首张)。
|
||||||
|
List<PostMediaAttachRequest>? _mediaAttachOrNull() =>
|
||||||
|
_uploader.isEmpty ? null : _uploader.buildAttachRequests();
|
||||||
|
|
||||||
|
// ---- 发布(建草稿 → 迁移发布)----
|
||||||
|
|
||||||
|
Future<void> _publish() async {
|
||||||
|
if (!_canPublish) return;
|
||||||
|
FocusScope.of(context).unfocus();
|
||||||
|
_publishAttemptSeq += 1;
|
||||||
|
setState(() {
|
||||||
|
_publishing = true;
|
||||||
|
_publishErrorMessage = null;
|
||||||
|
_draftPreservedHint = false;
|
||||||
|
});
|
||||||
|
final signature = _mediaSignature();
|
||||||
|
final fromDraft = _fromDraft && _draftPostId != null;
|
||||||
|
try {
|
||||||
|
if (_draftPostId == null) {
|
||||||
|
final key = _idempotencyKey ??= _uuid.v4();
|
||||||
|
final draft = await _controller.repository.createPost(
|
||||||
|
CreatePostRequest(
|
||||||
|
content: _content,
|
||||||
|
category: _category,
|
||||||
|
status: PostStatus.draft,
|
||||||
|
media: _mediaAttachOrNull(),
|
||||||
|
),
|
||||||
|
idempotencyKey: key,
|
||||||
|
);
|
||||||
|
_draftPostId = draft.id;
|
||||||
|
_draftVersion = draft.version;
|
||||||
|
_syncedMediaSignature = signature;
|
||||||
|
}
|
||||||
|
await _patchPublish(signature);
|
||||||
|
if (!mounted) return;
|
||||||
|
widget.analytics?.postPublishSucceeded(
|
||||||
|
durationMs: _elapsedSinceStart(),
|
||||||
|
mediaCount: _uploader.items.length + _keptDraftMediaCount(signature),
|
||||||
|
// 话题(TopicChip / 话题选择 sheet)无契约端点,M3 恒 0。
|
||||||
|
topicCount: 0,
|
||||||
|
textLength: _content.length,
|
||||||
|
fromDraft: fromDraft,
|
||||||
|
);
|
||||||
|
_uploader.reset();
|
||||||
|
Navigator.of(context).pop(true);
|
||||||
|
} on ApiException catch (error) {
|
||||||
|
if (!mounted) return;
|
||||||
|
_handlePublishError(error);
|
||||||
|
} finally {
|
||||||
|
if (mounted) setState(() => _publishing = false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// PATCH 迁移发布;乐观锁过期(40902,别处改过草稿)自动刷新 version
|
||||||
|
/// 重提一次。已发布帖重复提交为幂等 no-op(15 号 §2.4),弱网重放安全。
|
||||||
|
Future<void> _patchPublish(String signature) async {
|
||||||
|
final media = signature == _syncedMediaSignature
|
||||||
|
? null // 缺席不动(服务端媒体与本地选图一致)
|
||||||
|
: _mediaAttachOrNull() ?? const <PostMediaAttachRequest>[];
|
||||||
|
UpdatePostRequest request(int version) => UpdatePostRequest(
|
||||||
|
version: version,
|
||||||
|
content: _content,
|
||||||
|
category: _category,
|
||||||
|
publish: true,
|
||||||
|
media: media,
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
await _controller.repository.updatePost(
|
||||||
|
_draftPostId!,
|
||||||
|
request(_draftVersion!),
|
||||||
|
);
|
||||||
|
} on PostVersionConflictException {
|
||||||
|
final latest = await _controller.repository.getPost(_draftPostId!);
|
||||||
|
_draftVersion = latest.version;
|
||||||
|
await _controller.repository.updatePost(
|
||||||
|
_draftPostId!,
|
||||||
|
request(latest.version),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_syncedMediaSignature = signature;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 恢复草稿的服务端既有媒体在本次发布中被保留的张数(mediaCount 口径)。
|
||||||
|
int _keptDraftMediaCount(String signature) =>
|
||||||
|
signature == _syncedMediaSignature && _uploader.isEmpty
|
||||||
|
? _draftMedia.length
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
int _elapsedSinceStart() {
|
||||||
|
final startedAt = _startedAt;
|
||||||
|
if (startedAt == null) return 0;
|
||||||
|
return _nowValue().difference(startedAt).inMilliseconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _handlePublishError(ApiException error) {
|
||||||
|
final reason = postPublishFailureReasonOf(error);
|
||||||
|
if (reason != null) {
|
||||||
|
widget.analytics?.postPublishFailed(
|
||||||
|
reason: reason,
|
||||||
|
attemptSeq: _publishAttemptSeq,
|
||||||
|
errorCode: error is ApiBusinessException ? error.code : null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (error is IdempotencyMismatchException) {
|
||||||
|
// 同键异 payload:弃用旧键,下次提交换新键即可成功。
|
||||||
|
_idempotencyKey = null;
|
||||||
|
}
|
||||||
|
if (error is PostNotFoundException) {
|
||||||
|
// 草稿在别处被删:解除关联,重试走全新建草稿。
|
||||||
|
_draftPostId = null;
|
||||||
|
_draftVersion = null;
|
||||||
|
_fromDraft = false;
|
||||||
|
}
|
||||||
|
setState(() {
|
||||||
|
_publishErrorMessage = postPublishErrorMessage(error);
|
||||||
|
// 草稿已在服务端 → 明确告知内容未丢(本单核心提示语义)。
|
||||||
|
_draftPreservedHint = _draftPostId != null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- 存草稿 ----
|
||||||
|
|
||||||
|
Future<bool> _saveDraft(DraftSaveTrigger trigger) async {
|
||||||
|
if (_isEmptyForm) return true;
|
||||||
|
if (_content.isEmpty) {
|
||||||
|
_showSnackBar('请先写点什么再保存草稿');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (_uploader.hasBusyItem) {
|
||||||
|
_showSnackBar('图片还在上传中,请稍候再保存草稿');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (_uploader.hasFailure) {
|
||||||
|
_showSnackBar('有图片上传失败,请重试或删除后再保存草稿');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
setState(() {
|
||||||
|
_savingDraft = true;
|
||||||
|
_publishErrorMessage = null;
|
||||||
|
});
|
||||||
|
final signature = _mediaSignature();
|
||||||
|
try {
|
||||||
|
if (_draftPostId == null) {
|
||||||
|
final key = _idempotencyKey ??= _uuid.v4();
|
||||||
|
final draft = await _controller.repository.createPost(
|
||||||
|
CreatePostRequest(
|
||||||
|
content: _content,
|
||||||
|
category: _category,
|
||||||
|
status: PostStatus.draft,
|
||||||
|
media: _mediaAttachOrNull(),
|
||||||
|
),
|
||||||
|
idempotencyKey: key,
|
||||||
|
);
|
||||||
|
_draftPostId = draft.id;
|
||||||
|
_draftVersion = draft.version;
|
||||||
|
} else {
|
||||||
|
final updated = await _controller.repository.updatePost(
|
||||||
|
_draftPostId!,
|
||||||
|
UpdatePostRequest(
|
||||||
|
version: _draftVersion!,
|
||||||
|
content: _content,
|
||||||
|
category: _category,
|
||||||
|
media: signature == _syncedMediaSignature
|
||||||
|
? null
|
||||||
|
: _mediaAttachOrNull() ?? const <PostMediaAttachRequest>[],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
_draftVersion = updated.version;
|
||||||
|
}
|
||||||
|
_syncedMediaSignature = signature;
|
||||||
|
_fromDraft = true;
|
||||||
|
widget.analytics?.postDraftSaved(
|
||||||
|
trigger: trigger,
|
||||||
|
mediaCount: _uploader.items.length + _keptDraftMediaCount(signature),
|
||||||
|
);
|
||||||
|
if (mounted) setState(() => _draftSavedHint = true);
|
||||||
|
return true;
|
||||||
|
} on ApiException catch (error) {
|
||||||
|
if (mounted) _showSnackBar(draftSaveErrorMessage(error));
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
if (mounted) setState(() => _savingDraft = false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 「不保留」:已落服务端的草稿一并软删(`post_deleted` 触点)。
|
||||||
|
Future<void> _discardDraft() async {
|
||||||
|
final draftId = _draftPostId;
|
||||||
|
if (draftId == null) return;
|
||||||
|
try {
|
||||||
|
await _controller.repository.deletePost(draftId);
|
||||||
|
widget.analytics?.postDeleted();
|
||||||
|
} on ApiException {
|
||||||
|
// 删除失败不拦住离页(草稿留在服务端,下次进页可恢复)。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- 离页 ----
|
||||||
|
|
||||||
|
Future<void> _onCancel() async {
|
||||||
|
if (_publishing || _savingDraft) return;
|
||||||
|
if (_isEmptyForm) {
|
||||||
|
Navigator.of(context).pop(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final choice = await showDialog<_ExitChoice>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => AlertDialog(
|
||||||
|
title: const Text('保留草稿?'),
|
||||||
|
content: const Text('保留后下次进入发布页可继续编辑。'),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(context, _ExitChoice.keepEditing),
|
||||||
|
child: const Text('继续编辑'),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(context, _ExitChoice.discard),
|
||||||
|
child: const Text('不保留'),
|
||||||
|
),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () => Navigator.pop(context, _ExitChoice.keep),
|
||||||
|
child: const Text('保留'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (!mounted || choice == null || choice == _ExitChoice.keepEditing) return;
|
||||||
|
if (choice == _ExitChoice.discard) {
|
||||||
|
await _discardDraft();
|
||||||
|
if (!mounted) return;
|
||||||
|
Navigator.of(context).pop(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final saved = await _saveDraft(DraftSaveTrigger.onExit);
|
||||||
|
if (!mounted || !saved) return;
|
||||||
|
Navigator.of(context).pop(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _showSnackBar(String message) {
|
||||||
|
ScaffoldMessenger.of(
|
||||||
|
context,
|
||||||
|
).showSnackBar(SnackBar(content: Text(message)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- 渲染 ----
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final theme = Theme.of(context);
|
||||||
|
return PopScope(
|
||||||
|
canPop: _isEmptyForm && !_publishing && !_savingDraft,
|
||||||
|
onPopInvokedWithResult: (didPop, _) {
|
||||||
|
if (!didPop) unawaited(_onCancel());
|
||||||
|
},
|
||||||
|
child: Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
leadingWidth: 76,
|
||||||
|
leading: Center(
|
||||||
|
child: TextButton(
|
||||||
|
onPressed: _publishing ? null : () => unawaited(_onCancel()),
|
||||||
|
style: TextButton.styleFrom(foregroundColor: AppColors.ink),
|
||||||
|
child: const Text('取消'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
title: const Text('发布动态'),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: _publishing || _savingDraft
|
||||||
|
? null
|
||||||
|
: () => unawaited(_saveDraft(DraftSaveTrigger.manual)),
|
||||||
|
child: const Text('存草稿'),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 4),
|
||||||
|
FilledButton(
|
||||||
|
style: FilledButton.styleFrom(
|
||||||
|
minimumSize: const Size(0, 40),
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 20),
|
||||||
|
),
|
||||||
|
onPressed: _canPublish ? () => unawaited(_publish()) : null,
|
||||||
|
child: _publishing
|
||||||
|
? const SizedBox.square(
|
||||||
|
dimension: 18,
|
||||||
|
child: CircularProgressIndicator(
|
||||||
|
strokeWidth: 2,
|
||||||
|
color: Colors.white,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: const Text('发布'),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 12),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
body: ListView(
|
||||||
|
padding: const EdgeInsets.fromLTRB(16, 12, 16, 28),
|
||||||
|
children: [
|
||||||
|
if (_restoredBannerVisible) ...[
|
||||||
|
_RestoredDraftBanner(onClear: _clearRestoredDraft),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
],
|
||||||
|
if (_publishErrorMessage != null) ...[
|
||||||
|
InlineErrorBanner(message: _publishErrorMessage!),
|
||||||
|
if (_draftPreservedHint) ...[
|
||||||
|
const SizedBox(height: 6),
|
||||||
|
const Text(
|
||||||
|
'草稿已保存,可稍后继续发布',
|
||||||
|
style: TextStyle(fontSize: 12, color: AppColors.inkSoft),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
],
|
||||||
|
if (_draftSavedHint) ...[
|
||||||
|
const Text(
|
||||||
|
'已保存草稿 ✓',
|
||||||
|
style: TextStyle(fontSize: 12, color: AppColors.inkSoft),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
],
|
||||||
|
..._mediaSection(),
|
||||||
|
if (_uploader.hasBusyItem) ...[
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
_UploadSummaryBar(
|
||||||
|
progress: _uploader.overallProgress,
|
||||||
|
readyCount: _uploader.readyCount,
|
||||||
|
total: _uploader.items.length,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
TextField(
|
||||||
|
controller: _contentController,
|
||||||
|
minLines: 6,
|
||||||
|
maxLines: null,
|
||||||
|
maxLength: _maxContentLength,
|
||||||
|
keyboardType: TextInputType.multiline,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
hintText: '分享毛孩子的日常,或向宠友求助…',
|
||||||
|
alignLabelWithHint: true,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
Text('分类', style: theme.textTheme.bodySmall),
|
||||||
|
const SizedBox(height: 7),
|
||||||
|
Wrap(
|
||||||
|
spacing: 8,
|
||||||
|
children: [
|
||||||
|
for (final entry in const [
|
||||||
|
(PostCategory.general, '日常分享'),
|
||||||
|
(PostCategory.help, '求助'),
|
||||||
|
])
|
||||||
|
ChoiceChip(
|
||||||
|
label: Text(entry.$2),
|
||||||
|
selected: _category == entry.$1,
|
||||||
|
onSelected: (_) {
|
||||||
|
_markDirty();
|
||||||
|
setState(() => _category = entry.$1);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
ListTile(
|
||||||
|
contentPadding: EdgeInsets.zero,
|
||||||
|
leading: const Icon(Icons.location_on_outlined),
|
||||||
|
title: const Text('添加位置(选填)'),
|
||||||
|
trailing: const Icon(Icons.chevron_right),
|
||||||
|
onTap: () => _showSnackBar('位置功能即将上线'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Widget> _mediaSection() {
|
||||||
|
final showDraftMedia = _uploader.isEmpty && _draftMedia.isNotEmpty;
|
||||||
|
return [
|
||||||
|
PostMediaEditGrid(
|
||||||
|
items: _uploader.items,
|
||||||
|
canAdd: _uploader.remainingSlots > 0,
|
||||||
|
onAdd: _uploader.isPicking ? null : () => unawaited(_pickImages()),
|
||||||
|
onRemove: _uploader.remove,
|
||||||
|
onRetry: _uploader.retry,
|
||||||
|
),
|
||||||
|
if (showDraftMedia) ...[
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Text(
|
||||||
|
'草稿已含 ${_draftMedia.length} 张图片(发布时保留;重新选图将整组替换)',
|
||||||
|
style: const TextStyle(fontSize: 12, color: AppColors.inkSoft),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum _ExitChoice { keep, discard, keepEditing }
|
||||||
|
|
||||||
|
/// 「已恢复上次草稿」提示条(05 §2.3:surfaceTint 底、圆角 sm12)。
|
||||||
|
class _RestoredDraftBanner extends StatelessWidget {
|
||||||
|
const _RestoredDraftBanner({required this.onClear});
|
||||||
|
|
||||||
|
final VoidCallback onClear;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Container(
|
||||||
|
padding: const EdgeInsets.fromLTRB(12, 4, 4, 4),
|
||||||
|
decoration: const BoxDecoration(
|
||||||
|
color: AppColors.surfaceTint,
|
||||||
|
borderRadius: BorderRadius.all(Radius.circular(AppRadius.sm)),
|
||||||
|
),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
const Expanded(
|
||||||
|
child: Text(
|
||||||
|
'已恢复上次草稿',
|
||||||
|
style: TextStyle(fontSize: 12, color: AppColors.primaryDark),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
TextButton(onPressed: onClear, child: const Text('清空')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 页级上传汇总条(05 §3.3 末段:线性进度 + 「正在上传 n/N」)。
|
||||||
|
class _UploadSummaryBar extends StatelessWidget {
|
||||||
|
const _UploadSummaryBar({
|
||||||
|
required this.progress,
|
||||||
|
required this.readyCount,
|
||||||
|
required this.total,
|
||||||
|
});
|
||||||
|
|
||||||
|
final double progress;
|
||||||
|
final int readyCount;
|
||||||
|
final int total;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Row(
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
'正在上传 $readyCount/$total',
|
||||||
|
style: const TextStyle(fontSize: 12, color: AppColors.inkSoft),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 10),
|
||||||
|
Expanded(
|
||||||
|
child: LinearProgressIndicator(
|
||||||
|
value: progress,
|
||||||
|
minHeight: 4,
|
||||||
|
color: AppColors.primaryStrong,
|
||||||
|
backgroundColor: AppColors.surfaceTint,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,15 +7,23 @@ import 'package:patbond_flutter/widgets/common.dart';
|
|||||||
|
|
||||||
enum CreationMode { image, video }
|
enum CreationMode { image, video }
|
||||||
|
|
||||||
|
/// 创作 Tab。
|
||||||
|
///
|
||||||
|
/// **AI 生成模拟(700/650/500ms 假延时、风格/模型/分辨率设置、结果卡)
|
||||||
|
/// 属 M4 范围,T3-17 原样保留**;社区发布半边自 T3-17 起改由真实发布页
|
||||||
|
/// (`PostComposePage`,push 全屏)承担——本页顶部「发布动态」入口即其
|
||||||
|
/// 入口(entryPoint=create_tab),`AppState.publishPost` demo 发布流退役。
|
||||||
class CreatePage extends StatefulWidget {
|
class CreatePage extends StatefulWidget {
|
||||||
const CreatePage({
|
const CreatePage({
|
||||||
required this.appState,
|
required this.appState,
|
||||||
required this.onPublished,
|
required this.onOpenCompose,
|
||||||
super.key,
|
super.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
final AppState appState;
|
final AppState appState;
|
||||||
final ValueChanged<PostModel> onPublished;
|
|
||||||
|
/// 真实发布页入口(主壳 push,发布成功后回首页 Feed 刷新)。
|
||||||
|
final VoidCallback onOpenCompose;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<CreatePage> createState() => _CreatePageState();
|
State<CreatePage> createState() => _CreatePageState();
|
||||||
@@ -111,36 +119,13 @@ class _CreatePageState extends State<CreatePage> {
|
|||||||
setState(() => tags = [...tags, value]);
|
setState(() => tags = [...tags, value]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// AI 作品的社区发布留待 M4:AI 结果是生成图(无本地文件、无 media
|
||||||
|
/// asset),走不了两步上传,故不接真实发布链路;demo 发布流(写
|
||||||
|
/// `AppState.posts`)随 T3-17 退役,此处只余占位提示。
|
||||||
void publish() {
|
void publish() {
|
||||||
if (resultUrl == null || titleController.text.trim().isEmpty) {
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
ScaffoldMessenger.of(
|
const SnackBar(content: Text('AI 作品发布随 AI 创作能力上线(M4);发布普通动态请用上方「发布动态」')),
|
||||||
context,
|
|
||||||
).showSnackBar(const SnackBar(content: Text('请先完成生成并填写标题')));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final post = PostModel(
|
|
||||||
id: 'post_user_${DateTime.now().millisecondsSinceEpoch}',
|
|
||||||
authorName: '萌宠新手(我)',
|
|
||||||
authorAvatar: userAvatar,
|
|
||||||
time: '刚刚',
|
|
||||||
breedTag: 'AI创作',
|
|
||||||
content:
|
|
||||||
'${titleController.text.trim()}\n\n${contentController.text.trim()}',
|
|
||||||
mainImage: resultUrl!,
|
|
||||||
likes: 1,
|
|
||||||
tags: [...tags, mode == CreationMode.image ? 'AI生图' : 'AI视频'],
|
|
||||||
comments: const [],
|
|
||||||
hasLiked: true,
|
|
||||||
);
|
);
|
||||||
widget.appState.publishPost(post);
|
|
||||||
setState(() {
|
|
||||||
uploaded = false;
|
|
||||||
resultUrl = null;
|
|
||||||
generationStep = 0;
|
|
||||||
titleController.clear();
|
|
||||||
contentController.clear();
|
|
||||||
});
|
|
||||||
widget.onPublished(post);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -148,6 +133,8 @@ class _CreatePageState extends State<CreatePage> {
|
|||||||
return ListView(
|
return ListView(
|
||||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 28),
|
padding: const EdgeInsets.fromLTRB(16, 12, 16, 28),
|
||||||
children: [
|
children: [
|
||||||
|
_ComposeEntryCard(onTap: widget.onOpenCompose),
|
||||||
|
const SizedBox(height: 18),
|
||||||
SegmentedButton<CreationMode>(
|
SegmentedButton<CreationMode>(
|
||||||
segments: const [
|
segments: const [
|
||||||
ButtonSegment(
|
ButtonSegment(
|
||||||
@@ -402,6 +389,35 @@ class _CreatePageState extends State<CreatePage> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 真实发布入口(T3-17):本页其余部分是 AI 创作模拟(M4),社区发帖
|
||||||
|
/// 走这里 push 的发布页。
|
||||||
|
class _ComposeEntryCard extends StatelessWidget {
|
||||||
|
const _ComposeEntryCard({required this.onTap});
|
||||||
|
|
||||||
|
final VoidCallback onTap;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return SectionCard(
|
||||||
|
padding: EdgeInsets.zero,
|
||||||
|
child: ListTile(
|
||||||
|
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
|
||||||
|
leading: const CircleAvatar(
|
||||||
|
backgroundColor: AppColors.surfaceTint,
|
||||||
|
child: Icon(Icons.edit_outlined, color: AppColors.primary),
|
||||||
|
),
|
||||||
|
title: const Text(
|
||||||
|
'发布动态',
|
||||||
|
style: TextStyle(fontWeight: FontWeight.w800),
|
||||||
|
),
|
||||||
|
subtitle: const Text('写点文字、配上照片,分享给宠友'),
|
||||||
|
trailing: const Icon(Icons.chevron_right),
|
||||||
|
onTap: onTap,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class _UploadCard extends StatelessWidget {
|
class _UploadCard extends StatelessWidget {
|
||||||
const _UploadCard({
|
const _UploadCard({
|
||||||
required this.uploaded,
|
required this.uploaded,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import 'package:patbond_flutter/features/community/community_display.dart';
|
|||||||
import 'package:patbond_flutter/features/community/community_models.dart';
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
import 'package:patbond_flutter/features/community/feed_analytics.dart';
|
import 'package:patbond_flutter/features/community/feed_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/community/feed_exposure.dart';
|
import 'package:patbond_flutter/features/community/feed_exposure.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||||
import 'package:patbond_flutter/models/models.dart';
|
import 'package:patbond_flutter/models/models.dart';
|
||||||
import 'package:patbond_flutter/state/app_state.dart';
|
import 'package:patbond_flutter/state/app_state.dart';
|
||||||
import 'package:patbond_flutter/widgets/common.dart';
|
import 'package:patbond_flutter/widgets/common.dart';
|
||||||
@@ -17,15 +18,28 @@ import 'package:patbond_flutter/widgets/common.dart';
|
|||||||
enum HomeSegment { feed, services }
|
enum HomeSegment { feed, services }
|
||||||
|
|
||||||
/// 首页 Tab:Feed 段自 T3-14 起消费 [CommunityController] 真实数据
|
/// 首页 Tab:Feed 段自 T3-14 起消费 [CommunityController] 真实数据
|
||||||
/// (四态 + 游标翻页 + 聚合曝光埋点);天气条/问候卡/搜索/服务段与
|
/// (四态 + 游标翻页 + 聚合曝光埋点);问候语自 T3.5-10 起用真实展示名。
|
||||||
/// `_StoryRow` 家具保留 demo 形态(03 号评估 §1.1 判定)。
|
///
|
||||||
|
/// **刻意保留的 demo 占位**(ADR-022 决策 D3.5-1 钉死范围,留待对应里程碑;
|
||||||
|
/// 在此登记以免实测重复反馈):
|
||||||
|
/// - **天气条与地区选择**([_WeatherStatusBar] / [_AreaPickerSheet] /
|
||||||
|
/// [_WeatherDetailsSheet]):需接外部天气服务(含 API key 与配额管理),
|
||||||
|
/// 不属本迭代。
|
||||||
|
/// - **story 环的「柴犬圈 / 猫咪圈 / 救助站」**([_StoryRow]):实为话题,
|
||||||
|
/// ADR-018 已把话题剪出 M3 范围;环内「发布」是真入口。
|
||||||
|
/// - **促销卡「新用户首单立减 ¥20」**([_PromoCard]):属 M5 服务域
|
||||||
|
/// (优惠/订单能力尚不存在)。
|
||||||
|
/// - **搜索框与「本地服务」段**:搜索只过滤已加载页(契约无检索端点);
|
||||||
|
/// 服务商数据为 demo 常量,同属 M5。
|
||||||
class HomePage extends StatefulWidget {
|
class HomePage extends StatefulWidget {
|
||||||
const HomePage({
|
const HomePage({
|
||||||
required this.appState,
|
required this.appState,
|
||||||
required this.communityController,
|
required this.communityController,
|
||||||
required this.onOpenServices,
|
required this.onOpenServices,
|
||||||
required this.onOpenCreate,
|
required this.onOpenCompose,
|
||||||
super.key,
|
super.key,
|
||||||
|
this.profileController,
|
||||||
|
this.onOpenPost,
|
||||||
this.feedAnalytics,
|
this.feedAnalytics,
|
||||||
this.isActive = true,
|
this.isActive = true,
|
||||||
});
|
});
|
||||||
@@ -35,8 +49,19 @@ class HomePage extends StatefulWidget {
|
|||||||
/// Feed 数据源(Tab 级单例,app.dart 装配注入)。
|
/// Feed 数据源(Tab 级单例,app.dart 装配注入)。
|
||||||
final CommunityController communityController;
|
final CommunityController communityController;
|
||||||
|
|
||||||
|
/// 展示名数据源(T3.5-10):与资料页**共用同一控制器**,
|
||||||
|
/// 故改昵称后两处一起变,且全程只发一次 `/me`。
|
||||||
|
/// 未注入或资料尚未到手时问候语不带名字(不编造「豆豆」这类假名)。
|
||||||
|
final ProfileController? profileController;
|
||||||
|
|
||||||
final ValueChanged<bool> onOpenServices;
|
final ValueChanged<bool> onOpenServices;
|
||||||
final VoidCallback onOpenCreate;
|
|
||||||
|
/// 发布页入口(T3-17:story 环「发布」与空态 CTA 均 push 真实发布页,
|
||||||
|
/// entryPoint=feed;创作 Tab 的 AI 模拟不再是社区发帖入口)。
|
||||||
|
final VoidCallback onOpenCompose;
|
||||||
|
|
||||||
|
/// 帖子详情导航(T3-15 接通;主壳 push PostDetailPage)。
|
||||||
|
final ValueChanged<String>? onOpenPost;
|
||||||
|
|
||||||
/// feed 域埋点(feed_viewed 聚合曝光 + feed_load_failed)。
|
/// feed 域埋点(feed_viewed 聚合曝光 + feed_load_failed)。
|
||||||
final FeedAnalytics? feedAnalytics;
|
final FeedAnalytics? feedAnalytics;
|
||||||
@@ -72,6 +97,8 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
WidgetsBinding.instance.addObserver(this);
|
WidgetsBinding.instance.addObserver(this);
|
||||||
|
// 展示名变化(资料首次到手 / 用户改昵称后返回)即重建问候语。
|
||||||
|
widget.profileController?.addListener(_onProfileChanged);
|
||||||
// 主壳挂载即预取(pets 先例);重登后控制器已 reset 回 initial。
|
// 主壳挂载即预取(pets 先例);重登后控制器已 reset 回 initial。
|
||||||
// 首屏自动预取不计入浏览段 refreshCount(非用户动作)。
|
// 首屏自动预取不计入浏览段 refreshCount(非用户动作)。
|
||||||
if (_feed.phase == FeedPhase.initial) {
|
if (_feed.phase == FeedPhase.initial) {
|
||||||
@@ -80,6 +107,10 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
if (_feedSurfaceVisible) _startSegment();
|
if (_feedSurfaceVisible) _startSegment();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _onProfileChanged() {
|
||||||
|
if (mounted) setState(() {});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void didUpdateWidget(HomePage oldWidget) {
|
void didUpdateWidget(HomePage oldWidget) {
|
||||||
super.didUpdateWidget(oldWidget);
|
super.didUpdateWidget(oldWidget);
|
||||||
@@ -107,6 +138,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_settleSegment();
|
_settleSegment();
|
||||||
|
widget.profileController?.removeListener(_onProfileChanged);
|
||||||
WidgetsBinding.instance.removeObserver(this);
|
WidgetsBinding.instance.removeObserver(this);
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
@@ -231,14 +263,6 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// T3-14 取舍:详情页数据层重写属 T3-15,demo 详情页无法按服务端
|
|
||||||
/// postId 渲染真实帖,故整卡点按先提示、互动按钮为纯展示禁用态。
|
|
||||||
void _showDetailPending() {
|
|
||||||
ScaffoldMessenger.of(context)
|
|
||||||
..hideCurrentSnackBar()
|
|
||||||
..showSnackBar(const SnackBar(content: Text('帖子详情正在接入真实数据,敬请期待')));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 搜索词过滤(demo 交互保留:只过滤已加载的多页缓存,不发检索请求;
|
/// 搜索词过滤(demo 交互保留:只过滤已加载的多页缓存,不发检索请求;
|
||||||
/// 契约 v1.3.0 无搜索端点)。
|
/// 契约 v1.3.0 无搜索端点)。
|
||||||
List<FeedCard> get _visibleCards {
|
List<FeedCard> get _visibleCards {
|
||||||
@@ -309,6 +333,17 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
return ListenableBuilder(
|
return ListenableBuilder(
|
||||||
listenable: _feed,
|
listenable: _feed,
|
||||||
builder: (context, _) {
|
builder: (context, _) {
|
||||||
|
// 点赞/收藏对账失败的一次性 SnackBar(§3.5 回滚提示;与详情页
|
||||||
|
// 共用 controller 的 toggleError 消费口,先消费者清空)。
|
||||||
|
if (_feed.toggleError != null) {
|
||||||
|
_feed.clearToggleError();
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(
|
||||||
|
context,
|
||||||
|
).showSnackBar(const SnackBar(content: Text('操作失败,请重试')));
|
||||||
|
});
|
||||||
|
}
|
||||||
// ready 后(含翻页追加)补一次可见性扫描:无滚动也能记首屏曝光。
|
// ready 后(含翻页追加)补一次可见性扫描:无滚动也能记首屏曝光。
|
||||||
if (_viewSegment != null && _feed.phase == FeedPhase.ready) {
|
if (_viewSegment != null && _feed.phase == FeedPhase.ready) {
|
||||||
_scheduleVisibilityScan();
|
_scheduleVisibilityScan();
|
||||||
@@ -331,7 +366,10 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
_PetGreetingCard(
|
_PetGreetingCard(
|
||||||
greeting: greeting,
|
greeting: greeting,
|
||||||
petName: widget.appState.pet.name,
|
// T3.5-10:真实展示名(`nickname ?? username`,与资料页
|
||||||
|
// 同源同规则)取代硬编码的 demo 宠物名「豆豆」。
|
||||||
|
// 资料未到手时为 null → 只问候不称名,不编造假名。
|
||||||
|
displayName: widget.profileController?.displayName,
|
||||||
petAvatar: widget.appState.pet.avatarUrl,
|
petAvatar: widget.appState.pet.avatarUrl,
|
||||||
advice: widget.appState.locationWeather.petAdvice,
|
advice: widget.appState.locationWeather.petAdvice,
|
||||||
),
|
),
|
||||||
@@ -363,7 +401,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
),
|
),
|
||||||
const SizedBox(height: 18),
|
const SizedBox(height: 18),
|
||||||
if (segment == HomeSegment.feed) ...[
|
if (segment == HomeSegment.feed) ...[
|
||||||
_StoryRow(onCreate: widget.onOpenCreate),
|
_StoryRow(onCreate: widget.onOpenCompose),
|
||||||
const SizedBox(height: 18),
|
const SizedBox(height: 18),
|
||||||
_PromoCard(onTap: () => widget.onOpenServices(true)),
|
_PromoCard(onTap: () => widget.onOpenServices(true)),
|
||||||
const SizedBox(height: 18),
|
const SizedBox(height: 18),
|
||||||
@@ -452,7 +490,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
title: '还没有动态',
|
title: '还没有动态',
|
||||||
description: '关注的毛孩子们还没发帖,去逛逛话题吧',
|
description: '关注的毛孩子们还没发帖,去逛逛话题吧',
|
||||||
ctaLabel: '发布第一条',
|
ctaLabel: '发布第一条',
|
||||||
onCtaPressed: widget.onOpenCreate,
|
onCtaPressed: widget.onOpenCompose,
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -467,7 +505,15 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
padding: const EdgeInsets.only(bottom: 16),
|
padding: const EdgeInsets.only(bottom: 16),
|
||||||
child: KeyedSubtree(
|
child: KeyedSubtree(
|
||||||
key: _cardKeys.putIfAbsent(card.id, GlobalKey.new),
|
key: _cardKeys.putIfAbsent(card.id, GlobalKey.new),
|
||||||
child: PostCard(card: card, onTap: _showDetailPending),
|
// 点赞/收藏经共享 ToggleSync(source=feed 缺省);整卡与
|
||||||
|
// 评论钮进详情(T3-15 导航接通,T3-14 的占位提示移除)。
|
||||||
|
child: PostCard(
|
||||||
|
card: card,
|
||||||
|
onTap: () => widget.onOpenPost?.call(card.id),
|
||||||
|
onCommentTap: () => widget.onOpenPost?.call(card.id),
|
||||||
|
onLikeTap: () => _feed.toggleLike(card.id),
|
||||||
|
onBookmarkTap: () => _feed.toggleBookmark(card.id),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
// 搜索过滤中不渲染尾部(过滤只作用于已加载页,翻页语义混淆)。
|
// 搜索过滤中不渲染尾部(过滤只作用于已加载页,翻页语义混淆)。
|
||||||
@@ -534,6 +580,8 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// **刻意保留的 demo 占位**(ADR-022 决策 D3.5-1):定位与天气需接外部
|
||||||
|
/// 服务(含 API key 与配额管理),本迭代不做;地区选择只改本机 demo 数据。
|
||||||
class _WeatherStatusBar extends StatelessWidget {
|
class _WeatherStatusBar extends StatelessWidget {
|
||||||
const _WeatherStatusBar({
|
const _WeatherStatusBar({
|
||||||
required this.weather,
|
required this.weather,
|
||||||
@@ -623,14 +671,23 @@ class _WeatherStatusBar extends StatelessWidget {
|
|||||||
class _PetGreetingCard extends StatelessWidget {
|
class _PetGreetingCard extends StatelessWidget {
|
||||||
const _PetGreetingCard({
|
const _PetGreetingCard({
|
||||||
required this.greeting,
|
required this.greeting,
|
||||||
required this.petName,
|
required this.displayName,
|
||||||
required this.petAvatar,
|
required this.petAvatar,
|
||||||
required this.advice,
|
required this.advice,
|
||||||
});
|
});
|
||||||
|
|
||||||
final String greeting;
|
final String greeting;
|
||||||
final String petName;
|
|
||||||
|
/// 当前用户展示名;null 即资料未到手(问候语退化为不称名形态)。
|
||||||
|
final String? displayName;
|
||||||
|
|
||||||
|
/// **刻意保留的 demo 占位**:卡右侧大图仍是 demo 宠物图(用户头像与宠物
|
||||||
|
/// 头像本单已真实化,但「首页该显示哪只宠物」需要一个『当前宠物』概念,
|
||||||
|
/// 尚不存在——不在 ADR-022 范围内)。
|
||||||
final String petAvatar;
|
final String petAvatar;
|
||||||
|
|
||||||
|
/// **刻意保留的 demo 占位**:养宠建议来自 demo 天气数据(外部天气服务
|
||||||
|
/// 未接,ADR-022 决策 D3.5-1)。
|
||||||
final String advice;
|
final String advice;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -673,7 +730,9 @@ class _PetGreetingCard extends StatelessWidget {
|
|||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
'$greeting,$petName 👋',
|
displayName == null
|
||||||
|
? '$greeting 👋'
|
||||||
|
: '$greeting,$displayName 👋',
|
||||||
maxLines: 1,
|
maxLines: 1,
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
@@ -868,6 +927,9 @@ Color _weatherColor(WeatherCondition condition) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// **刻意保留的 demo 占位**(ADR-022 决策 D3.5-1):「柴犬圈 / 猫咪圈 /
|
||||||
|
/// 救助站」三个圈子实为**话题**,ADR-018 已把话题能力剪出 M3 范围,
|
||||||
|
/// 留待话题里程碑。首格「发布」是真入口(T3-17 起 push 真实发布页)。
|
||||||
class _StoryRow extends StatelessWidget {
|
class _StoryRow extends StatelessWidget {
|
||||||
const _StoryRow({required this.onCreate});
|
const _StoryRow({required this.onCreate});
|
||||||
|
|
||||||
@@ -938,6 +1000,8 @@ class _StoryRow extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// **刻意保留的 demo 占位**(ADR-022 决策 D3.5-1):促销文案与「去使用」
|
||||||
|
/// 属 **M5 服务域**——优惠券与订单能力尚不存在,当前点按只跳本地服务段。
|
||||||
class _PromoCard extends StatelessWidget {
|
class _PromoCard extends StatelessWidget {
|
||||||
const _PromoCard({required this.onTap});
|
const _PromoCard({required this.onTap});
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,16 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:patbond_flutter/analytics/analytics_page_name.dart';
|
import 'package:patbond_flutter/analytics/analytics_page_name.dart';
|
||||||
import 'package:patbond_flutter/analytics/page_view_tracker.dart';
|
import 'package:patbond_flutter/analytics/page_view_tracker.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/avatar_upload_sheet.dart';
|
||||||
import 'package:patbond_flutter/features/community/community_controller.dart';
|
import 'package:patbond_flutter/features/community/community_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_interaction_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/community/feed_analytics.dart';
|
import 'package:patbond_flutter/features/community/feed_analytics.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/post_analytics.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/post_compose_page.dart';
|
||||||
import 'package:patbond_flutter/features/create/create_page.dart';
|
import 'package:patbond_flutter/features/create/create_page.dart';
|
||||||
import 'package:patbond_flutter/features/home/home_page.dart';
|
import 'package:patbond_flutter/features/home/home_page.dart';
|
||||||
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
||||||
@@ -11,9 +18,9 @@ import 'package:patbond_flutter/features/pets/pet_analytics.dart';
|
|||||||
import 'package:patbond_flutter/features/pets/pets_controller.dart';
|
import 'package:patbond_flutter/features/pets/pets_controller.dart';
|
||||||
import 'package:patbond_flutter/features/pets/pets_page.dart';
|
import 'package:patbond_flutter/features/pets/pets_page.dart';
|
||||||
import 'package:patbond_flutter/features/post/post_detail_page.dart';
|
import 'package:patbond_flutter/features/post/post_detail_page.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||||
import 'package:patbond_flutter/features/profile/profile_page.dart';
|
import 'package:patbond_flutter/features/profile/profile_page.dart';
|
||||||
import 'package:patbond_flutter/features/services/services_page.dart';
|
import 'package:patbond_flutter/features/services/services_page.dart';
|
||||||
import 'package:patbond_flutter/models/models.dart';
|
|
||||||
import 'package:patbond_flutter/state/app_state.dart';
|
import 'package:patbond_flutter/state/app_state.dart';
|
||||||
import 'package:patbond_flutter/widgets/common.dart';
|
import 'package:patbond_flutter/widgets/common.dart';
|
||||||
|
|
||||||
@@ -22,10 +29,16 @@ class MainShellPage extends StatefulWidget {
|
|||||||
required this.appState,
|
required this.appState,
|
||||||
required this.petsController,
|
required this.petsController,
|
||||||
required this.communityController,
|
required this.communityController,
|
||||||
|
required this.profileController,
|
||||||
super.key,
|
super.key,
|
||||||
|
this.currentUserId,
|
||||||
this.petAnalytics,
|
this.petAnalytics,
|
||||||
this.healthRecordAnalytics,
|
this.healthRecordAnalytics,
|
||||||
this.feedAnalytics,
|
this.feedAnalytics,
|
||||||
|
this.interactionAnalytics,
|
||||||
|
this.postAnalytics,
|
||||||
|
this.mediaUploaderFactory,
|
||||||
|
this.avatarUploaderBuilder,
|
||||||
this.pageViewTracker,
|
this.pageViewTracker,
|
||||||
this.onLogout,
|
this.onLogout,
|
||||||
});
|
});
|
||||||
@@ -38,6 +51,12 @@ class MainShellPage extends StatefulWidget {
|
|||||||
/// 社区状态(T3-12 数据层;首页 Feed segment 数据源,T3-14 接线)。
|
/// 社区状态(T3-12 数据层;首页 Feed segment 数据源,T3-14 接线)。
|
||||||
final CommunityController communityController;
|
final CommunityController communityController;
|
||||||
|
|
||||||
|
/// 本人资料与社区数字(T3.5-08);资料 Tab 与首页问候语共用。
|
||||||
|
final ProfileController profileController;
|
||||||
|
|
||||||
|
/// 当前登录用户 id(详情页评论删除入口 / 关注钮自见性的 UI 判定)。
|
||||||
|
final String? currentUserId;
|
||||||
|
|
||||||
/// pet 域埋点强类型封装(建宠漏斗三事件)。
|
/// pet 域埋点强类型封装(建宠漏斗三事件)。
|
||||||
final PetAnalytics? petAnalytics;
|
final PetAnalytics? petAnalytics;
|
||||||
|
|
||||||
@@ -47,6 +66,18 @@ class MainShellPage extends StatefulWidget {
|
|||||||
/// feed 域埋点(T3-14 聚合曝光 + 加载失败)。
|
/// feed 域埋点(T3-14 聚合曝光 + 加载失败)。
|
||||||
final FeedAnalytics? feedAnalytics;
|
final FeedAnalytics? feedAnalytics;
|
||||||
|
|
||||||
|
/// 互动域埋点(T3-16 评论成败对 + 关注对;详情页消费)。
|
||||||
|
final CommunityInteractionAnalytics? interactionAnalytics;
|
||||||
|
|
||||||
|
/// post 域埋点(T3-17 发布漏斗五事件 + 媒体三段;发布页消费)。
|
||||||
|
final PostAnalytics? postAnalytics;
|
||||||
|
|
||||||
|
/// 发布页 [MediaUploader] 构造口(桌面实测 / 测试替换选图与压缩层)。
|
||||||
|
final MediaUploaderFactory? mediaUploaderFactory;
|
||||||
|
|
||||||
|
/// 头像上传构造口(T3.5-08/09:资料编辑页与宠物详情页)。
|
||||||
|
final AvatarUploaderBuilder? avatarUploaderBuilder;
|
||||||
|
|
||||||
/// Tab 曝光补点(IndexedStack 切换不产生路由事件,03 号评估 §3.2)。
|
/// Tab 曝光补点(IndexedStack 切换不产生路由事件,03 号评估 §3.2)。
|
||||||
final PageViewTracker? pageViewTracker;
|
final PageViewTracker? pageViewTracker;
|
||||||
|
|
||||||
@@ -93,16 +124,44 @@ class _MainShellPageState extends State<MainShellPage> {
|
|||||||
widget.pageViewTracker?.reportTab(_tabPages[3]);
|
widget.pageViewTracker?.reportTab(_tabPages[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void openPost(PostModel post) {
|
/// 帖子详情(T3-15:真实数据整页;Feed 卡片与详情共享
|
||||||
|
/// communityController,互动状态跨页一致)。
|
||||||
|
void openPost(String postId) {
|
||||||
Navigator.of(context).push(
|
Navigator.of(context).push(
|
||||||
MaterialPageRoute<void>(
|
MaterialPageRoute<void>(
|
||||||
settings: RouteSettings(name: AnalyticsPageName.postDetail.pageName),
|
settings: RouteSettings(name: AnalyticsPageName.postDetail.pageName),
|
||||||
builder: (context) =>
|
builder: (context) => PostDetailPage(
|
||||||
PostDetailPage(appState: widget.appState, postId: post.id),
|
controller: widget.communityController,
|
||||||
|
postId: postId,
|
||||||
|
currentUserId: widget.currentUserId,
|
||||||
|
analytics: widget.interactionAnalytics,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 发布页(T3-17:真实发布链路)。发布成功后回首页 Feed 并整体刷新,
|
||||||
|
/// 新帖按 `(published_at DESC, id DESC)` 落在首位(跨客户端同一序)。
|
||||||
|
Future<void> openCompose(PostEntryPoint entryPoint) async {
|
||||||
|
final published = await Navigator.of(context).push<bool>(
|
||||||
|
MaterialPageRoute<bool>(
|
||||||
|
settings: RouteSettings(name: AnalyticsPageName.postForm.pageName),
|
||||||
|
builder: (context) => PostComposePage(
|
||||||
|
controller: widget.communityController,
|
||||||
|
entryPoint: entryPoint,
|
||||||
|
analytics: widget.postAnalytics,
|
||||||
|
uploaderFactory: widget.mediaUploaderFactory,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (!mounted || published != true) return;
|
||||||
|
selectTab(0);
|
||||||
|
unawaited(widget.communityController.refresh());
|
||||||
|
ScaffoldMessenger.of(
|
||||||
|
context,
|
||||||
|
).showSnackBar(const SnackBar(content: Text('已发布,去首页看看吧 🐾')));
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return AnimatedBuilder(
|
return AnimatedBuilder(
|
||||||
@@ -118,30 +177,34 @@ class _MainShellPageState extends State<MainShellPage> {
|
|||||||
HomePage(
|
HomePage(
|
||||||
appState: widget.appState,
|
appState: widget.appState,
|
||||||
communityController: widget.communityController,
|
communityController: widget.communityController,
|
||||||
|
profileController: widget.profileController,
|
||||||
feedAnalytics: widget.feedAnalytics,
|
feedAnalytics: widget.feedAnalytics,
|
||||||
isActive: currentIndex == 0,
|
isActive: currentIndex == 0,
|
||||||
onOpenServices: openServices,
|
onOpenServices: openServices,
|
||||||
onOpenCreate: () => selectTab(1),
|
onOpenCompose: () => openCompose(PostEntryPoint.feed),
|
||||||
|
onOpenPost: openPost,
|
||||||
),
|
),
|
||||||
CreatePage(
|
CreatePage(
|
||||||
appState: widget.appState,
|
appState: widget.appState,
|
||||||
onPublished: (post) {
|
// T3-17:发布半边已真实化(发布页 push),AI 生成模拟原样留 M4。
|
||||||
selectTab(0);
|
onOpenCompose: () => openCompose(PostEntryPoint.createTab),
|
||||||
WidgetsBinding.instance.addPostFrameCallback(
|
|
||||||
(_) => openPost(post),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
PetsPage(
|
PetsPage(
|
||||||
controller: widget.petsController,
|
controller: widget.petsController,
|
||||||
analytics: widget.petAnalytics,
|
analytics: widget.petAnalytics,
|
||||||
healthAnalytics: widget.healthRecordAnalytics,
|
healthAnalytics: widget.healthRecordAnalytics,
|
||||||
|
avatarUploaderBuilder: widget.avatarUploaderBuilder,
|
||||||
),
|
),
|
||||||
ServicesPage(
|
ServicesPage(
|
||||||
showPersonal: showPersonalServices,
|
showPersonal: showPersonalServices,
|
||||||
locationWeather: widget.appState.locationWeather,
|
locationWeather: widget.appState.locationWeather,
|
||||||
),
|
),
|
||||||
ProfilePage(appState: widget.appState, onLogout: widget.onLogout),
|
ProfilePage(
|
||||||
|
appState: widget.appState,
|
||||||
|
controller: widget.profileController,
|
||||||
|
avatarUploaderBuilder: widget.avatarUploaderBuilder,
|
||||||
|
onLogout: widget.onLogout,
|
||||||
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter/semantics.dart';
|
import 'package:flutter/semantics.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/app_date_picker.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/primary_button.dart';
|
import 'package:patbond_flutter/core/widgets/primary_button.dart';
|
||||||
@@ -265,14 +266,22 @@ class _CareReminderFormPageState extends State<CareReminderFormPage> {
|
|||||||
_dueDate == null ? '未选择' : dateToJson(_dueDate!),
|
_dueDate == null ? '未选择' : dateToJson(_dueDate!),
|
||||||
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
||||||
),
|
),
|
||||||
trailing: const Icon(
|
trailing: AppDateFieldTrailing(
|
||||||
Icons.calendar_month_outlined,
|
firstDate: DateTime.now(),
|
||||||
color: AppColors.muted,
|
lastDate: DateTime(DateTime.now().year + 5),
|
||||||
|
enabled: !_submitting,
|
||||||
|
onToday: (value) {
|
||||||
|
_markStarted();
|
||||||
|
setState(() {
|
||||||
|
_dueDate = value;
|
||||||
|
_dueError = null;
|
||||||
|
});
|
||||||
|
},
|
||||||
),
|
),
|
||||||
enabled: !_submitting,
|
enabled: !_submitting,
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
final value = await showDatePicker(
|
final value = await pickAppDate(
|
||||||
context: context,
|
context: context,
|
||||||
initialDate: _dueDate ?? now,
|
initialDate: _dueDate ?? now,
|
||||||
firstDate: now,
|
firstDate: now,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import 'package:patbond_flutter/analytics/analytics_page_name.dart';
|
|||||||
import 'package:patbond_flutter/core/navigation/fade_route.dart';
|
import 'package:patbond_flutter/core/navigation/fade_route.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/app_date_picker.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/record_type_dot.dart';
|
import 'package:patbond_flutter/core/widgets/record_type_dot.dart';
|
||||||
@@ -484,7 +485,7 @@ class _CompleteDialogState extends State<_CompleteDialog> {
|
|||||||
),
|
),
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
final value = await showDatePicker(
|
final value = await pickAppDate(
|
||||||
context: context,
|
context: context,
|
||||||
initialDate: _date,
|
initialDate: _date,
|
||||||
firstDate: DateTime(1990),
|
firstDate: DateTime(1990),
|
||||||
@@ -494,6 +495,11 @@ class _CompleteDialogState extends State<_CompleteDialog> {
|
|||||||
setState(() => _date = value);
|
setState(() => _date = value);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
trailing: AppDateFieldTrailing(
|
||||||
|
firstDate: DateTime(1990),
|
||||||
|
lastDate: DateTime.now(),
|
||||||
|
onToday: (value) => setState(() => _date = value),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter/semantics.dart';
|
import 'package:flutter/semantics.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/app_date_picker.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/primary_button.dart';
|
import 'package:patbond_flutter/core/widgets/primary_button.dart';
|
||||||
@@ -269,14 +270,19 @@ class _HealthEventFormPageState extends State<HealthEventFormPage> {
|
|||||||
dateToJson(_occurredDate),
|
dateToJson(_occurredDate),
|
||||||
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
||||||
),
|
),
|
||||||
trailing: const Icon(
|
trailing: AppDateFieldTrailing(
|
||||||
Icons.calendar_month_outlined,
|
firstDate: DateTime(1990),
|
||||||
color: AppColors.muted,
|
lastDate: DateTime.now(),
|
||||||
|
enabled: !_submitting,
|
||||||
|
onToday: (value) {
|
||||||
|
_markStarted();
|
||||||
|
setState(() => _occurredDate = value);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
enabled: !_submitting,
|
enabled: !_submitting,
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
final value = await showDatePicker(
|
final value = await pickAppDate(
|
||||||
context: context,
|
context: context,
|
||||||
initialDate: _occurredDate,
|
initialDate: _occurredDate,
|
||||||
firstDate: DateTime(1990),
|
firstDate: DateTime(1990),
|
||||||
|
|||||||
@@ -125,6 +125,27 @@ String healthEventMonthHeader(DateTime occurredAt) {
|
|||||||
return '${local.year} 年 ${local.month} 月';
|
return '${local.year} 年 ${local.month} 月';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 月度花费卡标签(M3.5-03)。
|
||||||
|
///
|
||||||
|
/// 根因备忘:卡片标签此前硬编码「本月花费」,用户无法自证「本月」到底是
|
||||||
|
/// 哪个月——他把当月记录误录到 2026-04 后看到「本月花费 ¥0」,以为聚合坏了。
|
||||||
|
/// 服务端 `summary.monthlyExpense.month` 本就返回 ISO year-month(如 `2026-09`,
|
||||||
|
/// 按 `tz` 归月),直接展示即可自查,无需改后端。
|
||||||
|
///
|
||||||
|
/// [month] 形如 `2026-09`。窄卡(一行四卡)只放得下 4~5 个字,故取「9 月花费」
|
||||||
|
/// 而非「2026-09 花费」;跨年(服务端归月的年份与设备当前年份不一致,如设备
|
||||||
|
/// 已跨到 1 月而窗口仍是去年 12 月)时补年份消歧。解析失败退回「本月花费」。
|
||||||
|
String monthlyExpenseCardLabel(String month, {DateTime? now}) {
|
||||||
|
final match = RegExp(r'^(\d{4})-(\d{2})$').firstMatch(month);
|
||||||
|
if (match == null) return '本月花费';
|
||||||
|
final year = int.parse(match.group(1)!);
|
||||||
|
final monthNo = int.parse(match.group(2)!);
|
||||||
|
if (monthNo < 1 || monthNo > 12) return '本月花费';
|
||||||
|
final currentYear = (now ?? DateTime.now()).year;
|
||||||
|
if (year != currentYear) return '$year/$monthNo 花费';
|
||||||
|
return '$monthNo 月花费';
|
||||||
|
}
|
||||||
|
|
||||||
/// 设备时区 → summary `tz` 参数(契约接受固定偏移形如 `+08:00`;
|
/// 设备时区 → summary `tz` 参数(契约接受固定偏移形如 `+08:00`;
|
||||||
/// Flutter 无 IANA 名可取,固定偏移语义等价——只作用于月度窗口)。
|
/// Flutter 无 IANA 名可取,固定偏移语义等价——只作用于月度窗口)。
|
||||||
String tzOffsetQueryValue(Duration offset) {
|
String tzOffsetQueryValue(Duration offset) {
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:patbond_flutter/core/navigation/fade_route.dart';
|
import 'package:patbond_flutter/core/navigation/fade_route.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/avatar_upload_sheet.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/pet_avatar.dart';
|
import 'package:patbond_flutter/core/widgets/pet_avatar.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/record_type_dot.dart';
|
import 'package:patbond_flutter/core/widgets/record_type_dot.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
import 'package:patbond_flutter/features/pets/care_reminders_page.dart';
|
import 'package:patbond_flutter/features/pets/care_reminders_page.dart';
|
||||||
import 'package:patbond_flutter/features/pets/health_events_page.dart';
|
import 'package:patbond_flutter/features/pets/health_events_page.dart';
|
||||||
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
||||||
@@ -43,6 +45,7 @@ class PetDetailPage extends StatefulWidget {
|
|||||||
super.key,
|
super.key,
|
||||||
this.analytics,
|
this.analytics,
|
||||||
this.healthAnalytics,
|
this.healthAnalytics,
|
||||||
|
this.avatarUploaderBuilder,
|
||||||
});
|
});
|
||||||
|
|
||||||
final PetsController controller;
|
final PetsController controller;
|
||||||
@@ -50,6 +53,10 @@ class PetDetailPage extends StatefulWidget {
|
|||||||
final PetAnalytics? analytics;
|
final PetAnalytics? analytics;
|
||||||
final HealthRecordAnalytics? healthAnalytics;
|
final HealthRecordAnalytics? healthAnalytics;
|
||||||
|
|
||||||
|
/// 头像上传编排器构造口(T3.5-09);null 即本次构建未装配上传能力,
|
||||||
|
/// 隐藏头像编辑入口(生产装配恒注入,见 `app.dart`)。
|
||||||
|
final AvatarUploaderBuilder? avatarUploaderBuilder;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<PetDetailPage> createState() => _PetDetailPageState();
|
State<PetDetailPage> createState() => _PetDetailPageState();
|
||||||
}
|
}
|
||||||
@@ -186,6 +193,109 @@ class _PetDetailPageState extends State<PetDetailPage> {
|
|||||||
/// 记录写入权限档 WRITE(owner + caregiver);viewer 隐藏录入入口。
|
/// 记录写入权限档 WRITE(owner + caregiver);viewer 隐藏录入入口。
|
||||||
bool get _canWriteRecords => _pet?.myRole != PetRole.viewer;
|
bool get _canWriteRecords => _pet?.myRole != PetRole.viewer;
|
||||||
|
|
||||||
|
/// 头像写入权限档 **WRITE**(ADR-022 决策 D3.5-3:头像属日常照护信息,
|
||||||
|
/// 与体重/疫苗同档,owner + caregiver 均可改;viewer 只读)。
|
||||||
|
///
|
||||||
|
/// 注意这与 [_canEdit](MANAGE,仅 owner)**刻意不同**:服务端按「本次
|
||||||
|
/// 请求触及了哪些字段」定档——只带 avatarAssetId 走 WRITE,碰任一资料
|
||||||
|
/// 字段即 MANAGE。故这里只发纯头像 PATCH,不夹带任何资料字段。
|
||||||
|
bool get _canEditAvatar =>
|
||||||
|
_phase == _DetailPhase.ready &&
|
||||||
|
_pet?.myRole != PetRole.viewer &&
|
||||||
|
widget.avatarUploaderBuilder != null;
|
||||||
|
|
||||||
|
/// 头像入口:已有头像时先给「更换 / 移除」二选一,没有则直接拉起上传。
|
||||||
|
Future<void> _onAvatarTap() async {
|
||||||
|
final pet = _pet;
|
||||||
|
if (pet == null || !_canEditAvatar) return;
|
||||||
|
if (pet.avatarUrl == null) {
|
||||||
|
await _uploadAvatar();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final action = await showModalBottomSheet<String>(
|
||||||
|
context: context,
|
||||||
|
showDragHandle: true,
|
||||||
|
builder: (context) => SafeArea(
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.photo_camera_outlined),
|
||||||
|
title: const Text('更换头像'),
|
||||||
|
onTap: () => Navigator.pop(context, 'replace'),
|
||||||
|
),
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(
|
||||||
|
Icons.delete_outline,
|
||||||
|
color: AppColors.errorDark,
|
||||||
|
),
|
||||||
|
title: const Text(
|
||||||
|
'移除头像',
|
||||||
|
style: TextStyle(color: AppColors.errorDark),
|
||||||
|
),
|
||||||
|
onTap: () => Navigator.pop(context, 'clear'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (!mounted || action == null) return;
|
||||||
|
if (action == 'replace') {
|
||||||
|
await _uploadAvatar();
|
||||||
|
} else {
|
||||||
|
// 三态「显式 null」= 清除头像(缺省是「不改」,两者在 JSON 上不同)。
|
||||||
|
await _patchAvatar(const PatchField<String>.clear(), '已移除头像');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _uploadAvatar() async {
|
||||||
|
final builder = widget.avatarUploaderBuilder;
|
||||||
|
if (builder == null) return;
|
||||||
|
final assetId = await showAvatarUploadSheet(
|
||||||
|
context,
|
||||||
|
builder: builder,
|
||||||
|
// 用途即引用侧的类型检查:帖图或用户头像挂到宠物上会被答 404/40405。
|
||||||
|
purpose: MediaPurpose.petAvatar,
|
||||||
|
);
|
||||||
|
if (assetId == null || !mounted) return;
|
||||||
|
await _patchAvatar(PatchField<String>.value(assetId), '头像已更新');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 纯头像 PATCH:请求体只有 `version` + `avatarAssetId`,**不带任何资料
|
||||||
|
/// 字段**——夹带资料字段会把权限档从 WRITE 抬到 MANAGE,caregiver 立刻
|
||||||
|
/// 403(服务端刻意按更严的一半判,堵「夹带改名」)。
|
||||||
|
Future<void> _patchAvatar(PatchField<String> intent, String okMessage) async {
|
||||||
|
final pet = _pet;
|
||||||
|
if (pet == null) return;
|
||||||
|
try {
|
||||||
|
final updated = await widget.controller.updatePet(
|
||||||
|
pet.id,
|
||||||
|
UpdatePetRequest(version: pet.version, avatarAssetId: intent),
|
||||||
|
);
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() {
|
||||||
|
_pet = updated;
|
||||||
|
_phase = _DetailPhase.ready;
|
||||||
|
});
|
||||||
|
ScaffoldMessenger.of(
|
||||||
|
context,
|
||||||
|
).showSnackBar(SnackBar(content: Text(okMessage)));
|
||||||
|
} on PetVersionConflictException catch (error) {
|
||||||
|
// 40902:他人已改过这行。重取档案拿新 version,让用户自行决定是否重来
|
||||||
|
// (不静默重放——头像是用户可见的覆盖操作,不该自动生效两次)。
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(
|
||||||
|
context,
|
||||||
|
).showSnackBar(SnackBar(content: Text(petAvatarSaveErrorMessage(error))));
|
||||||
|
await _load();
|
||||||
|
} on ApiException catch (error) {
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(
|
||||||
|
context,
|
||||||
|
).showSnackBar(SnackBar(content: Text(petAvatarSaveErrorMessage(error))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _openWeights(Pet pet) async {
|
Future<void> _openWeights(Pet pet) async {
|
||||||
await Navigator.of(context).push(
|
await Navigator.of(context).push(
|
||||||
// 列表页页名不在字典 v2 枚举内(06 §5.2 验收 4:字典外不上报),
|
// 列表页页名不在字典 v2 枚举内(06 §5.2 验收 4:字典外不上报),
|
||||||
@@ -306,11 +416,14 @@ class _PetDetailPageState extends State<PetDetailPage> {
|
|||||||
children: [
|
children: [
|
||||||
Column(
|
Column(
|
||||||
children: [
|
children: [
|
||||||
|
// T3.5-09:头像展示真实预签名 URL(无图回退爪印占位);铅笔角标
|
||||||
|
// 自此有功能——接 MediaUploader 上传 pet_avatar,WRITE 档可见。
|
||||||
PetAvatar(
|
PetAvatar(
|
||||||
size: PetAvatarSize.xl,
|
size: PetAvatarSize.xl,
|
||||||
showEditBadge: _canEdit,
|
url: pet.avatarUrl,
|
||||||
onTap: _canEdit ? _openEdit : null,
|
showEditBadge: _canEditAvatar,
|
||||||
semanticLabel: _canEdit ? '编辑宠物资料' : null,
|
onTap: _canEditAvatar ? _onAvatarTap : null,
|
||||||
|
semanticLabel: _canEditAvatar ? '更换宠物头像' : null,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
Text(pet.name, style: Theme.of(context).textTheme.headlineSmall),
|
Text(pet.name, style: Theme.of(context).textTheme.headlineSmall),
|
||||||
@@ -532,7 +645,9 @@ class _PetDetailPageState extends State<PetDetailPage> {
|
|||||||
// monthlyExpense 恒非 null(契约);金额整数分 → 元展示。
|
// monthlyExpense 恒非 null(契约);金额整数分 → 元展示。
|
||||||
value: '¥${formatCentsAsYuan(expense.amountCents)}',
|
value: '¥${formatCentsAsYuan(expense.amountCents)}',
|
||||||
emphasized: expense.amountCents > 0,
|
emphasized: expense.amountCents > 0,
|
||||||
label: '本月花费',
|
// M3.5-03:标签展示服务端归月的实际月份(此前硬编码
|
||||||
|
// 「本月花费」,用户无法自证记录落在哪个月)。
|
||||||
|
label: monthlyExpenseCardLabel(expense.month),
|
||||||
onTap: () => _openTimeline(pet),
|
onTap: () => _openTimeline(pet),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -545,6 +660,13 @@ class _PetDetailPageState extends State<PetDetailPage> {
|
|||||||
|
|
||||||
/// 数据卡(正典 stat-card 形态):图标 + 数值 15/w800 + 标签 12 inkSoft;
|
/// 数据卡(正典 stat-card 形态):图标 + 数值 15/w800 + 标签 12 inkSoft;
|
||||||
/// 空态数值降级 inkSoft 常规字重(区分「有数据」与「空态」两种视觉)。
|
/// 空态数值降级 inkSoft 常规字重(区分「有数据」与「空态」两种视觉)。
|
||||||
|
///
|
||||||
|
/// M3.5-03:可点卡([onTap] 非空)右上角补 `chevron_right`——四张卡本都可点进
|
||||||
|
/// 明细页,但此前无任何视觉提示,用户实测反馈不知道能点。提示形态沿用项目
|
||||||
|
/// 既有可点行/卡(宠物列表卡、健康提醒卡、资料页设置行)的 `chevron_right`,
|
||||||
|
/// 不自创。同时 [MergeSemantics] 把「数值 + 标签」并进 InkWell 的 button 节点,
|
||||||
|
/// 读屏一次读全「¥0,9 月花费,按钮」,而不是两段孤立文字(tap 动作仍在
|
||||||
|
/// InkWell 上,不用 `excludeSemantics` 以免连带丢掉可激活性)。
|
||||||
class _SummaryCard extends StatelessWidget {
|
class _SummaryCard extends StatelessWidget {
|
||||||
const _SummaryCard({
|
const _SummaryCard({
|
||||||
required this.icon,
|
required this.icon,
|
||||||
@@ -564,35 +686,51 @@ class _SummaryCard extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Card(
|
return MergeSemantics(
|
||||||
child: InkWell(
|
child: Card(
|
||||||
onTap: onTap,
|
child: InkWell(
|
||||||
borderRadius: BorderRadius.circular(AppRadius.xl),
|
onTap: onTap,
|
||||||
child: Padding(
|
borderRadius: BorderRadius.circular(AppRadius.xl),
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 12),
|
child: Padding(
|
||||||
child: Column(
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 12),
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
child: Column(
|
||||||
children: [
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
Icon(icon, size: 18, color: iconColor),
|
children: [
|
||||||
const SizedBox(height: 8),
|
Row(
|
||||||
Text(
|
children: [
|
||||||
value,
|
Icon(icon, size: 18, color: iconColor),
|
||||||
maxLines: 1,
|
const Spacer(),
|
||||||
overflow: TextOverflow.ellipsis,
|
if (onTap != null)
|
||||||
style: TextStyle(
|
const Icon(
|
||||||
color: emphasized ? AppColors.ink : AppColors.inkSoft,
|
Icons.chevron_right,
|
||||||
fontSize: emphasized ? 15 : 13,
|
size: 16,
|
||||||
fontWeight: emphasized ? FontWeight.w800 : FontWeight.w600,
|
color: AppColors.muted,
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
),
|
const SizedBox(height: 8),
|
||||||
const SizedBox(height: 4),
|
Text(
|
||||||
Text(
|
value,
|
||||||
label,
|
maxLines: 1,
|
||||||
maxLines: 1,
|
overflow: TextOverflow.ellipsis,
|
||||||
overflow: TextOverflow.ellipsis,
|
style: TextStyle(
|
||||||
style: const TextStyle(color: AppColors.inkSoft, fontSize: 11),
|
color: emphasized ? AppColors.ink : AppColors.inkSoft,
|
||||||
),
|
fontSize: emphasized ? 15 : 13,
|
||||||
],
|
fontWeight: emphasized ? FontWeight.w800 : FontWeight.w600,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Text(
|
||||||
|
label,
|
||||||
|
maxLines: 1,
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
style: const TextStyle(
|
||||||
|
color: AppColors.inkSoft,
|
||||||
|
fontSize: 11,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -54,3 +54,21 @@ String petLoadErrorMessage(ApiException? error) => switch (error) {
|
|||||||
ApiRateLimitException _ => '请求过于频繁,请稍后再试',
|
ApiRateLimitException _ => '请求过于频繁,请稍后再试',
|
||||||
_ => '加载失败,请稍后重试',
|
_ => '加载失败,请稍后重试',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// 宠物头像写入失败的文案(T3.5-09)。按契约 v1.4.0 为
|
||||||
|
/// `PATCH /pets/{petId}` 新增的两格分层:
|
||||||
|
///
|
||||||
|
/// - 404/40405:asset 不存在 / 非本人 / 已删 / **用途不符**(帖图或用户头像
|
||||||
|
/// 当宠物头像)→ 引导重新上传,而不是「宠物不存在」(40401 才是那个)。
|
||||||
|
/// - 422/42203:本人的 `pet_avatar` asset 仍在 uploading/failed → 可重试。
|
||||||
|
/// - 403/40300:viewer 改头像(入口本已按 myRole 隐藏,此处兜底防越权构造)。
|
||||||
|
/// - 409/40902:版本冲突 → 由调用方重取档案后重试。
|
||||||
|
String petAvatarSaveErrorMessage(ApiException? error) => switch (error) {
|
||||||
|
ApiBusinessException(code: ApiCodes.mediaNotFound) => '头像已失效,请重新上传',
|
||||||
|
ApiBusinessException(code: ApiCodes.mediaNotReady) => '头像还没上传完,请稍后重试',
|
||||||
|
ApiBusinessException(code: ApiCodes.petAccessDenied) => '你没有修改该宠物头像的权限',
|
||||||
|
ApiBusinessException(code: ApiCodes.versionConflict) => '档案已被更新,请重新操作',
|
||||||
|
ApiNetworkException _ => '网络异常,请检查网络后重试',
|
||||||
|
ApiRateLimitException _ => '请求过于频繁,请稍后再试',
|
||||||
|
_ => '头像保存失败,请稍后重试',
|
||||||
|
};
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter/semantics.dart';
|
import 'package:flutter/semantics.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/app_date_picker.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/pet_avatar.dart';
|
import 'package:patbond_flutter/core/widgets/pet_avatar.dart';
|
||||||
@@ -659,14 +660,19 @@ class _PetFormPageState extends State<PetFormPage> {
|
|||||||
_birthDate == null ? '未填写' : dateToJson(_birthDate!),
|
_birthDate == null ? '未填写' : dateToJson(_birthDate!),
|
||||||
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
||||||
),
|
),
|
||||||
trailing: const Icon(
|
trailing: AppDateFieldTrailing(
|
||||||
Icons.calendar_month_outlined,
|
firstDate: DateTime(1990),
|
||||||
color: AppColors.muted,
|
lastDate: DateTime.now(),
|
||||||
|
enabled: !_submitting,
|
||||||
|
onToday: (value) {
|
||||||
|
_markStarted();
|
||||||
|
setState(() => _birthDate = value);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
enabled: !_submitting,
|
enabled: !_submitting,
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
final value = await showDatePicker(
|
final value = await pickAppDate(
|
||||||
context: context,
|
context: context,
|
||||||
initialDate: _birthDate ?? DateTime(now.year - 1, now.month),
|
initialDate: _birthDate ?? DateTime(now.year - 1, now.month),
|
||||||
firstDate: DateTime(1990),
|
firstDate: DateTime(1990),
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
/// pets 域响应 / 请求模型(接口契约冻结稿 openapi.yaml v1.2.0,
|
/// pets 域响应 / 请求模型(接口契约冻结稿 openapi.yaml v1.4.0,
|
||||||
/// 字段名与后端逐字一致;枚举取值严格校验,未知值抛 [FormatException]
|
/// 字段名与后端逐字一致;枚举取值严格校验,未知值抛 [FormatException]
|
||||||
/// 以便契约漂移在测试期暴露而非静默吞掉)。
|
/// 以便契约漂移在测试期暴露而非静默吞掉)。
|
||||||
library;
|
library;
|
||||||
|
|
||||||
|
import 'package:patbond_flutter/core/models/patch_field.dart';
|
||||||
|
|
||||||
|
export 'package:patbond_flutter/core/models/patch_field.dart';
|
||||||
export 'package:patbond_flutter/core/models/cursor_page.dart';
|
export 'package:patbond_flutter/core/models/cursor_page.dart';
|
||||||
|
|
||||||
/// 物种(创建即定,不可修改)。
|
/// 物种(创建即定,不可修改)。
|
||||||
@@ -143,6 +146,7 @@ class Pet {
|
|||||||
required this.microchipNo,
|
required this.microchipNo,
|
||||||
required this.sterilizedOn,
|
required this.sterilizedOn,
|
||||||
required this.status,
|
required this.status,
|
||||||
|
required this.avatarUrl,
|
||||||
required this.myRole,
|
required this.myRole,
|
||||||
required this.createdAt,
|
required this.createdAt,
|
||||||
required this.updatedAt,
|
required this.updatedAt,
|
||||||
@@ -164,6 +168,9 @@ class Pet {
|
|||||||
microchipNo: json['microchipNo'] as String?,
|
microchipNo: json['microchipNo'] as String?,
|
||||||
sterilizedOn: _dateOrNull(json['sterilizedOn']),
|
sterilizedOn: _dateOrNull(json['sterilizedOn']),
|
||||||
status: PetStatus.fromJson(json['status'] as String),
|
status: PetStatus.fromJson(json['status'] as String),
|
||||||
|
// 契约 v1.4.0 新增:时效性预签名 GET URL,每次响应现签;
|
||||||
|
// 不得持久化、过期即重取。响应**不含 avatarAssetId**(只写不读)。
|
||||||
|
avatarUrl: json['avatarUrl'] as String?,
|
||||||
myRole: PetRole.fromJson(json['myRole'] as String),
|
myRole: PetRole.fromJson(json['myRole'] as String),
|
||||||
createdAt: DateTime.parse(json['createdAt'] as String),
|
createdAt: DateTime.parse(json['createdAt'] as String),
|
||||||
updatedAt: DateTime.parse(json['updatedAt'] as String),
|
updatedAt: DateTime.parse(json['updatedAt'] as String),
|
||||||
@@ -184,6 +191,11 @@ class Pet {
|
|||||||
final String? microchipNo;
|
final String? microchipNo;
|
||||||
final DateTime? sterilizedOn;
|
final DateTime? sterilizedOn;
|
||||||
final PetStatus status;
|
final PetStatus status;
|
||||||
|
|
||||||
|
/// 头像预签名 GET URL;无头像 / asset 非 ready / 对象存储未配置均为 null。
|
||||||
|
/// 「有头像」等价于本字段非 null(契约不外露 assetId)。
|
||||||
|
final String? avatarUrl;
|
||||||
|
|
||||||
final PetRole myRole;
|
final PetRole myRole;
|
||||||
final DateTime createdAt;
|
final DateTime createdAt;
|
||||||
final DateTime updatedAt;
|
final DateTime updatedAt;
|
||||||
@@ -230,8 +242,17 @@ class CreatePetRequest {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 更新宠物请求(部分更新:缺席字段不变;不支持清空回 null;
|
/// 更新宠物请求(部分更新:缺席字段不变;品种对整体替换;species 不可改;
|
||||||
/// 品种对整体替换;species 不可改;version 乐观锁必填)。
|
/// version 乐观锁必填)。
|
||||||
|
///
|
||||||
|
/// **两态与三态并存**:除 [avatarAssetId] 外的字段沿 M2 两态语义(缺省或
|
||||||
|
/// null 皆为「不改」,不支持清空回 null——它们的 CHECK 约束本就不允许空值);
|
||||||
|
/// [avatarAssetId] 是本 schema **唯一的三态字段**(契约 v1.4.0),因为
|
||||||
|
/// 「删掉我设的那张头像」是一等公民操作,两态根本无法表达。
|
||||||
|
///
|
||||||
|
/// **权限随本次触及的字段变档**(ADR-022 + 服务端 `requiredLevel`):
|
||||||
|
/// 只带 [avatarAssetId] 走 WRITE(owner + caregiver 均可);碰任一资料字段
|
||||||
|
/// 即 MANAGE(仅 owner);混合请求取更严的一半(防「夹带改名」)。
|
||||||
class UpdatePetRequest {
|
class UpdatePetRequest {
|
||||||
const UpdatePetRequest({
|
const UpdatePetRequest({
|
||||||
required this.version,
|
required this.version,
|
||||||
@@ -245,6 +266,7 @@ class UpdatePetRequest {
|
|||||||
this.microchipNo,
|
this.microchipNo,
|
||||||
this.sterilizedOn,
|
this.sterilizedOn,
|
||||||
this.status,
|
this.status,
|
||||||
|
this.avatarAssetId = const PatchField<String>.absent(),
|
||||||
});
|
});
|
||||||
|
|
||||||
final int version;
|
final int version;
|
||||||
@@ -259,19 +281,26 @@ class UpdatePetRequest {
|
|||||||
final DateTime? sterilizedOn;
|
final DateTime? sterilizedOn;
|
||||||
final PetStatus? status;
|
final PetStatus? status;
|
||||||
|
|
||||||
Map<String, Object?> toJson() => {
|
/// 宠物头像 asset(两步上传产物,`purpose` 须为 `pet_avatar`)。三态。
|
||||||
'version': version,
|
final PatchField<String> avatarAssetId;
|
||||||
if (name != null) 'name': name,
|
|
||||||
if (breedId != null) 'breedId': breedId,
|
Map<String, Object?> toJson() {
|
||||||
if (customBreedName != null) 'customBreedName': customBreedName,
|
final json = <String, Object?>{
|
||||||
if (sex != null) 'sex': sex!.name,
|
'version': version,
|
||||||
if (birthDate != null) 'birthDate': dateToJson(birthDate!),
|
if (name != null) 'name': name,
|
||||||
if (birthDateEstimated != null) 'birthDateEstimated': birthDateEstimated,
|
if (breedId != null) 'breedId': breedId,
|
||||||
if (personality != null) 'personality': personality,
|
if (customBreedName != null) 'customBreedName': customBreedName,
|
||||||
if (microchipNo != null) 'microchipNo': microchipNo,
|
if (sex != null) 'sex': sex!.name,
|
||||||
if (sterilizedOn != null) 'sterilizedOn': dateToJson(sterilizedOn!),
|
if (birthDate != null) 'birthDate': dateToJson(birthDate!),
|
||||||
if (status != null) 'status': status!.name,
|
if (birthDateEstimated != null) 'birthDateEstimated': birthDateEstimated,
|
||||||
};
|
if (personality != null) 'personality': personality,
|
||||||
|
if (microchipNo != null) 'microchipNo': microchipNo,
|
||||||
|
if (sterilizedOn != null) 'sterilizedOn': dateToJson(sterilizedOn!),
|
||||||
|
if (status != null) 'status': status!.name,
|
||||||
|
};
|
||||||
|
avatarAssetId.writeTo(json, 'avatarAssetId');
|
||||||
|
return json;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 品种目录项(只读字典)。
|
/// 品种目录项(只读字典)。
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:patbond_flutter/analytics/analytics_page_name.dart';
|
import 'package:patbond_flutter/analytics/analytics_page_name.dart';
|
||||||
import 'package:patbond_flutter/core/navigation/fade_route.dart';
|
import 'package:patbond_flutter/core/navigation/fade_route.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/avatar_upload_sheet.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/pet_avatar.dart';
|
import 'package:patbond_flutter/core/widgets/pet_avatar.dart';
|
||||||
@@ -25,6 +26,7 @@ class PetsPage extends StatefulWidget {
|
|||||||
super.key,
|
super.key,
|
||||||
this.analytics,
|
this.analytics,
|
||||||
this.healthAnalytics,
|
this.healthAnalytics,
|
||||||
|
this.avatarUploaderBuilder,
|
||||||
});
|
});
|
||||||
|
|
||||||
final PetsController controller;
|
final PetsController controller;
|
||||||
@@ -33,6 +35,9 @@ class PetsPage extends StatefulWidget {
|
|||||||
/// health_record 域埋点(T2-13,记录页面族透传)。
|
/// health_record 域埋点(T2-13,记录页面族透传)。
|
||||||
final HealthRecordAnalytics? healthAnalytics;
|
final HealthRecordAnalytics? healthAnalytics;
|
||||||
|
|
||||||
|
/// 头像上传编排器构造口(T3.5-09,透传详情页)。
|
||||||
|
final AvatarUploaderBuilder? avatarUploaderBuilder;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<PetsPage> createState() => _PetsPageState();
|
State<PetsPage> createState() => _PetsPageState();
|
||||||
}
|
}
|
||||||
@@ -75,6 +80,7 @@ class _PetsPageState extends State<PetsPage> {
|
|||||||
petId: pet.id,
|
petId: pet.id,
|
||||||
analytics: widget.analytics,
|
analytics: widget.analytics,
|
||||||
healthAnalytics: widget.healthAnalytics,
|
healthAnalytics: widget.healthAnalytics,
|
||||||
|
avatarUploaderBuilder: widget.avatarUploaderBuilder,
|
||||||
),
|
),
|
||||||
settings: RouteSettings(name: AnalyticsPageName.petDetail.pageName),
|
settings: RouteSettings(name: AnalyticsPageName.petDetail.pageName),
|
||||||
),
|
),
|
||||||
@@ -189,7 +195,8 @@ class _PetCard extends StatelessWidget {
|
|||||||
padding: const EdgeInsets.all(14),
|
padding: const EdgeInsets.all(14),
|
||||||
child: Row(
|
child: Row(
|
||||||
children: [
|
children: [
|
||||||
const PetAvatar(size: PetAvatarSize.lg),
|
// T3.5-09:列表卡展示真实头像(预签名 URL,无图回退爪印占位)。
|
||||||
|
PetAvatar(size: PetAvatarSize.lg, url: pet.avatarUrl),
|
||||||
const SizedBox(width: 12),
|
const SizedBox(width: 12),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Column(
|
child: Column(
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter/semantics.dart';
|
import 'package:flutter/semantics.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/app_date_picker.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/primary_button.dart';
|
import 'package:patbond_flutter/core/widgets/primary_button.dart';
|
||||||
@@ -503,14 +504,21 @@ class _VaccinationFormPageState extends State<VaccinationFormPage> {
|
|||||||
value == null ? '未选择' : dateToJson(value),
|
value == null ? '未选择' : dateToJson(value),
|
||||||
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
||||||
),
|
),
|
||||||
trailing: const Icon(
|
trailing: AppDateFieldTrailing(
|
||||||
Icons.calendar_month_outlined,
|
firstDate: DateTime(1990),
|
||||||
color: AppColors.muted,
|
lastDate: allowFuture
|
||||||
|
? DateTime(DateTime.now().year + 5)
|
||||||
|
: DateTime.now(),
|
||||||
|
enabled: !_submitting,
|
||||||
|
onToday: (value) {
|
||||||
|
_markStarted();
|
||||||
|
onPicked(value);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
enabled: !_submitting,
|
enabled: !_submitting,
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
final picked = await showDatePicker(
|
final picked = await pickAppDate(
|
||||||
context: context,
|
context: context,
|
||||||
initialDate: value ?? now,
|
initialDate: value ?? now,
|
||||||
firstDate: DateTime(1990),
|
firstDate: DateTime(1990),
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import 'package:patbond_flutter/analytics/analytics_page_name.dart';
|
|||||||
import 'package:patbond_flutter/core/navigation/fade_route.dart';
|
import 'package:patbond_flutter/core/navigation/fade_route.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/app_date_picker.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
@@ -514,9 +515,19 @@ class _CompleteVaccinationDialogState
|
|||||||
value == null ? '未选择' : dateToJson(value),
|
value == null ? '未选择' : dateToJson(value),
|
||||||
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
||||||
),
|
),
|
||||||
|
trailing: AppDateFieldTrailing(
|
||||||
|
firstDate: DateTime(1990),
|
||||||
|
lastDate: allowFuture
|
||||||
|
? DateTime(DateTime.now().year + 5)
|
||||||
|
: DateTime.now(),
|
||||||
|
onToday: (picked) => setState(() {
|
||||||
|
onPicked(picked);
|
||||||
|
_dateError = null;
|
||||||
|
}),
|
||||||
|
),
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
final picked = await showDatePicker(
|
final picked = await pickAppDate(
|
||||||
context: context,
|
context: context,
|
||||||
initialDate: value ?? now,
|
initialDate: value ?? now,
|
||||||
firstDate: DateTime(1990),
|
firstDate: DateTime(1990),
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter/semantics.dart';
|
import 'package:flutter/semantics.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/app_date_picker.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/primary_button.dart';
|
import 'package:patbond_flutter/core/widgets/primary_button.dart';
|
||||||
@@ -241,14 +242,19 @@ class _WeightFormPageState extends State<WeightFormPage> {
|
|||||||
dateToJson(_measuredDate),
|
dateToJson(_measuredDate),
|
||||||
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
style: const TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
||||||
),
|
),
|
||||||
trailing: const Icon(
|
trailing: AppDateFieldTrailing(
|
||||||
Icons.calendar_month_outlined,
|
firstDate: DateTime(1990),
|
||||||
color: AppColors.muted,
|
lastDate: DateTime.now(),
|
||||||
|
enabled: !_submitting,
|
||||||
|
onToday: (value) {
|
||||||
|
_markStarted();
|
||||||
|
setState(() => _measuredDate = value);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
enabled: !_submitting,
|
enabled: !_submitting,
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
final value = await showDatePicker(
|
final value = await pickAppDate(
|
||||||
context: context,
|
context: context,
|
||||||
initialDate: _measuredDate,
|
initialDate: _measuredDate,
|
||||||
firstDate: DateTime(1990),
|
firstDate: DateTime(1990),
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,137 @@
|
|||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/auth_repository.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_repository.dart';
|
||||||
|
|
||||||
|
/// 资料主链路四态(沿 pets/community 两域惯例)。
|
||||||
|
enum ProfileLoadPhase { initial, loading, ready, error }
|
||||||
|
|
||||||
|
/// 统计块的独立三态:`/me` 成功而统计失败时**只降级这一块**,不把整页
|
||||||
|
/// 打成 error——昵称与头像已经拿到了,为两个数字丢掉整页是过度反应。
|
||||||
|
enum ProfileStatsPhase { loading, ready, error }
|
||||||
|
|
||||||
|
/// 「我的资料」状态控制器(T3.5-08)。
|
||||||
|
///
|
||||||
|
/// 数据源三处:
|
||||||
|
/// - `GET /api/v1/me` → 昵称 / 头像(主链路,失败即页面 error 态)
|
||||||
|
/// - `GET /api/v1/me/community-stats` → 获赞总数 / 作品数
|
||||||
|
/// - `GET /api/v1/users/{me}/follow-stats` → 粉丝数 / 关注数
|
||||||
|
///
|
||||||
|
/// 首页问候语(T3.5-10)与资料页共用本控制器的 [displayName],
|
||||||
|
/// 保证两处展示名同源、改昵称后一起变(一次 `/me` 供两个消费点)。
|
||||||
|
class ProfileController extends ChangeNotifier {
|
||||||
|
ProfileController({
|
||||||
|
required AuthRepository authRepository,
|
||||||
|
required CommunityRepository communityRepository,
|
||||||
|
}) : _auth = authRepository,
|
||||||
|
_community = communityRepository;
|
||||||
|
|
||||||
|
final AuthRepository _auth;
|
||||||
|
final CommunityRepository _community;
|
||||||
|
|
||||||
|
ProfileLoadPhase _phase = ProfileLoadPhase.initial;
|
||||||
|
ProfileStatsPhase _statsPhase = ProfileStatsPhase.loading;
|
||||||
|
UserProfile? _profile;
|
||||||
|
CommunityStats? _communityStats;
|
||||||
|
FollowStats? _followStats;
|
||||||
|
ApiException? _lastError;
|
||||||
|
bool _disposed = false;
|
||||||
|
|
||||||
|
ProfileLoadPhase get phase => _phase;
|
||||||
|
ProfileStatsPhase get statsPhase => _statsPhase;
|
||||||
|
|
||||||
|
/// 本人资料(ready 态非 null)。
|
||||||
|
UserProfile? get profile => _profile;
|
||||||
|
|
||||||
|
CommunityStats? get communityStats => _communityStats;
|
||||||
|
FollowStats? get followStats => _followStats;
|
||||||
|
ApiException? get lastError => _lastError;
|
||||||
|
|
||||||
|
/// 展示名 `nickname ?? username`;资料未到手时为 null(调用方不造假名)。
|
||||||
|
String? get displayName => _profile?.displayName;
|
||||||
|
|
||||||
|
/// 加载 / 重试。主链路错误收敛为 error 态供页面渲染 + 重试,不外抛。
|
||||||
|
///
|
||||||
|
/// 已有资料副本时刷新失败**保留副本**(`_phase` 停在 ready):与详情页
|
||||||
|
/// 「有副本即不打断阅读」的既有取舍一致。
|
||||||
|
Future<void> refresh() async {
|
||||||
|
if (_profile == null) {
|
||||||
|
_phase = ProfileLoadPhase.loading;
|
||||||
|
}
|
||||||
|
_lastError = null;
|
||||||
|
_notify();
|
||||||
|
try {
|
||||||
|
_profile = await _auth.me();
|
||||||
|
_phase = ProfileLoadPhase.ready;
|
||||||
|
} on ApiException catch (error) {
|
||||||
|
_lastError = error;
|
||||||
|
if (_profile == null) {
|
||||||
|
_phase = ProfileLoadPhase.error;
|
||||||
|
_notify();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_notify();
|
||||||
|
await refreshStats();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 只重取两块统计(统计块的「重试」入口;主链路资料不动)。
|
||||||
|
Future<void> refreshStats() async {
|
||||||
|
final userId = _profile?.userId;
|
||||||
|
if (userId == null) return;
|
||||||
|
_statsPhase = ProfileStatsPhase.loading;
|
||||||
|
_notify();
|
||||||
|
try {
|
||||||
|
// 顺序取(同一服务、量级极小):任一失败即整块 error。两个数字并列
|
||||||
|
// 在同一张卡上,只有一半是数字、另一半是「—」比整块「加载失败 +
|
||||||
|
// 重试」更费解。
|
||||||
|
final community = await _community.getMyCommunityStats();
|
||||||
|
final follow = await _community.getFollowStats(userId);
|
||||||
|
_communityStats = community;
|
||||||
|
_followStats = follow;
|
||||||
|
_statsPhase = ProfileStatsPhase.ready;
|
||||||
|
} on ApiException {
|
||||||
|
_statsPhase = ProfileStatsPhase.error;
|
||||||
|
}
|
||||||
|
_notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 提交资料变更(三态语义见 [UpdateMeRequest])。
|
||||||
|
///
|
||||||
|
/// **空 patch 直接短路不发请求**:服务端对空 patch 答 400/40000(刻意不
|
||||||
|
/// 静默 200),客户端没有理由去撞这一枪。
|
||||||
|
/// 成功即以服务端回显的全量资料替换本地副本(`avatarUrl` 现签,
|
||||||
|
/// 不做本地拼装)。失败按类型化异常外抛给编辑页分层呈现。
|
||||||
|
Future<UserProfile?> save(UpdateMeRequest request) async {
|
||||||
|
if (request.isEmpty) return _profile;
|
||||||
|
final updated = await _auth.updateMe(request);
|
||||||
|
_profile = updated;
|
||||||
|
_phase = ProfileLoadPhase.ready;
|
||||||
|
_notify();
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 登出清空:回 initial 并清资料,避免上一账号昵称/头像跨会话泄漏
|
||||||
|
/// (沿 `PetsController.reset` 先例)。
|
||||||
|
void reset() {
|
||||||
|
_phase = ProfileLoadPhase.initial;
|
||||||
|
_statsPhase = ProfileStatsPhase.loading;
|
||||||
|
_profile = null;
|
||||||
|
_communityStats = null;
|
||||||
|
_followStats = null;
|
||||||
|
_lastError = null;
|
||||||
|
_notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _notify() {
|
||||||
|
if (!_disposed) notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_disposed = true;
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
|
||||||
|
/// 资料页/编辑页的错误文案映射(沿 `petLoadErrorMessage` 先例:
|
||||||
|
/// 服务端原始 message 一律不上屏)。
|
||||||
|
String profileLoadErrorMessage(ApiException? error) => switch (error) {
|
||||||
|
ApiNetworkException _ => '网络异常,请检查网络后重试',
|
||||||
|
ApiRateLimitException _ => '请求过于频繁,请稍后再试',
|
||||||
|
_ => '加载失败,请稍后重试',
|
||||||
|
};
|
||||||
|
|
||||||
|
/// 保存资料的失败文案,按契约 v1.4.0 的 `PATCH /api/v1/me` 错误谱分层:
|
||||||
|
///
|
||||||
|
/// | 码 | 成因 | 文案取向 |
|
||||||
|
/// | --- | --- | --- |
|
||||||
|
/// | 40000 | 昵称长度/纯空白、空 patch、非法 UUID | 指向输入本身可改 |
|
||||||
|
/// | 40405 | 头像 asset 不存在/非本人/已删/用途不符 | 引导重新上传 |
|
||||||
|
/// | 42203 | 本人头像 asset 仍在 uploading/failed | 明说「还没传完」,可重试 |
|
||||||
|
///
|
||||||
|
/// 40000 的三种成因在客户端已各自前置拦截(长度校验 / 空 patch 短路 /
|
||||||
|
/// assetId 来自服务端),真收到 40000 说明校验与服务端有偏差,
|
||||||
|
/// 故文案不写死「昵称」,只指向「填写内容」。
|
||||||
|
String profileSaveErrorMessage(ApiException? error) => switch (error) {
|
||||||
|
ApiBusinessException(code: ApiCodes.paramError) => '填写内容不符合要求,请检查后重试',
|
||||||
|
ApiBusinessException(code: ApiCodes.mediaNotFound) => '头像已失效,请重新上传',
|
||||||
|
ApiBusinessException(code: ApiCodes.mediaNotReady) => '头像还没上传完,请稍后重试',
|
||||||
|
ApiNetworkException _ => '网络异常,请检查网络后重试',
|
||||||
|
ApiRateLimitException _ => '请求过于频繁,请稍后再试',
|
||||||
|
_ => '保存失败,请稍后重试',
|
||||||
|
};
|
||||||
|
|
||||||
|
/// 昵称的客户端校验,与后端 `ck_users_nickname`(btrim + 1~32)对齐。
|
||||||
|
///
|
||||||
|
/// **长度按码点计**(`String.characters` 的等价物 `runes`):数据库
|
||||||
|
/// `char_length` 数的是码点,若按 UTF-16 `String.length` 校验,32 个 emoji
|
||||||
|
/// 的合法昵称(UTF-16 长度 64)会被客户端误拒。
|
||||||
|
///
|
||||||
|
/// 返回 null 即通过;非空即字段级 errorText。
|
||||||
|
String? validateNickname(String raw) {
|
||||||
|
// btrim 对齐:服务端先 btrim 再判长度,故校验也先 trim。
|
||||||
|
final trimmed = raw.trim();
|
||||||
|
if (trimmed.isEmpty) {
|
||||||
|
// 纯空白是 400/40000 而非隐式清空——清空走「清除昵称」显式入口。
|
||||||
|
return '昵称不能为空,如需清除请用下方「清除昵称」';
|
||||||
|
}
|
||||||
|
if (trimmed.runes.length > 32) return '昵称最多 32 个字';
|
||||||
|
return null;
|
||||||
|
}
|
||||||
@@ -0,0 +1,335 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/app_text_field.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/avatar_upload_sheet.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/primary_button.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_display.dart';
|
||||||
|
import 'package:patbond_flutter/widgets/common.dart';
|
||||||
|
|
||||||
|
/// 资料编辑页(T3.5-08):昵称 + 头像的唯一写入口。
|
||||||
|
///
|
||||||
|
/// **PATCH 三态在本页成形**(契约 v1.4.0 的 `UpdateMeRequest`):
|
||||||
|
///
|
||||||
|
/// | 用户动作 | 提交表现 |
|
||||||
|
/// | --- | --- |
|
||||||
|
/// | 没碰昵称 | `nickname` **键不出现**(不改) |
|
||||||
|
/// | 改了昵称 | `nickname: "新值"` |
|
||||||
|
/// | 点「清除昵称」 | `nickname: null`(清空) |
|
||||||
|
/// | 没碰头像 | `avatarAssetId` **键不出现** |
|
||||||
|
/// | 传了新头像 | `avatarAssetId: "<ready assetId>"` |
|
||||||
|
/// | 点「清除头像」 | `avatarAssetId: null` |
|
||||||
|
///
|
||||||
|
/// 「不改」与「清空」在 JSON 上是**键缺省 vs 显式 null** 两回事:若把未改
|
||||||
|
/// 字段也发成 null,用户只改昵称就会连头像一起被清掉。故本页不维护
|
||||||
|
/// 「当前值 → 直接整体提交」的表单模型,而是维护**三态意图**([PatchField]),
|
||||||
|
/// 与初值比对后只让真正变化的字段落键。
|
||||||
|
class ProfileEditPage extends StatefulWidget {
|
||||||
|
const ProfileEditPage({
|
||||||
|
required this.controller,
|
||||||
|
super.key,
|
||||||
|
this.avatarUploaderBuilder,
|
||||||
|
});
|
||||||
|
|
||||||
|
final ProfileController controller;
|
||||||
|
|
||||||
|
/// 头像上传编排器构造口;null 即本次构建未装配上传能力,隐藏头像入口
|
||||||
|
/// (生产装配恒注入,见 `app.dart`)。
|
||||||
|
final AvatarUploaderBuilder? avatarUploaderBuilder;
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<ProfileEditPage> createState() => _ProfileEditPageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ProfileEditPageState extends State<ProfileEditPage> {
|
||||||
|
late final TextEditingController _nickname;
|
||||||
|
|
||||||
|
/// 页面进入时的资料快照:差量比对的基准(服务端回显的权威值)。
|
||||||
|
late final UserProfile _initial;
|
||||||
|
|
||||||
|
/// 昵称的三态意图:null 表示「跟随输入框与初值的比对结果」,
|
||||||
|
/// 非 null 表示用户已显式点过「清除昵称」。
|
||||||
|
bool _nicknameCleared = false;
|
||||||
|
|
||||||
|
/// 头像的三态意图(absent / clear / 新 assetId)。
|
||||||
|
PatchField<String> _avatarIntent = const PatchField<String>.absent();
|
||||||
|
|
||||||
|
/// 新上传头像的本地预览(仅本页会话内有效,不落盘)。
|
||||||
|
String? _pendingAvatarUrl;
|
||||||
|
|
||||||
|
String? _nicknameError;
|
||||||
|
bool _saving = false;
|
||||||
|
ApiException? _saveError;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_initial = widget.controller.profile!;
|
||||||
|
// **预填只用 DB 原值**,不用 displayName:若把回退出来的 username
|
||||||
|
// 预填进来,用户会以为自己设过昵称,一保存就把展示约定固化成数据
|
||||||
|
// (服务端 /me 刻意不回退,正是为了留住这个区别)。
|
||||||
|
_nickname = TextEditingController(text: _initial.nickname ?? '');
|
||||||
|
_nickname.addListener(_onNicknameChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_nickname.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onNicknameChanged() {
|
||||||
|
// 每次输入都重建:保存钮的可用性由 `_hasChanges` 现算,不重建就会出现
|
||||||
|
// 「已经打了字但保存钮还是灰的」。同时用户重新打字即撤销「清除」意图,
|
||||||
|
// 并清掉上一次的字段级错误。
|
||||||
|
setState(() {
|
||||||
|
if (_nicknameCleared && _nickname.text.isNotEmpty) {
|
||||||
|
_nicknameCleared = false;
|
||||||
|
}
|
||||||
|
_nicknameError = null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 昵称三态:显式清除 > 与初值不同即设置 > 否则不改。
|
||||||
|
PatchField<String> get _nicknamePatch {
|
||||||
|
if (_nicknameCleared) {
|
||||||
|
// 本来就没有昵称时「清除」是空操作,不落键(避免制造空 patch 之外
|
||||||
|
// 的无意义写入)。
|
||||||
|
return _initial.nickname == null
|
||||||
|
? const PatchField<String>.absent()
|
||||||
|
: const PatchField<String>.clear();
|
||||||
|
}
|
||||||
|
final trimmed = _nickname.text.trim();
|
||||||
|
if (trimmed.isEmpty) return const PatchField<String>.absent();
|
||||||
|
if (trimmed == _initial.nickname) return const PatchField<String>.absent();
|
||||||
|
return PatchField<String>.value(trimmed);
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateMeRequest get _request =>
|
||||||
|
UpdateMeRequest(nickname: _nicknamePatch, avatarAssetId: _avatarIntent);
|
||||||
|
|
||||||
|
bool get _hasChanges => !_request.isEmpty;
|
||||||
|
|
||||||
|
/// 当前应展示的头像:新传的 > 清除意图(无图) > 服务端现值。
|
||||||
|
String? get _shownAvatarUrl {
|
||||||
|
if (_avatarIntent.isClear) return null;
|
||||||
|
return _pendingAvatarUrl ?? _initial.avatarUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get _hasAvatarNow => _shownAvatarUrl != null;
|
||||||
|
|
||||||
|
Future<void> _pickAvatar() async {
|
||||||
|
final builder = widget.avatarUploaderBuilder;
|
||||||
|
if (builder == null) return;
|
||||||
|
final assetId = await showAvatarUploadSheet(
|
||||||
|
context,
|
||||||
|
builder: builder,
|
||||||
|
purpose: MediaPurpose.userAvatar,
|
||||||
|
);
|
||||||
|
if (assetId == null || !mounted) return;
|
||||||
|
setState(() {
|
||||||
|
_avatarIntent = PatchField<String>.value(assetId);
|
||||||
|
// 预览沿用上传器给的可访问 URL?—— 没有:complete 响应的 url 与
|
||||||
|
// /me 的 avatarUrl 同为预签名,但本页不缓存它,改为保存后由服务端
|
||||||
|
// 回显。上传成功到保存之间以「已选择新头像」文案示意。
|
||||||
|
_pendingAvatarUrl = null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _clearAvatar() {
|
||||||
|
setState(() {
|
||||||
|
_avatarIntent = const PatchField<String>.clear();
|
||||||
|
_pendingAvatarUrl = null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _clearNickname() {
|
||||||
|
setState(() {
|
||||||
|
_nicknameCleared = true;
|
||||||
|
_nicknameError = null;
|
||||||
|
_nickname.clear();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _save() async {
|
||||||
|
// 有内容的昵称才校验:空输入框意为「不改」(清空走显式入口)。
|
||||||
|
final raw = _nickname.text;
|
||||||
|
if (!_nicknameCleared && raw.trim().isNotEmpty) {
|
||||||
|
final error = validateNickname(raw);
|
||||||
|
if (error != null) {
|
||||||
|
setState(() => _nicknameError = error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
final request = _request;
|
||||||
|
if (request.isEmpty) {
|
||||||
|
// 空 patch 服务端答 400/40000(刻意不静默 200);客户端不去撞这一枪。
|
||||||
|
Navigator.of(context).pop(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setState(() {
|
||||||
|
_saving = true;
|
||||||
|
_saveError = null;
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await widget.controller.save(request);
|
||||||
|
if (!mounted) return;
|
||||||
|
Navigator.of(context).pop(true);
|
||||||
|
} on ApiException catch (error) {
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() {
|
||||||
|
_saving = false;
|
||||||
|
_saveError = error;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final canUploadAvatar = widget.avatarUploaderBuilder != null;
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: const Text('编辑资料')),
|
||||||
|
body: ListView(
|
||||||
|
padding: const EdgeInsets.fromLTRB(20, 12, 20, 30),
|
||||||
|
children: [
|
||||||
|
if (_saveError != null) ...[
|
||||||
|
InlineErrorBanner(message: profileSaveErrorMessage(_saveError)),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
],
|
||||||
|
if (canUploadAvatar) ...[
|
||||||
|
Center(
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
_AvatarPreview(
|
||||||
|
url: _shownAvatarUrl,
|
||||||
|
pendingAssetId: _avatarIntent.valueOrNull,
|
||||||
|
onTap: _saving ? null : _pickAvatar,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 10),
|
||||||
|
TextButton.icon(
|
||||||
|
onPressed: _saving ? null : _pickAvatar,
|
||||||
|
icon: const Icon(Icons.photo_camera_outlined, size: 18),
|
||||||
|
label: const Text('更换头像'),
|
||||||
|
),
|
||||||
|
if (_hasAvatarNow)
|
||||||
|
TextButton(
|
||||||
|
onPressed: _saving ? null : _clearAvatar,
|
||||||
|
child: const Text(
|
||||||
|
'清除头像',
|
||||||
|
style: TextStyle(color: AppColors.errorDark),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
if (_avatarIntent.isClear)
|
||||||
|
const Text(
|
||||||
|
'保存后将移除头像',
|
||||||
|
style: TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 20),
|
||||||
|
],
|
||||||
|
AppTextField(
|
||||||
|
label: '昵称',
|
||||||
|
controller: _nickname,
|
||||||
|
enabled: !_saving,
|
||||||
|
errorText: _nicknameError,
|
||||||
|
// 未设昵称时点明「现在别人看到的是用户名」——展示回退是客户端
|
||||||
|
// 行为,不写进输入框(否则一保存就变成真昵称)。
|
||||||
|
helperText: _initial.nickname == null
|
||||||
|
? '未设置,当前展示为用户名「${_initial.username}」'
|
||||||
|
: '1~32 个字',
|
||||||
|
textInputAction: TextInputAction.done,
|
||||||
|
onSubmitted: (_) => _saving ? null : _save(),
|
||||||
|
),
|
||||||
|
if (_initial.nickname != null)
|
||||||
|
Align(
|
||||||
|
alignment: Alignment.centerLeft,
|
||||||
|
child: TextButton(
|
||||||
|
onPressed: _saving ? null : _clearNickname,
|
||||||
|
child: const Text(
|
||||||
|
'清除昵称',
|
||||||
|
style: TextStyle(color: AppColors.errorDark),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
if (_nicknameCleared)
|
||||||
|
const Text(
|
||||||
|
'保存后将清除昵称,展示名回退为用户名',
|
||||||
|
style: TextStyle(color: AppColors.inkSoft, fontSize: 12),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
PrimaryButton(
|
||||||
|
label: '保存',
|
||||||
|
isLoading: _saving,
|
||||||
|
// 无变更时禁用:避免用户以为「点了保存但什么都没发生」。
|
||||||
|
onPressed: _hasChanges ? _save : null,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 编辑页头像预览:有 URL 走 [RemoteImage](缓存 key 已剥签名参数);
|
||||||
|
/// 刚上传但尚未保存时无可用 URL,以「已选择新头像」占位说明。
|
||||||
|
class _AvatarPreview extends StatelessWidget {
|
||||||
|
const _AvatarPreview({
|
||||||
|
required this.url,
|
||||||
|
required this.pendingAssetId,
|
||||||
|
this.onTap,
|
||||||
|
});
|
||||||
|
|
||||||
|
final String? url;
|
||||||
|
final String? pendingAssetId;
|
||||||
|
final VoidCallback? onTap;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final Widget content;
|
||||||
|
if (url != null) {
|
||||||
|
content = RemoteImage(
|
||||||
|
url: url!,
|
||||||
|
width: 96,
|
||||||
|
height: 96,
|
||||||
|
borderRadius: BorderRadius.circular(48),
|
||||||
|
);
|
||||||
|
} else if (pendingAssetId != null) {
|
||||||
|
content = const ColoredBox(
|
||||||
|
color: AppColors.surfaceTint,
|
||||||
|
child: Center(
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Icon(Icons.check_circle, color: AppColors.primary, size: 24),
|
||||||
|
SizedBox(height: 4),
|
||||||
|
Text(
|
||||||
|
'已选择\n新头像',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: TextStyle(color: AppColors.primaryDark, fontSize: 10),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
content = const ColoredBox(
|
||||||
|
color: AppColors.surfaceTint,
|
||||||
|
child: Icon(Icons.person_outline, color: AppColors.muted, size: 40),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return Semantics(
|
||||||
|
label: '更换头像',
|
||||||
|
button: onTap != null,
|
||||||
|
child: InkWell(
|
||||||
|
onTap: onTap,
|
||||||
|
customBorder: const CircleBorder(),
|
||||||
|
child: SizedBox(width: 96, height: 96, child: ClipOval(child: content)),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,49 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
import 'package:patbond_flutter/data/demo_data.dart';
|
import 'package:patbond_flutter/core/widgets/avatar_upload_sheet.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_display.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_edit_page.dart';
|
||||||
import 'package:patbond_flutter/state/app_state.dart';
|
import 'package:patbond_flutter/state/app_state.dart';
|
||||||
import 'package:patbond_flutter/widgets/common.dart';
|
import 'package:patbond_flutter/widgets/common.dart';
|
||||||
|
|
||||||
class ProfilePage extends StatelessWidget {
|
/// 「我的资料」Tab(T3.5-08 真实化)。
|
||||||
const ProfilePage({required this.appState, super.key, this.onLogout});
|
///
|
||||||
|
/// 头部三项(展示名 / 头像 / 四个数字)自此全部来自服务端:
|
||||||
|
/// `GET /me`(昵称与头像)+ `GET /me/community-stats`(获赞 / 作品)+
|
||||||
|
/// `GET /users/{me}/follow-stats`(粉丝 / 关注)。M3 之前的硬编码
|
||||||
|
/// 「萌宠新手(豆豆家长)」/「24 / 1.8k / 2」已退役。
|
||||||
|
///
|
||||||
|
/// 四态:loading(居中转圈)/ error(横幅 + 重试)/ ready。**没有独立空态**
|
||||||
|
/// ——任何已认证用户都有资料,`/me/community-stats` 契约上「永不 404、空数据
|
||||||
|
/// 返回 0」,故「新用户什么都没有」的形态就是 ready 态里的一排 0,
|
||||||
|
/// 而不是另一个页面态。统计块另有独立三态(见 [ProfileStatsPhase])。
|
||||||
|
class ProfilePage extends StatefulWidget {
|
||||||
|
const ProfilePage({
|
||||||
|
required this.appState,
|
||||||
|
required this.controller,
|
||||||
|
super.key,
|
||||||
|
this.avatarUploaderBuilder,
|
||||||
|
this.onLogout,
|
||||||
|
});
|
||||||
|
|
||||||
final AppState appState;
|
final AppState appState;
|
||||||
|
|
||||||
|
/// 资料与统计数据源(Tab 级单例,`app.dart` 装配注入;首页问候语同源)。
|
||||||
|
final ProfileController controller;
|
||||||
|
|
||||||
|
/// 头像上传编排器构造口(透传编辑页)。
|
||||||
|
final AvatarUploaderBuilder? avatarUploaderBuilder;
|
||||||
|
|
||||||
/// 真实退出登录入口;未接线时保持演示提示。
|
/// 真实退出登录入口;未接线时保持演示提示。
|
||||||
final Future<void> Function()? onLogout;
|
final Future<void> Function()? onLogout;
|
||||||
|
|
||||||
|
/// 刻意保留的 demo 入口(ADR-022 未纳入本迭代):预约订单与健康卡包属
|
||||||
|
/// M5 服务域;地址定位需外部服务;设置与关于待有实际可设项。
|
||||||
|
/// 「我的收藏与草稿」的后端能力已就位(`/me/bookmarks`、`/me/posts`),
|
||||||
|
/// 但列表页本单未做(见 05 号报告遗留),故仍走演示提示。
|
||||||
static const menuItems = [
|
static const menuItems = [
|
||||||
(Icons.assignment_outlined, '我的预约订单', '查看进行中与历史服务'),
|
(Icons.assignment_outlined, '我的预约订单', '查看进行中与历史服务'),
|
||||||
(Icons.bookmarks_outlined, '我的收藏与草稿', '已保存的宠物作品与攻略'),
|
(Icons.bookmarks_outlined, '我的收藏与草稿', '已保存的宠物作品与攻略'),
|
||||||
@@ -20,18 +52,63 @@ class ProfilePage extends StatelessWidget {
|
|||||||
(Icons.settings_outlined, '设置与关于', '隐私设置与版本信息'),
|
(Icons.settings_outlined, '设置与关于', '隐私设置与版本信息'),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<ProfilePage> createState() => _ProfilePageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ProfilePageState extends State<ProfilePage> {
|
||||||
|
ProfileController get _controller => widget.controller;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
// 主壳挂载即预取(IndexedStack 各 Tab 同时构建,沿 pets/home 先例);
|
||||||
|
// 重登后控制器已 reset 回 initial,会重新拉取。首页问候语也消费这一次。
|
||||||
|
//
|
||||||
|
// **预取推到帧末**:`refresh()` 起手就同步 notify 一次(切 loading 态),
|
||||||
|
// 而同一控制器的另一个监听者(首页问候语)在本页 initState 时已构建完成
|
||||||
|
// ——在 initState 里同步通知它会命中 Flutter「build 期间 setState」断言。
|
||||||
|
// pets/home 各自只有自己一个监听者,故它们在 initState 里直取无妨。
|
||||||
|
if (_controller.phase == ProfileLoadPhase.initial) {
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (mounted && _controller.phase == ProfileLoadPhase.initial) {
|
||||||
|
_controller.refresh();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void showDemoMessage(BuildContext context, String name) {
|
void showDemoMessage(BuildContext context, String name) {
|
||||||
ScaffoldMessenger.of(
|
ScaffoldMessenger.of(
|
||||||
context,
|
context,
|
||||||
).showSnackBar(SnackBar(content: Text('「$name」功能为演示入口')));
|
).showSnackBar(SnackBar(content: Text('「$name」功能为演示入口')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _openEdit() async {
|
||||||
|
if (_controller.profile == null) return;
|
||||||
|
final saved = await Navigator.of(context).push<bool>(
|
||||||
|
MaterialPageRoute<bool>(
|
||||||
|
builder: (_) => ProfileEditPage(
|
||||||
|
controller: _controller,
|
||||||
|
avatarUploaderBuilder: widget.avatarUploaderBuilder,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (saved == true && mounted) {
|
||||||
|
ScaffoldMessenger.of(
|
||||||
|
context,
|
||||||
|
).showSnackBar(const SnackBar(content: Text('资料已更新')));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 刻意保留的 demo 家具:AppState 仍承载首页天气/本地服务的演示数据,
|
||||||
|
/// 这个入口是它唯一的复位口(不影响服务端真实资料)。
|
||||||
Future<void> reset(BuildContext context) async {
|
Future<void> reset(BuildContext context) async {
|
||||||
final confirmed = await showDialog<bool>(
|
final confirmed = await showDialog<bool>(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (context) => AlertDialog(
|
builder: (context) => AlertDialog(
|
||||||
title: const Text('恢复演示数据'),
|
title: const Text('恢复演示数据'),
|
||||||
content: const Text('宠物资料、疫苗记录以及新增帖子都会恢复为初始状态。'),
|
content: const Text('首页天气与本地服务的演示内容会恢复为初始状态(不影响账号资料与宠物档案)。'),
|
||||||
actions: [
|
actions: [
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () => Navigator.pop(context, false),
|
onPressed: () => Navigator.pop(context, false),
|
||||||
@@ -45,7 +122,7 @@ class ProfilePage extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
if (confirmed == true) {
|
if (confirmed == true) {
|
||||||
await appState.resetDemoData();
|
await widget.appState.resetDemoData();
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
ScaffoldMessenger.of(
|
ScaffoldMessenger.of(
|
||||||
context,
|
context,
|
||||||
@@ -56,6 +133,43 @@ class ProfilePage extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
return ListenableBuilder(
|
||||||
|
listenable: _controller,
|
||||||
|
builder: (context, _) {
|
||||||
|
switch (_controller.phase) {
|
||||||
|
case ProfileLoadPhase.initial:
|
||||||
|
case ProfileLoadPhase.loading:
|
||||||
|
return const Center(child: CircularProgressIndicator());
|
||||||
|
case ProfileLoadPhase.error:
|
||||||
|
return Center(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(24),
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
InlineErrorBanner(
|
||||||
|
message: profileLoadErrorMessage(_controller.lastError),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: _controller.refresh,
|
||||||
|
child: const Text('重试'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
case ProfileLoadPhase.ready:
|
||||||
|
return RefreshIndicator(
|
||||||
|
onRefresh: _controller.refresh,
|
||||||
|
child: _content(_controller.profile!),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _content(UserProfile profile) {
|
||||||
return ListView(
|
return ListView(
|
||||||
padding: const EdgeInsets.fromLTRB(16, 16, 16, 30),
|
padding: const EdgeInsets.fromLTRB(16, 16, 16, 30),
|
||||||
children: [
|
children: [
|
||||||
@@ -67,40 +181,38 @@ class ProfilePage extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
RemoteImage(
|
_ProfileAvatar(url: profile.avatarUrl, onTap: _openEdit),
|
||||||
url: userAvatar,
|
|
||||||
width: 82,
|
|
||||||
height: 82,
|
|
||||||
borderRadius: BorderRadius.circular(41),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
const Text(
|
Text(
|
||||||
'萌宠新手(豆豆家长)',
|
// 展示回退在客户端:`nickname ?? username`(服务端 /me 返回
|
||||||
style: TextStyle(
|
// DB 原值不回退,见 UserProfile.displayName 的理由)。
|
||||||
|
profile.displayName,
|
||||||
|
style: const TextStyle(
|
||||||
color: Colors.white,
|
color: Colors.white,
|
||||||
fontSize: 17,
|
fontSize: 17,
|
||||||
fontWeight: FontWeight.w800,
|
fontWeight: FontWeight.w800,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 5),
|
const SizedBox(height: 5),
|
||||||
const Text(
|
Text(
|
||||||
'Patbond 社区创作达人',
|
'@${profile.username}',
|
||||||
style: TextStyle(color: AppColors.accent, fontSize: 12),
|
style: const TextStyle(color: AppColors.accent, fontSize: 12),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 20),
|
const SizedBox(height: 12),
|
||||||
|
OutlinedButton.icon(
|
||||||
|
style: OutlinedButton.styleFrom(
|
||||||
|
foregroundColor: Colors.white,
|
||||||
|
side: const BorderSide(color: Colors.white38),
|
||||||
|
visualDensity: VisualDensity.compact,
|
||||||
|
),
|
||||||
|
onPressed: _openEdit,
|
||||||
|
icon: const Icon(Icons.edit_outlined, size: 16),
|
||||||
|
label: const Text('编辑资料'),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
const Divider(color: Colors.white24),
|
const Divider(color: Colors.white24),
|
||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
Row(
|
_statsRow(),
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
||||||
children: [
|
|
||||||
const _ProfileStat(value: '24', label: '关注我'),
|
|
||||||
const _ProfileStat(value: '1.8k', label: '获赞'),
|
|
||||||
_ProfileStat(
|
|
||||||
value: '${appState.posts.length}',
|
|
||||||
label: '我的作品',
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -108,7 +220,7 @@ class ProfilePage extends StatelessWidget {
|
|||||||
SectionCard(
|
SectionCard(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 6),
|
padding: const EdgeInsets.symmetric(vertical: 6),
|
||||||
child: Column(
|
child: Column(
|
||||||
children: menuItems.map((item) {
|
children: ProfilePage.menuItems.map((item) {
|
||||||
return ListTile(
|
return ListTile(
|
||||||
leading: CircleAvatar(
|
leading: CircleAvatar(
|
||||||
backgroundColor: AppColors.surfaceTint,
|
backgroundColor: AppColors.surfaceTint,
|
||||||
@@ -136,12 +248,112 @@ class ProfilePage extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: onLogout ?? () => showDemoMessage(context, '退出登录'),
|
onPressed: widget.onLogout ?? () => showDemoMessage(context, '退出登录'),
|
||||||
child: const Text('切换账号或退出登录'),
|
child: const Text('切换账号或退出登录'),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 统计块三态:loading 占位「—」+ 转圈 / error 提示 + 重试 / ready 实数。
|
||||||
|
///
|
||||||
|
/// 数字**不做 1.8k 式压缩**:获赞总数要能与帖子详情的 likeCount 逐一对上
|
||||||
|
/// (同一口径,含自赞),压缩会让「对不上」变成常态。
|
||||||
|
Widget _statsRow() {
|
||||||
|
switch (_controller.statsPhase) {
|
||||||
|
case ProfileStatsPhase.loading:
|
||||||
|
return const SizedBox(
|
||||||
|
height: 46,
|
||||||
|
child: Center(
|
||||||
|
child: SizedBox(
|
||||||
|
width: 20,
|
||||||
|
height: 20,
|
||||||
|
child: CircularProgressIndicator(strokeWidth: 2),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
case ProfileStatsPhase.error:
|
||||||
|
return SizedBox(
|
||||||
|
height: 46,
|
||||||
|
child: Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
const Text(
|
||||||
|
'统计加载失败',
|
||||||
|
style: TextStyle(color: Colors.white70, fontSize: 12),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: _controller.refreshStats,
|
||||||
|
child: const Text(
|
||||||
|
'重试',
|
||||||
|
style: TextStyle(color: AppColors.accent),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
case ProfileStatsPhase.ready:
|
||||||
|
final follow = _controller.followStats;
|
||||||
|
final community = _controller.communityStats;
|
||||||
|
return Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||||
|
children: [
|
||||||
|
_ProfileStat(value: '${follow?.followerCount ?? 0}', label: '关注我'),
|
||||||
|
_ProfileStat(value: '${follow?.followingCount ?? 0}', label: '我关注'),
|
||||||
|
_ProfileStat(
|
||||||
|
value: '${community?.receivedLikeCount ?? 0}',
|
||||||
|
label: '获赞',
|
||||||
|
),
|
||||||
|
_ProfileStat(
|
||||||
|
value: '${community?.publishedPostCount ?? 0}',
|
||||||
|
label: '我的作品',
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 资料页头像:有 URL 走 [RemoteImage](缓存 key 剥签名参数,URL 不落盘);
|
||||||
|
/// 无头像时本地占位(不显示破图)。
|
||||||
|
class _ProfileAvatar extends StatelessWidget {
|
||||||
|
const _ProfileAvatar({required this.url, required this.onTap});
|
||||||
|
|
||||||
|
final String? url;
|
||||||
|
final VoidCallback onTap;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Semantics(
|
||||||
|
label: '编辑资料',
|
||||||
|
button: true,
|
||||||
|
child: InkWell(
|
||||||
|
onTap: onTap,
|
||||||
|
customBorder: const CircleBorder(),
|
||||||
|
child: SizedBox(
|
||||||
|
width: 82,
|
||||||
|
height: 82,
|
||||||
|
child: ClipOval(
|
||||||
|
child: url == null
|
||||||
|
? const ColoredBox(
|
||||||
|
color: AppColors.surfaceTint,
|
||||||
|
child: Icon(
|
||||||
|
Icons.person_outline,
|
||||||
|
color: AppColors.muted,
|
||||||
|
size: 36,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: RemoteImage(
|
||||||
|
url: url!,
|
||||||
|
width: 82,
|
||||||
|
height: 82,
|
||||||
|
borderRadius: BorderRadius.circular(41),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ProfileStat extends StatelessWidget {
|
class _ProfileStat extends StatelessWidget {
|
||||||
|
|||||||
@@ -7,13 +7,16 @@ import 'package:shared_preferences/shared_preferences.dart';
|
|||||||
|
|
||||||
class AppState extends ChangeNotifier {
|
class AppState extends ChangeNotifier {
|
||||||
static const _petKey = 'patbond_pet';
|
static const _petKey = 'patbond_pet';
|
||||||
static const _postsKey = 'patbond_posts';
|
|
||||||
static const _locationWeatherKey = 'patbond_location_weather';
|
static const _locationWeatherKey = 'patbond_location_weather';
|
||||||
|
|
||||||
/// 首页问候卡 / 创作页 / 主壳头像仍消费的 demo 宠物(T2-12 起档案
|
/// 首页问候卡 / 创作页 / 主壳头像仍消费的 demo 宠物(T2-12 起档案
|
||||||
/// Tab 已切独立 pets feature 真实数据;此 demo 随后续工单收敛)。
|
/// Tab 已切独立 pets feature 真实数据;此 demo 随后续工单收敛)。
|
||||||
|
///
|
||||||
|
/// **demo 帖子列表已于 T3-17 退役**:Feed / 详情页自 T3-14/15 起消费
|
||||||
|
/// community 真实数据,发布页自 T3-17 起走真实两步上传 + 建草稿/迁移
|
||||||
|
/// 发布,`posts` / `publishPost` / `updatePost` 与其
|
||||||
|
/// shared_preferences 持久化一并删除(03 号评估 §1.1 判定)。
|
||||||
PetProfile pet = initialPet;
|
PetProfile pet = initialPet;
|
||||||
List<PostModel> posts = List<PostModel>.from(initialPosts);
|
|
||||||
LocationWeather locationWeather = initialLocationWeather;
|
LocationWeather locationWeather = initialLocationWeather;
|
||||||
bool isReady = false;
|
bool isReady = false;
|
||||||
|
|
||||||
@@ -21,17 +24,11 @@ class AppState extends ChangeNotifier {
|
|||||||
try {
|
try {
|
||||||
final preferences = await SharedPreferences.getInstance();
|
final preferences = await SharedPreferences.getInstance();
|
||||||
final savedPet = preferences.getString(_petKey);
|
final savedPet = preferences.getString(_petKey);
|
||||||
final savedPosts = preferences.getString(_postsKey);
|
|
||||||
final savedLocationWeather = preferences.getString(_locationWeatherKey);
|
final savedLocationWeather = preferences.getString(_locationWeatherKey);
|
||||||
|
|
||||||
if (savedPet != null) {
|
if (savedPet != null) {
|
||||||
pet = PetProfile.fromJson(jsonDecode(savedPet) as Map<String, dynamic>);
|
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) {
|
if (savedLocationWeather != null) {
|
||||||
locationWeather = LocationWeather.fromJson(
|
locationWeather = LocationWeather.fromJson(
|
||||||
jsonDecode(savedLocationWeather) as Map<String, dynamic>,
|
jsonDecode(savedLocationWeather) as Map<String, dynamic>,
|
||||||
@@ -40,7 +37,6 @@ class AppState extends ChangeNotifier {
|
|||||||
} catch (error, stackTrace) {
|
} catch (error, stackTrace) {
|
||||||
debugPrint('读取本地数据失败,已使用默认数据:$error\n$stackTrace');
|
debugPrint('读取本地数据失败,已使用默认数据:$error\n$stackTrace');
|
||||||
pet = initialPet;
|
pet = initialPet;
|
||||||
posts = List<PostModel>.from(initialPosts);
|
|
||||||
locationWeather = initialLocationWeather;
|
locationWeather = initialLocationWeather;
|
||||||
} finally {
|
} finally {
|
||||||
isReady = true;
|
isReady = true;
|
||||||
@@ -48,21 +44,6 @@ class AppState extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
Future<void> updateLocationWeather(LocationWeather value) async {
|
||||||
locationWeather = value;
|
locationWeather = value;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
@@ -71,21 +52,15 @@ class AppState extends ChangeNotifier {
|
|||||||
|
|
||||||
Future<void> resetDemoData() async {
|
Future<void> resetDemoData() async {
|
||||||
pet = initialPet;
|
pet = initialPet;
|
||||||
posts = List<PostModel>.from(initialPosts);
|
|
||||||
locationWeather = initialLocationWeather;
|
locationWeather = initialLocationWeather;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
final preferences = await SharedPreferences.getInstance();
|
final preferences = await SharedPreferences.getInstance();
|
||||||
await Future.wait([
|
await Future.wait([
|
||||||
preferences.remove(_petKey),
|
preferences.remove(_petKey),
|
||||||
preferences.remove(_postsKey),
|
|
||||||
preferences.remove(_locationWeatherKey),
|
preferences.remove(_locationWeatherKey),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _savePosts() {
|
|
||||||
return _save(_postsKey, posts.map((post) => post.toJson()).toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _save(String key, Object value) async {
|
Future<void> _save(String key, Object value) async {
|
||||||
try {
|
try {
|
||||||
final preferences = await SharedPreferences.getInstance();
|
final preferences = await SharedPreferences.getInstance();
|
||||||
|
|||||||
@@ -235,6 +235,11 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "6.0.0"
|
version: "6.0.0"
|
||||||
|
flutter_localizations:
|
||||||
|
dependency: "direct main"
|
||||||
|
description: flutter
|
||||||
|
source: sdk
|
||||||
|
version: "0.0.0"
|
||||||
flutter_plugin_android_lifecycle:
|
flutter_plugin_android_lifecycle:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -399,6 +404,14 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
intl:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: intl
|
||||||
|
sha256: "3df61194eb431efc39c4ceba583b95633a403f46c9fd341e550ce0bfa50e9aa5"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.20.2"
|
||||||
jni:
|
jni:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -43,6 +43,13 @@ dependencies:
|
|||||||
image_picker: ^1.2.0
|
image_picker: ^1.2.0
|
||||||
# T3-13 媒体上传:原生编解码压缩(长边重采样 + JPEG 质量 + EXIF 方向矫正)。
|
# T3-13 媒体上传:原生编解码压缩(长边重采样 + JPEG 质量 + EXIF 方向矫正)。
|
||||||
flutter_image_compress: ^2.4.0
|
flutter_image_compress: ^2.4.0
|
||||||
|
# M3.5-01 中文本地化:Material/Cupertino/Widgets 内置组件文案(日期选择器
|
||||||
|
# 标题、确定/取消、输入模式提示与格式错误)由 Global*Localizations 提供。
|
||||||
|
flutter_localizations:
|
||||||
|
sdk: flutter
|
||||||
|
# flutter_localizations 的日期符号/数字格式底座;显式直接依赖以锁定与 SDK
|
||||||
|
# 一致的版本(避免间接依赖升级悄悄改变格式化行为)。
|
||||||
|
intl: ^0.20.2
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/app/app.dart';
|
||||||
|
import 'package:patbond_flutter/app/app_localization.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/session_manager.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
import '../helpers/auth_test_helpers.dart';
|
||||||
|
import '../helpers/community_test_helpers.dart';
|
||||||
|
import '../helpers/pet_test_helpers.dart';
|
||||||
|
|
||||||
|
/// M3.5-01:根 [MaterialApp] 实际挂上 zh-CN 本地化。
|
||||||
|
///
|
||||||
|
/// 用户桌面实测反馈「日期选择器全英文」的根因就在这里——delegate 一行没配,
|
||||||
|
/// Material 静默回退英文兜底。本测试守住这一行不被回删。
|
||||||
|
void main() {
|
||||||
|
testWidgets('MaterialApp 挂三件套 delegate + 单语言 zh-CN', (tester) async {
|
||||||
|
SharedPreferences.setMockInitialValues({});
|
||||||
|
final session = SessionManager(store: InMemoryTokenStore())
|
||||||
|
..markAuthenticated();
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
App(
|
||||||
|
sessionManager: session,
|
||||||
|
authRepository: FakeAuthRepository(),
|
||||||
|
petsRepository: FakePetsRepository(),
|
||||||
|
communityRepository: FakeCommunityRepository()
|
||||||
|
..onFeed = (_, _) async => feedPage(const []),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final app = tester.widget<MaterialApp>(find.byType(MaterialApp));
|
||||||
|
expect(app.localizationsDelegates, appLocalizationsDelegates);
|
||||||
|
expect(app.supportedLocales, appSupportedLocales);
|
||||||
|
expect(app.locale, const Locale('zh', 'CN'));
|
||||||
|
|
||||||
|
// 运行期实际解析出的 Material 文案为中文(不是 Default* 英文兜底)。
|
||||||
|
final context = tester.element(find.byType(NavigationBar));
|
||||||
|
final l10n = MaterialLocalizations.of(context);
|
||||||
|
expect(l10n.okButtonLabel, '确定');
|
||||||
|
expect(l10n.cancelButtonLabel, '取消');
|
||||||
|
expect(l10n.datePickerHelpText, '选择日期');
|
||||||
|
expect(l10n.dateInputLabel, '输入日期');
|
||||||
|
expect(l10n.inputDateModeButtonLabel, '切换到输入模式');
|
||||||
|
expect(l10n.invalidDateFormatLabel, '格式无效。');
|
||||||
|
expect(l10n.formatMonthYear(DateTime(2026, 9, 10)), '2026年9月');
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/models/patch_field.dart';
|
||||||
|
|
||||||
|
/// PATCH 三态的 JSON 表现(契约 v1.4.0 的 `UpdateMeRequest` /
|
||||||
|
/// `UpdatePetRequest.avatarAssetId`)。
|
||||||
|
///
|
||||||
|
/// 本文件守住的红线只有一条:**absent 绝不落键**。若 absent 落成 `null`,
|
||||||
|
/// 用户「只改昵称」会把头像一起清掉(服务端把显式 null 当清空指令)。
|
||||||
|
void main() {
|
||||||
|
test('absent 不落键 / clear 落 null / value 落值', () {
|
||||||
|
final json = <String, Object?>{};
|
||||||
|
const PatchField<String>.absent().writeTo(json, 'a');
|
||||||
|
expect(json.containsKey('a'), isFalse, reason: 'absent 必须连键都不出现');
|
||||||
|
|
||||||
|
const PatchField<String>.clear().writeTo(json, 'b');
|
||||||
|
expect(json.containsKey('b'), isTrue);
|
||||||
|
expect(json['b'], isNull);
|
||||||
|
|
||||||
|
const PatchField<String>.value('x').writeTo(json, 'c');
|
||||||
|
expect(json['c'], 'x');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('absent 与 clear 的 valueOrNull 同为 null,只能靠 isPresent 区分', () {
|
||||||
|
const absent = PatchField<String>.absent();
|
||||||
|
const clear = PatchField<String>.clear();
|
||||||
|
expect(absent.valueOrNull, isNull);
|
||||||
|
expect(clear.valueOrNull, isNull);
|
||||||
|
expect(absent.isPresent, isFalse);
|
||||||
|
expect(clear.isPresent, isTrue);
|
||||||
|
expect(absent.isClear, isFalse);
|
||||||
|
expect(clear.isClear, isTrue);
|
||||||
|
expect(const PatchField<String>.value('x').isClear, isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('encode 只作用于有值态(clear 不调 encode,避免 null 解引用)', () {
|
||||||
|
final json = <String, Object?>{};
|
||||||
|
var encodeCalls = 0;
|
||||||
|
const PatchField<int>.clear().writeTo(
|
||||||
|
json,
|
||||||
|
'n',
|
||||||
|
encode: (value) {
|
||||||
|
encodeCalls += 1;
|
||||||
|
return value.toString();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
expect(json['n'], isNull);
|
||||||
|
expect(encodeCalls, 0);
|
||||||
|
|
||||||
|
const PatchField<int>.value(7).writeTo(
|
||||||
|
json,
|
||||||
|
'm',
|
||||||
|
encode: (value) {
|
||||||
|
encodeCalls += 1;
|
||||||
|
return value.toString();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
expect(json['m'], '7');
|
||||||
|
expect(encodeCalls, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('值相等即相等(absent 与 clear 不相等)', () {
|
||||||
|
expect(
|
||||||
|
const PatchField<String>.value('a'),
|
||||||
|
const PatchField<String>.value('a'),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
const PatchField<String>.absent(),
|
||||||
|
isNot(const PatchField<String>.clear()),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,385 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/app/app_localization.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/app_date_picker.dart';
|
||||||
|
|
||||||
|
/// M3.5-01/02:日期录入共享层。
|
||||||
|
///
|
||||||
|
/// 覆盖三条用户实测反馈:中文文案实际渲染、键盘输入模式可用、「今天」快捷键。
|
||||||
|
void main() {
|
||||||
|
/// 挂真实 app 配置(主题 + zh-CN delegate)的宿主:不挂 delegate 的话
|
||||||
|
/// Material 会回退英文兜底,测出来的「中文」是假的。
|
||||||
|
Widget host(Widget child) => MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
localizationsDelegates: appLocalizationsDelegates,
|
||||||
|
supportedLocales: appSupportedLocales,
|
||||||
|
locale: appLocale,
|
||||||
|
home: Scaffold(body: child),
|
||||||
|
);
|
||||||
|
|
||||||
|
group('dateOnly / today / isDateSelectable', () {
|
||||||
|
test('dateOnly 抹掉时分秒', () {
|
||||||
|
expect(
|
||||||
|
dateOnly(DateTime(2026, 4, 9, 23, 59, 58, 777)),
|
||||||
|
DateTime(2026, 4, 9),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('today 为本地当天零点', () {
|
||||||
|
final now = DateTime.now();
|
||||||
|
expect(today(), DateTime(now.year, now.month, now.day));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('isDateSelectable 闭区间按日粒度判定', () {
|
||||||
|
final first = DateTime(2026, 9, 1);
|
||||||
|
final last = DateTime(2026, 9, 30, 8, 30);
|
||||||
|
expect(
|
||||||
|
isDateSelectable(
|
||||||
|
date: DateTime(2026, 9, 1),
|
||||||
|
firstDate: first,
|
||||||
|
lastDate: last,
|
||||||
|
),
|
||||||
|
isTrue,
|
||||||
|
);
|
||||||
|
// 边界当天含时分也算落在区间内(lastDate 的时分不该把当天挤出去)。
|
||||||
|
expect(
|
||||||
|
isDateSelectable(
|
||||||
|
date: DateTime(2026, 9, 30, 23, 0),
|
||||||
|
firstDate: first,
|
||||||
|
lastDate: last,
|
||||||
|
),
|
||||||
|
isTrue,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
isDateSelectable(
|
||||||
|
date: DateTime(2026, 8, 31),
|
||||||
|
firstDate: first,
|
||||||
|
lastDate: last,
|
||||||
|
),
|
||||||
|
isFalse,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
isDateSelectable(
|
||||||
|
date: DateTime(2026, 10, 1),
|
||||||
|
firstDate: first,
|
||||||
|
lastDate: last,
|
||||||
|
),
|
||||||
|
isFalse,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('pickAppDate · 中文本地化实际渲染', () {
|
||||||
|
testWidgets('日历模式:标题/确定/取消全中文(此前为 Select date / OK / Cancel)', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
host(
|
||||||
|
Builder(
|
||||||
|
builder: (context) => TextButton(
|
||||||
|
onPressed: () => pickAppDate(
|
||||||
|
context: context,
|
||||||
|
initialDate: DateTime(2026, 9, 10),
|
||||||
|
firstDate: DateTime(1990),
|
||||||
|
lastDate: DateTime(2026, 9, 30),
|
||||||
|
),
|
||||||
|
child: const Text('打开'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.text('打开'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('选择日期'), findsOneWidget);
|
||||||
|
expect(find.text('确定'), findsOneWidget);
|
||||||
|
expect(find.text('取消'), findsOneWidget);
|
||||||
|
expect(find.text('Select date'), findsNothing);
|
||||||
|
expect(find.text('OK'), findsNothing);
|
||||||
|
expect(find.text('Cancel'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('保留手输切换按钮(不用 calendarOnly),切换后提示与标签为中文', (tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
host(
|
||||||
|
Builder(
|
||||||
|
builder: (context) => TextButton(
|
||||||
|
onPressed: () => pickAppDate(
|
||||||
|
context: context,
|
||||||
|
initialDate: DateTime(2026, 9, 10),
|
||||||
|
firstDate: DateTime(1990),
|
||||||
|
lastDate: DateTime(2026, 9, 30),
|
||||||
|
),
|
||||||
|
child: const Text('打开'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.text('打开'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// calendarOnly 会砍掉这枚铅笔按钮——手输是「录一个已知日期」的快路,
|
||||||
|
// 必须留着。
|
||||||
|
final toggle = find.byIcon(Icons.edit_outlined);
|
||||||
|
expect(toggle, findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(toggle);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('输入日期'), findsOneWidget);
|
||||||
|
expect(find.byType(TextField), findsOneWidget);
|
||||||
|
final field = tester.widget<TextField>(find.byType(TextField));
|
||||||
|
// 手输提示/标签由 zh-CN 本地化提供(不硬编码),不再是 mm/dd/yyyy。
|
||||||
|
expect(field.decoration!.labelText, isNot(contains('Date')));
|
||||||
|
expect(field.decoration!.hintText, isNot('mm/dd/yyyy'));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('手输模式敲入已知日期即可返回(无需逐月点箭头)', (tester) async {
|
||||||
|
DateTime? picked;
|
||||||
|
await tester.pumpWidget(
|
||||||
|
host(
|
||||||
|
Builder(
|
||||||
|
builder: (context) => TextButton(
|
||||||
|
onPressed: () async {
|
||||||
|
picked = await pickAppDate(
|
||||||
|
context: context,
|
||||||
|
initialDate: DateTime(2026, 9, 10),
|
||||||
|
firstDate: DateTime(1990),
|
||||||
|
lastDate: DateTime(2026, 9, 30),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: const Text('打开'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.text('打开'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.byIcon(Icons.edit_outlined));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.enterText(find.byType(TextField), '2026/04/09');
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('确定'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(picked, DateTime(2026, 4, 9));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('取消返回 null;确认返回值已抹掉时分秒', (tester) async {
|
||||||
|
DateTime? picked;
|
||||||
|
var calls = 0;
|
||||||
|
await tester.pumpWidget(
|
||||||
|
host(
|
||||||
|
Builder(
|
||||||
|
builder: (context) => TextButton(
|
||||||
|
onPressed: () async {
|
||||||
|
calls++;
|
||||||
|
picked = await pickAppDate(
|
||||||
|
context: context,
|
||||||
|
initialDate: DateTime(2026, 9, 10, 15, 30),
|
||||||
|
firstDate: DateTime(1990),
|
||||||
|
lastDate: DateTime(2026, 9, 30),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: const Text('打开'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.text('打开'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('取消'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(calls, 1);
|
||||||
|
expect(picked, isNull);
|
||||||
|
|
||||||
|
await tester.tap(find.text('打开'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('确定'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(picked, DateTime(2026, 9, 10));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('initialDate 越界自动夹进 [firstDate, lastDate](不触发原生断言)', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
DateTime? picked;
|
||||||
|
await tester.pumpWidget(
|
||||||
|
host(
|
||||||
|
Builder(
|
||||||
|
builder: (context) => TextButton(
|
||||||
|
onPressed: () async {
|
||||||
|
// 历史值早于 firstDate:如「提醒到期日」firstDate 是今天,
|
||||||
|
// 而表单预填的是一个已过期的旧值。
|
||||||
|
picked = await pickAppDate(
|
||||||
|
context: context,
|
||||||
|
initialDate: DateTime(2020, 1, 1),
|
||||||
|
firstDate: DateTime(2026, 9, 1),
|
||||||
|
lastDate: DateTime(2026, 9, 30),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: const Text('打开'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.text('打开'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('确定'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(picked, DateTime(2026, 9, 1));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('AppDateFieldTrailing · 「今天」快捷键', () {
|
||||||
|
testWidgets('点「今天」直接回调今天,不开弹窗', (tester) async {
|
||||||
|
DateTime? got;
|
||||||
|
await tester.pumpWidget(
|
||||||
|
host(
|
||||||
|
AppDateFieldTrailing(
|
||||||
|
firstDate: DateTime(1990),
|
||||||
|
lastDate: DateTime(2100),
|
||||||
|
onToday: (value) => got = value,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(find.text('今天'), findsOneWidget);
|
||||||
|
expect(find.byIcon(Icons.calendar_month_outlined), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('今天'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(got, today());
|
||||||
|
// 快捷键不经日期选择器(少两次点击、绕开月份导航)。
|
||||||
|
expect(find.text('选择日期'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('今天越界时隐藏按钮,只留日历图标', (tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
host(
|
||||||
|
AppDateFieldTrailing(
|
||||||
|
firstDate: DateTime(1990),
|
||||||
|
lastDate: DateTime(1999, 12, 31),
|
||||||
|
onToday: (_) {},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(find.text('今天'), findsNothing);
|
||||||
|
expect(find.byIcon(Icons.calendar_month_outlined), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('enabled=false(提交中)按钮禁用', (tester) async {
|
||||||
|
var calls = 0;
|
||||||
|
await tester.pumpWidget(
|
||||||
|
host(
|
||||||
|
AppDateFieldTrailing(
|
||||||
|
firstDate: DateTime(1990),
|
||||||
|
lastDate: DateTime(2100),
|
||||||
|
enabled: false,
|
||||||
|
onToday: (_) => calls++,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
tester.widget<TextButton>(find.byType(TextButton)).onPressed,
|
||||||
|
isNull,
|
||||||
|
);
|
||||||
|
await tester.tap(find.text('今天'), warnIfMissed: false);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(calls, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('触控目标不小于 44×44', (tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
host(
|
||||||
|
AppDateFieldTrailing(
|
||||||
|
firstDate: DateTime(1990),
|
||||||
|
lastDate: DateTime(2100),
|
||||||
|
onToday: (_) {},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final size = tester.getSize(find.byType(TextButton));
|
||||||
|
expect(size.width, greaterThanOrEqualTo(44));
|
||||||
|
expect(size.height, greaterThanOrEqualTo(44));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('datePickerTheme · 品牌配色(不再是 fromSeed 的暗红棕)', () {
|
||||||
|
test('选中日/选中年为 primaryStrong 实底白字,今日 primaryStrong 描边', () {
|
||||||
|
final theme = buildAppTheme().datePickerTheme;
|
||||||
|
const selected = <WidgetState>{WidgetState.selected};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
theme.dayBackgroundColor!.resolve(selected),
|
||||||
|
AppColors.primaryStrong,
|
||||||
|
);
|
||||||
|
expect(theme.dayForegroundColor!.resolve(selected), Colors.white);
|
||||||
|
expect(
|
||||||
|
theme.yearBackgroundColor!.resolve(selected),
|
||||||
|
AppColors.primaryStrong,
|
||||||
|
);
|
||||||
|
expect(theme.yearForegroundColor!.resolve(selected), Colors.white);
|
||||||
|
expect(theme.todayBorder!.color, AppColors.primaryStrong);
|
||||||
|
expect(
|
||||||
|
theme.todayForegroundColor!.resolve(const {}),
|
||||||
|
AppColors.primaryStrong,
|
||||||
|
);
|
||||||
|
// 头部沿用「选中 chip」既有色对:surfaceTint 底 + primaryDark 字 7.98:1。
|
||||||
|
expect(theme.headerBackgroundColor, AppColors.surfaceTint);
|
||||||
|
expect(theme.headerForegroundColor, AppColors.primaryDark);
|
||||||
|
expect(theme.backgroundColor, AppColors.surface);
|
||||||
|
// 越界不可选日走禁用态 muted(DEBT-2 允许的 muted 用途)。
|
||||||
|
expect(
|
||||||
|
theme.dayForegroundColor!.resolve(const {WidgetState.disabled}),
|
||||||
|
AppColors.muted,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('实际渲染:选中日圆底取 primaryStrong', (tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
host(
|
||||||
|
Builder(
|
||||||
|
builder: (context) => TextButton(
|
||||||
|
onPressed: () => pickAppDate(
|
||||||
|
context: context,
|
||||||
|
initialDate: DateTime(2026, 9, 10),
|
||||||
|
firstDate: DateTime(1990),
|
||||||
|
lastDate: DateTime(2026, 9, 30),
|
||||||
|
),
|
||||||
|
child: const Text('打开'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.tap(find.text('打开'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// 选中日的圆底由 Ink(decoration: ShapeDecoration) 承载,色值取自
|
||||||
|
// datePickerTheme.dayBackgroundColor / todayBackgroundColor。
|
||||||
|
final inkColors = tester
|
||||||
|
.widgetList<Ink>(find.byType(Ink))
|
||||||
|
.map((ink) => ink.decoration)
|
||||||
|
.whereType<ShapeDecoration>()
|
||||||
|
.map((d) => d.color)
|
||||||
|
.toList();
|
||||||
|
expect(
|
||||||
|
inkColors,
|
||||||
|
contains(AppColors.primaryStrong),
|
||||||
|
reason: '选中日未使用品牌 primaryStrong 实底(仍是 fromSeed 派生色)',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,162 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/avatar_upload_sheet.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_direct_upload.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
|
||||||
|
import '../../helpers/community_test_helpers.dart';
|
||||||
|
import '../../helpers/media_test_helpers.dart';
|
||||||
|
|
||||||
|
/// 头像上传 sheet 的六态呈现(状态机本身由 MediaUploader 单测覆盖,
|
||||||
|
/// 本文件只验「哪个阶段给用户看什么、能点什么」)。
|
||||||
|
void main() {
|
||||||
|
late FakeCommunityRepository community;
|
||||||
|
late FakeMediaImagePicker picker;
|
||||||
|
late FakeMediaCompressor compressor;
|
||||||
|
late FakeDirectUploadClient direct;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
community = FakeCommunityRepository();
|
||||||
|
picker = FakeMediaImagePicker([decodablePickedImage()]);
|
||||||
|
compressor = FakeMediaCompressor();
|
||||||
|
direct = FakeDirectUploadClient();
|
||||||
|
community.onCreateMediaUpload = (_) async => credentials();
|
||||||
|
community.onCompleteMediaUpload = (assetId) async =>
|
||||||
|
readyAsset(assetId: assetId);
|
||||||
|
});
|
||||||
|
|
||||||
|
MediaUploader build(MediaPurpose purpose) => MediaUploader(
|
||||||
|
repository: community,
|
||||||
|
purpose: purpose,
|
||||||
|
picker: picker,
|
||||||
|
compressor: compressor,
|
||||||
|
directUpload: direct,
|
||||||
|
maxImages: 1,
|
||||||
|
maxConcurrentUploads: 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// 挂一个按钮拉起 sheet,并把 pop 结果记录下来。
|
||||||
|
Future<List<String?>> pumpSheet(
|
||||||
|
WidgetTester tester, {
|
||||||
|
MediaPurpose purpose = MediaPurpose.userAvatar,
|
||||||
|
}) async {
|
||||||
|
final results = <String?>[];
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: Builder(
|
||||||
|
builder: (context) => Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: ElevatedButton(
|
||||||
|
onPressed: () async {
|
||||||
|
results.add(
|
||||||
|
await showAvatarUploadSheet(
|
||||||
|
context,
|
||||||
|
builder: build,
|
||||||
|
purpose: purpose,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: const Text('打开'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.tap(find.text('打开'));
|
||||||
|
await tester.pump();
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgets('打开即自动拉起选择器(无需多点一次「选择图片」)', (tester) async {
|
||||||
|
picker.gate = Completer<void>();
|
||||||
|
await pumpSheet(tester);
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(find.text('正在打开相册…'), findsOneWidget);
|
||||||
|
expect(picker.limits, [1], reason: 'maxImages=1 → 只取一张');
|
||||||
|
|
||||||
|
picker.gate!.complete();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('上传中:线性进度 + 百分比;ready 后给预览与「使用这张」', (tester) async {
|
||||||
|
direct.manual = true;
|
||||||
|
final results = await pumpSheet(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.byType(LinearProgressIndicator), findsOneWidget);
|
||||||
|
final call = direct.calls.single;
|
||||||
|
call.emitProgress(30, 100);
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.text('上传中 30%'), findsOneWidget);
|
||||||
|
|
||||||
|
call.succeed();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('使用这张'), findsOneWidget);
|
||||||
|
expect(find.byType(Image), findsOneWidget, reason: 'ready 态给整幅预览');
|
||||||
|
|
||||||
|
await tester.tap(find.text('使用这张'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(results.single, 'a-1', reason: '只交付 ready 态的 assetId(孤儿防护)');
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('purpose 透传到 createUpload(宠物头像 = pet_avatar)', (tester) async {
|
||||||
|
await pumpSheet(tester, purpose: MediaPurpose.petAvatar);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(community.lastMediaUploadRequest!.purpose, MediaPurpose.petAvatar);
|
||||||
|
expect(community.lastMediaUploadRequest!.purpose.wire, 'pet_avatar');
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('可重试失败:给「重试」+「重新选择」,重试成功后可确认', (tester) async {
|
||||||
|
direct.scriptedOutcomes.add(
|
||||||
|
const MediaDirectUploadException(message: '断网'),
|
||||||
|
);
|
||||||
|
final results = await pumpSheet(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('网络中断,上传失败'), findsOneWidget);
|
||||||
|
expect(find.text('重试'), findsOneWidget);
|
||||||
|
expect(find.text('重新选择'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('重试'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('使用这张'), findsOneWidget);
|
||||||
|
await tester.tap(find.text('使用这张'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(results.single, 'a-1');
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('不可重试失败(压缩后仍超限):只给「重新选择」,不给「重试」', (tester) async {
|
||||||
|
compressor.sizePerQuality = {80: 20 * 1024 * 1024, 60: 15 * 1024 * 1024};
|
||||||
|
await pumpSheet(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('图片过大,压缩后仍超过 10 MB'), findsOneWidget);
|
||||||
|
expect(find.text('重试'), findsNothing, reason: '重试同一张必然再失败,给重试钮是误导');
|
||||||
|
expect(find.text('重新选择'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('用户在系统选择器里取消 → 空态可再次选择,pop 结果为 null', (tester) async {
|
||||||
|
picker = FakeMediaImagePicker(const []);
|
||||||
|
final results = await pumpSheet(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('还没有选择图片'), findsOneWidget);
|
||||||
|
expect(find.text('选择图片'), findsOneWidget);
|
||||||
|
|
||||||
|
// 关闭 sheet:未走到 ready,不交付任何 assetId。
|
||||||
|
await tester.tapAt(const Offset(10, 10));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(results.single, isNull);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/comment_tile.dart';
|
||||||
|
|
||||||
|
import '../../helpers/community_test_helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
final now = DateTime.parse('2026-09-08T13:00:00.000Z');
|
||||||
|
|
||||||
|
Widget wrap(Widget child) => MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: Scaffold(body: Center(child: child)),
|
||||||
|
);
|
||||||
|
|
||||||
|
testWidgets('渲染作者名 / 内容 / 相对时间;无 onDelete 不显删除', (tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
wrap(CommentTile(comment: sampleComment(), now: now)),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(find.text('毛毛的铲屎官'), findsOneWidget);
|
||||||
|
expect(find.text('好可爱!', findRichText: true), findsOneWidget);
|
||||||
|
expect(find.text('2 小时前'), findsOneWidget);
|
||||||
|
expect(find.text('删除'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('@ 回复以「回复 @昵称:」前缀呈现;降级作者「宠友」占位', (tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
wrap(
|
||||||
|
CommentTile(
|
||||||
|
comment: sampleComment(
|
||||||
|
author: sampleAuthorJson(nickname: null, avatarUrl: null),
|
||||||
|
replyToUser: sampleAuthorJson(userId: 'u-2', nickname: '豆豆麻麻'),
|
||||||
|
),
|
||||||
|
now: now,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(find.text('宠友'), findsOneWidget);
|
||||||
|
expect(
|
||||||
|
find.textContaining('回复 @豆豆麻麻:', findRichText: true),
|
||||||
|
findsOneWidget,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('onDelete 提供时显删除入口并回调;deleting 期间转圈锁定', (tester) async {
|
||||||
|
var deleted = 0;
|
||||||
|
await tester.pumpWidget(
|
||||||
|
wrap(
|
||||||
|
CommentTile(
|
||||||
|
comment: sampleComment(),
|
||||||
|
now: now,
|
||||||
|
onDelete: () => deleted++,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.text('删除'));
|
||||||
|
expect(deleted, 1);
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
wrap(
|
||||||
|
CommentTile(
|
||||||
|
comment: sampleComment(),
|
||||||
|
now: now,
|
||||||
|
deleting: true,
|
||||||
|
onDelete: () => deleted++,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
expect(find.text('删除'), findsNothing);
|
||||||
|
expect(find.byType(CircularProgressIndicator), findsOneWidget);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/like_button.dart';
|
||||||
|
|
||||||
|
/// 宿主:模拟持有方(controller)的状态翻转——点按经 [onTap] 乐观翻转,
|
||||||
|
/// 外部(回滚/对账)经 [setActive] 直接改。
|
||||||
|
class _Host extends StatefulWidget {
|
||||||
|
const _Host({required this.initialActive, required this.initialCount});
|
||||||
|
|
||||||
|
final bool initialActive;
|
||||||
|
final int initialCount;
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<_Host> createState() => _HostState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _HostState extends State<_Host> {
|
||||||
|
late bool active = widget.initialActive;
|
||||||
|
late int count = widget.initialCount;
|
||||||
|
|
||||||
|
void setExternal({required bool active, required int count}) {
|
||||||
|
setState(() {
|
||||||
|
this.active = active;
|
||||||
|
this.count = count;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: LikeButton(
|
||||||
|
variant: LikeButtonVariant.like,
|
||||||
|
active: active,
|
||||||
|
count: count,
|
||||||
|
onPressed: () => setState(() {
|
||||||
|
active = !active;
|
||||||
|
count += active ? 1 : -1;
|
||||||
|
}),
|
||||||
|
semanticLabel: '点赞',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
double scaleOf(WidgetTester tester) => tester
|
||||||
|
.widget<ScaleTransition>(
|
||||||
|
find.descendant(
|
||||||
|
of: find.byType(LikeButton),
|
||||||
|
matching: find.byType(ScaleTransition),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.scale
|
||||||
|
.value;
|
||||||
|
|
||||||
|
testWidgets('点按激活:240ms 弹性缩放动画(中途 >1,播完归位)', (tester) async {
|
||||||
|
await tester.pumpWidget(const _Host(initialActive: false, initialCount: 6));
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.favorite_border));
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pump(const Duration(milliseconds: 90));
|
||||||
|
expect(scaleOf(tester), greaterThan(1.0));
|
||||||
|
expect(find.byIcon(Icons.favorite), findsOneWidget);
|
||||||
|
expect(find.text('7'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(scaleOf(tester), 1.0);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('点按取消:仅颜色切换,无缩放动画', (tester) async {
|
||||||
|
await tester.pumpWidget(const _Host(initialActive: true, initialCount: 7));
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.favorite));
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pump(const Duration(milliseconds: 90));
|
||||||
|
expect(scaleOf(tester), 1.0);
|
||||||
|
expect(find.byIcon(Icons.favorite_border), findsOneWidget);
|
||||||
|
expect(find.text('6'), findsOneWidget);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('外部回滚:零动画直接跳变,计数与状态成对恢复', (tester) async {
|
||||||
|
await tester.pumpWidget(const _Host(initialActive: false, initialCount: 6));
|
||||||
|
|
||||||
|
// 点按激活并播完动画(在途请求随后失败的场景)。
|
||||||
|
await tester.tap(find.byIcon(Icons.favorite_border));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byIcon(Icons.favorite), findsOneWidget);
|
||||||
|
|
||||||
|
// 外部(非点按)回滚:下一帧即恢复,无过渡动画。
|
||||||
|
tester
|
||||||
|
.state<_HostState>(find.byType(_Host))
|
||||||
|
.setExternal(active: false, count: 6);
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.byIcon(Icons.favorite_border), findsOneWidget);
|
||||||
|
expect(find.text('6'), findsOneWidget);
|
||||||
|
expect(scaleOf(tester), 1.0);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('激活动画未播完时回滚:等播完再成对跳变(§4.3a 抖动抑制)', (tester) async {
|
||||||
|
await tester.pumpWidget(const _Host(initialActive: false, initialCount: 6));
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.favorite_border));
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pump(const Duration(milliseconds: 60));
|
||||||
|
expect(find.byIcon(Icons.favorite), findsOneWidget);
|
||||||
|
|
||||||
|
// 动画中途回滚:本帧仍显示激活视觉(等待播完),计数同持。
|
||||||
|
tester
|
||||||
|
.state<_HostState>(find.byType(_Host))
|
||||||
|
.setExternal(active: false, count: 6);
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.byIcon(Icons.favorite), findsOneWidget);
|
||||||
|
expect(find.text('7'), findsOneWidget);
|
||||||
|
|
||||||
|
// 播完后成对跳回。
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byIcon(Icons.favorite_border), findsOneWidget);
|
||||||
|
expect(find.text('6'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('对账静默:状态不变的计数变化直接替换,无动画', (tester) async {
|
||||||
|
await tester.pumpWidget(const _Host(initialActive: true, initialCount: 7));
|
||||||
|
|
||||||
|
tester
|
||||||
|
.state<_HostState>(find.byType(_Host))
|
||||||
|
.setExternal(active: true, count: 9);
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.text('9'), findsOneWidget);
|
||||||
|
expect(scaleOf(tester), 1.0);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('onPressed 为 null 时禁用但照常渲染激活态', (tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: const Scaffold(
|
||||||
|
body: LikeButton(
|
||||||
|
variant: LikeButtonVariant.bookmark,
|
||||||
|
active: true,
|
||||||
|
count: 2,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
expect(find.byIcon(Icons.bookmark), findsOneWidget);
|
||||||
|
expect(find.text('2'), findsOneWidget);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -3,8 +3,12 @@ import 'package:flutter_test/flutter_test.dart';
|
|||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/feed_skeleton.dart';
|
import 'package:patbond_flutter/core/widgets/feed_skeleton.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/post_media_grid.dart';
|
import 'package:patbond_flutter/core/widgets/post_media_grid.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/upload_progress_overlay.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
import 'package:patbond_flutter/widgets/common.dart';
|
import 'package:patbond_flutter/widgets/common.dart';
|
||||||
|
|
||||||
|
import '../../helpers/media_test_helpers.dart';
|
||||||
|
|
||||||
Widget wrap(Widget child, {bool disableAnimations = false}) => MaterialApp(
|
Widget wrap(Widget child, {bool disableAnimations = false}) => MaterialApp(
|
||||||
theme: buildAppTheme(),
|
theme: buildAppTheme(),
|
||||||
home: MediaQuery(
|
home: MediaQuery(
|
||||||
@@ -97,4 +101,92 @@ void main() {
|
|||||||
expect(fade.opacity.value, 1.0);
|
expect(fade.opacity.value, 1.0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
group('PostMediaEditGrid 编辑态(05 §3.2)', () {
|
||||||
|
MediaUploadItem item({
|
||||||
|
int localId = 1,
|
||||||
|
MediaItemPhase phase = MediaItemPhase.ready,
|
||||||
|
double progress = 0,
|
||||||
|
bool retryable = false,
|
||||||
|
}) => MediaUploadItem(
|
||||||
|
localId: localId,
|
||||||
|
phase: phase,
|
||||||
|
previewBytes: tinyPngBytes,
|
||||||
|
progress: progress,
|
||||||
|
assetId: phase == MediaItemPhase.ready ? 'a-$localId' : null,
|
||||||
|
retryable: retryable,
|
||||||
|
);
|
||||||
|
|
||||||
|
testWidgets('空列表只渲染「+」格;3 张图渲染 3 格 + 「+」', (tester) async {
|
||||||
|
await tester.pumpWidget(wrap(const PostMediaEditGrid(items: [])));
|
||||||
|
expect(find.byIcon(Icons.add_photo_alternate_outlined), findsOneWidget);
|
||||||
|
expect(find.byType(Image), findsNothing);
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
wrap(
|
||||||
|
PostMediaEditGrid(
|
||||||
|
items: [item(localId: 1), item(localId: 2), item(localId: 3)],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.byType(Image), findsNWidgets(3));
|
||||||
|
expect(find.byIcon(Icons.add_photo_alternate_outlined), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('满 9 张(canAdd=false)隐藏「+」格', (tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
wrap(
|
||||||
|
PostMediaEditGrid(
|
||||||
|
items: List.generate(9, (i) => item(localId: i + 1)),
|
||||||
|
canAdd: false,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
expect(find.byIcon(Icons.add_photo_alternate_outlined), findsNothing);
|
||||||
|
expect(find.byType(Image), findsNWidgets(9));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('删除角标回传 localId;「+」格回调', (tester) async {
|
||||||
|
final removed = <int>[];
|
||||||
|
var addTaps = 0;
|
||||||
|
await tester.pumpWidget(
|
||||||
|
wrap(
|
||||||
|
PostMediaEditGrid(
|
||||||
|
items: [item(localId: 7), item(localId: 8)],
|
||||||
|
onAdd: () => addTaps++,
|
||||||
|
onRemove: removed.add,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pump();
|
||||||
|
await tester.tap(find.byIcon(Icons.close).first);
|
||||||
|
expect(removed, [7]);
|
||||||
|
await tester.tap(find.byIcon(Icons.add_photo_alternate_outlined));
|
||||||
|
expect(addTaps, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('上传中格叠进度覆盖层;可重试失败格整格点按重试,终态不响应', (tester) async {
|
||||||
|
final retried = <int>[];
|
||||||
|
await tester.pumpWidget(
|
||||||
|
wrap(
|
||||||
|
PostMediaEditGrid(
|
||||||
|
items: [
|
||||||
|
item(localId: 1, phase: MediaItemPhase.uploading, progress: 0.42),
|
||||||
|
item(localId: 2, phase: MediaItemPhase.failed, retryable: true),
|
||||||
|
item(localId: 3, phase: MediaItemPhase.failed),
|
||||||
|
],
|
||||||
|
onRetry: retried.add,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.byType(UploadProgressOverlay), findsNWidgets(3));
|
||||||
|
expect(find.text('42%'), findsOneWidget);
|
||||||
|
// 可重试格有「重试」通栏,终态格没有。
|
||||||
|
expect(find.text('重试'), findsOneWidget);
|
||||||
|
await tester.tap(find.text('重试'));
|
||||||
|
expect(retried, [2]);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,126 @@
|
|||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_interaction_analytics.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late List<(String, Map<String, dynamic>?)> events;
|
||||||
|
late CommunityInteractionAnalytics analytics;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
events = [];
|
||||||
|
analytics = CommunityInteractionAnalytics(
|
||||||
|
(name, [props]) async => events.add((name, props)),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
List<String> names() => events.map((e) => e.$1).toList();
|
||||||
|
List<Map<String, dynamic>?> props() => events.map((e) => e.$2).toList();
|
||||||
|
|
||||||
|
test('互动四事件:source 逐字段(22 号白名单键集)', () {
|
||||||
|
analytics.postLiked(source: InteractionSource.feed);
|
||||||
|
analytics.postUnliked(source: InteractionSource.postDetail);
|
||||||
|
analytics.postFavorited(source: InteractionSource.postDetail);
|
||||||
|
analytics.postUnfavorited(source: InteractionSource.feed);
|
||||||
|
|
||||||
|
expect(names(), [
|
||||||
|
'post_liked',
|
||||||
|
'post_unliked',
|
||||||
|
'post_favorited',
|
||||||
|
'post_unfavorited',
|
||||||
|
]);
|
||||||
|
expect(props(), [
|
||||||
|
{'source': 'feed'},
|
||||||
|
{'source': 'post_detail'},
|
||||||
|
{'source': 'post_detail'},
|
||||||
|
{'source': 'feed'},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('comment_create_succeeded:durationMs/isReply/textLengthBucket', () {
|
||||||
|
analytics.commentCreateSucceeded(
|
||||||
|
durationMs: 4200,
|
||||||
|
isReply: false,
|
||||||
|
textLength: 12,
|
||||||
|
);
|
||||||
|
expect(names(), ['comment_create_succeeded']);
|
||||||
|
expect(props().single, {
|
||||||
|
'durationMs': 4200,
|
||||||
|
'isReply': false,
|
||||||
|
'textLengthBucket': 'short',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('comment_create_failed:httpStatus 由五位业务码推导,网络错误缺席', () {
|
||||||
|
analytics.commentCreateFailed(
|
||||||
|
reason: CommentCreateFailureReason.validationError,
|
||||||
|
attemptSeq: 2,
|
||||||
|
errorCode: 40000,
|
||||||
|
);
|
||||||
|
analytics.commentCreateFailed(
|
||||||
|
reason: CommentCreateFailureReason.networkError,
|
||||||
|
attemptSeq: 3,
|
||||||
|
);
|
||||||
|
expect(names(), ['comment_create_failed', 'comment_create_failed']);
|
||||||
|
expect(props(), [
|
||||||
|
{
|
||||||
|
'failureReason': 'validation_error',
|
||||||
|
'attemptSeq': 2,
|
||||||
|
'errorCode': 40000,
|
||||||
|
'httpStatus': 400,
|
||||||
|
},
|
||||||
|
{'failureReason': 'network_error', 'attemptSeq': 3},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('关注对:user_followed / user_unfollowed', () {
|
||||||
|
analytics.userFollowed(source: InteractionSource.postDetail);
|
||||||
|
analytics.userUnfollowed(source: InteractionSource.postDetail);
|
||||||
|
expect(names(), ['user_followed', 'user_unfollowed']);
|
||||||
|
expect(props(), [
|
||||||
|
{'source': 'post_detail'},
|
||||||
|
{'source': 'post_detail'},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('textLengthBucketOf 分桶边界(06 §1.3 红线:不报精确字数)', () {
|
||||||
|
expect(textLengthBucketOf(0), 'empty');
|
||||||
|
expect(textLengthBucketOf(1), 'short');
|
||||||
|
expect(textLengthBucketOf(50), 'short');
|
||||||
|
expect(textLengthBucketOf(51), 'medium');
|
||||||
|
expect(textLengthBucketOf(500), 'medium');
|
||||||
|
expect(textLengthBucketOf(501), 'long');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('失败原因映射:网络归并口径 + 防枚举族归 not_found + 会话失效 null', () {
|
||||||
|
expect(
|
||||||
|
commentCreateFailureReasonOf(const ApiNetworkException()),
|
||||||
|
CommentCreateFailureReason.networkError,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
commentCreateFailureReasonOf(const ApiRateLimitException()),
|
||||||
|
CommentCreateFailureReason.rateLimited,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
commentCreateFailureReasonOf(
|
||||||
|
const ApiBusinessException(code: ApiCodes.paramError, message: 'x'),
|
||||||
|
),
|
||||||
|
CommentCreateFailureReason.validationError,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
commentCreateFailureReasonOf(
|
||||||
|
const ApiBusinessException(code: ApiCodes.postNotFound, message: 'x'),
|
||||||
|
),
|
||||||
|
CommentCreateFailureReason.notFound,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
commentCreateFailureReasonOf(
|
||||||
|
const ApiBusinessException(code: 50000, message: 'x'),
|
||||||
|
),
|
||||||
|
CommentCreateFailureReason.serverError,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
commentCreateFailureReasonOf(const SessionExpiredException()),
|
||||||
|
isNull,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import 'package:patbond_flutter/features/community/community_exceptions.dart';
|
|||||||
import 'package:patbond_flutter/features/community/community_models.dart';
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
import 'package:patbond_flutter/features/community/media_direct_upload.dart';
|
import 'package:patbond_flutter/features/community/media_direct_upload.dart';
|
||||||
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/post_analytics.dart';
|
||||||
|
|
||||||
import '../../helpers/community_test_helpers.dart';
|
import '../../helpers/community_test_helpers.dart';
|
||||||
import '../../helpers/media_test_helpers.dart';
|
import '../../helpers/media_test_helpers.dart';
|
||||||
@@ -24,12 +25,14 @@ void main() {
|
|||||||
int maxConcurrentUploads = 2,
|
int maxConcurrentUploads = 2,
|
||||||
int maxByteSize = 10 * 1024 * 1024,
|
int maxByteSize = 10 * 1024 * 1024,
|
||||||
DateTime Function()? now,
|
DateTime Function()? now,
|
||||||
|
PostAnalytics? analytics,
|
||||||
}) {
|
}) {
|
||||||
return MediaUploader(
|
return MediaUploader(
|
||||||
repository: repository,
|
repository: repository,
|
||||||
picker: picker,
|
picker: picker,
|
||||||
compressor: compressor,
|
compressor: compressor,
|
||||||
directUpload: direct,
|
directUpload: direct,
|
||||||
|
analytics: analytics,
|
||||||
maxConcurrentUploads: maxConcurrentUploads,
|
maxConcurrentUploads: maxConcurrentUploads,
|
||||||
maxByteSize: maxByteSize,
|
maxByteSize: maxByteSize,
|
||||||
now: now,
|
now: now,
|
||||||
@@ -453,6 +456,151 @@ void main() {
|
|||||||
expect(uploader.overallProgress, closeTo(0.75, 0.001));
|
expect(uploader.overallProgress, closeTo(0.75, 0.001));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ---- 媒体上传三段埋点(T3-17;22 号白名单 v3 事件 27~29)----
|
||||||
|
group('媒体三段埋点', () {
|
||||||
|
late List<(String, Map<String, dynamic>?)> events;
|
||||||
|
late PostAnalytics analytics;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
events = [];
|
||||||
|
analytics = PostAnalytics(
|
||||||
|
(name, [props]) async => events.add((name, props)),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
List<Map<String, dynamic>?> named(String name) =>
|
||||||
|
events.where((event) => event.$1 == name).map((e) => e.$2).toList();
|
||||||
|
|
||||||
|
test('成功一图:started → succeeded 各一条,sizeBucket 同值 + durationMs', () async {
|
||||||
|
var clock = DateTime.utc(2026, 9, 10, 8);
|
||||||
|
// 凭据有效期以注入时钟为基准(避免与真实时钟错位触发过期重取)。
|
||||||
|
repository.onCreateMediaUpload = (_) async => credentials(
|
||||||
|
assetId: 'a-1',
|
||||||
|
expiresAt: clock.add(const Duration(minutes: 10)),
|
||||||
|
);
|
||||||
|
final uploader = build(analytics: analytics, now: () => clock);
|
||||||
|
|
||||||
|
direct.manual = true;
|
||||||
|
uploader.addImages([pickedImage(size: 2048)]);
|
||||||
|
await pumpEventQueue();
|
||||||
|
// started 已记时;上传耗时 1200ms 后 confirm 成功。
|
||||||
|
clock = clock.add(const Duration(milliseconds: 1200));
|
||||||
|
direct.calls.single.succeed();
|
||||||
|
await pumpEventQueue();
|
||||||
|
|
||||||
|
expect(events.map((e) => e.$1), [
|
||||||
|
'post_media_upload_started',
|
||||||
|
'post_media_upload_succeeded',
|
||||||
|
]);
|
||||||
|
expect(named('post_media_upload_started').single, {
|
||||||
|
'mediaType': 'image',
|
||||||
|
'sizeBucket': 'lt_1mb',
|
||||||
|
});
|
||||||
|
expect(named('post_media_upload_succeeded').single, {
|
||||||
|
'mediaType': 'image',
|
||||||
|
'sizeBucket': 'lt_1mb',
|
||||||
|
'durationMs': 1200,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('压缩后仍超限:started + failed(media_too_large, attemptSeq 1)', () async {
|
||||||
|
compressor.sizePerQuality = {80: 40, 60: 30};
|
||||||
|
final uploader = build(maxByteSize: 20, analytics: analytics);
|
||||||
|
|
||||||
|
uploader.addImages([pickedImage()]);
|
||||||
|
await pumpEventQueue();
|
||||||
|
|
||||||
|
expect(uploader.items.single.retryable, isFalse);
|
||||||
|
expect(named('post_media_upload_succeeded'), isEmpty);
|
||||||
|
expect(named('post_media_upload_failed').single, {
|
||||||
|
'mediaType': 'image',
|
||||||
|
'sizeBucket': 'lt_1mb',
|
||||||
|
'failureReason': 'media_too_large',
|
||||||
|
'attemptSeq': 1,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('断连失败 → retry:failed(network_error) 后 attemptSeq 递增再成功', () async {
|
||||||
|
direct.scriptedOutcomes.add(
|
||||||
|
const MediaDirectUploadException(message: '断连'),
|
||||||
|
);
|
||||||
|
final uploader = build(analytics: analytics);
|
||||||
|
|
||||||
|
uploader.addImages([pickedImage()]);
|
||||||
|
await pumpEventQueue();
|
||||||
|
expect(named('post_media_upload_failed').single, {
|
||||||
|
'mediaType': 'image',
|
||||||
|
'sizeBucket': 'lt_1mb',
|
||||||
|
'failureReason': 'network_error',
|
||||||
|
'attemptSeq': 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
uploader.retry(uploader.items.single.localId);
|
||||||
|
await pumpEventQueue();
|
||||||
|
|
||||||
|
expect(uploader.items.single.phase, MediaItemPhase.ready);
|
||||||
|
expect(named('post_media_upload_started'), hasLength(2));
|
||||||
|
expect(named('post_media_upload_succeeded'), hasLength(1));
|
||||||
|
});
|
||||||
|
|
||||||
|
test(
|
||||||
|
'createUpload 40000:failed(unsupported_format) 带 errorCode/httpStatus',
|
||||||
|
() async {
|
||||||
|
repository.onCreateMediaUpload = (_) async =>
|
||||||
|
throw const ApiBusinessException(code: 40000, message: 'bad mime');
|
||||||
|
final uploader = build(analytics: analytics);
|
||||||
|
|
||||||
|
uploader.addImages([pickedImage()]);
|
||||||
|
await pumpEventQueue();
|
||||||
|
|
||||||
|
expect(named('post_media_upload_failed').single, {
|
||||||
|
'mediaType': 'image',
|
||||||
|
'sizeBucket': 'lt_1mb',
|
||||||
|
'failureReason': 'unsupported_format',
|
||||||
|
'attemptSeq': 1,
|
||||||
|
'errorCode': 40000,
|
||||||
|
'httpStatus': 400,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test('在途删格报 cancelled;ready 后删格不报', () async {
|
||||||
|
direct.manual = true;
|
||||||
|
final uploader = build(analytics: analytics);
|
||||||
|
uploader.addImages([pickedImage(seed: 1)]);
|
||||||
|
await pumpEventQueue();
|
||||||
|
|
||||||
|
uploader.remove(uploader.items.single.localId);
|
||||||
|
expect(named('post_media_upload_failed').single, {
|
||||||
|
'mediaType': 'image',
|
||||||
|
'sizeBucket': 'lt_1mb',
|
||||||
|
'failureReason': 'cancelled',
|
||||||
|
'attemptSeq': 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
events.clear();
|
||||||
|
direct.manual = false;
|
||||||
|
uploader.addImages([pickedImage(seed: 2)]);
|
||||||
|
await pumpEventQueue();
|
||||||
|
expect(uploader.items.single.isReady, isTrue);
|
||||||
|
uploader.remove(uploader.items.single.localId);
|
||||||
|
expect(named('post_media_upload_failed'), isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('会话失效不上报失败(应用即将回登录页)', () async {
|
||||||
|
repository.onCreateMediaUpload = (_) async =>
|
||||||
|
throw const SessionExpiredException();
|
||||||
|
final uploader = build(analytics: analytics);
|
||||||
|
|
||||||
|
uploader.addImages([pickedImage()]);
|
||||||
|
await pumpEventQueue();
|
||||||
|
|
||||||
|
expect(uploader.items.single.isFailed, isTrue);
|
||||||
|
expect(named('post_media_upload_failed'), isEmpty);
|
||||||
|
expect(named('post_media_upload_started'), hasLength(1));
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> readyAssetJson({required String assetId}) => {
|
Map<String, dynamic> readyAssetJson({required String assetId}) => {
|
||||||
|
|||||||
@@ -0,0 +1,193 @@
|
|||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_exceptions.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/post_analytics.dart';
|
||||||
|
|
||||||
|
/// post 域埋点封装(T3-17):键集与 22 号白名单 v3 事件 22~29 逐一对齐、
|
||||||
|
/// 分桶边界、异常 → failureReason 映射、隐私红线(无内容 ID / 无精确字数)。
|
||||||
|
void main() {
|
||||||
|
late List<(String, Map<String, dynamic>?)> events;
|
||||||
|
late PostAnalytics analytics;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
events = [];
|
||||||
|
analytics = PostAnalytics(
|
||||||
|
(name, [props]) async => events.add((name, props)),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('键集与白名单对齐(22 号 §1)', () {
|
||||||
|
test('post_create_started:entryPoint', () {
|
||||||
|
analytics.postCreateStarted(entryPoint: PostEntryPoint.createTab);
|
||||||
|
expect(events.single.$1, 'post_create_started');
|
||||||
|
expect(events.single.$2, {'entryPoint': 'create_tab'});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('post_draft_saved:trigger + mediaCount', () {
|
||||||
|
analytics.postDraftSaved(trigger: DraftSaveTrigger.onExit, mediaCount: 3);
|
||||||
|
expect(events.single.$1, 'post_draft_saved');
|
||||||
|
expect(events.single.$2, {'trigger': 'on_exit', 'mediaCount': 3});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('post_publish_succeeded:五键齐 + 正文只出分桶', () {
|
||||||
|
analytics.postPublishSucceeded(
|
||||||
|
durationMs: 8200,
|
||||||
|
mediaCount: 2,
|
||||||
|
topicCount: 0,
|
||||||
|
textLength: 120,
|
||||||
|
fromDraft: true,
|
||||||
|
);
|
||||||
|
final props = events.single.$2!;
|
||||||
|
expect(
|
||||||
|
props.keys,
|
||||||
|
containsAll(<String>[
|
||||||
|
'durationMs',
|
||||||
|
'mediaCount',
|
||||||
|
'topicCount',
|
||||||
|
'textLengthBucket',
|
||||||
|
'fromDraft',
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(props['textLengthBucket'], 'medium');
|
||||||
|
expect(props['fromDraft'], isTrue);
|
||||||
|
// 红线 1/2:不带精确字数、不带 postId。
|
||||||
|
expect(props.containsKey('textLength'), isFalse);
|
||||||
|
expect(props.containsKey('postId'), isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(
|
||||||
|
'post_publish_failed:failureReason/attemptSeq/errorCode/httpStatus',
|
||||||
|
() {
|
||||||
|
analytics.postPublishFailed(
|
||||||
|
reason: PostPublishFailureReason.mediaUploadIncomplete,
|
||||||
|
attemptSeq: 2,
|
||||||
|
errorCode: 42203,
|
||||||
|
);
|
||||||
|
expect(events.single.$2, {
|
||||||
|
'failureReason': 'media_upload_incomplete',
|
||||||
|
'attemptSeq': 2,
|
||||||
|
'errorCode': 42203,
|
||||||
|
'httpStatus': 422,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test('post_publish_failed:网络错误无 errorCode/httpStatus', () {
|
||||||
|
analytics.postPublishFailed(
|
||||||
|
reason: PostPublishFailureReason.networkError,
|
||||||
|
attemptSeq: 1,
|
||||||
|
);
|
||||||
|
expect(events.single.$2, {
|
||||||
|
'failureReason': 'network_error',
|
||||||
|
'attemptSeq': 1,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('post_deleted:空属性集(单事件风格)', () {
|
||||||
|
analytics.postDeleted();
|
||||||
|
expect(events.single.$1, 'post_deleted');
|
||||||
|
expect(events.single.$2, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('媒体三段:键集齐 + 只出桶不出字节数/文件名', () {
|
||||||
|
analytics.mediaUploadStarted(
|
||||||
|
mediaType: MediaType.image,
|
||||||
|
byteSize: 3 * 1024 * 1024,
|
||||||
|
);
|
||||||
|
analytics.mediaUploadSucceeded(
|
||||||
|
mediaType: MediaType.image,
|
||||||
|
byteSize: 3 * 1024 * 1024,
|
||||||
|
durationMs: 4300,
|
||||||
|
);
|
||||||
|
analytics.mediaUploadFailed(
|
||||||
|
mediaType: MediaType.image,
|
||||||
|
byteSize: 3 * 1024 * 1024,
|
||||||
|
reason: MediaUploadFailureReason.cancelled,
|
||||||
|
attemptSeq: 1,
|
||||||
|
);
|
||||||
|
expect(events.map((event) => event.$1), [
|
||||||
|
'post_media_upload_started',
|
||||||
|
'post_media_upload_succeeded',
|
||||||
|
'post_media_upload_failed',
|
||||||
|
]);
|
||||||
|
expect(events[0].$2, {'mediaType': 'image', 'sizeBucket': 'mb_1_5'});
|
||||||
|
expect(events[1].$2, {
|
||||||
|
'mediaType': 'image',
|
||||||
|
'sizeBucket': 'mb_1_5',
|
||||||
|
'durationMs': 4300,
|
||||||
|
});
|
||||||
|
expect(events[2].$2, {
|
||||||
|
'mediaType': 'image',
|
||||||
|
'sizeBucket': 'mb_1_5',
|
||||||
|
'failureReason': 'cancelled',
|
||||||
|
'attemptSeq': 1,
|
||||||
|
});
|
||||||
|
for (final event in events) {
|
||||||
|
expect(event.$2!.containsKey('byteSize'), isFalse);
|
||||||
|
expect(event.$2!.containsKey('fileName'), isFalse);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('分桶边界(06 §1.3 红线 4)', () {
|
||||||
|
test('mediaSizeBucketOf 四档', () {
|
||||||
|
const mib = 1024 * 1024;
|
||||||
|
expect(mediaSizeBucketOf(0), 'lt_1mb');
|
||||||
|
expect(mediaSizeBucketOf(mib - 1), 'lt_1mb');
|
||||||
|
expect(mediaSizeBucketOf(mib), 'mb_1_5');
|
||||||
|
expect(mediaSizeBucketOf(5 * mib - 1), 'mb_1_5');
|
||||||
|
expect(mediaSizeBucketOf(5 * mib), 'mb_5_20');
|
||||||
|
expect(mediaSizeBucketOf(20 * mib - 1), 'mb_5_20');
|
||||||
|
expect(mediaSizeBucketOf(20 * mib), 'gte_20mb');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('异常 → 发布失败原因', () {
|
||||||
|
test('42203 → media_upload_incomplete;40905/40000 → validation_error', () {
|
||||||
|
expect(
|
||||||
|
postPublishFailureReasonOf(const MediaNotReadyException(message: 'x')),
|
||||||
|
PostPublishFailureReason.mediaUploadIncomplete,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
postPublishFailureReasonOf(
|
||||||
|
const IdempotencyMismatchException(message: 'x'),
|
||||||
|
),
|
||||||
|
PostPublishFailureReason.validationError,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
postPublishFailureReasonOf(
|
||||||
|
const ApiBusinessException(code: 40000, message: 'x'),
|
||||||
|
),
|
||||||
|
PostPublishFailureReason.validationError,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('40403 → not_found;40902 → server_error;限流/网络各自归位', () {
|
||||||
|
expect(
|
||||||
|
postPublishFailureReasonOf(const PostNotFoundException(message: 'x')),
|
||||||
|
PostPublishFailureReason.notFound,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
postPublishFailureReasonOf(
|
||||||
|
const PostVersionConflictException(message: 'x'),
|
||||||
|
),
|
||||||
|
PostPublishFailureReason.serverError,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
postPublishFailureReasonOf(const ApiRateLimitException()),
|
||||||
|
PostPublishFailureReason.rateLimited,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
postPublishFailureReasonOf(const ApiNetworkException()),
|
||||||
|
PostPublishFailureReason.networkError,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('会话失效不上报(null)', () {
|
||||||
|
expect(
|
||||||
|
postPublishFailureReasonOf(const SessionExpiredException()),
|
||||||
|
isNull,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,571 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/post_media_grid.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_exceptions.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_repository.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/post_analytics.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/post_compose_page.dart';
|
||||||
|
|
||||||
|
import '../../helpers/community_test_helpers.dart';
|
||||||
|
import '../../helpers/media_test_helpers.dart';
|
||||||
|
|
||||||
|
/// 发布页(T3-17):gating、两条提交路径(直接发布 / 存草稿退出)、
|
||||||
|
/// 三条失败语义(40905 / 42203 / 网络可重试同键)、删格重排、草稿恢复与
|
||||||
|
/// 发布漏斗埋点断言。
|
||||||
|
void main() {
|
||||||
|
late FakeCommunityRepository repository;
|
||||||
|
late CommunityController controller;
|
||||||
|
late FakeMediaImagePicker picker;
|
||||||
|
late FakeMediaCompressor compressor;
|
||||||
|
late FakeDirectUploadClient direct;
|
||||||
|
late List<(String, Map<String, dynamic>?)> events;
|
||||||
|
late PostAnalytics analytics;
|
||||||
|
late List<CreatePostRequest> created;
|
||||||
|
late List<UpdatePostRequest> updated;
|
||||||
|
bool? popResult;
|
||||||
|
int uploadSeq = 0;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
repository = FakeCommunityRepository();
|
||||||
|
controller = CommunityController(repository: repository);
|
||||||
|
picker = FakeMediaImagePicker([
|
||||||
|
decodablePickedImage(seed: 1),
|
||||||
|
decodablePickedImage(seed: 2),
|
||||||
|
decodablePickedImage(seed: 3),
|
||||||
|
]);
|
||||||
|
compressor = FakeMediaCompressor();
|
||||||
|
direct = FakeDirectUploadClient();
|
||||||
|
events = [];
|
||||||
|
analytics = PostAnalytics(
|
||||||
|
(name, [props]) async => events.add((name, props)),
|
||||||
|
);
|
||||||
|
created = [];
|
||||||
|
updated = [];
|
||||||
|
popResult = null;
|
||||||
|
uploadSeq = 0;
|
||||||
|
|
||||||
|
repository.onCreateMediaUpload = (_) async =>
|
||||||
|
credentials(assetId: 'a-${++uploadSeq}');
|
||||||
|
repository.onCompleteMediaUpload = (assetId) async =>
|
||||||
|
readyAsset(assetId: assetId);
|
||||||
|
repository.onCreatePost = (request, _) async {
|
||||||
|
created.add(request);
|
||||||
|
return Post.fromJson(samplePostJson(id: 'p-new', status: 'draft'));
|
||||||
|
};
|
||||||
|
repository.onUpdatePost = (postId, request) async {
|
||||||
|
updated.add(request);
|
||||||
|
return Post.fromJson(
|
||||||
|
samplePostJson(id: postId, version: request.version + 1),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
List<Map<String, dynamic>?> eventsNamed(String name) =>
|
||||||
|
events.where((event) => event.$1 == name).map((e) => e.$2).toList();
|
||||||
|
|
||||||
|
MediaUploader uploaderFactory(
|
||||||
|
CommunityRepository repo,
|
||||||
|
PostAnalytics? postAnalytics,
|
||||||
|
) => MediaUploader(
|
||||||
|
repository: repo,
|
||||||
|
picker: picker,
|
||||||
|
compressor: compressor,
|
||||||
|
directUpload: direct,
|
||||||
|
analytics: postAnalytics,
|
||||||
|
);
|
||||||
|
|
||||||
|
Future<void> pumpCompose(
|
||||||
|
WidgetTester tester, {
|
||||||
|
PostEntryPoint entryPoint = PostEntryPoint.createTab,
|
||||||
|
}) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: Builder(
|
||||||
|
builder: (context) => Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: ElevatedButton(
|
||||||
|
onPressed: () async {
|
||||||
|
popResult = await Navigator.of(context).push<bool>(
|
||||||
|
MaterialPageRoute<bool>(
|
||||||
|
builder: (_) => PostComposePage(
|
||||||
|
controller: controller,
|
||||||
|
entryPoint: entryPoint,
|
||||||
|
analytics: analytics,
|
||||||
|
uploaderFactory: uploaderFactory,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: const Text('打开发布页'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.tap(find.text('打开发布页'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
}
|
||||||
|
|
||||||
|
Finder publishButton() => find.widgetWithText(FilledButton, '发布');
|
||||||
|
|
||||||
|
bool publishEnabled(WidgetTester tester) =>
|
||||||
|
tester.widget<FilledButton>(publishButton()).onPressed != null;
|
||||||
|
|
||||||
|
Future<void> writeContent(
|
||||||
|
WidgetTester tester, [
|
||||||
|
String text = '带豆豆去了公园',
|
||||||
|
]) async {
|
||||||
|
await tester.enterText(find.byType(TextField), text);
|
||||||
|
await tester.pump();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 选 [count] 张图并等上传完成。
|
||||||
|
Future<void> pickImages(WidgetTester tester, int count) async {
|
||||||
|
picker.results
|
||||||
|
..clear()
|
||||||
|
..addAll(List.generate(count, (i) => decodablePickedImage(seed: i + 1)));
|
||||||
|
await tester.tap(find.byIcon(Icons.add_photo_alternate_outlined));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
}
|
||||||
|
|
||||||
|
group('gating(05 §2.2/§2.3 + 后端 content 必填)', () {
|
||||||
|
testWidgets('空正文禁用;输入即启用;post_create_started 只报一次', (tester) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
expect(publishEnabled(tester), isFalse);
|
||||||
|
|
||||||
|
await writeContent(tester, '豆');
|
||||||
|
expect(publishEnabled(tester), isTrue);
|
||||||
|
await writeContent(tester, '豆豆');
|
||||||
|
expect(eventsNamed('post_create_started'), hasLength(1));
|
||||||
|
expect(eventsNamed('post_create_started').single, {
|
||||||
|
'entryPoint': 'create_tab',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('图片在途禁用发布,全部 ready 才放行(孤儿防护)', (tester) async {
|
||||||
|
direct.manual = true;
|
||||||
|
await pumpCompose(tester, entryPoint: PostEntryPoint.feed);
|
||||||
|
await writeContent(tester);
|
||||||
|
|
||||||
|
picker.results
|
||||||
|
..clear()
|
||||||
|
..addAll([decodablePickedImage()]);
|
||||||
|
await tester.tap(find.byIcon(Icons.add_photo_alternate_outlined));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(publishEnabled(tester), isFalse);
|
||||||
|
// 页级汇总条在途可见。
|
||||||
|
expect(find.textContaining('正在上传'), findsOneWidget);
|
||||||
|
|
||||||
|
direct.calls.single.succeed();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(publishEnabled(tester), isTrue);
|
||||||
|
expect(find.textContaining('正在上传'), findsNothing);
|
||||||
|
// 选媒体也算首次输入(entryPoint 归因 feed)。
|
||||||
|
expect(eventsNamed('post_create_started').single, {'entryPoint': 'feed'});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('直接发布路径(建草稿 → PATCH 迁移)', () {
|
||||||
|
testWidgets('纯文字帖:createPost(draft) + PATCH publish → pop(true) + 漏斗事件', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester, '带豆豆去了公园');
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(created.single.status, PostStatus.draft);
|
||||||
|
expect(created.single.content, '带豆豆去了公园');
|
||||||
|
expect(created.single.category, PostCategory.general);
|
||||||
|
expect(created.single.media, isNull);
|
||||||
|
expect(updated.single.publish, isTrue);
|
||||||
|
expect(updated.single.version, 1);
|
||||||
|
// 建草稿刚落,media 与服务端一致 → PATCH 缺席不动。
|
||||||
|
expect(updated.single.media, isNull);
|
||||||
|
expect(popResult, isTrue);
|
||||||
|
|
||||||
|
final succeeded = eventsNamed('post_publish_succeeded').single!;
|
||||||
|
expect(succeeded['mediaCount'], 0);
|
||||||
|
expect(succeeded['topicCount'], 0);
|
||||||
|
expect(succeeded['textLengthBucket'], 'short');
|
||||||
|
expect(succeeded['fromDraft'], isFalse);
|
||||||
|
expect(eventsNamed('post_publish_failed'), isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('求助类目 + 两图:position 0..1、首图封面、mediaCount 2', (tester) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester);
|
||||||
|
await tester.tap(find.text('求助'));
|
||||||
|
await tester.pump();
|
||||||
|
await pickImages(tester, 2);
|
||||||
|
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(created.single.category, PostCategory.help);
|
||||||
|
final media = created.single.media!;
|
||||||
|
expect(media.map((item) => item.position), [0, 1]);
|
||||||
|
expect(media.map((item) => item.isCover), [true, false]);
|
||||||
|
expect(media.map((item) => item.assetId), ['a-1', 'a-2']);
|
||||||
|
expect(eventsNamed('post_publish_succeeded').single!['mediaCount'], 2);
|
||||||
|
// 媒体三段逐文件各一对。
|
||||||
|
expect(eventsNamed('post_media_upload_started'), hasLength(2));
|
||||||
|
expect(eventsNamed('post_media_upload_succeeded'), hasLength(2));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('删格重排:3 图删中间 → position 重发号 0..1,弃掉的 asset 不被引用', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester);
|
||||||
|
await pickImages(tester, 3);
|
||||||
|
expect(find.byType(Image), findsNWidgets(3));
|
||||||
|
|
||||||
|
// 第二格删除角标(角标按格序渲染)。
|
||||||
|
await tester.tap(find.byIcon(Icons.close).at(1));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byType(Image), findsNWidgets(2));
|
||||||
|
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final media = created.single.media!;
|
||||||
|
expect(media.map((item) => item.position), [0, 1]);
|
||||||
|
expect(media.map((item) => item.assetId), ['a-1', 'a-3']);
|
||||||
|
expect(media.map((item) => item.isCover), [true, false]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('发布失败三语义', () {
|
||||||
|
testWidgets('网络失败:横幅可重试 + 同键重放(不重复建帖)', (tester) async {
|
||||||
|
var attempts = 0;
|
||||||
|
repository.onCreatePost = (request, _) async {
|
||||||
|
attempts += 1;
|
||||||
|
if (attempts == 1) throw const ApiNetworkException();
|
||||||
|
created.add(request);
|
||||||
|
return Post.fromJson(samplePostJson(id: 'p-new', status: 'draft'));
|
||||||
|
};
|
||||||
|
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester);
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.byType(InlineErrorBanner), findsOneWidget);
|
||||||
|
expect(find.text('网络异常,请检查网络后重试'), findsOneWidget);
|
||||||
|
// 草稿还没落服务端 → 不谎称已保存。
|
||||||
|
expect(find.text('草稿已保存,可稍后继续发布'), findsNothing);
|
||||||
|
expect(eventsNamed('post_publish_failed').single, {
|
||||||
|
'failureReason': 'network_error',
|
||||||
|
'attemptSeq': 1,
|
||||||
|
});
|
||||||
|
expect(popResult, isNull);
|
||||||
|
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// 两次尝试同一幂等键(服务端命中首帖,不重复建帖)。
|
||||||
|
expect(repository.idempotencyKeys, hasLength(2));
|
||||||
|
expect(repository.idempotencyKeys[0], isNotNull);
|
||||||
|
expect(repository.idempotencyKeys[0], repository.idempotencyKeys[1]);
|
||||||
|
expect(popResult, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('迁移发布失败:明确「草稿已保存」,重试只补 PATCH', (tester) async {
|
||||||
|
var patches = 0;
|
||||||
|
repository.onUpdatePost = (postId, request) async {
|
||||||
|
patches += 1;
|
||||||
|
if (patches == 1) throw const ApiNetworkException();
|
||||||
|
updated.add(request);
|
||||||
|
return Post.fromJson(samplePostJson(id: postId));
|
||||||
|
};
|
||||||
|
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester);
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('草稿已保存,可稍后继续发布'), findsOneWidget);
|
||||||
|
expect(created, hasLength(1));
|
||||||
|
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// 草稿不重建,只重发 PATCH。
|
||||||
|
expect(created, hasLength(1));
|
||||||
|
expect(patches, 2);
|
||||||
|
expect(popResult, isTrue);
|
||||||
|
expect(eventsNamed('post_publish_failed').single!['attemptSeq'], 1);
|
||||||
|
expect(eventsNamed('post_publish_succeeded'), hasLength(1));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('40905 同键异 hash:提示已重置提交标识,再点即换新键', (tester) async {
|
||||||
|
var attempts = 0;
|
||||||
|
repository.onCreatePost = (request, _) async {
|
||||||
|
attempts += 1;
|
||||||
|
if (attempts == 1) {
|
||||||
|
throw const IdempotencyMismatchException(message: 'mismatch');
|
||||||
|
}
|
||||||
|
created.add(request);
|
||||||
|
return Post.fromJson(samplePostJson(id: 'p-new', status: 'draft'));
|
||||||
|
};
|
||||||
|
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester);
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('提交内容与上次重试不一致,已重置提交标识,请再点一次「发布」'), findsOneWidget);
|
||||||
|
expect(eventsNamed('post_publish_failed').single, {
|
||||||
|
'failureReason': 'validation_error',
|
||||||
|
'attemptSeq': 1,
|
||||||
|
'errorCode': 40905,
|
||||||
|
'httpStatus': 409,
|
||||||
|
});
|
||||||
|
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(repository.idempotencyKeys, hasLength(2));
|
||||||
|
expect(
|
||||||
|
repository.idempotencyKeys[0],
|
||||||
|
isNot(repository.idempotencyKeys[1]),
|
||||||
|
);
|
||||||
|
expect(popResult, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('42203 asset 未 ready:提示等图片就绪 + media_upload_incomplete', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
repository.onCreatePost = (_, _) async =>
|
||||||
|
throw const MediaNotReadyException(message: 'not ready');
|
||||||
|
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester);
|
||||||
|
await pickImages(tester, 1);
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('有图片还没上传完成,请等图片就绪后再发布'), findsOneWidget);
|
||||||
|
expect(eventsNamed('post_publish_failed').single, {
|
||||||
|
'failureReason': 'media_upload_incomplete',
|
||||||
|
'attemptSeq': 1,
|
||||||
|
'errorCode': 42203,
|
||||||
|
'httpStatus': 422,
|
||||||
|
});
|
||||||
|
expect(popResult, isNull);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('存草稿退出路径', () {
|
||||||
|
testWidgets('「存草稿」按钮:createPost(draft) + post_draft_saved(manual)', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester);
|
||||||
|
await pickImages(tester, 1);
|
||||||
|
await tester.tap(find.widgetWithText(TextButton, '存草稿'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(created.single.status, PostStatus.draft);
|
||||||
|
expect(updated, isEmpty);
|
||||||
|
expect(find.text('已保存草稿 ✓'), findsOneWidget);
|
||||||
|
expect(eventsNamed('post_draft_saved').single, {
|
||||||
|
'trigger': 'manual',
|
||||||
|
'mediaCount': 1,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('「取消」有内容 → 保留草稿:on_exit 保存后离页', (tester) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester);
|
||||||
|
await tester.tap(find.widgetWithText(TextButton, '取消'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('保留草稿?'), findsOneWidget);
|
||||||
|
await tester.tap(find.widgetWithText(FilledButton, '保留'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(created, hasLength(1));
|
||||||
|
expect(eventsNamed('post_draft_saved').single, {
|
||||||
|
'trigger': 'on_exit',
|
||||||
|
'mediaCount': 0,
|
||||||
|
});
|
||||||
|
expect(popResult, isFalse);
|
||||||
|
expect(find.byType(PostComposePage), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('「不保留」:已落服务端的草稿软删 + post_deleted', (tester) async {
|
||||||
|
final deleted = <String>[];
|
||||||
|
repository.onDeletePost = (postId) async => deleted.add(postId);
|
||||||
|
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester);
|
||||||
|
// 先显式存一次草稿,使服务端已有草稿。
|
||||||
|
await tester.tap(find.widgetWithText(TextButton, '存草稿'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.widgetWithText(TextButton, '取消'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.widgetWithText(TextButton, '不保留'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(deleted, ['p-new']);
|
||||||
|
expect(eventsNamed('post_deleted'), hasLength(1));
|
||||||
|
expect(popResult, isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('「继续编辑」留在页内,不动服务端', (tester) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester);
|
||||||
|
await tester.tap(find.widgetWithText(TextButton, '取消'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.widgetWithText(TextButton, '继续编辑'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.byType(PostComposePage), findsOneWidget);
|
||||||
|
expect(created, isEmpty);
|
||||||
|
expect(
|
||||||
|
repository.calls.where((c) => c.startsWith('deletePost')),
|
||||||
|
isEmpty,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('空表单「取消」直接离页,无弹窗无请求', (tester) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await tester.tap(find.widgetWithText(TextButton, '取消'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('保留草稿?'), findsNothing);
|
||||||
|
expect(created, isEmpty);
|
||||||
|
expect(popResult, isFalse);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('草稿恢复(最小实现:最新一条)', () {
|
||||||
|
setUp(() {
|
||||||
|
repository.onListMyPosts = (status) async =>
|
||||||
|
postPage([sampleDraft(version: 3, content: '上次没写完的草稿')]);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('进页恢复:提示条 + 正文预填 + 已有图片说明', (tester) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
|
||||||
|
expect(find.text('已恢复上次草稿'), findsOneWidget);
|
||||||
|
expect(find.text('上次没写完的草稿'), findsOneWidget);
|
||||||
|
expect(find.textContaining('草稿已含 1 张图片'), findsOneWidget);
|
||||||
|
// 恢复不是用户输入 → 不发 post_create_started。
|
||||||
|
expect(eventsNamed('post_create_started'), isEmpty);
|
||||||
|
expect(repository.calls, contains('listMyPosts:draft'));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('恢复后发布:不再建草稿,PATCH 带 version 与 fromDraft=true', (tester) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(created, isEmpty);
|
||||||
|
expect(updated.single.version, 3);
|
||||||
|
expect(updated.single.publish, isTrue);
|
||||||
|
// 未重新选图 → media 缺席不动(服务端既有图保留)。
|
||||||
|
expect(updated.single.media, isNull);
|
||||||
|
final succeeded = eventsNamed('post_publish_succeeded').single!;
|
||||||
|
expect(succeeded['fromDraft'], isTrue);
|
||||||
|
expect(succeeded['mediaCount'], 1);
|
||||||
|
expect(popResult, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('恢复后重新选图:PATCH 整组替换', (tester) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await pickImages(tester, 1);
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(updated.single.media!.map((item) => item.assetId), ['a-1']);
|
||||||
|
expect(eventsNamed('post_publish_succeeded').single!['mediaCount'], 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('40902 乐观锁:自动取新 version 重提一次即成功', (tester) async {
|
||||||
|
var patches = 0;
|
||||||
|
repository.onUpdatePost = (postId, request) async {
|
||||||
|
patches += 1;
|
||||||
|
if (patches == 1) {
|
||||||
|
throw const PostVersionConflictException(message: 'stale');
|
||||||
|
}
|
||||||
|
updated.add(request);
|
||||||
|
return Post.fromJson(samplePostJson(id: postId));
|
||||||
|
};
|
||||||
|
repository.onGetPost = (postId) async => Post.fromJson(
|
||||||
|
samplePostJson(id: postId, status: 'draft', version: 9),
|
||||||
|
);
|
||||||
|
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(updated.single.version, 9);
|
||||||
|
expect(eventsNamed('post_publish_failed'), isEmpty);
|
||||||
|
expect(popResult, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('「清空」清掉恢复内容与提示条', (tester) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await tester.tap(find.widgetWithText(TextButton, '清空'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('已恢复上次草稿'), findsNothing);
|
||||||
|
expect(find.text('上次没写完的草稿'), findsNothing);
|
||||||
|
expect(publishEnabled(tester), isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('草稿恢复失败静默降级为新建(不打断发布)', (tester) async {
|
||||||
|
repository.onListMyPosts = (_) async => throw const ApiNetworkException();
|
||||||
|
|
||||||
|
await pumpCompose(tester);
|
||||||
|
expect(find.text('已恢复上次草稿'), findsNothing);
|
||||||
|
expect(find.byType(InlineErrorBanner), findsNothing);
|
||||||
|
|
||||||
|
await writeContent(tester);
|
||||||
|
await tester.tap(publishButton());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(created, hasLength(1));
|
||||||
|
expect(popResult, isTrue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('页面结构(05 §2.3)', () {
|
||||||
|
testWidgets('AppBar 三件套 + 媒体编辑格 + 类目二选 + 位置占位', (tester) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
|
||||||
|
expect(find.text('发布动态'), findsOneWidget);
|
||||||
|
expect(find.widgetWithText(TextButton, '取消'), findsOneWidget);
|
||||||
|
expect(find.widgetWithText(TextButton, '存草稿'), findsOneWidget);
|
||||||
|
expect(publishButton(), findsOneWidget);
|
||||||
|
expect(find.byType(PostMediaEditGrid), findsOneWidget);
|
||||||
|
expect(find.text('日常分享'), findsOneWidget);
|
||||||
|
expect(find.text('求助'), findsOneWidget);
|
||||||
|
// ai_creation 是 M4 预留读侧值,不给提交面。
|
||||||
|
expect(find.textContaining('AI'), findsNothing);
|
||||||
|
await tester.drag(find.byType(ListView), const Offset(0, -240));
|
||||||
|
await tester.pump();
|
||||||
|
await tester.tap(find.text('添加位置(选填)'));
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.text('位置功能即将上线'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('正文 1000 字上限计数器在位', (tester) async {
|
||||||
|
await pumpCompose(tester);
|
||||||
|
await writeContent(tester, '豆豆');
|
||||||
|
expect(find.text('2/1000'), findsOneWidget);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/home/home_page.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||||
|
import 'package:patbond_flutter/state/app_state.dart';
|
||||||
|
|
||||||
|
import '../../helpers/auth_test_helpers.dart';
|
||||||
|
import '../../helpers/community_test_helpers.dart';
|
||||||
|
|
||||||
|
/// T3.5-10 首页问候语真实化。
|
||||||
|
///
|
||||||
|
/// ADR-022 决策 D3.5-1:本迭代**只做问候语**,天气 / 位置 / 圈子 / 促销卡
|
||||||
|
/// 刻意保留为 demo 占位——本文件同时钉住「保留项仍在」,避免后续有人以
|
||||||
|
/// 「顺手清理」为名越出拍板范围(真要动它们得先改 ADR)。
|
||||||
|
void main() {
|
||||||
|
late FakeCommunityRepository community;
|
||||||
|
late CommunityController feed;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
community = FakeCommunityRepository();
|
||||||
|
community.onFeed = (_, _) async => feedPage(const []);
|
||||||
|
feed = CommunityController(repository: community);
|
||||||
|
});
|
||||||
|
|
||||||
|
Future<ProfileController> pumpHome(
|
||||||
|
WidgetTester tester, {
|
||||||
|
required FakeAuthRepository auth,
|
||||||
|
}) async {
|
||||||
|
tester.view.physicalSize = const Size(700, 1600);
|
||||||
|
tester.view.devicePixelRatio = 1.0;
|
||||||
|
addTearDown(tester.view.reset);
|
||||||
|
final profile = ProfileController(
|
||||||
|
authRepository: auth,
|
||||||
|
communityRepository: community,
|
||||||
|
);
|
||||||
|
addTearDown(profile.dispose);
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: Scaffold(
|
||||||
|
body: HomePage(
|
||||||
|
appState: AppState(),
|
||||||
|
communityController: feed,
|
||||||
|
profileController: profile,
|
||||||
|
onOpenServices: (_) {},
|
||||||
|
onOpenCompose: () {},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return profile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 当前时段问候前缀(与页面实现同一套阈值;避免测试跟着挂钟漂移)。
|
||||||
|
String greetingPrefix([DateTime? now]) {
|
||||||
|
final hour = (now ?? DateTime.now()).hour;
|
||||||
|
if (hour < 6) return '夜深了';
|
||||||
|
if (hour < 11) return '早上好';
|
||||||
|
if (hour < 14) return '中午好';
|
||||||
|
if (hour < 18) return '下午好';
|
||||||
|
return '晚上好';
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgets('有昵称:问候语用昵称,demo 宠物名「豆豆」不再出现', (tester) async {
|
||||||
|
final profile = await pumpHome(
|
||||||
|
tester,
|
||||||
|
auth: FakeAuthRepository(
|
||||||
|
meHandler: () async => buildProfile(username: 'llx', nickname: '小柴'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await profile.refresh();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('${greetingPrefix()},小柴 👋'), findsOneWidget);
|
||||||
|
expect(find.textContaining('豆豆'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('无昵称:问候语回退 username(与资料页同规则)', (tester) async {
|
||||||
|
final profile = await pumpHome(
|
||||||
|
tester,
|
||||||
|
auth: FakeAuthRepository(
|
||||||
|
meHandler: () async => buildProfile(username: 'llx'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await profile.refresh();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('${greetingPrefix()},llx 👋'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('资料未到手:只问候不称名(不编造假名)', (tester) async {
|
||||||
|
await pumpHome(
|
||||||
|
tester,
|
||||||
|
auth: FakeAuthRepository(
|
||||||
|
meHandler: () async => throw const ApiNetworkException('断网'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('${greetingPrefix()} 👋'), findsOneWidget);
|
||||||
|
// 不出现「问候,某某」形态(更不会退回 demo 宠物名)。
|
||||||
|
expect(find.textContaining('${greetingPrefix()},'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('改昵称后首页同步(与资料页共用同一控制器,无需手动刷新)', (tester) async {
|
||||||
|
final auth = FakeAuthRepository(
|
||||||
|
meHandler: () async => buildProfile(username: 'llx'),
|
||||||
|
updateMeHandler: (_) async =>
|
||||||
|
buildProfile(username: 'llx', nickname: '小柴'),
|
||||||
|
);
|
||||||
|
final profile = await pumpHome(tester, auth: auth);
|
||||||
|
await profile.refresh();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('${greetingPrefix()},llx 👋'), findsOneWidget);
|
||||||
|
|
||||||
|
await profile.save(
|
||||||
|
const UpdateMeRequest(nickname: PatchField<String>.value('小柴')),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('${greetingPrefix()},小柴 👋'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('刻意保留的 demo 占位仍在(ADR-022 钉死的范围)', (tester) async {
|
||||||
|
final profile = await pumpHome(
|
||||||
|
tester,
|
||||||
|
auth: FakeAuthRepository(meHandler: () async => buildProfile()),
|
||||||
|
);
|
||||||
|
await profile.refresh();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// 天气 / 位置(外部服务未接)。
|
||||||
|
expect(find.text('北京 · 朝阳区'), findsOneWidget);
|
||||||
|
expect(find.text('28°C 晴'), findsOneWidget);
|
||||||
|
// 圈子 = 话题(ADR-018 剪出)。
|
||||||
|
expect(find.text('柴犬圈'), findsOneWidget);
|
||||||
|
// 促销卡(M5 服务域)。
|
||||||
|
expect(find.text('新用户首单立减 ¥20'), findsOneWidget);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ import 'package:patbond_flutter/core/widgets/feed_skeleton.dart';
|
|||||||
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/post_card.dart';
|
import 'package:patbond_flutter/core/widgets/post_card.dart';
|
||||||
import 'package:patbond_flutter/features/community/community_controller.dart';
|
import 'package:patbond_flutter/features/community/community_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_interaction_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/community/community_models.dart';
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
import 'package:patbond_flutter/features/community/feed_analytics.dart';
|
import 'package:patbond_flutter/features/community/feed_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/home/home_page.dart';
|
import 'package:patbond_flutter/features/home/home_page.dart';
|
||||||
@@ -38,16 +39,23 @@ void main() {
|
|||||||
late CommunityController controller;
|
late CommunityController controller;
|
||||||
late List<(String, Map<String, dynamic>?)> events;
|
late List<(String, Map<String, dynamic>?)> events;
|
||||||
late FeedAnalytics analytics;
|
late FeedAnalytics analytics;
|
||||||
late int createTaps;
|
late int composeTaps;
|
||||||
|
late List<String> openedPosts;
|
||||||
|
|
||||||
setUp(() {
|
setUp(() {
|
||||||
repository = FakeCommunityRepository();
|
repository = FakeCommunityRepository();
|
||||||
controller = CommunityController(repository: repository);
|
|
||||||
events = [];
|
events = [];
|
||||||
|
controller = CommunityController(
|
||||||
|
repository: repository,
|
||||||
|
interactionAnalytics: CommunityInteractionAnalytics(
|
||||||
|
(name, [props]) async => events.add((name, props)),
|
||||||
|
),
|
||||||
|
);
|
||||||
analytics = FeedAnalytics(
|
analytics = FeedAnalytics(
|
||||||
(name, [props]) async => events.add((name, props)),
|
(name, [props]) async => events.add((name, props)),
|
||||||
);
|
);
|
||||||
createTaps = 0;
|
composeTaps = 0;
|
||||||
|
openedPosts = [];
|
||||||
});
|
});
|
||||||
|
|
||||||
List<Map<String, dynamic>?> eventsNamed(String name) =>
|
List<Map<String, dynamic>?> eventsNamed(String name) =>
|
||||||
@@ -64,7 +72,8 @@ void main() {
|
|||||||
feedAnalytics: analytics,
|
feedAnalytics: analytics,
|
||||||
isActive: isActive,
|
isActive: isActive,
|
||||||
onOpenServices: (_) {},
|
onOpenServices: (_) {},
|
||||||
onOpenCreate: () => createTaps++,
|
onOpenCompose: () => composeTaps++,
|
||||||
|
onOpenPost: openedPosts.add,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -89,7 +98,7 @@ void main() {
|
|||||||
expect(find.byType(FeedSkeleton), findsNothing);
|
expect(find.byType(FeedSkeleton), findsNothing);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('四态 · empty:空态插画 + 「发布第一条」CTA 去创作', (tester) async {
|
testWidgets('四态 · empty:空态插画 + 「发布第一条」CTA 进发布页', (tester) async {
|
||||||
repository.onFeed = (_, _) async => feedPage(const []);
|
repository.onFeed = (_, _) async => feedPage(const []);
|
||||||
|
|
||||||
await pumpHome(tester);
|
await pumpHome(tester);
|
||||||
@@ -99,7 +108,7 @@ void main() {
|
|||||||
await tester.drag(list(), const Offset(0, -300));
|
await tester.drag(list(), const Offset(0, -300));
|
||||||
await tester.pump();
|
await tester.pump();
|
||||||
await tester.tap(find.text('发布第一条'));
|
await tester.tap(find.text('发布第一条'));
|
||||||
expect(createTaps, 1);
|
expect(composeTaps, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('四态 · error:横幅 + 重试恢复 ready,feed_load_failed 上报', (tester) async {
|
testWidgets('四态 · error:横幅 + 重试恢复 ready,feed_load_failed 上报', (tester) async {
|
||||||
@@ -361,7 +370,7 @@ void main() {
|
|||||||
expect(eventsNamed('feed_viewed'), hasLength(2));
|
expect(eventsNamed('feed_viewed'), hasLength(2));
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('T3-14 取舍:整卡点按提示详情接入中(不导航 demo 详情)', (tester) async {
|
testWidgets('T3-15 导航接通:整卡与评论钮点按回调 onOpenPost(占位提示移除)', (tester) async {
|
||||||
repository.onFeed = (_, _) async => feedPage([textCard('p-1')]);
|
repository.onFeed = (_, _) async => feedPage([textCard('p-1')]);
|
||||||
|
|
||||||
await pumpHome(tester);
|
await pumpHome(tester);
|
||||||
@@ -371,7 +380,72 @@ void main() {
|
|||||||
|
|
||||||
await tester.tap(find.text('动态内容 p-1'));
|
await tester.tap(find.text('动态内容 p-1'));
|
||||||
await tester.pump();
|
await tester.pump();
|
||||||
expect(find.text('帖子详情正在接入真实数据,敬请期待'), findsOneWidget);
|
expect(find.text('帖子详情正在接入真实数据,敬请期待'), findsNothing);
|
||||||
|
expect(openedPosts, ['p-1']);
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.chat_bubble_outline));
|
||||||
|
await tester.pump();
|
||||||
|
expect(openedPosts, ['p-1', 'p-1']);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('T3-16 互动接线:点赞乐观翻转即时显示,成功报 post_liked(source=feed)', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
repository.onFeed = (_, _) async => feedPage([
|
||||||
|
FeedCard.fromJson({
|
||||||
|
...sampleFeedCardJson(likeCount: 6),
|
||||||
|
'coverImage': null,
|
||||||
|
'mediaCount': 0,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
final like = Completer<LikeState>();
|
||||||
|
repository.onLikeToggle = (_, _) => like.future;
|
||||||
|
|
||||||
|
await pumpHome(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.drag(list(), const Offset(0, -800));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.favorite_border));
|
||||||
|
await tester.pump();
|
||||||
|
// 乐观翻转:请求未回已 +1(同帧反馈)。
|
||||||
|
expect(find.text('7'), findsOneWidget);
|
||||||
|
expect(repository.calls, contains('like:p-1'));
|
||||||
|
|
||||||
|
like.complete(const LikeState(liked: true, likeCount: 7));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(eventsNamed('post_liked'), [
|
||||||
|
{'source': 'feed'},
|
||||||
|
]);
|
||||||
|
expect(find.text('7'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('T3-16 互动接线:点赞失败回滚 + SnackBar「操作失败,请重试」', (tester) async {
|
||||||
|
repository.onFeed = (_, _) async => feedPage([
|
||||||
|
FeedCard.fromJson({
|
||||||
|
...sampleFeedCardJson(likeCount: 6),
|
||||||
|
'coverImage': null,
|
||||||
|
'mediaCount': 0,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
final like = Completer<LikeState>();
|
||||||
|
repository.onLikeToggle = (_, _) => like.future;
|
||||||
|
|
||||||
|
await pumpHome(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.drag(list(), const Offset(0, -800));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.favorite_border));
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.text('7'), findsOneWidget);
|
||||||
|
|
||||||
|
like.completeError(const ApiNetworkException());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
// 回滚成对恢复 + SnackBar;不报 post_liked。
|
||||||
|
expect(find.text('6'), findsOneWidget);
|
||||||
|
expect(find.text('操作失败,请重试'), findsOneWidget);
|
||||||
|
expect(eventsNamed('post_liked'), isEmpty);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('搜索词过滤已加载卡片;无命中显搜索空态', (tester) async {
|
testWidgets('搜索词过滤已加载卡片;无命中显搜索空态', (tester) async {
|
||||||
|
|||||||
@@ -0,0 +1,87 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/app/app.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/post_card.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/session_manager.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
import '../../helpers/auth_test_helpers.dart';
|
||||||
|
import '../../helpers/community_test_helpers.dart';
|
||||||
|
import '../../helpers/media_test_helpers.dart';
|
||||||
|
import '../../helpers/pet_test_helpers.dart';
|
||||||
|
|
||||||
|
/// 主壳发布闭环(T3-17):创作 Tab 入口 → 发布页 → 发布成功 →
|
||||||
|
/// 回首页 Feed 并整体刷新 → 新帖置顶可见(M3 验收「发布后可见」的
|
||||||
|
/// 客户端侧断言;跨客户端可见性走 compose 真链路实测,见 26 号报告 §5)。
|
||||||
|
void main() {
|
||||||
|
testWidgets('创作 Tab「发布动态」→ 发布 → 回首页 Feed 刷新,新帖置顶', (tester) async {
|
||||||
|
SharedPreferences.setMockInitialValues({});
|
||||||
|
final session = SessionManager(store: InMemoryTokenStore())
|
||||||
|
..markAuthenticated();
|
||||||
|
final repository = FakeCommunityRepository();
|
||||||
|
|
||||||
|
var published = false;
|
||||||
|
repository.onFeed = (_, _) async => feedPage(
|
||||||
|
published
|
||||||
|
? [
|
||||||
|
FeedCard.fromJson({
|
||||||
|
...sampleFeedCardJson(id: 'p-new'),
|
||||||
|
'contentPreview': '刚发布的动态',
|
||||||
|
}),
|
||||||
|
sampleFeedCard(id: 'p-old'),
|
||||||
|
]
|
||||||
|
: [sampleFeedCard(id: 'p-old')],
|
||||||
|
);
|
||||||
|
repository.onCreatePost = (request, _) async =>
|
||||||
|
Post.fromJson(samplePostJson(id: 'p-new', status: 'draft'));
|
||||||
|
repository.onUpdatePost = (postId, request) async {
|
||||||
|
published = true;
|
||||||
|
return Post.fromJson(samplePostJson(id: postId, version: 2));
|
||||||
|
};
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
App(
|
||||||
|
sessionManager: session,
|
||||||
|
authRepository: FakeAuthRepository(),
|
||||||
|
petsRepository: FakePetsRepository(),
|
||||||
|
communityRepository: repository,
|
||||||
|
mediaUploaderFactory: (repo, analytics) => MediaUploader(
|
||||||
|
repository: repo,
|
||||||
|
picker: FakeMediaImagePicker([decodablePickedImage()]),
|
||||||
|
compressor: FakeMediaCompressor(),
|
||||||
|
directUpload: FakeDirectUploadClient(),
|
||||||
|
analytics: analytics,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// 创作 Tab → 发布页(entryPoint=create_tab)。
|
||||||
|
await tester.tap(find.text('创作'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('发布动态'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.widgetWithText(TextButton, '存草稿'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.enterText(find.byType(TextField).first, '刚发布的动态');
|
||||||
|
await tester.pump();
|
||||||
|
await tester.tap(find.widgetWithText(FilledButton, '发布'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// 回首页 Feed(发布页已出栈)+ 整体刷新 + 新帖置顶。
|
||||||
|
expect(find.widgetWithText(TextButton, '存草稿'), findsNothing);
|
||||||
|
expect(find.text('已发布,去首页看看吧 🐾'), findsOneWidget);
|
||||||
|
expect(
|
||||||
|
repository.calls.where((call) => call.startsWith('feed:')),
|
||||||
|
hasLength(2),
|
||||||
|
);
|
||||||
|
// 卡片区在首页家具(story 环 / 促销位)之下,滚到可见后断言首位。
|
||||||
|
await tester.drag(find.byType(ListView).first, const Offset(0, -320));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
final cards = tester.widgetList<PostCard>(find.byType(PostCard)).toList();
|
||||||
|
expect(cards.first.card.id, 'p-new');
|
||||||
|
expect(find.text('刚发布的动态'), findsWidgets);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/app/app_localization.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/app_date_picker.dart';
|
||||||
import 'package:patbond_flutter/features/pets/care_reminder_form_page.dart';
|
import 'package:patbond_flutter/features/pets/care_reminder_form_page.dart';
|
||||||
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/pets/pet_exceptions.dart';
|
import 'package:patbond_flutter/features/pets/pet_exceptions.dart';
|
||||||
@@ -34,6 +36,10 @@ void main() {
|
|||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
MaterialApp(
|
MaterialApp(
|
||||||
theme: buildAppTheme(),
|
theme: buildAppTheme(),
|
||||||
|
// M3.5-01:与生产一致挂 zh-CN delegate。
|
||||||
|
localizationsDelegates: appLocalizationsDelegates,
|
||||||
|
supportedLocales: appSupportedLocales,
|
||||||
|
locale: appLocale,
|
||||||
home: Builder(
|
home: Builder(
|
||||||
builder: (context) => Scaffold(
|
builder: (context) => Scaffold(
|
||||||
body: Center(
|
body: Center(
|
||||||
@@ -67,7 +73,7 @@ void main() {
|
|||||||
);
|
);
|
||||||
await tester.tap(find.text('到期日期'));
|
await tester.tap(find.text('到期日期'));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
await tester.tap(find.text('OK'));
|
await tester.tap(find.text('确定'));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,4 +130,36 @@ void main() {
|
|||||||
expect(failed[1]!['failureReason'], 'network_error');
|
expect(failed[1]!['failureReason'], 'network_error');
|
||||||
expect(failed[1]!['attemptSeq'], 2);
|
expect(failed[1]!['attemptSeq'], 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
group('M3.5-02 · 到期日期录入(firstDate 就是今天的那一格)', () {
|
||||||
|
testWidgets('未选日期时也有「今天」快捷键;一键落今天并清掉必填校验错', (tester) async {
|
||||||
|
await pumpForm(tester);
|
||||||
|
|
||||||
|
// 先触发必填校验错。
|
||||||
|
await tester.tap(find.text('保存提醒'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('未选择'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('今天'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text(dateToJson(DateTime.now())), findsOneWidget);
|
||||||
|
expect(find.text('未选择'), findsNothing);
|
||||||
|
expect(find.text('选择日期'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('业务约束不变:firstDate 今天(不许补记过去)、lastDate 五年后', (tester) async {
|
||||||
|
await pumpForm(tester);
|
||||||
|
|
||||||
|
await tester.tap(find.widgetWithText(ListTile, '到期日期'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final dialog = tester.widget<DatePickerDialog>(
|
||||||
|
find.byType(DatePickerDialog),
|
||||||
|
);
|
||||||
|
expect(dialog.firstDate, dateOnly(DateTime.now()));
|
||||||
|
expect(dialog.lastDate, DateTime(DateTime.now().year + 5));
|
||||||
|
expect(find.text('选择日期'), findsOneWidget);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import 'dart:async';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/app/app_localization.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
||||||
import 'package:patbond_flutter/features/pets/care_reminder_form_page.dart';
|
import 'package:patbond_flutter/features/pets/care_reminder_form_page.dart';
|
||||||
@@ -39,6 +40,11 @@ void main() {
|
|||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
MaterialApp(
|
MaterialApp(
|
||||||
theme: buildAppTheme(),
|
theme: buildAppTheme(),
|
||||||
|
// M3.5-01:与生产一致挂 zh-CN delegate,日期选择器等内置组件文案
|
||||||
|
// 才是用户真机看到的中文。
|
||||||
|
localizationsDelegates: appLocalizationsDelegates,
|
||||||
|
supportedLocales: appSupportedLocales,
|
||||||
|
locale: appLocale,
|
||||||
home: const Scaffold(body: Text('详情基底')),
|
home: const Scaffold(body: Text('详情基底')),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -206,7 +212,7 @@ void main() {
|
|||||||
);
|
);
|
||||||
await tester.tap(find.text('到期日期'));
|
await tester.tap(find.text('到期日期'));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
await tester.tap(find.text('OK'));
|
await tester.tap(find.text('确定'));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
await tester.tap(find.text('保存提醒'));
|
await tester.tap(find.text('保存提醒'));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/app/app_localization.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/app_date_picker.dart';
|
||||||
import 'package:patbond_flutter/features/pets/health_event_form_page.dart';
|
import 'package:patbond_flutter/features/pets/health_event_form_page.dart';
|
||||||
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/pets/pet_exceptions.dart';
|
import 'package:patbond_flutter/features/pets/pet_exceptions.dart';
|
||||||
@@ -34,6 +38,11 @@ void main() {
|
|||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
MaterialApp(
|
MaterialApp(
|
||||||
theme: buildAppTheme(),
|
theme: buildAppTheme(),
|
||||||
|
// M3.5-01:与生产一致挂 zh-CN delegate,否则测里的日期选择器是
|
||||||
|
// 英文兜底,测不出用户真机看到的东西。
|
||||||
|
localizationsDelegates: appLocalizationsDelegates,
|
||||||
|
supportedLocales: appSupportedLocales,
|
||||||
|
locale: appLocale,
|
||||||
home: Builder(
|
home: Builder(
|
||||||
builder: (context) => Scaffold(
|
builder: (context) => Scaffold(
|
||||||
body: Center(
|
body: Center(
|
||||||
@@ -192,4 +201,77 @@ void main() {
|
|||||||
expect(failed[2]!['failureReason'], 'network_error');
|
expect(failed[2]!['failureReason'], 'network_error');
|
||||||
expect(failed[2]!['attemptSeq'], 3);
|
expect(failed[2]!['attemptSeq'], 3);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
group('M3.5-02 · 日期录入(用户误录 2026-04 的那一格)', () {
|
||||||
|
testWidgets('点「今天」直接落今天,不开弹窗;同时算首次输入触发 started', (tester) async {
|
||||||
|
await pumpForm(tester);
|
||||||
|
|
||||||
|
final expected = dateToJson(DateTime.now());
|
||||||
|
// 先把日期改成远月,模拟用户翻错月份后的状态。
|
||||||
|
await tester.tap(find.widgetWithText(ListTile, '发生日期'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.byIcon(Icons.edit_outlined));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.enterText(
|
||||||
|
find.descendant(
|
||||||
|
of: find.byType(DatePickerDialog),
|
||||||
|
matching: find.byType(TextField),
|
||||||
|
),
|
||||||
|
'2026/04/09',
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('确定'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('2026-04-09'), findsOneWidget);
|
||||||
|
|
||||||
|
// 一键回今天:不经日期选择器。
|
||||||
|
await tester.tap(find.text('今天'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text(expected), findsOneWidget);
|
||||||
|
expect(find.text('选择日期'), findsNothing);
|
||||||
|
expect(eventsOf('health_record_create_started'), hasLength(1));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('选择器为中文,且业务约束不变(firstDate 1990 / lastDate 今天,不许未来)', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
await pumpForm(tester);
|
||||||
|
|
||||||
|
await tester.tap(find.widgetWithText(ListTile, '发生日期'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('选择日期'), findsOneWidget);
|
||||||
|
expect(find.text('确定'), findsOneWidget);
|
||||||
|
expect(find.text('取消'), findsOneWidget);
|
||||||
|
// 手输快路可达(原生 calendarOnly 会砍掉它)。
|
||||||
|
expect(find.byIcon(Icons.edit_outlined), findsOneWidget);
|
||||||
|
|
||||||
|
final dialog = tester.widget<DatePickerDialog>(
|
||||||
|
find.byType(DatePickerDialog),
|
||||||
|
);
|
||||||
|
expect(dialog.firstDate, DateTime(1990));
|
||||||
|
expect(dialog.lastDate, dateOnly(DateTime.now()));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('提交中禁用「今天」快捷键', (tester) async {
|
||||||
|
final gate = Completer<HealthEvent>();
|
||||||
|
repository.createHealthEventHandler = (petId, req) => gate.future;
|
||||||
|
await pumpForm(tester);
|
||||||
|
await fillValid(tester);
|
||||||
|
|
||||||
|
await tester.tap(find.text('保存记录'));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(
|
||||||
|
tester
|
||||||
|
.widget<TextButton>(find.widgetWithText(TextButton, '今天'))
|
||||||
|
.onPressed,
|
||||||
|
isNull,
|
||||||
|
);
|
||||||
|
|
||||||
|
gate.complete(buildHealthEvent('he-1'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -277,4 +277,30 @@ void main() {
|
|||||||
expect(reminderStatusColor(dismissed, now), AppColors.muted);
|
expect(reminderStatusColor(dismissed, now), AppColors.muted);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
group('M3.5-03 · monthlyExpenseCardLabel(花费卡展示实际月份)', () {
|
||||||
|
test('同年只给月份,卡片一行放得下(不再硬编码「本月花费」)', () {
|
||||||
|
final now = DateTime(2026, 9, 10);
|
||||||
|
expect(monthlyExpenseCardLabel('2026-09', now: now), '9 月花费');
|
||||||
|
// 用户误录场景:记录落在 4 月,卡片就该明说是 4 月,而不是「本月」。
|
||||||
|
expect(monthlyExpenseCardLabel('2026-04', now: now), '4 月花费');
|
||||||
|
expect(monthlyExpenseCardLabel('2026-12', now: now), '12 月花费');
|
||||||
|
expect(monthlyExpenseCardLabel('2026-01', now: now), '1 月花费');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('跨年(服务端归月年份 ≠ 设备当前年份)补年份消歧', () {
|
||||||
|
// 设备已跨到 2027-01,服务端 tz 窗口仍落在 2026-12。
|
||||||
|
final now = DateTime(2027, 1, 1);
|
||||||
|
expect(monthlyExpenseCardLabel('2026-12', now: now), '2026/12 花费');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('月份串非法退回「本月花费」(不崩、不显示脏值)', () {
|
||||||
|
final now = DateTime(2026, 9, 10);
|
||||||
|
expect(monthlyExpenseCardLabel('', now: now), '本月花费');
|
||||||
|
expect(monthlyExpenseCardLabel('2026-9', now: now), '本月花费');
|
||||||
|
expect(monthlyExpenseCardLabel('2026-13', now: now), '本月花费');
|
||||||
|
expect(monthlyExpenseCardLabel('2026-00', now: now), '本月花费');
|
||||||
|
expect(monthlyExpenseCardLabel('2026-09-01', now: now), '本月花费');
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,323 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/pet_avatar.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
import 'package:patbond_flutter/features/pets/pet_detail_page.dart';
|
||||||
|
import 'package:patbond_flutter/features/pets/pet_display.dart';
|
||||||
|
import 'package:patbond_flutter/features/pets/pet_exceptions.dart';
|
||||||
|
import 'package:patbond_flutter/features/pets/pet_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/pets/pets_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/pets/pets_page.dart';
|
||||||
|
|
||||||
|
import '../../helpers/community_test_helpers.dart';
|
||||||
|
import '../../helpers/media_test_helpers.dart';
|
||||||
|
import '../../helpers/pet_test_helpers.dart';
|
||||||
|
|
||||||
|
/// T3.5-09 宠物头像接线。
|
||||||
|
///
|
||||||
|
/// 两条不变量在这里守住:
|
||||||
|
/// 1. **权限按 WRITE 档呈现**(owner + caregiver 有入口,viewer 没有),与
|
||||||
|
/// 资料编辑(MANAGE,仅 owner)不同档。
|
||||||
|
/// 2. **纯头像 PATCH 不夹带资料字段**——夹带会把服务端定档抬到 MANAGE,
|
||||||
|
/// caregiver 立刻 403。
|
||||||
|
void main() {
|
||||||
|
const signedAvatar =
|
||||||
|
'https://minio.local/patbond-media/pet_avatar/2026/09/a-1'
|
||||||
|
'?X-Amz-Signature=sig';
|
||||||
|
|
||||||
|
late FakePetsRepository pets;
|
||||||
|
late PetsController controller;
|
||||||
|
late FakeCommunityRepository community;
|
||||||
|
late FakeMediaImagePicker picker;
|
||||||
|
late FakeMediaCompressor compressor;
|
||||||
|
late FakeDirectUploadClient direct;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
pets = FakePetsRepository();
|
||||||
|
controller = PetsController(repository: pets);
|
||||||
|
community = FakeCommunityRepository();
|
||||||
|
picker = FakeMediaImagePicker([decodablePickedImage()]);
|
||||||
|
compressor = FakeMediaCompressor();
|
||||||
|
direct = FakeDirectUploadClient();
|
||||||
|
community.onCreateMediaUpload = (_) async => credentials();
|
||||||
|
community.onCompleteMediaUpload = (assetId) async =>
|
||||||
|
readyAsset(assetId: assetId);
|
||||||
|
});
|
||||||
|
|
||||||
|
MediaUploader buildUploader(MediaPurpose purpose) => MediaUploader(
|
||||||
|
repository: community,
|
||||||
|
purpose: purpose,
|
||||||
|
picker: picker,
|
||||||
|
compressor: compressor,
|
||||||
|
directUpload: direct,
|
||||||
|
maxImages: 1,
|
||||||
|
maxConcurrentUploads: 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
Future<void> pumpDetail(
|
||||||
|
WidgetTester tester, {
|
||||||
|
bool withUploader = true,
|
||||||
|
}) async {
|
||||||
|
tester.view.physicalSize = const Size(700, 2400);
|
||||||
|
tester.view.devicePixelRatio = 1.0;
|
||||||
|
addTearDown(tester.view.reset);
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: const Scaffold(body: Text('列表基底')),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final navigator = tester.state<NavigatorState>(find.byType(Navigator));
|
||||||
|
unawaited(
|
||||||
|
navigator.push(
|
||||||
|
MaterialPageRoute<void>(
|
||||||
|
builder: (_) => PetDetailPage(
|
||||||
|
controller: controller,
|
||||||
|
petId: 'p-1',
|
||||||
|
avatarUploaderBuilder: withUploader ? buildUploader : null,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pump();
|
||||||
|
}
|
||||||
|
|
||||||
|
group('契约字段', () {
|
||||||
|
test('Pet.avatarUrl 解析(无头像为 null;不外露 avatarAssetId)', () {
|
||||||
|
expect(buildPet('p-1').avatarUrl, isNull);
|
||||||
|
expect(
|
||||||
|
buildPet('p-1', overrides: {'avatarUrl': signedAvatar}).avatarUrl,
|
||||||
|
signedAvatar,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('UpdatePetRequest:只带头像时载荷仅 version + avatarAssetId', () {
|
||||||
|
const request = UpdatePetRequest(
|
||||||
|
version: 3,
|
||||||
|
avatarAssetId: PatchField<String>.value('a-1'),
|
||||||
|
);
|
||||||
|
expect(request.toJson(), {'version': 3, 'avatarAssetId': 'a-1'});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('UpdatePetRequest:清除头像为显式 null;缺省则键不出现', () {
|
||||||
|
expect(
|
||||||
|
const UpdatePetRequest(
|
||||||
|
version: 3,
|
||||||
|
avatarAssetId: PatchField<String>.clear(),
|
||||||
|
).toJson(),
|
||||||
|
{'version': 3, 'avatarAssetId': null},
|
||||||
|
);
|
||||||
|
final absent = const UpdatePetRequest(version: 3, name: '豆豆').toJson();
|
||||||
|
expect(absent, {'version': 3, 'name': '豆豆'});
|
||||||
|
expect(
|
||||||
|
absent.containsKey('avatarAssetId'),
|
||||||
|
isFalse,
|
||||||
|
reason: '改名请求不该顺手把头像清掉',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('头像写入失败文案分层(40405 / 42203 与 40401 区分开)', () {
|
||||||
|
expect(
|
||||||
|
petAvatarSaveErrorMessage(
|
||||||
|
const ApiBusinessException(
|
||||||
|
code: ApiCodes.mediaNotFound,
|
||||||
|
message: 'x',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'头像已失效,请重新上传',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
petAvatarSaveErrorMessage(
|
||||||
|
const ApiBusinessException(
|
||||||
|
code: ApiCodes.mediaNotReady,
|
||||||
|
message: 'x',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'头像还没上传完,请稍后重试',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
petAvatarSaveErrorMessage(
|
||||||
|
const ApiBusinessException(
|
||||||
|
code: ApiCodes.petAccessDenied,
|
||||||
|
message: 'x',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'你没有修改该宠物头像的权限',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('权限呈现(WRITE 档)', () {
|
||||||
|
testWidgets('owner:头像可点 + 铅笔角标', (tester) async {
|
||||||
|
pets.getPetHandler = (_) async => buildPet('p-1');
|
||||||
|
await pumpDetail(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final avatar = tester.widget<PetAvatar>(find.byType(PetAvatar));
|
||||||
|
expect(avatar.showEditBadge, isTrue);
|
||||||
|
expect(avatar.onTap, isNotNull);
|
||||||
|
expect(avatar.semanticLabel, '更换宠物头像');
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('caregiver:头像入口在(WRITE 档),但资料编辑入口不在(MANAGE 档)', (tester) async {
|
||||||
|
pets.getPetHandler = (_) async =>
|
||||||
|
buildPet('p-1', overrides: {'myRole': 'caregiver'});
|
||||||
|
await pumpDetail(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final avatar = tester.widget<PetAvatar>(find.byType(PetAvatar));
|
||||||
|
expect(avatar.showEditBadge, isTrue, reason: 'ADR-022:头像属 WRITE 档');
|
||||||
|
expect(avatar.onTap, isNotNull);
|
||||||
|
expect(find.text('编辑资料'), findsNothing, reason: '改档案仍是 MANAGE 档');
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('viewer:头像不可点、无角标', (tester) async {
|
||||||
|
pets.getPetHandler = (_) async =>
|
||||||
|
buildPet('p-1', overrides: {'myRole': 'viewer'});
|
||||||
|
await pumpDetail(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final avatar = tester.widget<PetAvatar>(find.byType(PetAvatar));
|
||||||
|
expect(avatar.showEditBadge, isFalse);
|
||||||
|
expect(avatar.onTap, isNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('未装配上传能力:owner 也不渲染头像入口', (tester) async {
|
||||||
|
pets.getPetHandler = (_) async => buildPet('p-1');
|
||||||
|
await pumpDetail(tester, withUploader: false);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final avatar = tester.widget<PetAvatar>(find.byType(PetAvatar));
|
||||||
|
expect(avatar.showEditBadge, isFalse);
|
||||||
|
expect(avatar.onTap, isNull);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('上传与清除', () {
|
||||||
|
testWidgets('无头像 → 点头像直接上传 → PATCH 只带 version + avatarAssetId', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
pets.getPetHandler = (_) async => buildPet('p-1', version: 3);
|
||||||
|
UpdatePetRequest? captured;
|
||||||
|
pets.updatePetHandler = (petId, request) async {
|
||||||
|
captured = request;
|
||||||
|
return buildPet(
|
||||||
|
'p-1',
|
||||||
|
version: 4,
|
||||||
|
overrides: {'avatarUrl': signedAvatar},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
await pumpDetail(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.byType(PetAvatar));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('使用这张'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(community.lastMediaUploadRequest!.purpose, MediaPurpose.petAvatar);
|
||||||
|
expect(captured!.toJson(), {'version': 3, 'avatarAssetId': 'a-1'});
|
||||||
|
expect(find.text('头像已更新'), findsOneWidget);
|
||||||
|
// 详情页头像随即换成服务端回显的现签 URL。
|
||||||
|
expect(
|
||||||
|
tester.widget<PetAvatar>(find.byType(PetAvatar)).url,
|
||||||
|
signedAvatar,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('已有头像 → 点头像给「更换 / 移除」;移除发显式 null', (tester) async {
|
||||||
|
pets.getPetHandler = (_) async =>
|
||||||
|
buildPet('p-1', version: 5, overrides: {'avatarUrl': signedAvatar});
|
||||||
|
UpdatePetRequest? captured;
|
||||||
|
pets.updatePetHandler = (petId, request) async {
|
||||||
|
captured = request;
|
||||||
|
return buildPet('p-1', version: 6);
|
||||||
|
};
|
||||||
|
await pumpDetail(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.byType(PetAvatar));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('更换头像'), findsOneWidget);
|
||||||
|
expect(find.text('移除头像'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('移除头像'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final payload = captured!.toJson();
|
||||||
|
expect(payload.containsKey('avatarAssetId'), isTrue);
|
||||||
|
expect(payload['avatarAssetId'], isNull);
|
||||||
|
expect(payload['version'], 5);
|
||||||
|
expect(payload.keys.length, 2, reason: '不得夹带任何资料字段(否则抬到 MANAGE 档)');
|
||||||
|
expect(find.text('已移除头像'), findsOneWidget);
|
||||||
|
expect(tester.widget<PetAvatar>(find.byType(PetAvatar)).url, isNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('40902 版本冲突:提示 + 自动重取档案(不静默重放)', (tester) async {
|
||||||
|
var getCalls = 0;
|
||||||
|
pets.getPetHandler = (_) async {
|
||||||
|
getCalls += 1;
|
||||||
|
return buildPet('p-1', version: getCalls == 1 ? 3 : 9);
|
||||||
|
};
|
||||||
|
pets.updatePetHandler = (petId, request) async =>
|
||||||
|
throw const PetVersionConflictException(message: 'stale');
|
||||||
|
await pumpDetail(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.byType(PetAvatar));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('使用这张'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('档案已被更新,请重新操作'), findsOneWidget);
|
||||||
|
expect(getCalls, 2, reason: '冲突后重取档案拿新 version');
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('42203 头像未就绪:SnackBar 提示,不改动头像', (tester) async {
|
||||||
|
pets.getPetHandler = (_) async => buildPet('p-1', version: 3);
|
||||||
|
pets.updatePetHandler = (petId, request) async =>
|
||||||
|
throw const ApiBusinessException(
|
||||||
|
code: ApiCodes.mediaNotReady,
|
||||||
|
message: 'uploading',
|
||||||
|
);
|
||||||
|
await pumpDetail(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.byType(PetAvatar));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('使用这张'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('头像还没上传完,请稍后重试'), findsOneWidget);
|
||||||
|
expect(tester.widget<PetAvatar>(find.byType(PetAvatar)).url, isNull);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('列表展示', () {
|
||||||
|
testWidgets('列表卡展示真实头像;无头像回退占位', (tester) async {
|
||||||
|
pets.listPetsHandler = () async => [
|
||||||
|
buildPet('p-1', name: '豆豆', overrides: {'avatarUrl': signedAvatar}),
|
||||||
|
buildPet('p-2', name: '花花'),
|
||||||
|
];
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: Scaffold(body: PetsPage(controller: controller)),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final avatars = tester
|
||||||
|
.widgetList<PetAvatar>(find.byType(PetAvatar))
|
||||||
|
.toList();
|
||||||
|
expect(avatars.length, 2);
|
||||||
|
expect(avatars[0].url, signedAvatar);
|
||||||
|
expect(avatars[1].url, isNull, reason: '无头像回退爪印占位(不显示破图)');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -188,12 +188,24 @@ void main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
group('T2-13 · 数据卡行接 summary', () {
|
group('T2-13 · 数据卡行接 summary', () {
|
||||||
testWidgets('四卡取数:最新体重 / 疫苗进度 / 下一针 / 本月花费(tz 透传)', (tester) async {
|
testWidgets('四卡取数:最新体重 / 疫苗进度 / 下一针 / 当月花费(tz 透传)', (tester) async {
|
||||||
repository.getPetHandler = (petId) async => buildPet('p-1');
|
repository.getPetHandler = (petId) async => buildPet('p-1');
|
||||||
String? capturedTz;
|
String? capturedTz;
|
||||||
|
// M3.5-03:花费卡标签展示服务端归月的实际月份,故样本月份取「本月」,
|
||||||
|
// 断言不随年份漂移(跨年格式另由 monthlyExpenseCardLabel 单测覆盖)。
|
||||||
|
final now = DateTime.now();
|
||||||
|
final thisMonth = '${now.year}-${now.month.toString().padLeft(2, '0')}';
|
||||||
repository.getPetSummaryHandler = (petId, tz) async {
|
repository.getPetSummaryHandler = (petId, tz) async {
|
||||||
capturedTz = tz;
|
capturedTz = tz;
|
||||||
return buildSummary();
|
return buildSummary(
|
||||||
|
overrides: {
|
||||||
|
'monthlyExpense': {
|
||||||
|
'month': thisMonth,
|
||||||
|
'timezone': 'Asia/Shanghai',
|
||||||
|
'amountCents': 12850,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
await pumpDetail(tester);
|
await pumpDetail(tester);
|
||||||
@@ -208,12 +220,45 @@ void main() {
|
|||||||
expect(find.text('下一针·狂犬疫苗'), findsOneWidget);
|
expect(find.text('下一针·狂犬疫苗'), findsOneWidget);
|
||||||
// T2-14:月度花费卡接 monthlyExpense(12850 分 → 元展示)。
|
// T2-14:月度花费卡接 monthlyExpense(12850 分 → 元展示)。
|
||||||
expect(find.text('¥128.50'), findsOneWidget);
|
expect(find.text('¥128.50'), findsOneWidget);
|
||||||
expect(find.text('本月花费'), findsOneWidget);
|
// M3.5-03:不再硬编码「本月花费」,展示实际月份便于用户自查。
|
||||||
|
expect(find.text('${now.month} 月花费'), findsOneWidget);
|
||||||
|
expect(find.text('本月花费'), findsNothing);
|
||||||
// T2-13 遗留③:tz 透传设备时区固定偏移(月度窗口随设备时区)。
|
// T2-13 遗留③:tz 透传设备时区固定偏移(月度窗口随设备时区)。
|
||||||
expect(capturedTz, tzOffsetQueryValue(DateTime.now().timeZoneOffset));
|
expect(capturedTz, tzOffsetQueryValue(DateTime.now().timeZoneOffset));
|
||||||
expect(capturedTz, matches(RegExp(r'^[+-]\d{2}:\d{2}$')));
|
expect(capturedTz, matches(RegExp(r'^[+-]\d{2}:\d{2}$')));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('M3.5-03 · 四张数据卡均有 chevron 可点提示', (tester) async {
|
||||||
|
repository.getPetHandler = (petId) async => buildPet('p-1');
|
||||||
|
final now = DateTime.now();
|
||||||
|
final thisMonth = '${now.year}-${now.month.toString().padLeft(2, '0')}';
|
||||||
|
repository.getPetSummaryHandler = (petId, tz) async => buildSummary(
|
||||||
|
overrides: {
|
||||||
|
'monthlyExpense': {
|
||||||
|
'month': thisMonth,
|
||||||
|
'timezone': 'Asia/Shanghai',
|
||||||
|
'amountCents': 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await pumpDetail(tester);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// 四张 stat 卡都挂了 onTap,此前无任何视觉提示;现统一补
|
||||||
|
// chevron_right(与宠物列表卡 / 健康提醒卡同款)。
|
||||||
|
for (final label in ['最新体重', '疫苗进度', '下一针·狂犬疫苗', '${now.month} 月花费']) {
|
||||||
|
expect(
|
||||||
|
find.ancestor(
|
||||||
|
of: find.text(label),
|
||||||
|
matching: find.widgetWithIcon(Card, Icons.chevron_right),
|
||||||
|
),
|
||||||
|
findsOneWidget,
|
||||||
|
reason: '「$label」卡缺可点提示',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
testWidgets('null 语义:无登记显示空态而非 0/0', (tester) async {
|
testWidgets('null 语义:无登记显示空态而非 0/0', (tester) async {
|
||||||
repository.getPetHandler = (petId) async => buildPet('p-1');
|
repository.getPetHandler = (petId) async => buildPet('p-1');
|
||||||
repository.getPetSummaryHandler = (petId, tz) async => buildSummary(
|
repository.getPetSummaryHandler = (petId, tz) async => buildSummary(
|
||||||
@@ -302,12 +347,22 @@ void main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
group('T2-14 · 时间线入口', () {
|
group('T2-14 · 时间线入口', () {
|
||||||
testWidgets('点本月花费卡 → 健康时间线页;返回后重拉摘要', (tester) async {
|
testWidgets('点当月花费卡 → 健康时间线页;返回后重拉摘要', (tester) async {
|
||||||
repository.getPetHandler = (petId) async => buildPet('p-1');
|
repository.getPetHandler = (petId) async => buildPet('p-1');
|
||||||
var summaryCalls = 0;
|
var summaryCalls = 0;
|
||||||
|
final now = DateTime.now();
|
||||||
|
final thisMonth = '${now.year}-${now.month.toString().padLeft(2, '0')}';
|
||||||
repository.getPetSummaryHandler = (petId, tz) async {
|
repository.getPetSummaryHandler = (petId, tz) async {
|
||||||
summaryCalls++;
|
summaryCalls++;
|
||||||
return buildSummary();
|
return buildSummary(
|
||||||
|
overrides: {
|
||||||
|
'monthlyExpense': {
|
||||||
|
'month': thisMonth,
|
||||||
|
'timezone': 'Asia/Shanghai',
|
||||||
|
'amountCents': 12850,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
};
|
};
|
||||||
repository.listHealthEventsHandler = (petId, limit, cursor) async =>
|
repository.listHealthEventsHandler = (petId, limit, cursor) async =>
|
||||||
const CursorPage(items: [], nextCursor: null, hasMore: false);
|
const CursorPage(items: [], nextCursor: null, hasMore: false);
|
||||||
@@ -315,7 +370,7 @@ void main() {
|
|||||||
await pumpDetail(tester);
|
await pumpDetail(tester);
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
await tester.tap(find.text('本月花费'));
|
await tester.tap(find.text('${now.month} 月花费'));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
expect(find.byType(HealthEventsPage), findsOneWidget);
|
expect(find.byType(HealthEventsPage), findsOneWidget);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import 'dart:async';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/app/app_localization.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
||||||
import 'package:patbond_flutter/features/pets/pet_exceptions.dart';
|
import 'package:patbond_flutter/features/pets/pet_exceptions.dart';
|
||||||
@@ -36,6 +37,11 @@ void main() {
|
|||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
MaterialApp(
|
MaterialApp(
|
||||||
theme: buildAppTheme(),
|
theme: buildAppTheme(),
|
||||||
|
// M3.5-01:与生产一致挂 zh-CN delegate,日期选择器等内置组件文案
|
||||||
|
// 才是用户真机看到的中文。
|
||||||
|
localizationsDelegates: appLocalizationsDelegates,
|
||||||
|
supportedLocales: appSupportedLocales,
|
||||||
|
locale: appLocale,
|
||||||
home: const Scaffold(body: Text('列表基底')),
|
home: const Scaffold(body: Text('列表基底')),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -65,7 +71,7 @@ void main() {
|
|||||||
Future<void> pickDate(WidgetTester tester, String tileLabel) async {
|
Future<void> pickDate(WidgetTester tester, String tileLabel) async {
|
||||||
await tester.tap(find.text(tileLabel));
|
await tester.tap(find.text(tileLabel));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
await tester.tap(find.text('OK'));
|
await tester.tap(find.text('确定'));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import 'dart:async';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:patbond_flutter/core/network/api_exception.dart';
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/app/app_localization.dart';
|
||||||
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
import 'package:patbond_flutter/core/widgets/empty_state_illustration.dart';
|
||||||
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
import 'package:patbond_flutter/features/pets/health_record_analytics.dart';
|
||||||
@@ -39,6 +40,11 @@ void main() {
|
|||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
MaterialApp(
|
MaterialApp(
|
||||||
theme: buildAppTheme(),
|
theme: buildAppTheme(),
|
||||||
|
// M3.5-01:与生产一致挂 zh-CN delegate,日期选择器等内置组件文案
|
||||||
|
// 才是用户真机看到的中文。
|
||||||
|
localizationsDelegates: appLocalizationsDelegates,
|
||||||
|
supportedLocales: appSupportedLocales,
|
||||||
|
locale: appLocale,
|
||||||
home: const Scaffold(body: Text('详情基底')),
|
home: const Scaffold(body: Text('详情基底')),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -166,7 +172,7 @@ void main() {
|
|||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
await tester.tap(find.text('计划接种日期'));
|
await tester.tap(find.text('计划接种日期'));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
await tester.tap(find.text('OK'));
|
await tester.tap(find.text('确定'));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
await tester.tap(find.text('保存登记'));
|
await tester.tap(find.text('保存登记'));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
|
|||||||
@@ -0,0 +1,551 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/comment_tile.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/post_media_grid.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_exceptions.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_interaction_analytics.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/post/post_detail_page.dart';
|
||||||
|
|
||||||
|
import '../../helpers/community_test_helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late FakeCommunityRepository repository;
|
||||||
|
late CommunityController controller;
|
||||||
|
late List<(String, Map<String, dynamic>?)> events;
|
||||||
|
late CommunityInteractionAnalytics analytics;
|
||||||
|
|
||||||
|
/// 无媒体帖(避免测试环境网络图噪音;媒体形态单测另立)。
|
||||||
|
Post textPost({
|
||||||
|
String id = 'p-1',
|
||||||
|
bool likedByMe = false,
|
||||||
|
int likeCount = 6,
|
||||||
|
int commentCount = 3,
|
||||||
|
}) => Post.fromJson({
|
||||||
|
...samplePostJson(id: id, likedByMe: likedByMe, likeCount: likeCount),
|
||||||
|
'media': <Object>[],
|
||||||
|
'commentCount': commentCount,
|
||||||
|
});
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
repository = FakeCommunityRepository();
|
||||||
|
events = [];
|
||||||
|
analytics = CommunityInteractionAnalytics(
|
||||||
|
(name, [props]) async => events.add((name, props)),
|
||||||
|
);
|
||||||
|
controller = CommunityController(
|
||||||
|
repository: repository,
|
||||||
|
interactionAnalytics: analytics,
|
||||||
|
);
|
||||||
|
// 缺省行为:正常帖 + 空评论 + 未关注(各测试按需覆盖)。
|
||||||
|
repository.onGetPost = (_) async => textPost();
|
||||||
|
repository.onListComments = (_, _) async => commentPage(const []);
|
||||||
|
repository.onGetFollowStats = (_) async => const FollowStats(
|
||||||
|
followerCount: 1,
|
||||||
|
followingCount: 2,
|
||||||
|
followedByMe: false,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
List<Map<String, dynamic>?> eventsNamed(String name) =>
|
||||||
|
events.where((e) => e.$1 == name).map((e) => e.$2).toList();
|
||||||
|
|
||||||
|
Widget detailApp({String? currentUserId = 'me'}) => MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: PostDetailPage(
|
||||||
|
controller: controller,
|
||||||
|
postId: 'p-1',
|
||||||
|
currentUserId: currentUserId,
|
||||||
|
analytics: analytics,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// 经启动页 push 详情(40403 返回 Feed 的 pop 断言用)。
|
||||||
|
Widget launcherApp({String? currentUserId = 'me'}) => MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: Builder(
|
||||||
|
builder: (context) => Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: TextButton(
|
||||||
|
onPressed: () => Navigator.of(context).push(
|
||||||
|
MaterialPageRoute<void>(
|
||||||
|
builder: (_) => PostDetailPage(
|
||||||
|
controller: controller,
|
||||||
|
postId: 'p-1',
|
||||||
|
currentUserId: currentUserId,
|
||||||
|
analytics: analytics,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: const Text('打开详情'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
group('详情四态', () {
|
||||||
|
testWidgets('loading:请求未回渲染转圈', (tester) async {
|
||||||
|
final post = Completer<Post>();
|
||||||
|
repository.onGetPost = (_) => post.future;
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.byType(CircularProgressIndicator), findsOneWidget);
|
||||||
|
|
||||||
|
post.complete(textPost());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('晒了一下午太阳。'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('ready:正文/作者/求助标/评论标题齐全', (tester) async {
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('今天的豆豆'), findsOneWidget);
|
||||||
|
expect(find.text('晒了一下午太阳。'), findsOneWidget);
|
||||||
|
expect(find.text('毛毛的铲屎官'), findsOneWidget);
|
||||||
|
expect(find.text('评论 (3)'), findsOneWidget);
|
||||||
|
expect(find.text('还没有评论,来抢沙发'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('error:横幅 + 重试恢复 ready', (tester) async {
|
||||||
|
var attempts = 0;
|
||||||
|
repository.onGetPost = (_) async {
|
||||||
|
attempts += 1;
|
||||||
|
if (attempts == 1) throw const ApiNetworkException();
|
||||||
|
return textPost();
|
||||||
|
};
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byType(InlineErrorBanner), findsOneWidget);
|
||||||
|
expect(find.text('网络异常,请检查网络后重试'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('重试'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byType(InlineErrorBanner), findsNothing);
|
||||||
|
expect(find.text('晒了一下午太阳。'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('40403 不存在态:SnackBar + 返回 Feed 并触发刷新', (tester) async {
|
||||||
|
repository.onGetPost = (_) async =>
|
||||||
|
throw const PostNotFoundException(message: 'gone');
|
||||||
|
repository.onFeed = (_, _) async => feedPage(const []);
|
||||||
|
|
||||||
|
await tester.pumpWidget(launcherApp());
|
||||||
|
await tester.tap(find.text('打开详情'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// 已弹回启动页 + 提示 + Feed 整体刷新(失效帖剔除)。
|
||||||
|
expect(find.text('打开详情'), findsOneWidget);
|
||||||
|
expect(find.text('帖子不存在或已被删除'), findsOneWidget);
|
||||||
|
expect(repository.calls, contains('feed:cursor=null'));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('内存副本先渲染:进入即展示缓存,后台拉新不阻塞', (tester) async {
|
||||||
|
await controller.getPost('p-1'); // 预热详情副本。
|
||||||
|
final refresh = Completer<Post>();
|
||||||
|
repository.onGetPost = (_) => refresh.future;
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pump();
|
||||||
|
// 拉新未回已渲染缓存内容,无全页转圈。
|
||||||
|
expect(find.text('晒了一下午太阳。'), findsOneWidget);
|
||||||
|
|
||||||
|
refresh.complete(textPost(likeCount: 9));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('9'), findsOneWidget);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('媒体', () {
|
||||||
|
testWidgets('多图渲染真九宫格,点格进全屏大图(页码 + 关闭)', (tester) async {
|
||||||
|
repository.onGetPost = (_) async => Post.fromJson({
|
||||||
|
...samplePostJson(),
|
||||||
|
'media': [
|
||||||
|
for (var i = 0; i < 5; i++)
|
||||||
|
samplePostMediaItemJson(
|
||||||
|
assetId: 'a-$i',
|
||||||
|
position: i,
|
||||||
|
isCover: i == 0,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byType(PostMediaGrid), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(
|
||||||
|
find
|
||||||
|
.descendant(
|
||||||
|
of: find.byType(PostMediaGrid),
|
||||||
|
matching: find.byType(InkWell),
|
||||||
|
)
|
||||||
|
.first,
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('1/5'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.close));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byType(PostMediaGrid), findsOneWidget);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('评论区', () {
|
||||||
|
testWidgets('游标列表:首页渲染 + 触底携游标补页', (tester) async {
|
||||||
|
repository.onListComments = (_, cursor) async {
|
||||||
|
if (cursor == null) {
|
||||||
|
return commentPage(
|
||||||
|
[
|
||||||
|
sampleComment(id: 'c-1', content: '第一条'),
|
||||||
|
sampleComment(id: 'c-2', content: '第二条'),
|
||||||
|
],
|
||||||
|
nextCursor: 'cc1',
|
||||||
|
hasMore: true,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return commentPage([sampleComment(id: 'c-3', content: '第三条')]);
|
||||||
|
};
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('第一条'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.drag(find.byType(ListView), const Offset(0, -800));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(
|
||||||
|
repository.calls.where((c) => c.startsWith('comments:')).toList(),
|
||||||
|
['comments:p-1:cursor=null', 'comments:p-1:cursor=cc1'],
|
||||||
|
);
|
||||||
|
expect(find.text('第三条'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('列表失败:话术 + 点按重试恢复', (tester) async {
|
||||||
|
var attempts = 0;
|
||||||
|
repository.onListComments = (_, _) async {
|
||||||
|
attempts += 1;
|
||||||
|
if (attempts == 1) throw const ApiNetworkException();
|
||||||
|
return commentPage([sampleComment(content: '恢复后的评论')]);
|
||||||
|
};
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('加载失败,点此重试'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('加载失败,点此重试'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('恢复后的评论'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('创建成功:插入列表头 + 计数 +1 + comment_create_succeeded + 清空输入', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
repository.onCreateComment = (_, request) async =>
|
||||||
|
sampleComment(id: 'c-new', content: request.content);
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.enterText(find.byType(TextField), '沙发!');
|
||||||
|
await tester.pump();
|
||||||
|
await tester.tap(find.byIcon(Icons.send_rounded));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(repository.calls, contains('createComment:p-1:沙发!'));
|
||||||
|
expect(find.text('沙发!'), findsOneWidget); // 列表中的新评论。
|
||||||
|
expect(find.text('评论 (4)'), findsOneWidget);
|
||||||
|
expect(
|
||||||
|
tester.widget<TextField>(find.byType(TextField)).controller?.text,
|
||||||
|
'',
|
||||||
|
);
|
||||||
|
final succeeded = eventsNamed('comment_create_succeeded').single!;
|
||||||
|
expect(succeeded['isReply'], false);
|
||||||
|
expect(succeeded['textLengthBucket'], 'short');
|
||||||
|
expect(succeeded['durationMs'], greaterThanOrEqualTo(0));
|
||||||
|
// Feed 卡片同源计数(跨页一致的另一半在 like 测试)。
|
||||||
|
expect(controller.cachedPost('p-1')!.commentCount, 4);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('创建失败:SnackBar + comment_create_failed,attemptSeq 递增', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
repository.onCreateComment = (_, _) async =>
|
||||||
|
throw const ApiBusinessException(code: 40000, message: 'bad');
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.enterText(find.byType(TextField), '不合规内容');
|
||||||
|
await tester.pump();
|
||||||
|
await tester.tap(find.byIcon(Icons.send_rounded));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('评论内容不合规,请修改后重试'), findsOneWidget);
|
||||||
|
|
||||||
|
// 等 SnackBar 退场(遮挡底部发送钮)后重试第二次。
|
||||||
|
await tester.pump(const Duration(seconds: 5));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.byIcon(Icons.send_rounded));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(eventsNamed('comment_create_failed'), [
|
||||||
|
{
|
||||||
|
'failureReason': 'validation_error',
|
||||||
|
'attemptSeq': 1,
|
||||||
|
'errorCode': 40000,
|
||||||
|
'httpStatus': 400,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'failureReason': 'validation_error',
|
||||||
|
'attemptSeq': 2,
|
||||||
|
'errorCode': 40000,
|
||||||
|
'httpStatus': 400,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
// 失败不清空输入、不入列表、不动计数。
|
||||||
|
expect(find.text('评论 (3)'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('删除权限呈现:仅本人评论有「删除」;确认后删除 + 计数 -1', (tester) async {
|
||||||
|
repository.onListComments = (_, _) async => commentPage([
|
||||||
|
sampleComment(
|
||||||
|
id: 'c-mine',
|
||||||
|
author: sampleAuthorJson(userId: 'me', nickname: '我自己'),
|
||||||
|
content: '我的评论',
|
||||||
|
),
|
||||||
|
sampleComment(id: 'c-other', content: '别人的评论'),
|
||||||
|
]);
|
||||||
|
repository.onDeleteComment = (_) async {};
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.drag(find.byType(ListView), const Offset(0, -400));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
// 两条评论,删除入口恰一个(本人条目)。
|
||||||
|
expect(find.byType(CommentTile), findsNWidgets(2));
|
||||||
|
expect(find.text('删除'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('删除'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('删除这条评论?'), findsOneWidget);
|
||||||
|
await tester.tap(find.widgetWithText(FilledButton, '删除'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(repository.calls, contains('deleteComment:c-mine'));
|
||||||
|
expect(find.text('我的评论'), findsNothing);
|
||||||
|
expect(find.text('别人的评论'), findsOneWidget);
|
||||||
|
expect(find.text('评论 (2)'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('删除失败:40301 提示无权限,条目保留', (tester) async {
|
||||||
|
repository.onListComments = (_, _) async => commentPage([
|
||||||
|
sampleComment(
|
||||||
|
id: 'c-mine',
|
||||||
|
author: sampleAuthorJson(userId: 'me', nickname: '我自己'),
|
||||||
|
content: '我的评论',
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
repository.onDeleteComment = (_) async =>
|
||||||
|
throw const PostAccessDeniedException(message: 'denied');
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.drag(find.byType(ListView), const Offset(0, -400));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
await tester.tap(find.text('删除'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.widgetWithText(FilledButton, '删除'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('没有权限删除这条评论'), findsOneWidget);
|
||||||
|
expect(find.text('我的评论'), findsOneWidget);
|
||||||
|
expect(find.text('评论 (3)'), findsOneWidget);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('互动(ToggleSync UI 层)', () {
|
||||||
|
testWidgets('点赞乐观翻转即时 +1,成功报 post_liked(source=post_detail)', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final like = Completer<LikeState>();
|
||||||
|
repository.onLikeToggle = (_, _) => like.future;
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.favorite_border));
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.text('7'), findsOneWidget);
|
||||||
|
expect(find.byIcon(Icons.favorite), findsOneWidget);
|
||||||
|
|
||||||
|
like.complete(const LikeState(liked: true, likeCount: 7));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(eventsNamed('post_liked'), [
|
||||||
|
{'source': 'post_detail'},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('点赞失败:零动画回滚成对恢复 + SnackBar,不报事件', (tester) async {
|
||||||
|
final like = Completer<LikeState>();
|
||||||
|
repository.onLikeToggle = (_, _) => like.future;
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.favorite_border));
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.text('7'), findsOneWidget);
|
||||||
|
|
||||||
|
like.completeError(const ApiNetworkException());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('6'), findsOneWidget);
|
||||||
|
expect(find.byIcon(Icons.favorite_border), findsOneWidget);
|
||||||
|
expect(find.text('操作失败,请重试'), findsOneWidget);
|
||||||
|
expect(eventsNamed('post_liked'), isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('收藏接线:成功报 post_favorited(source=post_detail)', (tester) async {
|
||||||
|
repository.onBookmarkToggle = (_, target) async =>
|
||||||
|
BookmarkState(bookmarked: target, bookmarkCount: target ? 3 : 2);
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.bookmark_border));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byIcon(Icons.bookmark), findsOneWidget);
|
||||||
|
expect(eventsNamed('post_favorited'), [
|
||||||
|
{'source': 'post_detail'},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('跨页一致:详情页点赞,Feed 卡片同帖同帧更新(共享同一实例)', (tester) async {
|
||||||
|
repository.onFeed = (_, _) async =>
|
||||||
|
feedPage([sampleFeedCard(likeCount: 6)]);
|
||||||
|
await controller.refresh();
|
||||||
|
repository.onLikeToggle = (_, target) async =>
|
||||||
|
LikeState(liked: target, likeCount: target ? 7 : 6);
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: Column(
|
||||||
|
children: [
|
||||||
|
// 模拟 Feed 侧对同一 controller 的消费。
|
||||||
|
ListenableBuilder(
|
||||||
|
listenable: controller,
|
||||||
|
builder: (context, _) {
|
||||||
|
final card = controller.feed.single;
|
||||||
|
return Text('card:${card.likedByMe}:${card.likeCount}');
|
||||||
|
},
|
||||||
|
),
|
||||||
|
Expanded(
|
||||||
|
child: PostDetailPage(
|
||||||
|
controller: controller,
|
||||||
|
postId: 'p-1',
|
||||||
|
currentUserId: 'me',
|
||||||
|
analytics: analytics,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('card:false:6'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.favorite_border));
|
||||||
|
await tester.pump();
|
||||||
|
// 乐观写入即同源:卡片副本同帧翻转。
|
||||||
|
expect(find.text('card:true:7'), findsOneWidget);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('card:true:7'), findsOneWidget);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('关注', () {
|
||||||
|
testWidgets('未关注 → 关注:乐观翻转 + user_followed(post_detail)', (tester) async {
|
||||||
|
repository.onFollowToggle = (_, target) async =>
|
||||||
|
FollowState(following: target, followerCount: target ? 2 : 1);
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('+ 关注'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('+ 关注'));
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.text('已关注'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(repository.calls, contains('follow:u-1'));
|
||||||
|
expect(eventsNamed('user_followed'), [
|
||||||
|
{'source': 'post_detail'},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('已关注 → 取关:确认弹窗 + user_unfollowed', (tester) async {
|
||||||
|
repository.onGetFollowStats = (_) async => const FollowStats(
|
||||||
|
followerCount: 2,
|
||||||
|
followingCount: 2,
|
||||||
|
followedByMe: true,
|
||||||
|
);
|
||||||
|
repository.onFollowToggle = (_, target) async =>
|
||||||
|
FollowState(following: target, followerCount: target ? 2 : 1);
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('已关注'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('已关注'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('不再关注 TA?'), findsOneWidget);
|
||||||
|
await tester.tap(find.widgetWithText(FilledButton, '不再关注'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('+ 关注'), findsOneWidget);
|
||||||
|
expect(repository.calls, contains('unfollow:u-1'));
|
||||||
|
expect(eventsNamed('user_unfollowed'), [
|
||||||
|
{'source': 'post_detail'},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('关注失败:回滚直接跳变 + SnackBar', (tester) async {
|
||||||
|
final follow = Completer<FollowState>();
|
||||||
|
repository.onFollowToggle = (_, _) => follow.future;
|
||||||
|
|
||||||
|
await tester.pumpWidget(detailApp());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.text('+ 关注'));
|
||||||
|
await tester.pump();
|
||||||
|
expect(find.text('已关注'), findsOneWidget);
|
||||||
|
|
||||||
|
follow.completeError(const ApiNetworkException());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('+ 关注'), findsOneWidget);
|
||||||
|
expect(find.text('操作失败,请重试'), findsOneWidget);
|
||||||
|
expect(eventsNamed('user_followed'), isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('本人帖:不渲染关注钮、不拉 follow-stats', (tester) async {
|
||||||
|
await tester.pumpWidget(detailApp(currentUserId: 'u-1'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('+ 关注'), findsNothing);
|
||||||
|
expect(find.text('已关注'), findsNothing);
|
||||||
|
expect(
|
||||||
|
repository.calls.where((c) => c.startsWith('followStats:')),
|
||||||
|
isEmpty,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,200 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||||
|
|
||||||
|
import '../../helpers/auth_test_helpers.dart';
|
||||||
|
import '../../helpers/community_test_helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late FakeCommunityRepository community;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
community = FakeCommunityRepository();
|
||||||
|
});
|
||||||
|
|
||||||
|
ProfileController build(FakeAuthRepository auth) =>
|
||||||
|
ProfileController(authRepository: auth, communityRepository: community);
|
||||||
|
|
||||||
|
test('四态:initial → loading → ready(资料 + 两块统计齐备)', () async {
|
||||||
|
final gate = Completer<UserProfile>();
|
||||||
|
final controller = build(FakeAuthRepository(meHandler: () => gate.future));
|
||||||
|
community.onGetMyCommunityStats = () async =>
|
||||||
|
CommunityStats.fromJson(sampleCommunityStatsJson());
|
||||||
|
community.onGetFollowStats = (_) async =>
|
||||||
|
FollowStats.fromJson(sampleFollowStatsJson());
|
||||||
|
|
||||||
|
expect(controller.phase, ProfileLoadPhase.initial);
|
||||||
|
final pending = controller.refresh();
|
||||||
|
expect(controller.phase, ProfileLoadPhase.loading);
|
||||||
|
|
||||||
|
gate.complete(buildProfile(nickname: '小柴'));
|
||||||
|
await pending;
|
||||||
|
|
||||||
|
expect(controller.phase, ProfileLoadPhase.ready);
|
||||||
|
expect(controller.statsPhase, ProfileStatsPhase.ready);
|
||||||
|
expect(controller.displayName, '小柴');
|
||||||
|
expect(controller.communityStats!.receivedLikeCount, 128);
|
||||||
|
expect(controller.followStats!.followerCount, 24);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('/me 失败且无副本 → error 态 + 可重试;不外抛', () async {
|
||||||
|
var attempts = 0;
|
||||||
|
final auth = FakeAuthRepository(
|
||||||
|
meHandler: () async {
|
||||||
|
attempts += 1;
|
||||||
|
if (attempts == 1) throw const ApiNetworkException('断网');
|
||||||
|
return buildProfile(nickname: '小柴');
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final controller = build(auth);
|
||||||
|
|
||||||
|
await controller.refresh();
|
||||||
|
expect(controller.phase, ProfileLoadPhase.error);
|
||||||
|
expect(controller.lastError, isA<ApiNetworkException>());
|
||||||
|
expect(controller.profile, isNull);
|
||||||
|
|
||||||
|
await controller.refresh();
|
||||||
|
expect(controller.phase, ProfileLoadPhase.ready);
|
||||||
|
expect(controller.displayName, '小柴');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('已有副本时刷新失败保留副本(不打断阅读,停在 ready)', () async {
|
||||||
|
var attempts = 0;
|
||||||
|
final controller = build(
|
||||||
|
FakeAuthRepository(
|
||||||
|
meHandler: () async {
|
||||||
|
attempts += 1;
|
||||||
|
if (attempts == 1) return buildProfile(nickname: '小柴');
|
||||||
|
throw const ApiNetworkException('断网');
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await controller.refresh();
|
||||||
|
await controller.refresh();
|
||||||
|
|
||||||
|
expect(controller.phase, ProfileLoadPhase.ready);
|
||||||
|
expect(controller.displayName, '小柴');
|
||||||
|
expect(controller.lastError, isA<ApiNetworkException>());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('统计失败只降级统计块,资料仍 ready;refreshStats 可单独重试', () async {
|
||||||
|
var statsAttempts = 0;
|
||||||
|
community.onGetMyCommunityStats = () async {
|
||||||
|
statsAttempts += 1;
|
||||||
|
if (statsAttempts == 1) throw const ApiNetworkException('断网');
|
||||||
|
return CommunityStats.fromJson(sampleCommunityStatsJson());
|
||||||
|
};
|
||||||
|
community.onGetFollowStats = (_) async =>
|
||||||
|
FollowStats.fromJson(sampleFollowStatsJson());
|
||||||
|
final controller = build(FakeAuthRepository());
|
||||||
|
|
||||||
|
await controller.refresh();
|
||||||
|
expect(controller.phase, ProfileLoadPhase.ready);
|
||||||
|
expect(controller.statsPhase, ProfileStatsPhase.error);
|
||||||
|
expect(controller.communityStats, isNull);
|
||||||
|
|
||||||
|
await controller.refreshStats();
|
||||||
|
expect(controller.statsPhase, ProfileStatsPhase.ready);
|
||||||
|
expect(controller.phase, ProfileLoadPhase.ready);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('空数据:两个数字为 0(不是 null、不是 404)', () async {
|
||||||
|
community.onGetMyCommunityStats = () async => CommunityStats.fromJson(
|
||||||
|
sampleCommunityStatsJson(receivedLikeCount: 0, publishedPostCount: 0),
|
||||||
|
);
|
||||||
|
community.onGetFollowStats = (_) async => FollowStats.fromJson(
|
||||||
|
sampleFollowStatsJson(followerCount: 0, followingCount: 0),
|
||||||
|
);
|
||||||
|
final controller = build(FakeAuthRepository());
|
||||||
|
|
||||||
|
await controller.refresh();
|
||||||
|
|
||||||
|
expect(controller.communityStats!.receivedLikeCount, 0);
|
||||||
|
expect(controller.communityStats!.publishedPostCount, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('save:空 patch 直接短路,不发请求(服务端对空 patch 答 400)', () async {
|
||||||
|
final auth = FakeAuthRepository(
|
||||||
|
updateMeHandler: (_) async => buildProfile(nickname: '不该被调用'),
|
||||||
|
);
|
||||||
|
final controller = build(auth);
|
||||||
|
await controller.refresh();
|
||||||
|
|
||||||
|
await controller.save(const UpdateMeRequest());
|
||||||
|
|
||||||
|
expect(auth.updateMePayloads, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('save 成功即以服务端回显替换副本(含 avatarUrl 现签值)', () async {
|
||||||
|
final auth = FakeAuthRepository(
|
||||||
|
updateMeHandler: (_) async => buildProfile(
|
||||||
|
nickname: '小柴',
|
||||||
|
avatarUrl: 'https://m/a.jpg?X-Amz-Signature=new',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final controller = build(auth);
|
||||||
|
await controller.refresh();
|
||||||
|
expect(controller.displayName, 'llx');
|
||||||
|
|
||||||
|
await controller.save(
|
||||||
|
const UpdateMeRequest(nickname: PatchField<String>.value('小柴')),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(controller.displayName, '小柴');
|
||||||
|
expect(controller.profile!.avatarUrl, contains('X-Amz-Signature=new'));
|
||||||
|
expect(auth.updateMePayloads.single, {'nickname': '小柴'});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('save 失败按类型化异常外抛(供编辑页分层呈现),副本不动', () async {
|
||||||
|
final auth = FakeAuthRepository(
|
||||||
|
updateMeHandler: (_) async => throw const ApiBusinessException(
|
||||||
|
code: ApiCodes.mediaNotReady,
|
||||||
|
message: 'not ready',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final controller = build(auth);
|
||||||
|
await controller.refresh();
|
||||||
|
|
||||||
|
await expectLater(
|
||||||
|
controller.save(
|
||||||
|
const UpdateMeRequest(avatarAssetId: PatchField<String>.value('a-1')),
|
||||||
|
),
|
||||||
|
throwsA(
|
||||||
|
isA<ApiBusinessException>().having(
|
||||||
|
(e) => e.code,
|
||||||
|
'code',
|
||||||
|
ApiCodes.mediaNotReady,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
expect(controller.displayName, 'llx');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('reset:登出清空资料与统计,回 initial(跨账号不泄漏)', () async {
|
||||||
|
final controller = build(FakeAuthRepository());
|
||||||
|
await controller.refresh();
|
||||||
|
expect(controller.profile, isNotNull);
|
||||||
|
|
||||||
|
controller.reset();
|
||||||
|
|
||||||
|
expect(controller.phase, ProfileLoadPhase.initial);
|
||||||
|
expect(controller.profile, isNull);
|
||||||
|
expect(controller.displayName, isNull);
|
||||||
|
expect(controller.communityStats, isNull);
|
||||||
|
expect(controller.followStats, isNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('follow-stats 用的是本人 userId(路径主体正确)', () async {
|
||||||
|
final controller = build(
|
||||||
|
FakeAuthRepository(meHandler: () async => buildProfile(userId: 'u-42')),
|
||||||
|
);
|
||||||
|
await controller.refresh();
|
||||||
|
expect(community.calls, contains('followStats:u-42'));
|
||||||
|
expect(community.calls, contains('communityStats'));
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,295 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/media_uploader.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_edit_page.dart';
|
||||||
|
|
||||||
|
import '../../helpers/auth_test_helpers.dart';
|
||||||
|
import '../../helpers/community_test_helpers.dart';
|
||||||
|
import '../../helpers/media_test_helpers.dart';
|
||||||
|
|
||||||
|
/// T3.5-08 编辑页:**PATCH 三态载荷**是本文件的重点。
|
||||||
|
///
|
||||||
|
/// 每个成功用例都断言一次「未改字段的键不出现在 JSON 里」——这是三态与
|
||||||
|
/// 两态的唯一可观测差别,也是「只改昵称却把头像清掉」这类事故的唯一防线。
|
||||||
|
|
||||||
|
/// 昵称输入框(AppTextField 内层 TextFormField)。
|
||||||
|
Finder get nicknameField => find.byType(TextFormField);
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late FakeCommunityRepository community;
|
||||||
|
late FakeMediaImagePicker picker;
|
||||||
|
late FakeMediaCompressor compressor;
|
||||||
|
late FakeDirectUploadClient direct;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
community = FakeCommunityRepository();
|
||||||
|
community.onGetMyCommunityStats = () async =>
|
||||||
|
CommunityStats.fromJson(sampleCommunityStatsJson());
|
||||||
|
community.onGetFollowStats = (_) async =>
|
||||||
|
FollowStats.fromJson(sampleFollowStatsJson());
|
||||||
|
picker = FakeMediaImagePicker([decodablePickedImage()]);
|
||||||
|
compressor = FakeMediaCompressor();
|
||||||
|
direct = FakeDirectUploadClient();
|
||||||
|
community.onCreateMediaUpload = (_) async => credentials();
|
||||||
|
community.onCompleteMediaUpload = (assetId) async =>
|
||||||
|
readyAsset(assetId: assetId);
|
||||||
|
});
|
||||||
|
|
||||||
|
MediaUploader buildUploader(MediaPurpose purpose) => MediaUploader(
|
||||||
|
repository: community,
|
||||||
|
purpose: purpose,
|
||||||
|
picker: picker,
|
||||||
|
compressor: compressor,
|
||||||
|
directUpload: direct,
|
||||||
|
maxImages: 1,
|
||||||
|
maxConcurrentUploads: 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// 直接 push 编辑页(资料页链路另有测试;这里只测编辑页本身)。
|
||||||
|
Future<FakeAuthRepository> pumpEdit(
|
||||||
|
WidgetTester tester, {
|
||||||
|
String? nickname,
|
||||||
|
String? avatarUrl,
|
||||||
|
bool withUploader = true,
|
||||||
|
ApiException? saveError,
|
||||||
|
}) async {
|
||||||
|
tester.view.physicalSize = const Size(700, 1800);
|
||||||
|
tester.view.devicePixelRatio = 1.0;
|
||||||
|
addTearDown(tester.view.reset);
|
||||||
|
final auth = FakeAuthRepository(
|
||||||
|
meHandler: () async => buildProfile(
|
||||||
|
username: 'llx',
|
||||||
|
nickname: nickname,
|
||||||
|
avatarUrl: avatarUrl,
|
||||||
|
),
|
||||||
|
updateMeHandler: (_) async {
|
||||||
|
if (saveError != null) throw saveError;
|
||||||
|
return buildProfile(username: 'llx', nickname: nickname);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final controller = ProfileController(
|
||||||
|
authRepository: auth,
|
||||||
|
communityRepository: community,
|
||||||
|
);
|
||||||
|
addTearDown(controller.dispose);
|
||||||
|
await controller.refresh();
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: ProfileEditPage(
|
||||||
|
controller: controller,
|
||||||
|
avatarUploaderBuilder: withUploader ? buildUploader : null,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
return auth;
|
||||||
|
}
|
||||||
|
|
||||||
|
group('三态载荷', () {
|
||||||
|
testWidgets('只改昵称 → 载荷只有 nickname,avatarAssetId 键不出现', (tester) async {
|
||||||
|
final auth = await pumpEdit(
|
||||||
|
tester,
|
||||||
|
avatarUrl: 'https://m/a.jpg?X-Amz-Signature=s',
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.enterText(nicknameField, '小柴');
|
||||||
|
await tester.pump();
|
||||||
|
await tester.tap(find.text('保存'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final payload = auth.updateMePayloads.single;
|
||||||
|
expect(payload, {'nickname': '小柴'});
|
||||||
|
expect(
|
||||||
|
payload.containsKey('avatarAssetId'),
|
||||||
|
isFalse,
|
||||||
|
reason: '发成 avatarAssetId: null 会把用户已有头像一起清掉',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('点「清除昵称」 → nickname 显式 null,头像键不出现', (tester) async {
|
||||||
|
final auth = await pumpEdit(
|
||||||
|
tester,
|
||||||
|
nickname: '小柴',
|
||||||
|
avatarUrl: 'https://m/a.jpg?X-Amz-Signature=s',
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.text('清除昵称'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('保存后将清除昵称,展示名回退为用户名'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('保存'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final payload = auth.updateMePayloads.single;
|
||||||
|
expect(payload.containsKey('nickname'), isTrue);
|
||||||
|
expect(payload['nickname'], isNull);
|
||||||
|
expect(payload.containsKey('avatarAssetId'), isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('点「清除头像」 → avatarAssetId 显式 null,nickname 键不出现', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final auth = await pumpEdit(
|
||||||
|
tester,
|
||||||
|
nickname: '小柴',
|
||||||
|
avatarUrl: 'https://m/a.jpg?X-Amz-Signature=s',
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.text('清除头像'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('保存后将移除头像'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('保存'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final payload = auth.updateMePayloads.single;
|
||||||
|
expect(payload.containsKey('avatarAssetId'), isTrue);
|
||||||
|
expect(payload['avatarAssetId'], isNull);
|
||||||
|
expect(
|
||||||
|
payload.containsKey('nickname'),
|
||||||
|
isFalse,
|
||||||
|
reason: '昵称没碰过就不该出现在载荷里(更不该是 null)',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('输入与原昵称相同 → 视为未改,保存钮禁用、不发请求', (tester) async {
|
||||||
|
final auth = await pumpEdit(tester, nickname: '小柴');
|
||||||
|
|
||||||
|
// 预填即原值。
|
||||||
|
expect(find.text('小柴'), findsOneWidget);
|
||||||
|
final button = tester.widget<FilledButton>(
|
||||||
|
find.widgetWithText(FilledButton, '保存'),
|
||||||
|
);
|
||||||
|
expect(button.onPressed, isNull);
|
||||||
|
expect(auth.updateMePayloads, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('无昵称时预填空串(不预填 username,避免把回退固化成真昵称)', (tester) async {
|
||||||
|
await pumpEdit(tester);
|
||||||
|
|
||||||
|
final field = tester.widget<TextFormField>(nicknameField);
|
||||||
|
expect(field.controller!.text, '');
|
||||||
|
expect(find.text('未设置,当前展示为用户名「llx」'), findsOneWidget);
|
||||||
|
// 无昵称时不给「清除昵称」入口(清一个不存在的东西无意义)。
|
||||||
|
expect(find.text('清除昵称'), findsNothing);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('校验与失败语义', () {
|
||||||
|
testWidgets('33 码点昵称:字段级 errorText,不发请求', (tester) async {
|
||||||
|
final auth = await pumpEdit(tester);
|
||||||
|
|
||||||
|
await tester.enterText(nicknameField, '柴' * 33);
|
||||||
|
await tester.pump();
|
||||||
|
await tester.tap(find.text('保存'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('昵称最多 32 个字'), findsOneWidget);
|
||||||
|
expect(auth.updateMePayloads, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('纯空格昵称:不是隐式清空,提示走显式清除入口', (tester) async {
|
||||||
|
final auth = await pumpEdit(tester, nickname: '小柴');
|
||||||
|
|
||||||
|
await tester.enterText(nicknameField, ' ');
|
||||||
|
await tester.pump();
|
||||||
|
// 空白输入被判为「不改」,保存钮禁用(不会误发清空)。
|
||||||
|
final button = tester.widget<FilledButton>(
|
||||||
|
find.widgetWithText(FilledButton, '保存'),
|
||||||
|
);
|
||||||
|
expect(button.onPressed, isNull);
|
||||||
|
expect(auth.updateMePayloads, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('42203 头像未就绪 → 横幅提示,页面留在编辑态可重试', (tester) async {
|
||||||
|
await pumpEdit(
|
||||||
|
tester,
|
||||||
|
saveError: const ApiBusinessException(
|
||||||
|
code: ApiCodes.mediaNotReady,
|
||||||
|
message: 'asset uploading',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.enterText(nicknameField, '小柴');
|
||||||
|
await tester.pump();
|
||||||
|
await tester.tap(find.text('保存'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.byType(InlineErrorBanner), findsOneWidget);
|
||||||
|
expect(find.text('头像还没上传完,请稍后重试'), findsOneWidget);
|
||||||
|
expect(find.byType(ProfileEditPage), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('40405 头像 asset 失效 → 提示重新上传', (tester) async {
|
||||||
|
await pumpEdit(
|
||||||
|
tester,
|
||||||
|
saveError: const ApiBusinessException(
|
||||||
|
code: ApiCodes.mediaNotFound,
|
||||||
|
message: 'gone',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.enterText(nicknameField, '小柴');
|
||||||
|
await tester.pump();
|
||||||
|
await tester.tap(find.text('保存'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('头像已失效,请重新上传'), findsOneWidget);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('头像上传接线', () {
|
||||||
|
testWidgets('上传全链路:purpose=user_avatar,ready 后确认 → 载荷带新 assetId', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final auth = await pumpEdit(tester);
|
||||||
|
|
||||||
|
await tester.tap(find.text('更换头像'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// 上传成功后 sheet 给预览 + 「使用这张」。
|
||||||
|
expect(find.text('使用这张'), findsOneWidget);
|
||||||
|
await tester.tap(find.text('使用这张'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// 用途即引用侧的类型检查:必须是 user_avatar。
|
||||||
|
final request = community.lastMediaUploadRequest!;
|
||||||
|
expect(request.purpose, MediaPurpose.userAvatar);
|
||||||
|
expect(request.purpose.wire, 'user_avatar');
|
||||||
|
|
||||||
|
expect(find.text('已选择\n新头像'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('保存'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final payload = auth.updateMePayloads.single;
|
||||||
|
expect(payload, {'avatarAssetId': 'a-1'});
|
||||||
|
expect(payload.containsKey('nickname'), isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('上传后再改昵称 → 两键齐出(一次 PATCH 改两样)', (tester) async {
|
||||||
|
final auth = await pumpEdit(tester);
|
||||||
|
|
||||||
|
await tester.tap(find.text('更换头像'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('使用这张'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.enterText(nicknameField, '小柴');
|
||||||
|
await tester.pump();
|
||||||
|
await tester.tap(find.text('保存'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(auth.updateMePayloads.single, {
|
||||||
|
'nickname': '小柴',
|
||||||
|
'avatarAssetId': 'a-1',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_display.dart';
|
||||||
|
|
||||||
|
import '../../helpers/auth_test_helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('展示名回退 nickname ?? username', () {
|
||||||
|
test('有昵称:展示昵称,nickname 字段保留 DB 原值', () {
|
||||||
|
final profile = buildProfile(username: 'llx', nickname: '小柴');
|
||||||
|
expect(profile.displayName, '小柴');
|
||||||
|
expect(profile.nickname, '小柴');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('无昵称:展示 username,但 nickname 仍为 null(不被回退值污染)', () {
|
||||||
|
final profile = buildProfile(username: 'llx');
|
||||||
|
expect(profile.displayName, 'llx');
|
||||||
|
expect(
|
||||||
|
profile.nickname,
|
||||||
|
isNull,
|
||||||
|
reason: '回退只在展示层发生;nickname 必须保持 null,否则编辑页预填后会被固化成真昵称',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('hasAvatar 只看 avatarUrl(响应不外露 avatarAssetId)', () {
|
||||||
|
expect(buildProfile().hasAvatar, isFalse);
|
||||||
|
expect(
|
||||||
|
buildProfile(avatarUrl: 'https://m/a.jpg?X-Amz-Signature=s').hasAvatar,
|
||||||
|
isTrue,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('UpdateMeRequest 三态载荷', () {
|
||||||
|
test('什么都没碰 → isEmpty 且 JSON 为空对象', () {
|
||||||
|
const request = UpdateMeRequest();
|
||||||
|
expect(request.isEmpty, isTrue);
|
||||||
|
expect(request.toJson(), isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('只改昵称:avatarAssetId 键**不出现**(不改,而不是清空)', () {
|
||||||
|
const request = UpdateMeRequest(nickname: PatchField<String>.value('小柴'));
|
||||||
|
final json = request.toJson();
|
||||||
|
expect(json, {'nickname': '小柴'});
|
||||||
|
expect(
|
||||||
|
json.containsKey('avatarAssetId'),
|
||||||
|
isFalse,
|
||||||
|
reason: '发成 avatarAssetId: null 会把用户头像一起清掉',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('只清昵称:nickname 显式 null,头像键仍不出现', () {
|
||||||
|
const request = UpdateMeRequest(nickname: PatchField<String>.clear());
|
||||||
|
final json = request.toJson();
|
||||||
|
expect(json.containsKey('nickname'), isTrue);
|
||||||
|
expect(json['nickname'], isNull);
|
||||||
|
expect(json.containsKey('avatarAssetId'), isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('一次改两样:两键齐出', () {
|
||||||
|
const request = UpdateMeRequest(
|
||||||
|
nickname: PatchField<String>.value('小柴'),
|
||||||
|
avatarAssetId: PatchField<String>.value('asset-9'),
|
||||||
|
);
|
||||||
|
expect(request.toJson(), {'nickname': '小柴', 'avatarAssetId': 'asset-9'});
|
||||||
|
expect(request.isEmpty, isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('只清头像:nickname 键不出现', () {
|
||||||
|
const request = UpdateMeRequest(
|
||||||
|
avatarAssetId: PatchField<String>.clear(),
|
||||||
|
);
|
||||||
|
final json = request.toJson();
|
||||||
|
expect(json.containsKey('nickname'), isFalse);
|
||||||
|
expect(json['avatarAssetId'], isNull);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('昵称校验与 ck_users_nickname 对齐', () {
|
||||||
|
test('1 码点通过;32 CJK 通过;33 拒', () {
|
||||||
|
expect(validateNickname('柴'), isNull);
|
||||||
|
expect(validateNickname('柴' * 32), isNull);
|
||||||
|
expect(validateNickname('柴' * 33), isNotNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('32 个 emoji(UTF-16 长度 64)通过——长度按码点计而非 String.length', () {
|
||||||
|
const emoji = '🐕';
|
||||||
|
final name = emoji * 32;
|
||||||
|
expect(name.length, 64, reason: '前提:UTF-16 长度确为 64');
|
||||||
|
expect(
|
||||||
|
validateNickname(name),
|
||||||
|
isNull,
|
||||||
|
reason: '按 String.length 校验会误拒数据库能存的 32 emoji 昵称',
|
||||||
|
);
|
||||||
|
expect(validateNickname(emoji * 33), isNotNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('btrim 对齐:前后空格不计入长度', () {
|
||||||
|
expect(validateNickname(' 豆豆 '), isNull);
|
||||||
|
expect(validateNickname(' ${'柴' * 32} '), isNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('纯空白不是隐式清空,而是校验失败并指向显式清除入口', () {
|
||||||
|
final message = validateNickname(' ');
|
||||||
|
expect(message, isNotNull);
|
||||||
|
expect(message, contains('清除昵称'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('错误文案分层', () {
|
||||||
|
test('保存失败按 PATCH /me 错误谱分层', () {
|
||||||
|
expect(
|
||||||
|
profileSaveErrorMessage(
|
||||||
|
const ApiBusinessException(code: ApiCodes.paramError, message: 'x'),
|
||||||
|
),
|
||||||
|
'填写内容不符合要求,请检查后重试',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
profileSaveErrorMessage(
|
||||||
|
const ApiBusinessException(
|
||||||
|
code: ApiCodes.mediaNotFound,
|
||||||
|
message: 'x',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'头像已失效,请重新上传',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
profileSaveErrorMessage(
|
||||||
|
const ApiBusinessException(
|
||||||
|
code: ApiCodes.mediaNotReady,
|
||||||
|
message: 'x',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'头像还没上传完,请稍后重试',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
profileSaveErrorMessage(const ApiNetworkException()),
|
||||||
|
'网络异常,请检查网络后重试',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('加载失败文案不透出服务端 message', () {
|
||||||
|
expect(
|
||||||
|
profileLoadErrorMessage(
|
||||||
|
const ApiNetworkException('Connection refused'),
|
||||||
|
),
|
||||||
|
'网络异常,请检查网络后重试',
|
||||||
|
);
|
||||||
|
expect(profileLoadErrorMessage(null), '加载失败,请稍后重试');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,207 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/core/theme/app_theme.dart';
|
||||||
|
import 'package:patbond_flutter/core/widgets/inline_error_banner.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_edit_page.dart';
|
||||||
|
import 'package:patbond_flutter/features/profile/profile_page.dart';
|
||||||
|
import 'package:patbond_flutter/state/app_state.dart';
|
||||||
|
|
||||||
|
import '../../helpers/auth_test_helpers.dart';
|
||||||
|
import '../../helpers/community_test_helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late FakeCommunityRepository community;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
community = FakeCommunityRepository();
|
||||||
|
community.onGetMyCommunityStats = () async =>
|
||||||
|
CommunityStats.fromJson(sampleCommunityStatsJson());
|
||||||
|
community.onGetFollowStats = (_) async =>
|
||||||
|
FollowStats.fromJson(sampleFollowStatsJson());
|
||||||
|
});
|
||||||
|
|
||||||
|
Future<ProfileController> pumpProfile(
|
||||||
|
WidgetTester tester, {
|
||||||
|
required FakeAuthRepository auth,
|
||||||
|
}) async {
|
||||||
|
// 资料页头部 + 五行菜单 + 两个底部按钮,加高视口保证全在栏内。
|
||||||
|
tester.view.physicalSize = const Size(700, 2400);
|
||||||
|
tester.view.devicePixelRatio = 1.0;
|
||||||
|
addTearDown(tester.view.reset);
|
||||||
|
final controller = ProfileController(
|
||||||
|
authRepository: auth,
|
||||||
|
communityRepository: community,
|
||||||
|
);
|
||||||
|
addTearDown(controller.dispose);
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
home: Scaffold(
|
||||||
|
body: ProfilePage(appState: AppState(), controller: controller),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return controller;
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgets('loading 态:居中转圈,无 demo 文案', (tester) async {
|
||||||
|
final gate = Completer<UserProfile>();
|
||||||
|
await pumpProfile(
|
||||||
|
tester,
|
||||||
|
auth: FakeAuthRepository(meHandler: () => gate.future),
|
||||||
|
);
|
||||||
|
// 预取推到帧末,故先走一帧再断言。
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(find.byType(CircularProgressIndicator), findsOneWidget);
|
||||||
|
expect(find.text('萌宠新手(豆豆家长)'), findsNothing);
|
||||||
|
expect(find.text('Patbond 社区创作达人'), findsNothing);
|
||||||
|
|
||||||
|
gate.complete(buildProfile());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('ready + 有昵称:展示昵称与 @username,四个数字来自服务端', (tester) async {
|
||||||
|
await pumpProfile(
|
||||||
|
tester,
|
||||||
|
auth: FakeAuthRepository(
|
||||||
|
meHandler: () async => buildProfile(username: 'llx', nickname: '小柴'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('小柴'), findsOneWidget);
|
||||||
|
expect(find.text('@llx'), findsOneWidget);
|
||||||
|
// 24 关注我 / 7 我关注 / 128 获赞 / 12 作品(helpers 样本)。
|
||||||
|
expect(find.text('24'), findsOneWidget);
|
||||||
|
expect(find.text('7'), findsOneWidget);
|
||||||
|
expect(find.text('128'), findsOneWidget);
|
||||||
|
expect(find.text('12'), findsOneWidget);
|
||||||
|
expect(find.text('获赞'), findsOneWidget);
|
||||||
|
expect(find.text('我的作品'), findsOneWidget);
|
||||||
|
// demo 硬编码彻底退役。
|
||||||
|
expect(find.text('1.8k'), findsNothing);
|
||||||
|
expect(find.text('萌宠新手(豆豆家长)'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('ready + 无昵称:展示名回退 username(回退在客户端展示层)', (tester) async {
|
||||||
|
await pumpProfile(
|
||||||
|
tester,
|
||||||
|
auth: FakeAuthRepository(
|
||||||
|
meHandler: () async => buildProfile(username: 'llx'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// 展示名与 @username 两处都是 llx。
|
||||||
|
expect(find.text('llx'), findsOneWidget);
|
||||||
|
expect(find.text('@llx'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('空数据:四个数字全 0(不是空白、不是错误态)', (tester) async {
|
||||||
|
community.onGetMyCommunityStats = () async => CommunityStats.fromJson(
|
||||||
|
sampleCommunityStatsJson(receivedLikeCount: 0, publishedPostCount: 0),
|
||||||
|
);
|
||||||
|
community.onGetFollowStats = (_) async => FollowStats.fromJson(
|
||||||
|
sampleFollowStatsJson(followerCount: 0, followingCount: 0),
|
||||||
|
);
|
||||||
|
await pumpProfile(tester, auth: FakeAuthRepository());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('0'), findsNWidgets(4));
|
||||||
|
expect(find.text('统计加载失败'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('error 态:横幅 + 重试;重试成功进 ready', (tester) async {
|
||||||
|
var attempts = 0;
|
||||||
|
await pumpProfile(
|
||||||
|
tester,
|
||||||
|
auth: FakeAuthRepository(
|
||||||
|
meHandler: () async {
|
||||||
|
attempts += 1;
|
||||||
|
if (attempts == 1) throw const ApiNetworkException('断网');
|
||||||
|
return buildProfile(nickname: '小柴');
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.byType(InlineErrorBanner), findsOneWidget);
|
||||||
|
expect(find.text('网络异常,请检查网络后重试'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('重试'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('小柴'), findsOneWidget);
|
||||||
|
expect(find.byType(InlineErrorBanner), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('统计块独立降级:资料照常显示,统计给「加载失败 + 重试」', (tester) async {
|
||||||
|
var statsAttempts = 0;
|
||||||
|
community.onGetMyCommunityStats = () async {
|
||||||
|
statsAttempts += 1;
|
||||||
|
if (statsAttempts == 1) throw const ApiNetworkException('断网');
|
||||||
|
return CommunityStats.fromJson(sampleCommunityStatsJson());
|
||||||
|
};
|
||||||
|
await pumpProfile(
|
||||||
|
tester,
|
||||||
|
auth: FakeAuthRepository(
|
||||||
|
meHandler: () async => buildProfile(nickname: '小柴'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('小柴'), findsOneWidget, reason: '统计失败不该拖垮整页');
|
||||||
|
expect(find.text('统计加载失败'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('重试'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('128'), findsOneWidget);
|
||||||
|
expect(find.text('统计加载失败'), findsNothing);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('点「编辑资料」进编辑页;保存成功回资料页并提示', (tester) async {
|
||||||
|
final auth = FakeAuthRepository(
|
||||||
|
meHandler: () async => buildProfile(username: 'llx'),
|
||||||
|
updateMeHandler: (_) async =>
|
||||||
|
buildProfile(username: 'llx', nickname: '小柴'),
|
||||||
|
);
|
||||||
|
await pumpProfile(tester, auth: auth);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.text('编辑资料'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byType(ProfileEditPage), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.enterText(find.byType(TextFormField), '小柴');
|
||||||
|
await tester.pump();
|
||||||
|
await tester.tap(find.text('保存'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.byType(ProfileEditPage), findsNothing);
|
||||||
|
expect(find.text('资料已更新'), findsOneWidget);
|
||||||
|
// 头部随之更新为新昵称。
|
||||||
|
expect(find.text('小柴'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('未装配上传能力时编辑页不渲染头像入口', (tester) async {
|
||||||
|
await pumpProfile(
|
||||||
|
tester,
|
||||||
|
auth: FakeAuthRepository(meHandler: () async => buildProfile()),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.text('编辑资料'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('更换头像'), findsNothing);
|
||||||
|
expect(find.byType(TextFormField), findsOneWidget, reason: '昵称输入仍在');
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -26,15 +26,23 @@ class FakeAuthRepository implements AuthRepository {
|
|||||||
this.loginHandler,
|
this.loginHandler,
|
||||||
this.registerHandler,
|
this.registerHandler,
|
||||||
this.restoreHandler,
|
this.restoreHandler,
|
||||||
|
this.meHandler,
|
||||||
|
this.updateMeHandler,
|
||||||
});
|
});
|
||||||
|
|
||||||
final Future<void> Function()? loginHandler;
|
final Future<void> Function()? loginHandler;
|
||||||
final Future<void> Function()? registerHandler;
|
final Future<void> Function()? registerHandler;
|
||||||
final Future<SessionRestoreResult> Function()? restoreHandler;
|
final Future<SessionRestoreResult> Function()? restoreHandler;
|
||||||
|
final Future<UserProfile> Function()? meHandler;
|
||||||
|
final Future<UserProfile> Function(UpdateMeRequest)? updateMeHandler;
|
||||||
|
|
||||||
int loginCalls = 0;
|
int loginCalls = 0;
|
||||||
int registerCalls = 0;
|
int registerCalls = 0;
|
||||||
int logoutCalls = 0;
|
int logoutCalls = 0;
|
||||||
|
int meCalls = 0;
|
||||||
|
|
||||||
|
/// 收到的 PATCH 载荷(三态断言用:**未改字段不应出现在 map 里**)。
|
||||||
|
final List<Map<String, Object?>> updateMePayloads = [];
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> login({required String username, required String password}) {
|
Future<void> login({required String username, required String password}) {
|
||||||
@@ -64,9 +72,51 @@ class FakeAuthRepository implements AuthRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<UserProfile> me() async => throw UnimplementedError();
|
Future<UserProfile> me() async {
|
||||||
|
meCalls += 1;
|
||||||
|
// 缺省给一个「无昵称无头像」的样本(回退到 username 的形态):
|
||||||
|
// 主壳级测试不必逐个注入。
|
||||||
|
return meHandler?.call() ?? Future.value(buildProfile());
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<UserProfile> updateMe(UpdateMeRequest request) async {
|
||||||
|
updateMePayloads.add(request.toJson());
|
||||||
|
if (updateMeHandler == null) throw UnimplementedError();
|
||||||
|
return updateMeHandler!(request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `/me` 响应样本(契约 v1.4.0 六字段全字段)。
|
||||||
|
Map<String, dynamic> sampleMeJson({
|
||||||
|
String userId = 'user-1',
|
||||||
|
String username = 'llx',
|
||||||
|
String? nickname,
|
||||||
|
String? avatarUrl,
|
||||||
|
String? phone = '+8613800138000',
|
||||||
|
}) => {
|
||||||
|
'userId': userId,
|
||||||
|
'username': username,
|
||||||
|
'nickname': nickname,
|
||||||
|
'avatarUrl': avatarUrl,
|
||||||
|
'phone': phone,
|
||||||
|
'createdAt': '2026-09-01T10:00:00+08:00',
|
||||||
|
};
|
||||||
|
|
||||||
|
UserProfile buildProfile({
|
||||||
|
String userId = 'user-1',
|
||||||
|
String username = 'llx',
|
||||||
|
String? nickname,
|
||||||
|
String? avatarUrl,
|
||||||
|
}) => UserProfile.fromJson(
|
||||||
|
sampleMeJson(
|
||||||
|
userId: userId,
|
||||||
|
username: username,
|
||||||
|
nickname: nickname,
|
||||||
|
avatarUrl: avatarUrl,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
/// mock dio:用假 HttpClientAdapter 返回预设响应并记录请求。
|
/// mock dio:用假 HttpClientAdapter 返回预设响应并记录请求。
|
||||||
class FakeHttpAdapter implements HttpClientAdapter {
|
class FakeHttpAdapter implements HttpClientAdapter {
|
||||||
FakeHttpAdapter(this.handler);
|
FakeHttpAdapter(this.handler);
|
||||||
|
|||||||
@@ -76,13 +76,15 @@ Map<String, dynamic> sampleFeedCardJson({
|
|||||||
|
|
||||||
Map<String, dynamic> sampleCommentJson({
|
Map<String, dynamic> sampleCommentJson({
|
||||||
String id = 'c-1',
|
String id = 'c-1',
|
||||||
|
Map<String, dynamic>? author,
|
||||||
Map<String, dynamic>? replyToUser,
|
Map<String, dynamic>? replyToUser,
|
||||||
|
String content = '好可爱!',
|
||||||
}) => {
|
}) => {
|
||||||
'id': id,
|
'id': id,
|
||||||
'postId': 'p-1',
|
'postId': 'p-1',
|
||||||
'author': sampleAuthorJson(),
|
'author': author ?? sampleAuthorJson(),
|
||||||
'replyToUser': replyToUser,
|
'replyToUser': replyToUser,
|
||||||
'content': '好可爱!',
|
'content': content,
|
||||||
'createdAt': '2026-09-08T11:00:00.000Z',
|
'createdAt': '2026-09-08T11:00:00.000Z',
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -131,6 +133,57 @@ CursorPage<FeedCard> feedPage(
|
|||||||
bool hasMore = false,
|
bool hasMore = false,
|
||||||
}) => CursorPage(items: items, nextCursor: nextCursor, hasMore: hasMore);
|
}) => CursorPage(items: items, nextCursor: nextCursor, hasMore: hasMore);
|
||||||
|
|
||||||
|
Post samplePost({
|
||||||
|
String id = 'p-1',
|
||||||
|
bool likedByMe = false,
|
||||||
|
int likeCount = 6,
|
||||||
|
bool bookmarkedByMe = false,
|
||||||
|
int bookmarkCount = 2,
|
||||||
|
}) => Post.fromJson(
|
||||||
|
samplePostJson(
|
||||||
|
id: id,
|
||||||
|
likedByMe: likedByMe,
|
||||||
|
likeCount: likeCount,
|
||||||
|
bookmarkedByMe: bookmarkedByMe,
|
||||||
|
bookmarkCount: bookmarkCount,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
PostComment sampleComment({
|
||||||
|
String id = 'c-1',
|
||||||
|
Map<String, dynamic>? author,
|
||||||
|
Map<String, dynamic>? replyToUser,
|
||||||
|
String content = '好可爱!',
|
||||||
|
}) => PostComment.fromJson(
|
||||||
|
sampleCommentJson(
|
||||||
|
id: id,
|
||||||
|
author: author,
|
||||||
|
replyToUser: replyToUser,
|
||||||
|
content: content,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
CursorPage<PostComment> commentPage(
|
||||||
|
List<PostComment> items, {
|
||||||
|
String? nextCursor,
|
||||||
|
bool hasMore = false,
|
||||||
|
}) => CursorPage(items: items, nextCursor: nextCursor, hasMore: hasMore);
|
||||||
|
|
||||||
|
/// 草稿样本(T3-17 发布页草稿恢复用;media 可清空为纯文字草稿)。
|
||||||
|
Post sampleDraft({
|
||||||
|
String id = 'draft-1',
|
||||||
|
int version = 3,
|
||||||
|
String content = '草稿正文',
|
||||||
|
bool withMedia = true,
|
||||||
|
}) => Post.fromJson({
|
||||||
|
...samplePostJson(id: id, status: 'draft', version: version),
|
||||||
|
'content': content,
|
||||||
|
if (!withMedia) 'media': const <Map<String, dynamic>>[],
|
||||||
|
});
|
||||||
|
|
||||||
|
CursorPage<Post> postPage(List<Post> items) =>
|
||||||
|
CursorPage(items: items, nextCursor: null, hasMore: false);
|
||||||
|
|
||||||
/// 假仓库:controller 测试注入行为并记录调用(Completer 控时序)。
|
/// 假仓库:controller 测试注入行为并记录调用(Completer 控时序)。
|
||||||
/// 未注入 handler 的方法一律 UnimplementedError(误触发即测试失败)。
|
/// 未注入 handler 的方法一律 UnimplementedError(误触发即测试失败)。
|
||||||
class FakeCommunityRepository implements CommunityRepository {
|
class FakeCommunityRepository implements CommunityRepository {
|
||||||
@@ -141,9 +194,28 @@ class FakeCommunityRepository implements CommunityRepository {
|
|||||||
Future<LikeState> Function(String postId, bool target)? onLikeToggle;
|
Future<LikeState> Function(String postId, bool target)? onLikeToggle;
|
||||||
Future<BookmarkState> Function(String postId, bool target)? onBookmarkToggle;
|
Future<BookmarkState> Function(String postId, bool target)? onBookmarkToggle;
|
||||||
Future<Post> Function(String postId)? onGetPost;
|
Future<Post> Function(String postId)? onGetPost;
|
||||||
|
Future<CursorPage<PostComment>> Function(String postId, String? cursor)?
|
||||||
|
onListComments;
|
||||||
|
Future<PostComment> Function(String postId, CreateCommentRequest request)?
|
||||||
|
onCreateComment;
|
||||||
|
Future<void> Function(String commentId)? onDeleteComment;
|
||||||
|
Future<FollowState> Function(String userId, bool target)? onFollowToggle;
|
||||||
|
Future<FollowStats> Function(String userId)? onGetFollowStats;
|
||||||
|
Future<CommunityStats> Function()? onGetMyCommunityStats;
|
||||||
Future<MediaUploadCredentials> Function(CreateMediaUploadRequest request)?
|
Future<MediaUploadCredentials> Function(CreateMediaUploadRequest request)?
|
||||||
onCreateMediaUpload;
|
onCreateMediaUpload;
|
||||||
Future<MediaAsset> Function(String assetId)? onCompleteMediaUpload;
|
Future<MediaAsset> Function(String assetId)? onCompleteMediaUpload;
|
||||||
|
Future<Post> Function(CreatePostRequest request, String? idempotencyKey)?
|
||||||
|
onCreatePost;
|
||||||
|
Future<Post> Function(String postId, UpdatePostRequest request)? onUpdatePost;
|
||||||
|
Future<void> Function(String postId)? onDeletePost;
|
||||||
|
Future<CursorPage<Post>> Function(PostStatus? status)? onListMyPosts;
|
||||||
|
|
||||||
|
/// createPost 收到的幂等键顺序(同键重放断言用)。
|
||||||
|
final List<String?> idempotencyKeys = [];
|
||||||
|
|
||||||
|
/// 最近一次 createMediaUpload 的请求(purpose 断言用)。
|
||||||
|
CreateMediaUploadRequest? lastMediaUploadRequest;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<CursorPage<FeedCard>> getFeed({int? limit, String? cursor}) {
|
Future<CursorPage<FeedCard>> getFeed({int? limit, String? cursor}) {
|
||||||
@@ -186,6 +258,7 @@ class FakeCommunityRepository implements CommunityRepository {
|
|||||||
CreateMediaUploadRequest request,
|
CreateMediaUploadRequest request,
|
||||||
) {
|
) {
|
||||||
calls.add('createUpload:${request.mimeType}:${request.byteSize}');
|
calls.add('createUpload:${request.mimeType}:${request.byteSize}');
|
||||||
|
lastMediaUploadRequest = request;
|
||||||
return onCreateMediaUpload!(request);
|
return onCreateMediaUpload!(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,50 +269,118 @@ class FakeCommunityRepository implements CommunityRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Post> createPost(CreatePostRequest request) =>
|
Future<Post> createPost(CreatePostRequest request, {String? idempotencyKey}) {
|
||||||
throw UnimplementedError();
|
calls.add('createPost:${request.status?.name}:${request.content}');
|
||||||
|
idempotencyKeys.add(idempotencyKey);
|
||||||
|
return onCreatePost!(request, idempotencyKey);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Post> updatePost(String postId, UpdatePostRequest request) =>
|
Future<Post> updatePost(String postId, UpdatePostRequest request) {
|
||||||
throw UnimplementedError();
|
calls.add(
|
||||||
|
'updatePost:$postId:v${request.version}:publish=${request.publish}',
|
||||||
|
);
|
||||||
|
return onUpdatePost!(postId, request);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> deletePost(String postId) => throw UnimplementedError();
|
Future<void> deletePost(String postId) {
|
||||||
|
calls.add('deletePost:$postId');
|
||||||
|
return onDeletePost!(postId);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<CursorPage<Post>> listMyPosts({
|
Future<CursorPage<Post>> listMyPosts({
|
||||||
int? limit,
|
int? limit,
|
||||||
String? cursor,
|
String? cursor,
|
||||||
PostStatus? status,
|
PostStatus? status,
|
||||||
}) => throw UnimplementedError();
|
}) {
|
||||||
|
calls.add('listMyPosts:${status?.name}');
|
||||||
|
return onListMyPosts == null
|
||||||
|
? Future.value(
|
||||||
|
const CursorPage(items: [], nextCursor: null, hasMore: false),
|
||||||
|
)
|
||||||
|
: onListMyPosts!(status);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<CursorPage<PostComment>> listComments(
|
Future<CursorPage<PostComment>> listComments(
|
||||||
String postId, {
|
String postId, {
|
||||||
int? limit,
|
int? limit,
|
||||||
String? cursor,
|
String? cursor,
|
||||||
}) => throw UnimplementedError();
|
}) {
|
||||||
|
calls.add('comments:$postId:cursor=$cursor');
|
||||||
|
return onListComments!(postId, cursor);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<PostComment> createComment(
|
Future<PostComment> createComment(
|
||||||
String postId,
|
String postId,
|
||||||
CreateCommentRequest request,
|
CreateCommentRequest request,
|
||||||
) => throw UnimplementedError();
|
) {
|
||||||
|
calls.add('createComment:$postId:${request.content}');
|
||||||
|
return onCreateComment!(postId, request);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> deleteComment(String commentId) => throw UnimplementedError();
|
Future<void> deleteComment(String commentId) {
|
||||||
|
calls.add('deleteComment:$commentId');
|
||||||
|
return onDeleteComment!(commentId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<FollowState> followUser(String userId) {
|
||||||
|
calls.add('follow:$userId');
|
||||||
|
return onFollowToggle!(userId, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<FollowState> unfollowUser(String userId) {
|
||||||
|
calls.add('unfollow:$userId');
|
||||||
|
return onFollowToggle!(userId, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<FollowStats> getFollowStats(String userId) {
|
||||||
|
calls.add('followStats:$userId');
|
||||||
|
return onGetFollowStats?.call(userId) ??
|
||||||
|
Future.value(FollowStats.fromJson(sampleFollowStatsJson()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<CommunityStats> getMyCommunityStats() {
|
||||||
|
calls.add('communityStats');
|
||||||
|
return onGetMyCommunityStats?.call() ??
|
||||||
|
// 缺省零值(契约:空数据返回 0 而非 null,且永不 404)。
|
||||||
|
Future.value(
|
||||||
|
CommunityStats.fromJson(
|
||||||
|
sampleCommunityStatsJson(
|
||||||
|
receivedLikeCount: 0,
|
||||||
|
publishedPostCount: 0,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<CursorPage<FeedCard>> listMyBookmarks({int? limit, String? cursor}) =>
|
Future<CursorPage<FeedCard>> listMyBookmarks({int? limit, String? cursor}) =>
|
||||||
throw UnimplementedError();
|
throw UnimplementedError();
|
||||||
|
|
||||||
@override
|
|
||||||
Future<FollowState> followUser(String userId) => throw UnimplementedError();
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<FollowState> unfollowUser(String userId) => throw UnimplementedError();
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<FollowStats> getFollowStats(String userId) =>
|
|
||||||
throw UnimplementedError();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> sampleCommunityStatsJson({
|
||||||
|
int receivedLikeCount = 128,
|
||||||
|
int publishedPostCount = 12,
|
||||||
|
}) => {
|
||||||
|
'receivedLikeCount': receivedLikeCount,
|
||||||
|
'publishedPostCount': publishedPostCount,
|
||||||
|
};
|
||||||
|
|
||||||
|
Map<String, dynamic> sampleFollowStatsJson({
|
||||||
|
int followerCount = 24,
|
||||||
|
int followingCount = 7,
|
||||||
|
bool followedByMe = false,
|
||||||
|
}) => {
|
||||||
|
'followerCount': followerCount,
|
||||||
|
'followingCount': followingCount,
|
||||||
|
'followedByMe': followedByMe,
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:patbond_flutter/features/community/community_models.dart';
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
@@ -14,6 +15,17 @@ PickedMediaImage pickedImage({int seed = 1, int size = 64}) => PickedMediaImage(
|
|||||||
name: 'img-$seed.jpg',
|
name: 'img-$seed.jpg',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// 1x1 真 PNG 字节(70B):widget 测试里九宫格格子要真解码出图,
|
||||||
|
/// 用可解码的最小合法图片避免 Image.memory 走 errorBuilder 兜底。
|
||||||
|
final Uint8List tinyPngBytes = base64Decode(
|
||||||
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAF'
|
||||||
|
'AAH/q842iQAAAABJRU5ErkJggg==',
|
||||||
|
);
|
||||||
|
|
||||||
|
/// 可解码预览的选图(发布页 widget 测试用)。
|
||||||
|
PickedMediaImage decodablePickedImage({int seed = 1}) =>
|
||||||
|
PickedMediaImage(bytes: tinyPngBytes, name: 'img-$seed.png');
|
||||||
|
|
||||||
MediaUploadCredentials credentials({
|
MediaUploadCredentials credentials({
|
||||||
String assetId = 'a-1',
|
String assetId = 'a-1',
|
||||||
DateTime? expiresAt,
|
DateTime? expiresAt,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/// pets 域测试样本(契约 openapi.yaml v1.2.0 各 schema 全字段 JSON)
|
/// pets 域测试样本(契约 openapi.yaml v1.4.0 各 schema 全字段 JSON)
|
||||||
/// 与共享假仓库。
|
/// 与共享假仓库。
|
||||||
library;
|
library;
|
||||||
|
|
||||||
@@ -27,6 +27,7 @@ Map<String, dynamic> samplePetJson({
|
|||||||
'microchipNo': null,
|
'microchipNo': null,
|
||||||
'sterilizedOn': null,
|
'sterilizedOn': null,
|
||||||
'status': 'active',
|
'status': 'active',
|
||||||
|
'avatarUrl': null,
|
||||||
'myRole': 'owner',
|
'myRole': 'owner',
|
||||||
'createdAt': '2026-09-01T10:00:00+08:00',
|
'createdAt': '2026-09-01T10:00:00+08:00',
|
||||||
'updatedAt': '2026-09-02T10:00:00+08:00',
|
'updatedAt': '2026-09-02T10:00:00+08:00',
|
||||||
|
|||||||
@@ -0,0 +1,268 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_client.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/api_exception.dart';
|
||||||
|
import 'package:patbond_flutter/core/network/token_refresher.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/auth/session_manager.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_controller.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_models.dart';
|
||||||
|
import 'package:patbond_flutter/features/community/community_repository.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
import '../helpers/auth_test_helpers.dart';
|
||||||
|
|
||||||
|
/// T3-15/16 compose 真链路冒烟(默认跳过,不计入常规测试套件):
|
||||||
|
///
|
||||||
|
/// ```bash
|
||||||
|
/// # 先起后端六容器(patbond-api 仓库根):
|
||||||
|
/// # ./deploy/init-secrets.sh
|
||||||
|
/// # JAVA_HOME=<JDK17> ./mvnw -DskipTests package && docker compose up -d --build
|
||||||
|
/// PATBOND_DETAIL_SMOKE=1 flutter test test/smoke/detail_interactions_smoke_test.dart
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// 覆盖:发帖 → 点赞(含重复施加幂等)→ 收藏/取消 → 评论创建
|
||||||
|
/// (Idempotency-Key)→ 仅作者删除评论 → 计数权威对账;再走一轮
|
||||||
|
/// **断网点赞回滚**——[CommunityController]/ToggleSync 生产实现 +
|
||||||
|
/// 生产 ApiClient 错误链,community 端点切至不可达端口模拟断网
|
||||||
|
/// (连接拒绝走真实 ApiNetworkException 路径),断言乐观翻转即刻
|
||||||
|
/// 可见、失败后快照回滚、网络恢复后权威终态收敛。
|
||||||
|
void main() {
|
||||||
|
final enabled = Platform.environment['PATBOND_DETAIL_SMOKE'] == '1';
|
||||||
|
final env = Platform.environment;
|
||||||
|
final authBase = env['PATBOND_SMOKE_AUTH_BASE'] ?? 'http://127.0.0.1:8081';
|
||||||
|
final communityBase =
|
||||||
|
env['PATBOND_SMOKE_COMMUNITY_BASE'] ?? 'http://127.0.0.1:8084';
|
||||||
|
|
||||||
|
test(
|
||||||
|
'点赞/收藏/评论/删评真链路一轮 + 断网点赞回滚',
|
||||||
|
() async {
|
||||||
|
// ---- 注册一次性账号(随机凭据,不落任何持久化)----
|
||||||
|
final dio = Dio(BaseOptions(validateStatus: (_) => true));
|
||||||
|
final seed = DateTime.now().millisecondsSinceEpoch;
|
||||||
|
final register = await dio.post<Map<String, dynamic>>(
|
||||||
|
'$authBase/api/v1/auth/register',
|
||||||
|
data: {
|
||||||
|
'username': 'ismoke$seed',
|
||||||
|
'phone': '+86138${(seed % 100000000).toString().padLeft(8, '0')}',
|
||||||
|
'password': 'Smoke1234!$seed',
|
||||||
|
},
|
||||||
|
options: Options(
|
||||||
|
headers: {
|
||||||
|
'Idempotency-Key': const Uuid().v4(),
|
||||||
|
'X-Device-Id': const Uuid().v4(),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
expect(register.data?['code'], 0, reason: '注册失败:${register.data}');
|
||||||
|
|
||||||
|
final session = SessionManager(store: InMemoryTokenStore());
|
||||||
|
await session.updateTokens(
|
||||||
|
AuthTokens.fromJson(register.data!['data'] as Map<String, dynamic>),
|
||||||
|
);
|
||||||
|
final refresher = TokenRefresher(
|
||||||
|
dio: buildPatbondDio(session: session, baseUrl: authBase),
|
||||||
|
session: session,
|
||||||
|
);
|
||||||
|
final live = ApiCommunityRepository(
|
||||||
|
api: ApiClient(
|
||||||
|
dio: buildPatbondDio(session: session, baseUrl: communityBase),
|
||||||
|
session: session,
|
||||||
|
refresher: refresher,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
// 「断网」形态:community 域切至不可达端口(连接拒绝 → 生产
|
||||||
|
// ApiClient 映射 ApiNetworkException,与真实断网同一异常链)。
|
||||||
|
final dead = ApiCommunityRepository(
|
||||||
|
api: ApiClient(
|
||||||
|
dio: buildPatbondDio(session: session, baseUrl: 'http://127.0.0.1:9'),
|
||||||
|
session: session,
|
||||||
|
refresher: refresher,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final switchable = _SwitchableRepository(live);
|
||||||
|
|
||||||
|
// ---- 发帖(published,纯文字)----
|
||||||
|
final post = await live.createPost(
|
||||||
|
CreatePostRequest(
|
||||||
|
content: 'T3-15/16 互动冒烟 $seed',
|
||||||
|
status: PostStatus.published,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
expect(post.likeCount, 0);
|
||||||
|
expect(post.commentCount, 0);
|
||||||
|
|
||||||
|
// ---- 点赞:施加 / 重复施加幂等,权威终态 ----
|
||||||
|
var like = await live.likePost(post.id);
|
||||||
|
expect(like.liked, true);
|
||||||
|
expect(like.likeCount, 1);
|
||||||
|
like = await live.likePost(post.id);
|
||||||
|
expect(like.likeCount, 1, reason: '重复 PUT 不重复计数');
|
||||||
|
|
||||||
|
// ---- 收藏 / 取消 ----
|
||||||
|
var bookmark = await live.bookmarkPost(post.id);
|
||||||
|
expect(bookmark.bookmarked, true);
|
||||||
|
expect(bookmark.bookmarkCount, 1);
|
||||||
|
bookmark = await live.unbookmarkPost(post.id);
|
||||||
|
expect(bookmark.bookmarked, false);
|
||||||
|
expect(bookmark.bookmarkCount, 0);
|
||||||
|
|
||||||
|
// ---- 评论创建(Idempotency-Key 由仓库层携带)→ 计数 +1 ----
|
||||||
|
final comment = await live.createComment(
|
||||||
|
post.id,
|
||||||
|
const CreateCommentRequest(content: '冒烟评论:真链路一轮'),
|
||||||
|
);
|
||||||
|
expect(comment.content, '冒烟评论:真链路一轮');
|
||||||
|
var fresh = await live.getPost(post.id);
|
||||||
|
expect(fresh.commentCount, 1);
|
||||||
|
|
||||||
|
// ---- 仅作者删除评论 → 计数 -1、列表剔除 ----
|
||||||
|
await live.deleteComment(comment.id);
|
||||||
|
fresh = await live.getPost(post.id);
|
||||||
|
expect(fresh.commentCount, 0);
|
||||||
|
final comments = await live.listComments(post.id);
|
||||||
|
expect(comments.items, isEmpty);
|
||||||
|
|
||||||
|
// ---- 断网点赞回滚(生产 CommunityController + ToggleSync)----
|
||||||
|
final controller = CommunityController(repository: switchable);
|
||||||
|
await controller.refresh();
|
||||||
|
FeedCard card() => controller.feed.firstWhere((c) => c.id == post.id);
|
||||||
|
expect(card().likedByMe, true);
|
||||||
|
expect(card().likeCount, 1);
|
||||||
|
|
||||||
|
switchable.target = dead; // 拔网线。
|
||||||
|
final errored = Completer<void>();
|
||||||
|
controller.addListener(() {
|
||||||
|
if (controller.toggleError != null && !errored.isCompleted) {
|
||||||
|
errored.complete();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
controller.toggleLike(post.id);
|
||||||
|
// 乐观翻转同帧可见。
|
||||||
|
expect(card().likedByMe, false);
|
||||||
|
expect(card().likeCount, 0);
|
||||||
|
|
||||||
|
await errored.future.timeout(const Duration(seconds: 30));
|
||||||
|
// 快照回滚成对恢复 + 一次性错误可供 SnackBar 消费。
|
||||||
|
expect(card().likedByMe, true);
|
||||||
|
expect(card().likeCount, 1);
|
||||||
|
expect(controller.toggleError, isA<ApiNetworkException>());
|
||||||
|
controller.clearToggleError();
|
||||||
|
|
||||||
|
// ---- 网络恢复:取消点赞收敛到服务端权威终态 ----
|
||||||
|
switchable.target = live;
|
||||||
|
controller.toggleLike(post.id);
|
||||||
|
expect(card().likedByMe, false); // 乐观翻转。
|
||||||
|
// 轮询服务端权威终态(乐观值不作为收敛依据)。
|
||||||
|
final deadline = DateTime.now().add(const Duration(seconds: 15));
|
||||||
|
Post settled = await live.getPost(post.id);
|
||||||
|
while (settled.likedByMe) {
|
||||||
|
expect(DateTime.now().isBefore(deadline), true, reason: '收敛超时');
|
||||||
|
await Future<void>.delayed(const Duration(milliseconds: 300));
|
||||||
|
settled = await live.getPost(post.id);
|
||||||
|
}
|
||||||
|
expect(settled.likedByMe, false);
|
||||||
|
expect(settled.likeCount, 0);
|
||||||
|
expect(controller.toggleError, isNull);
|
||||||
|
expect(card().likedByMe, false);
|
||||||
|
controller.dispose();
|
||||||
|
},
|
||||||
|
skip: enabled ? false : '设 PATBOND_DETAIL_SMOKE=1 且后端六容器在本机运行时才执行',
|
||||||
|
timeout: const Timeout(Duration(minutes: 3)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 可切换目标的仓库代理(断网模拟:live ↔ dead 端点整体切换)。
|
||||||
|
class _SwitchableRepository implements CommunityRepository {
|
||||||
|
_SwitchableRepository(this.target);
|
||||||
|
|
||||||
|
CommunityRepository target;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<MediaUploadCredentials> createMediaUpload(
|
||||||
|
CreateMediaUploadRequest request,
|
||||||
|
) => target.createMediaUpload(request);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<MediaAsset> completeMediaUpload(String assetId) =>
|
||||||
|
target.completeMediaUpload(assetId);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Post> createPost(
|
||||||
|
CreatePostRequest request, {
|
||||||
|
String? idempotencyKey,
|
||||||
|
}) => target.createPost(request, idempotencyKey: idempotencyKey);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Post> getPost(String postId) => target.getPost(postId);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Post> updatePost(String postId, UpdatePostRequest request) =>
|
||||||
|
target.updatePost(postId, request);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> deletePost(String postId) => target.deletePost(postId);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<CursorPage<Post>> listMyPosts({
|
||||||
|
int? limit,
|
||||||
|
String? cursor,
|
||||||
|
PostStatus? status,
|
||||||
|
}) => target.listMyPosts(limit: limit, cursor: cursor, status: status);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<CursorPage<FeedCard>> getFeed({int? limit, String? cursor}) =>
|
||||||
|
target.getFeed(limit: limit, cursor: cursor);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<CursorPage<PostComment>> listComments(
|
||||||
|
String postId, {
|
||||||
|
int? limit,
|
||||||
|
String? cursor,
|
||||||
|
}) => target.listComments(postId, limit: limit, cursor: cursor);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<PostComment> createComment(
|
||||||
|
String postId,
|
||||||
|
CreateCommentRequest request,
|
||||||
|
) => target.createComment(postId, request);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> deleteComment(String commentId) =>
|
||||||
|
target.deleteComment(commentId);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<LikeState> likePost(String postId) => target.likePost(postId);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<LikeState> unlikePost(String postId) => target.unlikePost(postId);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<BookmarkState> bookmarkPost(String postId) =>
|
||||||
|
target.bookmarkPost(postId);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<BookmarkState> unbookmarkPost(String postId) =>
|
||||||
|
target.unbookmarkPost(postId);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<CursorPage<FeedCard>> listMyBookmarks({int? limit, String? cursor}) =>
|
||||||
|
target.listMyBookmarks(limit: limit, cursor: cursor);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<CommunityStats> getMyCommunityStats() => target.getMyCommunityStats();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<FollowState> followUser(String userId) => target.followUser(userId);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<FollowState> unfollowUser(String userId) =>
|
||||||
|
target.unfollowUser(userId);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<FollowStats> getFollowStats(String userId) =>
|
||||||
|
target.getFollowStats(userId);
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user