057c11eee2
- RoutingProfile/RoutingRule/Builtin 手写 fromJson/toJson/copyWith,对齐服务端 GET/POST /v1/me/routing 契约;enabled 缺省容错默认 true,toJson 恒输出该字段 - AccountApi.routingProfile()/saveRoutingProfile() 封装两端点 - RoutingProfileNotifier(AsyncNotifier):addRule/removeRule/updateRule/reorder/ setMode/setBuiltin 均本地乐观更新后落盘;save() 失败整体回退到变更前 state 并 rethrow(不静默吞,交调用方处理)——Riverpod asyncTransition 的 seamless copyWithPrevious 会用当前已存 state 覆盖手动挂的错误值,故不用 AsyncError 路径,改走显式回退
89 lines
3.8 KiB
Dart
89 lines
3.8 KiB
Dart
// routing_provider.dart — 可配置分流档案(GET/POST /v1/me/routing)状态层。
|
|
//
|
|
// 仿 account_providers.dart:DevicesNotifier 的 AsyncNotifier + invalidateSelf 范式,
|
|
// 但变更类操作(addRule/removeRule/reorder/setMode)采用「本地乐观更新 → 落盘」:
|
|
// 先改 state 再 saveRoutingProfile,失败则把 state 还原到变更前并 rethrow(不静默吞,
|
|
// 由调用方 catch 后提示用户;仿 DevicesNotifier.remove/rename 等「失败即抛」范式)。
|
|
//
|
|
// 注:不用 `state = AsyncError(...).copyWithPrevious(...)` 手动挂错误 ——
|
|
// Riverpod AsyncNotifier 的 `state=` setter 内部 asyncTransition() 总会用
|
|
// **当前已存的 state**(此时已是乐观更新后的 next)重新 copyWithPrevious 一次,
|
|
// 会把我们想还原的旧值又覆盖回 next,达不到「回退」效果。故失败时直接整体
|
|
// `state = AsyncData(previous)`(纯回退,无错误标记)+ rethrow 传递异常。
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../models/routing_profile.dart';
|
|
import 'account_providers.dart';
|
|
import 'auth_provider.dart';
|
|
|
|
/// 当前账户的分流档案。未登录返回默认档案(不打后端)。
|
|
class RoutingProfileNotifier extends AsyncNotifier<RoutingProfile> {
|
|
@override
|
|
Future<RoutingProfile> build() async {
|
|
final auth = ref.watch(authProvider);
|
|
if (!auth.isLoggedIn) return const RoutingProfile();
|
|
return ref.read(accountApiProvider).routingProfile();
|
|
}
|
|
|
|
/// 显式保存当前 state(供批量编辑后统一保存的场景;单条变更类方法已自带保存)。
|
|
Future<void> save() async {
|
|
final profile = state.valueOrNull;
|
|
if (profile == null) return;
|
|
await _persist(profile, previous: profile);
|
|
}
|
|
|
|
/// 切换分流模式(rule/global/direct)。
|
|
Future<void> setMode(String mode) => _mutateAndSave((p) => p.copyWith(mode: mode));
|
|
|
|
/// 切换内置分流开关。
|
|
Future<void> setBuiltin(Builtin builtin) => _mutateAndSave((p) => p.copyWith(builtin: builtin));
|
|
|
|
/// 追加一条自定义规则(默认追到末尾,规则命中顺序 = 列表顺序)。
|
|
Future<void> addRule(RoutingRule rule) =>
|
|
_mutateAndSave((p) => p.copyWith(rules: [...p.rules, rule]));
|
|
|
|
/// 删除第 index 条规则。
|
|
Future<void> removeRule(int index) => _mutateAndSave((p) {
|
|
final rules = [...p.rules]..removeAt(index);
|
|
return p.copyWith(rules: rules);
|
|
});
|
|
|
|
/// 替换第 index 条规则(如切换 enabled / 编辑字段)。
|
|
Future<void> updateRule(int index, RoutingRule rule) => _mutateAndSave((p) {
|
|
final rules = [...p.rules];
|
|
rules[index] = rule;
|
|
return p.copyWith(rules: rules);
|
|
});
|
|
|
|
/// 拖拽排序(语义对齐 Flutter `ReorderableListView.onReorder`:newIndex 是移除
|
|
/// oldIndex 之前的目标下标)。
|
|
Future<void> reorder(int oldIndex, int newIndex) => _mutateAndSave((p) {
|
|
final rules = [...p.rules];
|
|
var target = newIndex;
|
|
if (oldIndex < newIndex) target -= 1;
|
|
final item = rules.removeAt(oldIndex);
|
|
rules.insert(target, item);
|
|
return p.copyWith(rules: rules);
|
|
});
|
|
|
|
Future<void> _mutateAndSave(RoutingProfile Function(RoutingProfile) transform) async {
|
|
final prev = state.valueOrNull ?? const RoutingProfile();
|
|
final next = transform(prev);
|
|
state = AsyncData(next); // 乐观更新,UI 立即反映
|
|
await _persist(next, previous: prev);
|
|
}
|
|
|
|
/// 落盘;失败回退 state 到 [previous] 并 rethrow(不静默吞,由调用方 catch 提示)。
|
|
Future<void> _persist(RoutingProfile profile, {required RoutingProfile previous}) async {
|
|
try {
|
|
await ref.read(accountApiProvider).saveRoutingProfile(profile);
|
|
} catch (_) {
|
|
state = AsyncData(previous);
|
|
rethrow;
|
|
}
|
|
}
|
|
}
|
|
|
|
final routingProfileProvider =
|
|
AsyncNotifierProvider<RoutingProfileNotifier, RoutingProfile>(RoutingProfileNotifier.new);
|