2024-03-18 13:23:58 +08:00
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:sk_base_mobile/store/auth.store.dart';
|
|
|
|
|
|
|
|
class StorageService extends GetxService {
|
|
|
|
static StorageService get to => Get.find();
|
|
|
|
late final SharedPreferences _prefs;
|
|
|
|
|
|
|
|
Future<StorageService> init() async {
|
|
|
|
_prefs = await SharedPreferences.getInstance();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> setString(String key, String value,
|
|
|
|
{bool isWithUser = true}) async {
|
|
|
|
String storeKey = key;
|
|
|
|
if (isWithUser) {
|
2024-03-19 08:59:08 +08:00
|
|
|
storeKey = '${AuthStore.to.userInfo.value.id}_$key';
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
return await _prefs.setString(storeKey, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> setBool(String key, bool value, {bool isWithUser = true}) async {
|
|
|
|
String storeKey = key;
|
|
|
|
if (isWithUser) {
|
2024-03-19 08:59:08 +08:00
|
|
|
storeKey = '${AuthStore.to.userInfo.value.id}_$key';
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
return await _prefs.setBool(storeKey, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> setInt(String key, int value, {bool isWithUser = true}) async {
|
|
|
|
String storeKey = key;
|
|
|
|
if (isWithUser) {
|
2024-03-19 08:59:08 +08:00
|
|
|
storeKey = '${AuthStore.to.userInfo.value.id}_$key';
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
return await _prefs.setInt(storeKey, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> setList(String key, List<String> value,
|
|
|
|
{bool isWithUser = true}) async {
|
|
|
|
String storeKey = key;
|
|
|
|
if (isWithUser) {
|
2024-03-19 08:59:08 +08:00
|
|
|
storeKey = '${AuthStore.to.userInfo.value.id}_$key';
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
return await _prefs.setStringList(storeKey, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
String? getString(String key, {bool isWithUser = true}) {
|
|
|
|
String storeKey = key;
|
|
|
|
if (isWithUser) {
|
2024-03-19 08:59:08 +08:00
|
|
|
storeKey = '${AuthStore.to.userInfo.value.id}_$key';
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
return _prefs.getString(storeKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
int? getInt(String key, {bool isWithUser = true}) {
|
|
|
|
String storeKey = key;
|
|
|
|
if (isWithUser) {
|
2024-03-19 08:59:08 +08:00
|
|
|
storeKey = '${AuthStore.to.userInfo.value.id}_$key';
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
return _prefs.getInt(storeKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool? getBool(String key, {bool isWithUser = true}) {
|
|
|
|
String storeKey = key;
|
|
|
|
if (isWithUser) {
|
2024-03-19 08:59:08 +08:00
|
|
|
storeKey = '${AuthStore.to.userInfo.value.id}_$key';
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
return _prefs.getBool(storeKey) ?? false;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> getList(String key, {bool isWithUser = true}) {
|
|
|
|
String storeKey = key;
|
|
|
|
if (isWithUser) {
|
2024-03-19 08:59:08 +08:00
|
|
|
storeKey = '${AuthStore.to.userInfo.value.id}_$key';
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
return _prefs.getStringList(storeKey) ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> remove(String key, {bool isWithUser = true}) async {
|
|
|
|
String storeKey = key;
|
|
|
|
if (isWithUser) {
|
2024-03-19 08:59:08 +08:00
|
|
|
storeKey = '${AuthStore.to.userInfo.value.id}_$key';
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
return await _prefs.remove(storeKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Warning. if you use this method, all users data will be deleted
|
|
|
|
Future<bool> clear() async {
|
|
|
|
return await _prefs.clear();
|
|
|
|
}
|
|
|
|
}
|