Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. class CachedSharedPreferences {
  2. static SharedPreferences sharedPreferences;
  3. static CachedSharedPreferences instance;
  4. static final cachedKeyList = {
  5. Constants.firstRun,
  6. Constants.sessionUid,
  7. Constants.sessionUsername,
  8. Constants.sessionName,
  9. Constants.configDarkMode,
  10. Constants.configMessagePaging,
  11. Constants.configMessagePeek,
  12. };
  13. static Map<String, dynamic> map = Map();
  14.  
  15. static Future<CachedSharedPreferences> getInstance() async {
  16. sharedPreferences = await SharedPreferences.getInstance();
  17. if(sharedPreferences.getBool(Constants.firstRun)==null || sharedPreferences.get(Constants.firstRun)){ // if first run, then set these values
  18. await sharedPreferences.setBool(Constants.configDarkMode, false);
  19. await sharedPreferences.setBool(Constants.configMessagePaging, true);
  20. await sharedPreferences.setBool(Constants.configImageCompression, true);
  21. await sharedPreferences.setBool(Constants.configMessagePeek, true);
  22. await sharedPreferences.setBool(Constants.firstRun, false);
  23. }
  24. for (String key in cachedKeyList) {
  25. map[key] = sharedPreferences.get(key);
  26. }
  27. if (instance == null) instance = CachedSharedPreferences();
  28. return instance;
  29. }
  30.  
  31. String getString(String key) {
  32. if (cachedKeyList.contains(key)) {
  33. return map[key];
  34. }
  35. return sharedPreferences.getString(key);
  36. }
  37.  
  38. bool getBool(String key) {
  39. if (cachedKeyList.contains(key)) {
  40. return map[key];
  41. }
  42. return sharedPreferences.getBool(key);
  43. }
  44. Future<bool> setString(String key, String value) async {
  45. bool result = await sharedPreferences.setString(key, value);
  46. if (result)
  47. map[key] = value;
  48. return result;
  49. }
  50. Future<bool> setBool(String key, bool value) async {
  51. bool result = await sharedPreferences.setBool(key, value);
  52. if (result)
  53. map[key] = value;
  54. return result;
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement