import 'dart:convert'; import 'package:mohem_flutter_app/models/config_model.dart'; import 'package:shared_preferences/shared_preferences.dart' as SharedPrefsPlugin; /// /// Taken from AlarmGuide Project /// abstract class ISharedPreferences { Future get authState; Future setAuthState(int authState); Future get configState; Future setConfigState(int confState); Future get config; Future setConfig(ConfigModel config); Future get promotionNotificationsEnabled; Future setPromotionNotificationEnabled(bool newSetting); Future get helpAlreadyShown; Future setHelpAlreadyShown(); Future get useS3; Future setUseS3(int value); } class SharedPreferences implements ISharedPreferences { static const String _AUTH_STATE_KEY = "auth_key"; static const String _CONFIG_KEY = "config"; static const String _CONFIG_STATE_KEY = "config_key"; static const String _PROMOTION_NOTIFICATION_KEY = "promotion"; static const String _HELP_ALREADY_SHOWN = "help_shown"; static const String _USE_S3 = "s3"; @override Future get authState async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); return sharedPrefs.getInt(_AUTH_STATE_KEY); } @override Future setAuthState(int authState) async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); sharedPrefs.setInt(_AUTH_STATE_KEY, authState); } @override Future get config async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); final configAsJson = sharedPrefs.getString(_CONFIG_KEY); return ConfigModel.fromJson(jsonDecode(configAsJson!)); } @override Future setConfig(ConfigModel config) async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); sharedPrefs.setString(_CONFIG_KEY, jsonEncode(config)); setConfigState(1); } @override Future get promotionNotificationsEnabled async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); return sharedPrefs.getBool(_PROMOTION_NOTIFICATION_KEY); } @override Future setPromotionNotificationEnabled(bool newSetting) async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); sharedPrefs.setBool(_PROMOTION_NOTIFICATION_KEY, newSetting); } @override Future get helpAlreadyShown async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); return sharedPrefs.getBool(_HELP_ALREADY_SHOWN); } @override Future setHelpAlreadyShown() async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); sharedPrefs.setBool(_HELP_ALREADY_SHOWN, true); } @override Future get configState async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); return sharedPrefs.getInt(_CONFIG_STATE_KEY); } @override Future setConfigState(int confState) async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); sharedPrefs.setInt(_CONFIG_STATE_KEY, confState); } @override Future setUseS3(int value) async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); sharedPrefs.setInt(_USE_S3, value); } @override Future get useS3 async { final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance(); return sharedPrefs.getInt(_USE_S3); } }