Files
GC/GC-UI/frontend/lib/controller.dart
T
Morris ea1fa6b6d1
PR 审核 / 后端编译 (pull_request) Waiting to run
PR 审核 / 前端检查(Flutter) (pull_request) Waiting to run
PR 审核 / 安全扫描 (pull_request) Waiting to run
feat: 全面迁移到 Flutter + 新增 GitHub 源与 CI 流水线
前端
- 移除全部 Avalonia 依赖,模型渲染与界面统一改用 Flutter
- 新增用户页:登录/注册(SHA-512 + 盐)、签到积分(30 天一循环)、
  主题色积分购买、二选一随机、今日人品
- 新增百宝箱页、模型内嵌渲染页、自绘标题栏(最小化/最大化/关闭)
- 下载页显示模型下载进度条,下载源读取加速
- 关于页新增「重新下载启动器」并在下载后做 sha256 完整性校验
- 修复浅色主题下标题栏不跟随主题色、无法连接后端等问题

后端
- 新增 UserService:.GC/user/<用户名>.yaml 存用户名/积分/SHA-512 口令,
  登录返回 session token,本地 .GC/user/user.yaml 保存登录态
- 新增 /api/user/{register,login,me,signin,theme} 接口
- 去掉 Avalonia 窗口宿主,后端只提供 HTTP API

下载源
- 新增 GitHub 镜像源(Morris-morris-cloud/GC),与 Xhmcl 并发测速
- 单源探测超时收紧到 2 秒,取最快且可用的源;GitHub 复用 GiteaRemote
  (contents 带 ref、releases 带 per_page=100)

构建与 CI
- macOS 上默认只出 macOS 包,Windows 交给原生 Windows 构建
- 修复 Windows 交叉编译中 flutter-cpp-client-wrapper 双层目录嵌套
- 新增 GitHub Actions:
  · nightly-prerelease:每天 21:00 编译三平台,发布 nightly 预发布
  · build-bot:Issue/PR 评论 @build-bot build <mac|win-amd|win-arm|all>,
    发布 ip-prerelease 预发布
  · pr-review:检查能否编译、静态分析、合并冲突、是否落后 main
- 忽略 79MB 课件产物等高线.html 与工具链临时备份
2026-11-21 19:18:58 +08:00

211 lines
5.5 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) {
// 后端可能刚换端口(端口占用)或刚写好握手文件:重试前重新解析一次
api.rediscover();
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;
activeModelId = null; // 切页时收起模型视图
notifyListeners();
}
/// 当前在壳内打开的模型(null 表示在看普通页面)。
///
/// 模型全部由 Flutter 自绘,不再由后端开 Avalonia 窗口:
/// 点「启动模型」只是把内容区换成模型视图,标题栏与侧栏保持可用。
String? activeModelId;
void openModel(String id) {
if (activeModelId == id) return;
activeModelId = id;
notifyListeners();
}
void closeModel() {
if (activeModelId == null) return;
activeModelId = null;
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();
}
}