2020-09-04 15:30:10 +06:30
|
|
|
import 'dart:convert';
|
|
|
|
|
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/domain/entities/user.dart';
|
2020-09-04 15:30:10 +06:30
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
|
|
class SharedPref {
|
|
|
|
|
static final SharedPref instance = SharedPref._();
|
|
|
|
|
SharedPref._();
|
|
|
|
|
|
2021-09-10 14:27:38 +06:30
|
|
|
static Future<bool?> isFirstLaunch() async {
|
2020-09-12 03:34:52 +06:30
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
|
return prefs.getBool('first_launch');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Future<void> finishFirstLaunch() async {
|
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2021-09-10 14:27:38 +06:30
|
|
|
prefs.setBool('first_launch', false);
|
2020-09-12 03:34:52 +06:30
|
|
|
}
|
|
|
|
|
|
2021-09-10 14:27:38 +06:30
|
|
|
static Future<String?> getLang() async {
|
2020-09-04 15:30:10 +06:30
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 14:27:38 +06:30
|
|
|
static Future<bool?> getStaffMode() async {
|
2020-10-12 03:34:05 +06:30
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 15:30:10 +06:30
|
|
|
static Future<void> saveUser(User user) async {
|
|
|
|
|
await _save("user", user.toJson());
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 14:27:38 +06:30
|
|
|
static Future<User?> getUser() async {
|
2020-09-04 15:30:10 +06:30
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 14:27:38 +06:30
|
|
|
static Future<bool?> getSkippedRecoverEmail() async {
|
2020-09-04 15:30:10 +06:30
|
|
|
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();
|
2021-09-10 14:27:38 +06:30
|
|
|
return json.decode(prefs.getString(key) ?? "");
|
2020-09-04 15:30:10 +06:30
|
|
|
} 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);
|
|
|
|
|
}
|
2024-09-23 18:36:23 +06:30
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2020-09-04 15:30:10 +06:30
|
|
|
}
|