- settings → general → language picks system, Chinese or English; the choice is stored on the device and read before the first frame - our Chinese strings are translated through tr()/trArgs() and AppText, with the English in lib/l10n/en_*.dart; a missing entry falls back to Chinese and a test checks that every literal in lib has one - AppText rebuilds on a language change (it depends on the resolved locale), so const text and whole pages switch at once; the built-in date/time pickers and selection menu follow the same language - server and user data (titles, messages, prompts, paths) is never translated - the chat list sits in a SelectionArea: long-press selects message text and shows the copy/select-all menu - bump to 0.5.2
109 lines
3.3 KiB
Dart
109 lines
3.3 KiB
Dart
// Model picker preferences: favorites, recently used models, and the effort
|
|
// last chosen for each. Device-local; the server has no say in them.
|
|
import 'dart:convert';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'l10n.dart';
|
|
|
|
class ModelPrefs {
|
|
ModelPrefs._();
|
|
static final ModelPrefs instance = ModelPrefs._();
|
|
|
|
static const _favoritesKey = 'model.favorites.v1';
|
|
static const _recentsKey = 'model.recents.v1';
|
|
static const _effortsKey = 'model.efforts.v1';
|
|
static const _maxRecents = 6;
|
|
|
|
final Set<String> favorites = {};
|
|
final List<String> recents = [];
|
|
final Map<String, String> efforts = {};
|
|
bool _loaded = false;
|
|
|
|
static String keyFor(String providerId, String id) => '$providerId/$id';
|
|
|
|
Future<void> ensureLoaded() async {
|
|
if (_loaded) return;
|
|
_loaded = true;
|
|
try {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
favorites
|
|
..clear()
|
|
..addAll(prefs.getStringList(_favoritesKey) ?? const []);
|
|
recents
|
|
..clear()
|
|
..addAll(prefs.getStringList(_recentsKey) ?? const []);
|
|
final raw = prefs.getString(_effortsKey);
|
|
if (raw != null && raw.isNotEmpty) {
|
|
final decoded = jsonDecode(raw);
|
|
if (decoded is Map) {
|
|
efforts
|
|
..clear()
|
|
..addAll({
|
|
for (final entry in decoded.entries)
|
|
if (entry.value is String)
|
|
'${entry.key}': entry.value as String,
|
|
});
|
|
}
|
|
}
|
|
} catch (_) {}
|
|
}
|
|
|
|
bool isFavorite(String key) => favorites.contains(key);
|
|
|
|
Future<void> toggleFavorite(String key) async {
|
|
if (!favorites.remove(key)) favorites.add(key);
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setStringList(_favoritesKey, favorites.toList());
|
|
}
|
|
|
|
Future<void> rememberModel(String key) async {
|
|
recents.remove(key);
|
|
recents.insert(0, key);
|
|
if (recents.length > _maxRecents) recents.removeRange(_maxRecents, recents.length);
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setStringList(_recentsKey, recents);
|
|
}
|
|
|
|
String? effortFor(String key) => efforts[key];
|
|
|
|
Future<void> rememberEffort(String key, String? variant) async {
|
|
if (variant == null || variant.isEmpty) {
|
|
efforts.remove(key);
|
|
} else {
|
|
efforts[key] = variant;
|
|
}
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(_effortsKey, jsonEncode(efforts));
|
|
}
|
|
}
|
|
|
|
/// `200000` reads as `200K`, `1000000` as `1M`.
|
|
String formatTokenCount(int tokens) {
|
|
if (tokens <= 0) return '';
|
|
String trim(String value) =>
|
|
value.endsWith('.0') ? value.substring(0, value.length - 2) : value;
|
|
if (tokens >= 1000000) {
|
|
return '${trim((tokens / 1000000).toStringAsFixed(1))}M';
|
|
}
|
|
if (tokens >= 1000) {
|
|
return '${(tokens / 1000).round()}K';
|
|
}
|
|
return '$tokens';
|
|
}
|
|
|
|
/// The Chinese label for an effort variant; unknown ids keep their own name.
|
|
String effortLabel(String? variant) {
|
|
if (variant == null || variant.isEmpty) return tr('默认');
|
|
return switch (variant.toLowerCase()) {
|
|
'default' => tr('默认'),
|
|
'none' => tr('无'),
|
|
'minimal' => tr('极低'),
|
|
'low' => tr('低'),
|
|
'medium' || 'mid' => tr('中'),
|
|
'high' => tr('高'),
|
|
'xhigh' || 'max' => tr('极高'),
|
|
_ => variant,
|
|
};
|
|
}
|