Files
openchamber-android-native/lib/model_prefs.dart
T
blumia 511c3b006b feat: write the UI in English, select only conversation content
- source strings are English again; Chinese comes from lib/l10n/zh.dart
  through tr()/trArgs() and AppText, and a language without a table
  (Spanish, ...) reads English instead of Chinese
- server and user data (titles, messages, file names, notes, task names)
  is drawn with plain Text, so it is never looked up in the table
- SelectionArea moved from the whole message list to the messages, the
  answer text, an opened tool call's details and the turn footer; status
  lines and folded tool rows no longer start a selection
- a message the transcript does not know (its start event was lost while
  the stream was down) fetches the tail once, so a turn continued from
  another client comes in with its prompt
- bump to 0.5.3
2026-11-26 11:48:16 +08:00

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('Default');
return switch (variant.toLowerCase()) {
'default' => tr('Default'),
'none' => tr('None'),
'minimal' => tr('Minimal'),
'low' => tr('Low'),
'medium' || 'mid' => tr('Medium'),
'high' => tr('High'),
'xhigh' || 'max' => tr('Extra high'),
_ => variant,
};
}