import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../configs/app_store.dart'; late SharedPreferences sharedPreferences; Future initializePrefs() async { sharedPreferences = await SharedPreferences.getInstance(); final String themeModeString = getPrefString( themeModePref, defaultValue: appStore.themeMode.toString(), ); await appStore.setThemeMode( ThemeMode.values.firstWhere( (ThemeMode element) => element.toString() == themeModeString), ); await appStore.setColorSchemeIndex( getPrefInt(colorSchemeIndexPref, defaultValue: appStore.colorSchemeIndex), ); await appStore.toggleSoundMode( value: getPrefBool(isSoundOnPref, defaultValue: appStore.isSoundOn), ); await appStore.toggleVibrationMode( value: getPrefBool(isVibrationOnPref, defaultValue: appStore.isVibrationOn), ); await appStore.setLanguage( getPrefString(languagePref, defaultValue: appStore.selectedLanguage), ); } /// Add a value in SharedPref based on their type - Must be a String, int, bool, double, Map or StringList Future setPrefValue(String key, dynamic value, {bool print = true}) async { if (value is String) { return sharedPreferences.setString(key, value); } else if (value is int) { return sharedPreferences.setInt(key, value); } else if (value is bool) { return sharedPreferences.setBool(key, value); } else if (value is double) { return sharedPreferences.setDouble(key, value); } else if (value is Map) { return sharedPreferences.setString(key, jsonEncode(value)); } else if (value is List) { return sharedPreferences.setStringList(key, value); } else { throw ArgumentError( 'Invalid value ${value.runtimeType} - Must be a String, int, bool, double, Map or StringList', ); } } /// Returns List of Keys that matches with given Key List getMatchingSharedPrefKeys(String key) { final List keys = []; sharedPreferences.getKeys().forEach((String element) { if (element.contains(key)) { keys.add(element); } }); return keys; } /// Returns a StringList if exists in SharedPref List? getPrefStringList(String key) => sharedPreferences.getStringList(key); /// Returns a Bool if exists in SharedPref bool getPrefBool(String key, {bool defaultValue = false}) => sharedPreferences.getBool(key) ?? defaultValue; /// Returns a Double if exists in SharedPref double getPrefDouble(String key, {double defaultValue = 0.0}) => sharedPreferences.getDouble(key) ?? defaultValue; /// Returns a Int if exists in SharedPref int getPrefInt(String key, {int defaultValue = 0}) => sharedPreferences.getInt(key) ?? defaultValue; /// Returns a String if exists in SharedPref String getPrefString(String key, {String defaultValue = ''}) => sharedPreferences.getString(key) ?? defaultValue; /// Returns a JSON if exists in SharedPref Map getPrefJSON(String key, {Map? defaultValue}) { if (sharedPreferences.containsKey(key)) { return jsonDecode(sharedPreferences.getString(key) ?? ''); } else { return defaultValue ?? {}; } } /// remove key from SharedPref Future removePrefKey(String key) async => sharedPreferences.remove(key); /// clear SharedPref Future clearSharedPref() async => sharedPreferences.clear();