|
|
|
@ -1,6 +1,13 @@
|
|
|
|
|
import 'dart:convert'; |
|
|
|
|
|
|
|
|
|
import 'package:collection/collection.dart'; |
|
|
|
|
import 'package:flutter/foundation.dart'; |
|
|
|
|
import 'package:flutter/material.dart'; |
|
|
|
|
import 'package:meta/meta.dart'; |
|
|
|
|
import 'package:nextcloud/ids.dart'; |
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart'; |
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; |
|
|
|
|
import 'package:flutter_secure_storage_aurora/flutter_secure_storage_aurora.dart'; |
|
|
|
|
|
|
|
|
|
/// Storage interface used by `Option`s. |
|
|
|
|
/// |
|
|
|
@ -91,6 +98,162 @@ enum StorageKeys implements Storable {
|
|
|
|
|
final String value; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@internal |
|
|
|
|
class SharedPreferencesAurora implements SharedPreferences { |
|
|
|
|
static FlutterSecureStorage? _flutterSecureStorage; |
|
|
|
|
static Map<String, String> tmp = <String, String>{}; |
|
|
|
|
|
|
|
|
|
static Future<void> init() async { |
|
|
|
|
FlutterSecureStorageAurora.setSecret('5872747ed1ceda3638qwerty8b2b18b20'); |
|
|
|
|
_flutterSecureStorage = FlutterSecureStorage(); |
|
|
|
|
tmp.addAll(await _flutterSecureStorage!.readAll()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
bool containsKey(final String key) => tmp.containsKey(key); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
bool? getBool(final String key) { |
|
|
|
|
if (tmp.containsKey(key)) { |
|
|
|
|
return bool.parse(tmp[key]!); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
String? getString(final String key) => tmp[key]; |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
List<String>? getStringList(final String key) { |
|
|
|
|
if (tmp.containsKey(key)) { |
|
|
|
|
final list = jsonDecode(tmp[key]!)['list'] as List<dynamic>; |
|
|
|
|
final resut = List<String>.empty(growable: true); |
|
|
|
|
for (final item in list) { |
|
|
|
|
resut.add(item as String); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return resut; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future<bool> remove(final String key) async { |
|
|
|
|
if (_flutterSecureStorage == null) { |
|
|
|
|
throw StateError( |
|
|
|
|
'NeonStorage has not been initialized yet. Please make sure NeonStorage.init() has been called before and completed.', |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tmp.remove(key); |
|
|
|
|
await _flutterSecureStorage!.delete(key: key); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future<bool> setBool(final String key, final bool value) async { |
|
|
|
|
if (_flutterSecureStorage == null) { |
|
|
|
|
throw StateError( |
|
|
|
|
'NeonStorage has not been initialized yet. Please make sure NeonStorage.init() has been called before and completed.', |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tmp[key] = value.toString(); |
|
|
|
|
await _flutterSecureStorage!.write(key: key, value: value.toString()); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future<bool> setString(final String key, final String value) async { |
|
|
|
|
if (_flutterSecureStorage == null) { |
|
|
|
|
throw StateError( |
|
|
|
|
'NeonStorage has not been initialized yet. Please make sure NeonStorage.init() has been called before and completed.', |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tmp[key] = value; |
|
|
|
|
await _flutterSecureStorage!.write(key: key, value: value); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future<bool> setStringList(final String key, final List<String> value) async { |
|
|
|
|
if (_flutterSecureStorage == null) { |
|
|
|
|
throw StateError( |
|
|
|
|
'NeonStorage has not been initialized yet. Please make sure NeonStorage.init() has been called before and completed.', |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
final list = jsonEncode({'list': value}); |
|
|
|
|
tmp[key] = list; |
|
|
|
|
await _flutterSecureStorage!.write(key: key, value: list); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future<bool> clear() async { |
|
|
|
|
if (_flutterSecureStorage == null) { |
|
|
|
|
throw StateError( |
|
|
|
|
'NeonStorage has not been initialized yet. Please make sure NeonStorage.init() has been called before and completed.', |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tmp.clear(); |
|
|
|
|
await _flutterSecureStorage!.deleteAll(); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future<bool> commit() { |
|
|
|
|
// TODO: implement commit |
|
|
|
|
throw UnimplementedError(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Object? get(String key) { |
|
|
|
|
// TODO: implement get |
|
|
|
|
throw UnimplementedError(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
double? getDouble(String key) { |
|
|
|
|
// TODO: implement getDouble |
|
|
|
|
throw UnimplementedError(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
int? getInt(String key) { |
|
|
|
|
// TODO: implement getInt |
|
|
|
|
throw UnimplementedError(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Set<String> getKeys() { |
|
|
|
|
// TODO: implement getKeys |
|
|
|
|
throw UnimplementedError(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future<void> reload() { |
|
|
|
|
// TODO: implement reload |
|
|
|
|
throw UnimplementedError(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future<bool> setDouble(String key, double value) { |
|
|
|
|
// TODO: implement setDouble |
|
|
|
|
throw UnimplementedError(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Future<bool> setInt(String key, int value) { |
|
|
|
|
// TODO: implement setInt |
|
|
|
|
throw UnimplementedError(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Neon storage that manages the storage backend. |
|
|
|
|
/// |
|
|
|
|
/// [init] must be called and completed before accessing individual storages. |
|
|
|
@ -122,8 +285,13 @@ final class NeonStorage {
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (TargetPlatform.aurora == defaultTargetPlatform) { |
|
|
|
|
await SharedPreferencesAurora.init(); |
|
|
|
|
_sharedPreferences = SharedPreferencesAurora(); |
|
|
|
|
} else { |
|
|
|
|
_sharedPreferences = await SharedPreferences.getInstance(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns the database instance. |
|
|
|
|
/// |
|
|
|
|