mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2025-02-02 16:14:21 +01:00
fix: Use shared preferences for storing custom data
This commit is contained in:
parent
40e01fa9f8
commit
c94db72041
@ -14,7 +14,6 @@ import 'package:vrouter/vrouter.dart';
|
|||||||
|
|
||||||
import 'package:fluffychat/config/routes.dart';
|
import 'package:fluffychat/config/routes.dart';
|
||||||
import 'package:fluffychat/utils/client_manager.dart';
|
import 'package:fluffychat/utils/client_manager.dart';
|
||||||
import 'package:fluffychat/utils/famedlysdk_store.dart';
|
|
||||||
import 'package:fluffychat/utils/platform_infos.dart';
|
import 'package:fluffychat/utils/platform_infos.dart';
|
||||||
import 'package:fluffychat/utils/sentry_controller.dart';
|
import 'package:fluffychat/utils/sentry_controller.dart';
|
||||||
import 'config/app_config.dart';
|
import 'config/app_config.dart';
|
||||||
@ -50,8 +49,6 @@ void main() async {
|
|||||||
.addAll(Uri.parse(html.window.location.href).queryParameters);
|
.addAll(Uri.parse(html.window.location.href).queryParameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Store.init();
|
|
||||||
|
|
||||||
runZonedGuarded(
|
runZonedGuarded(
|
||||||
() => runApp(PlatformInfos.isMobile
|
() => runApp(PlatformInfos.isMobile
|
||||||
? AppLock(
|
? AppLock(
|
||||||
|
@ -1,122 +1,43 @@
|
|||||||
import 'dart:async';
|
|
||||||
import 'dart:core';
|
import 'dart:core';
|
||||||
|
|
||||||
import 'package:device_info_plus/device_info_plus.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
||||||
import 'package:localstorage/localstorage.dart';
|
|
||||||
import 'package:path_provider/path_provider.dart';
|
|
||||||
|
|
||||||
import 'package:fluffychat/utils/platform_infos.dart';
|
|
||||||
|
|
||||||
// import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
||||||
|
|
||||||
// see https://github.com/mogol/flutter_secure_storage/issues/161#issuecomment-704578453
|
|
||||||
class AsyncMutex {
|
|
||||||
Completer<void>? _completer;
|
|
||||||
|
|
||||||
Future<void> lock() async {
|
|
||||||
while (_completer != null) {
|
|
||||||
await _completer!.future;
|
|
||||||
}
|
|
||||||
|
|
||||||
_completer = Completer<void>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void unlock() {
|
|
||||||
assert(_completer != null);
|
|
||||||
final completer = _completer!;
|
|
||||||
_completer = null;
|
|
||||||
completer.complete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Store {
|
class Store {
|
||||||
static FlutterSecureStorage? secureStorage;
|
SharedPreferences? _prefs;
|
||||||
|
|
||||||
static FutureOr<void> init() {
|
|
||||||
if (PlatformInfos.isMobile) {
|
|
||||||
if (PlatformInfos.isAndroid) {
|
|
||||||
return DeviceInfoPlugin().androidInfo.then((info) {
|
|
||||||
if ((info.version.sdkInt ?? 16) >= 19) {
|
|
||||||
secureStorage = const FlutterSecureStorage();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
secureStorage = const FlutterSecureStorage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LocalStorage? storage;
|
|
||||||
static final _mutex = AsyncMutex();
|
|
||||||
|
|
||||||
Future<void> _setupLocalStorage() async {
|
Future<void> _setupLocalStorage() async {
|
||||||
if (storage == null) {
|
_prefs ??= await SharedPreferences.getInstance();
|
||||||
final directory = PlatformInfos.isBetaDesktop
|
|
||||||
? await getApplicationSupportDirectory()
|
|
||||||
: (PlatformInfos.isWeb
|
|
||||||
? null
|
|
||||||
: await getApplicationDocumentsDirectory());
|
|
||||||
storage = LocalStorage('LocalStorage', directory?.path);
|
|
||||||
await storage!.ready;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String?> getItem(String key) async {
|
Future<String?> getItem(String key) async {
|
||||||
if (!PlatformInfos.isMobile) {
|
|
||||||
await _setupLocalStorage();
|
await _setupLocalStorage();
|
||||||
try {
|
return _prefs!.getString(key);
|
||||||
return storage!.getItem(key)?.toString();
|
|
||||||
} catch (_) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
await _mutex.lock();
|
|
||||||
return await secureStorage!.read(key: key);
|
|
||||||
} catch (_) {
|
|
||||||
return null;
|
|
||||||
} finally {
|
|
||||||
_mutex.unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> getItemBool(String key, [bool? defaultValue]) async {
|
Future<bool> getItemBool(String key, [bool? defaultValue]) async {
|
||||||
final value = await getItem(key);
|
await _setupLocalStorage();
|
||||||
if (value == null) {
|
return _prefs!.getBool(key) ?? defaultValue ?? true;
|
||||||
return defaultValue ?? false;
|
|
||||||
}
|
|
||||||
// we also check for '1' for legacy reasons, some booleans were stored that way
|
|
||||||
return value == '1' || value.toLowerCase() == 'true';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> setItem(String key, String? value) async {
|
Future<void> setItem(String key, String? value) async {
|
||||||
if (!PlatformInfos.isMobile) {
|
|
||||||
await _setupLocalStorage();
|
await _setupLocalStorage();
|
||||||
return await storage!.setItem(key, value);
|
if (value == null) {
|
||||||
}
|
await _prefs!.remove(key);
|
||||||
try {
|
return;
|
||||||
await _mutex.lock();
|
|
||||||
return await secureStorage!.write(key: key, value: value);
|
|
||||||
} finally {
|
|
||||||
_mutex.unlock();
|
|
||||||
}
|
}
|
||||||
|
await _prefs!.setString(key, value);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> setItemBool(String key, bool value) async {
|
Future<void> setItemBool(String key, bool value) async {
|
||||||
await setItem(key, value.toString());
|
await _setupLocalStorage();
|
||||||
|
await _prefs!.setBool(key, value);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> deleteItem(String key) async {
|
Future<void> deleteItem(String key) async {
|
||||||
if (!PlatformInfos.isMobile) {
|
|
||||||
await _setupLocalStorage();
|
await _setupLocalStorage();
|
||||||
return await storage!.deleteItem(key);
|
await _prefs!.remove(key);
|
||||||
}
|
return;
|
||||||
try {
|
|
||||||
await _mutex.lock();
|
|
||||||
return await secureStorage!.delete(key: key);
|
|
||||||
} finally {
|
|
||||||
_mutex.unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -903,13 +903,6 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.1"
|
version: "1.0.1"
|
||||||
localstorage:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: localstorage
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "4.0.0+1"
|
|
||||||
logging:
|
logging:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -54,7 +54,6 @@ dependencies:
|
|||||||
image: ^3.1.1
|
image: ^3.1.1
|
||||||
image_picker: ^0.8.4+8
|
image_picker: ^0.8.4+8
|
||||||
intl: any
|
intl: any
|
||||||
localstorage: ^4.0.0+1
|
|
||||||
lottie: ^1.2.2
|
lottie: ^1.2.2
|
||||||
matrix: ^0.8.9
|
matrix: ^0.8.9
|
||||||
matrix_link_text: ^1.0.2
|
matrix_link_text: ^1.0.2
|
||||||
|
Loading…
Reference in New Issue
Block a user