Files
Morris a361c889f8 修复审计发现的问题,默认构建即带 Flutter 前端
P0
- GC-SAVEDOG:更新改为整目录替换(旧文件备份到 .GC/update/backup,
  失败可完整回滚),不再只搬一个 exe;回滚会删掉新包独有文件
- GC-SAVEDOG:包名校验放宽为单文件名模式,Linux 启动器(无扩展名)可更新
- GC-DOWNLOAD:测速全失败时不再回退 GitCode,改为返回「不可用」并保留上次可用源
- GC-DOWNLOAD:识别并赋予 Linux 无扩展名启动器可执行位;兼容脚本清理旧版时
  也覆盖无扩展名产物
- CI:删除 nightly-prerelease(绕开 build.sh,产出无前端空壳);alpha 工作流
  去掉非法 cron,只保留手动触发

P1
- 前端:主题设置真正生效(深色/浅色/跟随系统 + 持久化),去掉死字段
- 前端:下载页轮询改为自适应单次链,页面不可见或任务结束后降频/停止
- 前端:下载完成 / 删除模型后首页列表自动刷新
- 前端:复用 http.Client,不再每次轮询新建连接
- 后端:/api/models 走缓存,不再每次全盘递归统计体积
- 设置页:外部改动能同步进表单,不再只同步一次

P2
- 错误响应统一为 ErrorDto,不再复用 LaunchResultDto
- 限流桶定期清理,避免无限增长
- 播放器窗口去重,避免同一模型重复开窗
- 封面按实际格式返回,去掉错误的 image/png 与 no-store
- 端口集中到 GcUiContract,前端可用 GC_UI_PORT 或 ui-backend.json 发现后端
- 构建:Windows 包改为散开发布,与后端共用运行时(体积约 109MB -> 72MB)
- 文档:更新仓库地址与更新流程说明

测试:dotnet build 全绿;GC.Tests 更新链路全通过;flutter analyze 无问题;
flutter test 11 项全通过。
2026-11-18 20:10:03 +08:00

188 lines
4.8 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'api_client.dart';
import 'i18n.dart';
import 'theme.dart';
/// 外壳状态:后端客户端 + 设置 + 健康状态 + 更新状态条。
///
/// 各页面只读这里的数据、通过这里的方法改后端,避免每页各持一份状态。
class ShellController extends ChangeNotifier {
ShellController({GcApi? api}) : api = api ?? GcApi();
final GcApi api;
int index = 0;
SettingsInfo? settings;
HealthInfo? health;
String? loadError;
bool updateBarVisible = false;
bool updateBusy = false;
double updateProgress = 0;
String updateStatus = '';
String updateDetail = '';
String launcherNotice = '';
/// 模型库版本号:安装 / 删除 / 下载完成后自增,首页据此重新拉列表。
///
/// 首页被 `Offstage` 保活,切走再切回不会重建;没有这个信号时,
/// 下载完模型回到首页仍然显示旧的空列表。
int modelsRevision = 0;
Timer? _retryTimer;
int _retryAttempt = 0;
bool get backendReady => settings != null;
Future<void> bootstrap() async {
_retryTimer?.cancel();
HealthInfo? healthResult;
SettingsInfo? settingsResult;
Object? firstError;
// 两个请求互不依赖,并行发;各自捕获异常,避免一方失败时
// 另一个的 Future 抛成未处理异常(Flutter 会在控制台打出恶性警告)。
await Future.wait<void>(<Future<void>>[
() async {
try {
healthResult = await api.health();
} catch (error) {
firstError ??= error;
}
}(),
() async {
try {
settingsResult = await api.settings();
} catch (error) {
firstError ??= error;
}
}(),
]);
if (healthResult != null) health = healthResult;
if (settingsResult != null) {
settings = settingsResult;
_applySettings(settingsResult!);
}
if (firstError != null && settingsResult == null) {
loadError = firstError.toString();
_scheduleRetry();
} else {
loadError = null;
_retryAttempt = 0;
}
notifyListeners();
}
/// 后端没起来时自动重试(2s、4s、8s…最多 5 次),不必让用户手动点。
void _scheduleRetry() {
if (_retryAttempt >= 5) return;
final seconds = 1 << _retryAttempt; // 1,2,4,8,16
_retryAttempt++;
_retryTimer?.cancel();
_retryTimer = Timer(Duration(seconds: seconds), () {
if (settings == null) bootstrap();
});
}
void _applySettings(SettingsInfo value) {
I18n.setLanguage(value.language);
GcPalette.apply(value.accentColor);
GcPalette.applyTheme(value.theme);
}
void goTo(int value) {
if (index == value) return;
index = value;
notifyListeners();
}
/// 模型库有变化(安装 / 删除 / 下载完成):通知首页刷新。
void modelsChanged() {
modelsRevision++;
notifyListeners();
}
Future<void> reloadSettings() async {
try {
settings = await api.settings();
loadError = null;
_applySettings(settings!);
} catch (error) {
loadError = error.toString();
}
notifyListeners();
}
Future<void> saveSettings(SettingsPatch patch) async {
try {
settings = await api.saveSettings(patch);
loadError = null;
_applySettings(settings!);
} catch (error) {
loadError = error.toString();
}
notifyListeners();
}
void setUpdateStatus({
bool? visible,
bool? busy,
double? progress,
String? status,
String? detail,
String? notice,
}) {
if (visible != null) updateBarVisible = visible;
if (busy != null) updateBusy = busy;
if (progress != null) updateProgress = progress;
if (status != null) updateStatus = status;
if (detail != null) updateDetail = detail;
if (notice != null) launcherNotice = notice;
notifyListeners();
}
/// 检查模型 / 启动器更新(设置页与状态条共用)。
Future<void> checkUpdate({bool force = true}) async {
if (updateBusy) return;
setUpdateStatus(
visible: true,
busy: true,
progress: 0,
status: T('update.checking'),
detail: '',
notice: '',
);
try {
final result = await api.checkUpdate(force: force);
setUpdateStatus(
busy: false,
progress: 1,
status: result.message.isEmpty ? T('update.done') : result.message,
detail: result.error ?? '',
notice: result.launcherMessage ?? '',
);
if (result.updatedModels > 0) modelsChanged();
} catch (error) {
setUpdateStatus(
busy: false,
status: T('update.offline'),
detail: error.toString(),
);
}
}
@override
void dispose() {
_retryTimer?.cancel();
super.dispose();
}
}