2930d76cf3
承接 FT-A(GET /v1/me/routing 含只读 system_locked_domains)。 - RoutingProfile 加只读 systemLockedDomains(fromJson 读/toJson 不输出); routing_screen 冲突检测扩展到域名类规则(domain/domain_suffix/domain_keyword 命中锁定域名 → systemLocked),ip_cidr 私网启发式保留。 - RoutingProfile.defaults() + RoutingProfileNotifier.resetToDefault()(复用 _persist:乐观更新/失败回滚/存成功后自动重连,保留只读 systemLockedDomains 不丢)+ routing_screen 加「重置默认」按钮与二次确认弹层(新增 3 个 l10n 键)。 - RoutingRule.copyWith 用哨兵支持 note 显式清空为 null;RoutingRule/Builtin/ RoutingProfile 加值相等 operator==/hashCode。 - T8 smartRouteSub 清理:grep 全仓发现 design/prototype/i18n/alias.json → gen_proto_i18n.mjs(CI 漂移闸)仍有活引用,按计划口径不删,详见 .superpowers/sdd/task-FTB-report.md。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
112 lines
5.2 KiB
Dart
112 lines
5.2 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 'dart:async';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../models/routing_profile.dart';
|
|
import 'account_providers.dart';
|
|
import 'auth_provider.dart';
|
|
import 'connection_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);
|
|
});
|
|
|
|
/// 重置为出厂默认档案(模式/内置开关/自定义规则/final 全部还原),但保留当前只读的
|
|
/// [RoutingProfile.systemLockedDomains] 回显不丢——它不是用户可改的档案内容,只是
|
|
/// 服务端下发的账户相关信息,与「重置」无关。复用 [_mutateAndSave]/[_persist]:
|
|
/// 乐观更新 + 失败回滚 rethrow + 存成功后连接态 on 则自动重连,同 addRule。
|
|
Future<void> resetToDefault() => _mutateAndSave(
|
|
(p) => RoutingProfile.defaults().copyWith(systemLockedDomains: p.systemLockedDomains),
|
|
);
|
|
|
|
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;
|
|
}
|
|
_reconnectIfConnected(); // Task 9:保存成功且已连接 → 重连使新规则生效
|
|
}
|
|
|
|
/// 用 ref.read(非 watch)单向读取 connectionProvider:仅本 notifier 在保存成功后
|
|
/// 顺带触发一次重连,不建立「connectionProvider 依赖 routingProfileProvider」的反向
|
|
/// 引用,provider 依赖图仍是单向(routing → connection),不成环。off 态不触发——
|
|
/// 下次连接本就会按最新档案渲染配置。重连失败由 connection_provider 自身状态承接
|
|
/// (沿用其既有错误提示),不 rethrow 到这里,不影响「保存」这个操作本身的成败。
|
|
void _reconnectIfConnected() {
|
|
if (ref.read(connectionProvider).phase == VpnPhase.on) {
|
|
unawaited(ref.read(connectionProvider.notifier).reapplyRoutingProfile());
|
|
}
|
|
}
|
|
}
|
|
|
|
final routingProfileProvider =
|
|
AsyncNotifierProvider<RoutingProfileNotifier, RoutingProfile>(RoutingProfileNotifier.new);
|