Files
fcs/lib/helpers/shared_pref.dart
2024-10-04 13:55:59 +06:30

122 lines
3.4 KiB
Dart

import 'dart:convert';
import 'package:fcs/domain/entities/user.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SharedPref {
static final SharedPref instance = SharedPref._();
SharedPref._();
static Future<bool?> isFirstLaunch() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool('first_launch');
}
static Future<void> finishFirstLaunch() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('first_launch', false);
}
static Future<String?> getLang() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString('language');
}
static Future<void> saveLang(String lang) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('language', lang);
}
static Future<bool?> getStaffMode() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool('staff_mode_on');
}
static Future<void> saveStaffMode(bool staffMode) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('staff_mode_on', staffMode);
}
static Future<void> saveUser(User user) async {
await _save("user", user.toJson());
}
static Future<User?> getUser() async {
try {
return User.fromJson(await _read("user"));
} catch (e) {
return null;
}
}
static Future<User> removeUser() async {
return await _remove("user");
}
static Future<void> saveSkippedRecoverEmail(bool skipped) async {
await _save("skipped_recovery_email", skipped);
}
static Future<bool?> getSkippedRecoverEmail() async {
try {
bool _skipped = await _read("skipped_recovery_email");
return _skipped;
} catch (e) {
return null;
}
}
static _read(String key) async {
try {
final prefs = await SharedPreferences.getInstance();
return json.decode(prefs.getString(key) ?? "");
} catch (e) {
print("Error:$e");
}
}
static _save(String key, value) async {
try {
final prefs = await SharedPreferences.getInstance();
prefs.setString(key, json.encode(value));
} catch (e) {
print("Error:$e");
}
}
static _remove(String key) async {
final prefs = await SharedPreferences.getInstance();
prefs.remove(key);
}
static Future<void> saveRecentSearch(String key, String query) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Use `Set` to avoid duplication of recentSearches
Set<String> allSearches = prefs.getStringList(key)?.toSet() ?? {};
//Place it at first in the set
allSearches = {query, ...allSearches};
prefs.setStringList(key, allSearches.toList());
}
static Future<List<String>?> getRecentSearch(String key, String query) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final allSearches = prefs.getStringList(key);
return allSearches!.where((search) => search.startsWith(query)).toList();
}
static Future<void> clearRecentSearch(String key) async {
return await _remove(key);
}
static Future<bool?> getPinLockOn() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool('pin_lock');
}
static Future<void> setPinLockOn(bool isLockOn) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('pin_lock', isLockOn);
}
}