50 lines
1.6 KiB
Dart
50 lines
1.6 KiB
Dart
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'models.dart';
|
|
|
|
class AppStorage {
|
|
static const _hostsKey = 'remote_hosts_v1';
|
|
static const _localeKey = 'locale';
|
|
static const _widthKey = 'desktop_width';
|
|
static const _heightKey = 'desktop_height';
|
|
static const _fpsKey = 'desktop_fps';
|
|
|
|
Future<List<RemoteHost>> loadHosts() async {
|
|
final preferences = await SharedPreferences.getInstance();
|
|
final encoded = preferences.getString(_hostsKey);
|
|
if (encoded == null) return const [];
|
|
try {
|
|
return RemoteHost.decodeList(encoded);
|
|
} on Object {
|
|
return const [];
|
|
}
|
|
}
|
|
|
|
Future<void> saveHosts(List<RemoteHost> hosts) async {
|
|
final preferences = await SharedPreferences.getInstance();
|
|
await preferences.setString(_hostsKey, RemoteHost.encodeList(hosts));
|
|
}
|
|
|
|
Future<AppSettings> loadSettings() async {
|
|
final preferences = await SharedPreferences.getInstance();
|
|
return AppSettings(
|
|
languageCode: preferences.getString(_localeKey),
|
|
maxWidth: preferences.getInt(_widthKey) ?? 1280,
|
|
maxHeight: preferences.getInt(_heightKey) ?? 720,
|
|
framesPerSecond: preferences.getInt(_fpsKey) ?? 15,
|
|
);
|
|
}
|
|
|
|
Future<void> saveSettings(AppSettings settings) async {
|
|
final preferences = await SharedPreferences.getInstance();
|
|
if (settings.languageCode == null) {
|
|
await preferences.remove(_localeKey);
|
|
} else {
|
|
await preferences.setString(_localeKey, settings.languageCode!);
|
|
}
|
|
await preferences.setInt(_widthKey, settings.maxWidth);
|
|
await preferences.setInt(_heightKey, settings.maxHeight);
|
|
await preferences.setInt(_fpsKey, settings.framesPerSecond);
|
|
}
|
|
}
|