让用户选择更新源,并同步在线更新历史

设置里可以在 YanJiu Git 和 GitHub 之间选择,默认是 YanJiu Git。选中的源连不上时改用另一个,结果会写明实际用的是哪一个。更新历史从所选源读取已发布版本并在本机缓存。表单里被省略号截断的提示可以点开。

Co-authored-by: yanjiu0721 <yanjiu0721@users.noreply.github.com>
This commit is contained in:
Cursor Agentandyanjiu0721 committed 2026-11-26 23:32:19 +00:00
1 parent b8cc446454
commit 2e77fcc376
14 files changed
+1771 -68

No files matched your search

+3 -1
View File
@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
import '../center/catalog.dart';
import '../center/center_client.dart';
import '../models/session_config.dart';
import 'expandable_hint.dart';
class AddedCenter {
const AddedCenter({
@@ -140,9 +141,10 @@ class _CenterAddDialogState extends State<_CenterAddDialog> {
obscureText: true,
decoration: const InputDecoration(
labelText: '密码',
hintText: '中心要求密码、手里还没有口令时填写',
hintText: '还没有口令时填写',
),
),
const ExpandableHint(text: '中心要求密码、手里还没有口令时填写。'),
if (_hint != null) ...[
const SizedBox(height: 8),
Text(_hint!, style: const TextStyle(color: Color(0xFF9A3412))),
+90
View File
@@ -0,0 +1,90 @@
// Copyright (C) 2026 言灸
// SPDX-License-Identifier: AGPL-3.0-only
import 'package:flutter/material.dart';
/// Helper or hint copy that stays one line until it actually overflows.
/// Text that fits is not a button. Text that would end in an ellipsis can be
/// tapped to show the rest, and tapped again to collapse.
class ExpandableHint extends StatefulWidget {
const ExpandableHint({super.key, required this.text});
final String text;
@override
State<ExpandableHint> createState() => _ExpandableHintState();
}
class _ExpandableHintState extends State<ExpandableHint> {
var _expanded = false;
@override
void didUpdateWidget(ExpandableHint oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.text != widget.text) {
_expanded = false;
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final style = (theme.textTheme.bodySmall ?? const TextStyle(fontSize: 12))
.copyWith(color: theme.colorScheme.onSurfaceVariant, height: 1.3);
return LayoutBuilder(
builder: (context, constraints) {
final overflows = _exceedsOneLine(
text: widget.text,
style: style,
maxWidth: constraints.maxWidth,
textScaler: MediaQuery.textScalerOf(context),
direction: Directionality.of(context),
);
final shown = Text(
widget.text,
style: style,
maxLines: overflows && !_expanded ? 1 : null,
overflow: overflows && !_expanded
? TextOverflow.ellipsis
: TextOverflow.clip,
);
final padded = Padding(
padding: const EdgeInsets.fromLTRB(12, 6, 12, 0),
child: shown,
);
if (!overflows) {
return padded;
}
return Semantics(
button: true,
hint: _expanded ? '点一下收起' : '点一下展开全文',
child: InkWell(
onTap: () => setState(() => _expanded = !_expanded),
child: padded,
),
);
},
);
}
}
bool _exceedsOneLine({
required String text,
required TextStyle style,
required double maxWidth,
required TextScaler textScaler,
required TextDirection direction,
}) {
if (!maxWidth.isFinite || maxWidth <= 0) {
return false;
}
final painter = TextPainter(
text: TextSpan(text: text, style: style),
maxLines: 1,
textDirection: direction,
textScaler: textScaler,
)..layout(maxWidth: maxWidth);
final exceeds = painter.didExceedMaxLines;
painter.dispose();
return exceeds;
}
+81 -4
View File
@@ -4,11 +4,65 @@
import 'package:flutter/material.dart';
import '../content/release_history.dart';
import '../update/apply_update.dart';
import '../update/release_feed.dart';
import '../update/update_check.dart';
import '../version.dart';
import 'quality_charts.dart';
class HistoryPage extends StatelessWidget {
const HistoryPage({super.key});
class HistoryPage extends StatefulWidget {
const HistoryPage({super.key, this.updateSource = UpdateSource.gitea});
final UpdateSource updateSource;
@override
State<HistoryPage> createState() => _HistoryPageState();
}
class _HistoryPageState extends State<HistoryPage> {
var _entries = const <ReleaseEntry>[];
var _note = '正在读取发布说明…';
var _showBundledNote = false;
@override
void initState() {
super.initState();
_load();
}
Future<void> _load() async {
final cached = await readStoredReleaseHistory();
if (!mounted) {
return;
}
if (cached != null) {
setState(() {
_entries = cached.entries;
_note = '正在向${updateSourceLabel(widget.updateSource)}核对发布说明…';
_showBundledNote = false;
});
}
try {
final result = await loadReleaseHistory(widget.updateSource);
if (!mounted) {
return;
}
setState(() {
_entries = result.entries;
_note = result.note;
_showBundledNote = result.origin == HistoryOrigin.bundled;
});
} on Object {
if (!mounted) {
return;
}
setState(() {
_entries = releaseHistory;
_note = staleHistoryNote(rateLimited: false, hasCache: false);
_showBundledNote = true;
});
}
}
@override
Widget build(BuildContext context) {
@@ -39,12 +93,25 @@ class HistoryPage extends StatelessWidget {
),
),
const SizedBox(height: 8),
if (_note.isNotEmpty)
Text(
_note,
style: const TextStyle(color: muted, height: 1.4),
),
const SizedBox(height: 8),
const Text(
releaseHistoryNote,
onlineHistoryLimitNote,
style: TextStyle(color: muted, height: 1.4),
),
if (_showBundledNote) ...[
const SizedBox(height: 8),
const Text(
releaseHistoryNote,
style: TextStyle(color: muted, height: 1.4),
),
],
const SizedBox(height: 12),
for (final entry in releaseHistory) _entry(entry),
for (final entry in _entries) _entry(entry),
],
),
),
@@ -54,6 +121,9 @@ class HistoryPage extends StatelessWidget {
}
Widget _entry(ReleaseEntry entry) {
final remote = SemVer.parse(entry.version);
final local = SemVer.parse(appVersion);
final newer = remote != null && local != null && remote.isNewerThan(local);
return Card(
margin: const EdgeInsets.only(bottom: 12),
shape: RoundedRectangleBorder(
@@ -69,6 +139,13 @@ class HistoryPage extends StatelessWidget {
entry.title,
style: const TextStyle(fontWeight: FontWeight.w700, color: ink),
),
if (newer) ...[
const SizedBox(height: 6),
Text(
'比当前安装的 $appVersion 新',
style: const TextStyle(color: Color(0xFF9A3412), fontSize: 13),
),
],
const SizedBox(height: 6),
Text(
entry.published == null
+47 -19
View File
@@ -16,7 +16,9 @@ import '../probe/remote_client.dart';
import '../storage/app_store.dart';
import '../storage/token_vault.dart';
import '../update/apply_update.dart';
import '../update/update_check.dart';
import 'address_text.dart';
import 'expandable_hint.dart';
import 'center_dialog.dart';
import 'format.dart';
import 'history_page.dart';
@@ -57,6 +59,7 @@ class _HomePageState extends State<HomePage> {
var _allow4 = true;
var _allow6 = true;
var _autoUpdate = true;
var _updateSource = UpdateSource.gitea;
var _centers = <CenterLink>[];
var _history = TargetHistory.empty;
var _centerHint = '';
@@ -79,12 +82,17 @@ class _HomePageState extends State<HomePage> {
Future<void> _load() async {
_vault = await openTokenVault();
final loaded = await _store.load();
final history = await _store.loadHistory();
final echo = await _vault.read(echoTokenKey);
if (!mounted) {
return;
}
// Read settings after the slower startup awaits, so a choice made while
// the vault was opening is not replaced by the older snapshot.
final loaded = await _store.load();
if (!mounted) {
return;
}
final config = loaded.config;
setState(() {
_remote = config.remote;
@@ -103,6 +111,7 @@ class _HomePageState extends State<HomePage> {
_allow4 = config.allowIPv4;
_allow6 = config.allowIPv6;
_autoUpdate = config.autoUpdate;
_updateSource = parseUpdateSource(config.updateSource);
_httpVersion = config.httpVersion;
_httpMethod = config.httpMethod;
_httpResolve.text = config.httpResolve;
@@ -177,6 +186,7 @@ class _HomePageState extends State<HomePage> {
allowIPv4: _allow4,
allowIPv6: _allow6,
autoUpdate: _autoUpdate,
updateSource: updateSourceId(_updateSource),
httpVersion: _httpVersion,
httpResolve: _httpResolve.text.trim(),
httpMethod: _httpMethod,
@@ -286,7 +296,7 @@ class _HomePageState extends State<HomePage> {
return;
}
final page = value == 'history'
? const HistoryPage()
? HistoryPage(updateSource: _updateSource)
: const MetricGuidePage();
Navigator.of(context)
.push(MaterialPageRoute<void>(builder: (_) => page));
@@ -398,8 +408,9 @@ class _HomePageState extends State<HomePage> {
_field(
_echo,
'目标回显口令',
'可选,只放进探测包,和节点口令不是同一个',
'可选',
secret: true,
helper: '只放进探测包,和节点口令不是同一个。',
),
],
if (profile.id == 'dns') ...[
@@ -410,7 +421,12 @@ class _HomePageState extends State<HomePage> {
const SizedBox(height: 12),
_field(_payload, '载荷(十六进制)', '例如 0102'),
const SizedBox(height: 12),
_field(_expect, '响应需以此开头(十六进制,可空)', '不填则任意响应都算成功'),
_field(
_expect,
'响应开头(十六进制)',
'可空',
helper: '不填则任意响应都算成功。',
),
],
],
),
@@ -460,7 +476,8 @@ class _HomePageState extends State<HomePage> {
TextButton(
onPressed: () => Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => const HistoryPage(),
builder: (_) =>
HistoryPage(updateSource: _updateSource),
),
),
child: const Text('更新历史'),
@@ -585,7 +602,12 @@ class _HomePageState extends State<HomePage> {
: (value) => setState(() => _httpVersion = value ?? 'auto'),
),
const SizedBox(height: 8),
_field(_httpResolve, '指定解析', '可选。Host 和 SNI 仍用上面的地址,连接改走这里的 IP 或主机名'),
_field(
_httpResolve,
'指定解析',
'可选',
helper: 'Host 和 SNI 仍用上面的地址,连接改走这里的 IP 或主机名。',
),
const SizedBox(height: 12),
DropdownButtonFormField<String>(
key: ValueKey('http-method-$_httpMethod-$_formEpoch'),
@@ -642,6 +664,7 @@ class _HomePageState extends State<HomePage> {
allowIPv4: _allow4,
allowIPv6: _allow6,
autoUpdate: _autoUpdate,
updateSource: _updateSource,
onAllowIPv4: (value) {
setState(() => _allow4 = value);
unawaited(_persist());
@@ -654,13 +677,17 @@ class _HomePageState extends State<HomePage> {
setState(() => _autoUpdate = value);
unawaited(_persist());
},
onUpdateSource: (value) {
setState(() => _updateSource = value);
unawaited(_persist());
},
),
),
);
}
Future<void> _launchUpdate() async {
final result = await checkForUpdate();
final result = await checkForUpdate(preferred: _updateSource);
final offer = result.offer;
if (offer == null || !mounted || !_autoUpdate) {
return;
@@ -1262,11 +1289,10 @@ class _NodeDialogState extends State<_NodeDialog> {
obscureText: true,
decoration: InputDecoration(
labelText: _editingId == null ? '节点口令(公开节点留空)' : '新口令(留空则保留)',
helperText: _editingId == null
? null
: '留空则保留已保存的口令,不会显示原口令。',
),
),
if (_editingId != null)
const ExpandableHint(text: '留空则保留已保存的口令,不会显示原口令。'),
if (_hint != null) ...[
const SizedBox(height: 8),
Text(_hint!, style: const TextStyle(fontSize: 13)),
@@ -1324,15 +1350,17 @@ Widget _field(
Key? fieldKey,
String? helper,
}) {
return TextField(
key: fieldKey,
controller: controller,
obscureText: secret,
decoration: InputDecoration(
labelText: label,
hintText: hint,
helperText: helper,
),
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextField(
key: fieldKey,
controller: controller,
obscureText: secret,
decoration: InputDecoration(labelText: label, hintText: hint),
),
if (helper != null && helper.isNotEmpty) ExpandableHint(text: helper),
],
);
}
+73 -8
View File
@@ -15,17 +15,21 @@ class SettingsPage extends StatefulWidget {
required this.allowIPv4,
required this.allowIPv6,
required this.autoUpdate,
required this.updateSource,
required this.onAllowIPv4,
required this.onAllowIPv6,
required this.onAutoUpdate,
required this.onUpdateSource,
});
final bool allowIPv4;
final bool allowIPv6;
final bool autoUpdate;
final UpdateSource updateSource;
final ValueChanged<bool> onAllowIPv4;
final ValueChanged<bool> onAllowIPv6;
final ValueChanged<bool> onAutoUpdate;
final ValueChanged<UpdateSource> onUpdateSource;
@override
State<SettingsPage> createState() => _SettingsPageState();
@@ -35,6 +39,7 @@ class _SettingsPageState extends State<SettingsPage> {
late bool _allow4;
late bool _allow6;
late bool _auto;
late UpdateSource _source;
var _checking = false;
String _message = '';
UpdateOffer? _offer;
@@ -45,6 +50,7 @@ class _SettingsPageState extends State<SettingsPage> {
_allow4 = widget.allowIPv4;
_allow6 = widget.allowIPv6;
_auto = widget.autoUpdate;
_source = widget.updateSource;
}
Future<void> _check() async {
@@ -53,7 +59,7 @@ class _SettingsPageState extends State<SettingsPage> {
_message = '';
_offer = null;
});
final result = await checkForUpdate();
final result = await checkForUpdate(preferred: _source);
if (!mounted) {
return;
}
@@ -94,11 +100,18 @@ class _SettingsPageState extends State<SettingsPage> {
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 760),
child: ListView(
padding: EdgeInsets.fromLTRB(wide ? 24 : 12, 8, wide ? 24 : 12, 28),
padding: EdgeInsets.fromLTRB(
wide ? 24 : 12,
8,
wide ? 24 : 12,
28,
),
children: [
SwitchListTile(
title: const Text('允许 IPv4'),
subtitle: const Text('关掉以后,解析结果里的 IPv4 不会用来发包。直接填写 IPv4 地址也会被拒绝。'),
subtitle: const Text(
'关掉以后,解析结果里的 IPv4 不会用来发包。直接填写 IPv4 地址也会被拒绝。',
),
value: _allow4,
onChanged: (value) {
setState(() => _allow4 = value);
@@ -107,7 +120,9 @@ class _SettingsPageState extends State<SettingsPage> {
),
SwitchListTile(
title: const Text('允许 IPv6'),
subtitle: const Text('关掉以后,解析结果里的 IPv6 不会用来发包。直接填写 IPv6 地址也会被拒绝。'),
subtitle: const Text(
'关掉以后,解析结果里的 IPv6 不会用来发包。直接填写 IPv6 地址也会被拒绝。',
),
value: _allow6,
onChanged: (value) {
setState(() => _allow6 = value);
@@ -118,7 +133,11 @@ class _SettingsPageState extends State<SettingsPage> {
padding: EdgeInsets.fromLTRB(16, 0, 16, 8),
child: Text(
'两个都关掉时不能开始探测。远程节点会收到同样的选择。1.1.2 的节点还不会遵守这个开关,遇到这种情况会提示升级。',
style: TextStyle(color: Color(0xFF5C656B), fontSize: 13, height: 1.4),
style: TextStyle(
color: Color(0xFF5C656B),
fontSize: 13,
height: 1.4,
),
),
),
const Divider(),
@@ -131,11 +150,53 @@ class _SettingsPageState extends State<SettingsPage> {
widget.onAutoUpdate(value);
},
),
Padding(
padding: const EdgeInsets.fromLTRB(16, 4, 16, 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text('更新源'),
const SizedBox(height: 4),
const Text(
'默认是 YanJiu Git。这边的网络经常解析不到 github.com。检查和下载都先用这里选中的源,连不上再试另一个。',
style: TextStyle(
color: Color(0xFF5C656B),
fontSize: 13,
height: 1.4,
),
),
const SizedBox(height: 8),
SegmentedButton<UpdateSource>(
key: const Key('update-source'),
segments: const [
ButtonSegment(
value: UpdateSource.gitea,
label: Text('YanJiu Git'),
),
ButtonSegment(
value: UpdateSource.github,
label: Text('GitHub'),
),
],
selected: {_source},
onSelectionChanged: (value) {
final next = value.first;
setState(() => _source = next);
widget.onUpdateSource(next);
},
),
],
),
),
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: Text(
'当前版本 $appVersion(构建 $appBuild)。1.1.2 没有自更新,从 1.1.2 升到 1.1.3 仍要手动安装。从这一版起才会自己检查。先试 GitHub,连不上再试言灸的 Git。\n\n要在没有新版本时试这个功能,可以把环境变量 NQ_UPDATE_GITHUB_API 指到一个同样格式的测试地址,NQ_UPDATE_GITEA_API 是备用。',
style: const TextStyle(color: Color(0xFF5C656B), fontSize: 13, height: 1.4),
'当前版本 $appVersion(构建 $appBuild)。1.1.2 没有自更新,从 1.1.2 升到 1.1.3 仍要手动安装。更新源默认是 YanJiu Git,也可以改成 GitHub。检查更新和下载都先用选中的源,连不上再试另一个,结果里会写明这次实际用的是哪一个。\n\n「更新历史」会读取所选源的发布列表,并在本机留一份。网络失败时用上次保存的列表,没有保存过再用安装包里的说明,并标明可能不是最新。已经发出的 1.1.2 和 1.1.3 安装包没有这份同步。\n\n要在没有新版本时试检查更新,可以把环境变量 NQ_UPDATE_GITHUB_API 指到一个同样格式的测试地址,NQ_UPDATE_GITEA_API 是另一个。',
style: const TextStyle(
color: Color(0xFF5C656B),
fontSize: 13,
height: 1.4,
),
),
),
if (Platform.isAndroid)
@@ -143,7 +204,11 @@ class _SettingsPageState extends State<SettingsPage> {
padding: EdgeInsets.fromLTRB(16, 0, 16, 8),
child: Text(
'在 Android 上安装时,系统会弹出安装确认。这一步不能跳过。',
style: TextStyle(color: Color(0xFF9A3412), fontSize: 13, height: 1.4),
style: TextStyle(
color: Color(0xFF9A3412),
fontSize: 13,
height: 1.4,
),
),
),
Padding(