Initial import of the 1.1.1 source tree. Corresponds to Origin commit f9d97e368818acfe7dd44fa9fe5088b9629d0dff. Co-authored-by: yanjiu0721 <yanjiu0721@users.noreply.github.com>
60 lines
1.8 KiB
Dart
60 lines
1.8 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../models/session_config.dart';
|
|
import 'token_vault.dart';
|
|
|
|
class AppStore {
|
|
static const _key = 'netquality.settings.v2';
|
|
static const _legacy = 'netquality.settings.v1';
|
|
|
|
Future<({SessionConfig config, String legacyEcho})> load() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final raw = prefs.getString(_key);
|
|
if (raw != null && raw.isNotEmpty) {
|
|
try {
|
|
final decoded = jsonDecode(raw);
|
|
if (decoded is Map) {
|
|
return (
|
|
config: SessionConfig.fromJson(Map<String, dynamic>.from(decoded)),
|
|
legacyEcho: '',
|
|
);
|
|
}
|
|
} on FormatException {
|
|
return (config: SessionConfig.defaults, legacyEcho: '');
|
|
}
|
|
}
|
|
final legacy = prefs.getString(_legacy);
|
|
if (legacy != null && legacy.isNotEmpty) {
|
|
try {
|
|
final migrated = migrateV1(legacy);
|
|
await save(migrated.config);
|
|
await prefs.remove(_legacy);
|
|
return (config: migrated.config, legacyEcho: migrated.echoToken);
|
|
} on FormatException {
|
|
return (config: SessionConfig.defaults, legacyEcho: '');
|
|
}
|
|
}
|
|
return (config: SessionConfig.defaults, legacyEcho: '');
|
|
}
|
|
|
|
Future<void> save(SessionConfig config) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(_key, jsonEncode(config.toJson()));
|
|
}
|
|
}
|
|
|
|
Future<TokenVault> openTokenVault() async {
|
|
try {
|
|
final dir = await getApplicationSupportDirectory();
|
|
final vault = FileTokenVault(File('${dir.path}/node-tokens.json'));
|
|
await vault.load();
|
|
return vault;
|
|
} on Object {
|
|
return MemoryTokenVault();
|
|
}
|
|
}
|