Browse Source
此前左右两侧会把对侧已选的那项从列表里删掉(excludeLanguage),看起来像那个 语言没了。语音翻译(同传/面对面/音视频)不再传这个参数,选中对侧语言时由 controller 对调来避免源=目标。文本/图片翻译仍走排除——它们的源语言有「自动 检测」,对调会把这个非法值挪进目标语言。 - 对调实现体拆出 _swapLanguagesInner,不包 _runLanguageSwitch:撞上对侧时是 在已经排队进来的那次切换里调它,再包一层会等自己前面那条链,死锁。 - RecentLanguages(GetStorage)记最近选过的语言并置顶,源/目标各一份列表。 「自动检测」不是语言,不记。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>main
4 changed files with 105 additions and 22 deletions
@ -0,0 +1,58 @@ |
|||||
|
import 'package:get_storage/get_storage.dart'; |
||||
|
|
||||
|
/// 语言选择弹窗的「最近使用」排序。 |
||||
|
/// |
||||
|
/// 语言表本身有几十项(面对面/同传/音视频共用 LanguageManager 那份), |
||||
|
/// 每次都从头翻很慢,所以用过的往上提,最顶的是最近一次选的。 |
||||
|
/// |
||||
|
/// ⚠️ 源/目标**分开记**:同一个人的源语言常年是母语、目标语言才是变的那个, |
||||
|
/// 合成一份会让刚选的目标语言跑到源语言列表顶上,越用越乱。 |
||||
|
class RecentLanguages { |
||||
|
RecentLanguages._(); |
||||
|
|
||||
|
/// 每侧最多记几个。再多就没有「置顶」的意义了,整张表都成了最近使用。 |
||||
|
static const int maxKeep = 8; |
||||
|
|
||||
|
static const String _sourceKey = 'recent_languages_source'; |
||||
|
static const String _targetKey = 'recent_languages_target'; |
||||
|
|
||||
|
static String _keyOf(bool isSource) => isSource ? _sourceKey : _targetKey; |
||||
|
|
||||
|
static final GetStorage _storage = GetStorage(); |
||||
|
|
||||
|
/// 读最近使用列表(最新的在前)。存储里可能是任意类型,逐项转字符串兜底。 |
||||
|
static List<String> read(bool isSource) { |
||||
|
final raw = _storage.read(_keyOf(isSource)); |
||||
|
if (raw is! List) return <String>[]; |
||||
|
return raw |
||||
|
.map((e) => e?.toString() ?? '') |
||||
|
.where((e) => e.isNotEmpty) |
||||
|
.toList(); |
||||
|
} |
||||
|
|
||||
|
/// 记一次使用:已存在就提到最前,不重复占位。 |
||||
|
static void touch(String language, {required bool isSource}) { |
||||
|
if (language.isEmpty) return; |
||||
|
final list = read(isSource)..remove(language); |
||||
|
list.insert(0, language); |
||||
|
if (list.length > maxKeep) list.removeRange(maxKeep, list.length); |
||||
|
_storage.write(_keyOf(isSource), list); |
||||
|
} |
||||
|
|
||||
|
/// 按最近使用重排:用过的按使用顺序排在前面,其余保持原有顺序。 |
||||
|
/// |
||||
|
/// 只提「本次列表里确实有」的语言 —— 各模块传进来的语言表不一样 |
||||
|
/// (语音翻译走 LanguageManager、图片翻译另有一份), |
||||
|
/// 直接把记录拼上去会冒出当前模式根本不支持的语言。 |
||||
|
static List<String> sort(List<String> languages, {required bool isSource}) { |
||||
|
final recent = read(isSource); |
||||
|
if (recent.isEmpty) return languages; |
||||
|
|
||||
|
final remaining = List<String>.from(languages); |
||||
|
final ordered = <String>[]; |
||||
|
for (final name in recent) { |
||||
|
if (remaining.remove(name)) ordered.add(name); |
||||
|
} |
||||
|
return [...ordered, ...remaining]; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue