preference_handler.dart 4.47 KB
Newer Older
1
import 'package:logair_application/utils/enums/preference_keys.dart';
2
3
4
5
6
7
import 'package:shared_preferences/shared_preferences.dart';

/// Handler enabling the storage and use of preferences.
class PreferencesHandler {
  static final PreferencesHandler _singleton = new PreferencesHandler._internal();

8
9
  factory PreferencesHandler() => _singleton;

10
11
12
  PreferencesHandler._internal() {
    this._sharedPreferences;
  }
13
14

  /// The singleton reference to the [SharedPreferences] instance.
15
  SharedPreferences _sharedPreferencesInternal;
16

17
  /// Whether the [SharedPreferences] have been initialized with default values.
18
19
  bool _setup = false;

20
  /// This enables the retrieval of the [SharedPreferences] ecosystem and setting it up if needed first.
21
  Future<SharedPreferences> get _sharedPreferences async {
22
    if (_sharedPreferencesInternal == null && !this._setup) {
23
      
24
25
26
27
      /// If some of the [PreferenceKeys] have mismatched types set to their default values.
      if (!PreferenceKeys.assertTypesMatch())
        throw new Exception('A <defaultValue<T>, T> pair is mismatched');
      
28
      this._sharedPreferencesInternal = await SharedPreferences.getInstance();
29

30
      /// Initial Setup
31
      PreferenceKeys.list.forEach((PreferenceKeys prefs) {
32
        Function setter;
33
        switch (prefs.type) {
34
35
36
37
38
39
40
41
42
43
44
45
          case int:
            setter = this._sharedPreferencesInternal.setInt;
            break;
          case bool:
            setter = this._sharedPreferencesInternal.setBool;
            break;
          case String:
            setter = this._sharedPreferencesInternal.setString;
            break;
          default:
            throw ArgumentError('Unexpected property type');
        }
46
47
48
        if (!this._sharedPreferencesInternal.containsKey(prefs.key)) {
          setter(prefs.key, prefs.defaultValue);
          setter(prefs.defaultKey, prefs.defaultValue);
49
        }
50
      });
51
      //print('Setup preferences done\n ${List.generate(this._sharedPreferencesInternal.getKeys().length, (i) => this._sharedPreferencesInternal.getKeys().elementAt(i) + ' => ' + this._sharedPreferencesInternal.get(this._sharedPreferencesInternal.getKeys().elementAt(i)).toString()).join("\n")}');
52
      this._setup = true;
53
    }
54

55
56
    return this._sharedPreferencesInternal;
  }
57

58
  /// Retrieves an [int] from the [SharedPreferences].
59
  Future<int> getPreferencesInt(String key) async => (await this._sharedPreferences).getInt(key);
60
61

  /// Retrieves a [String] from the [SharedPreferences].
62
  Future<String> getPreferencesString(String key) async => (await this._sharedPreferences).getString(key);
63
64

  /// Retrieves a [bool] from the [SharedPreferences].
65
  Future<bool> getPreferencesBool(String key) async => (await this._sharedPreferences).getBool(key);
66

67
  /// Retrieves a [dynamic] from the [SharedPreferences].
68
  Future<dynamic> getPreferences(String key) async => (await this._sharedPreferences).get(key);
69

70
  /// Sets an [int] within the [SharedPreferences].
71
72
73
74
  Future<void> setPreferencesInt(String key, int value) async { 
    //print('Setting $key to $value');
    (await this._sharedPreferences).setInt(key, value);
  }
75
76

  /// Sets a [String] within the [SharedPreferences].
77
78
79
80
  Future<void> setPreferencesString(String key, String value) async {
    //print('Setting $key to $value');
    (await this._sharedPreferences).setString(key, value);
  }
81
82
  
  /// Sets a [bool] within the [SharedPreferences].
83
84
85
86
  Future<void> setPreferencesBool(String key, bool value) async { 
    // print('Setting $key to $value');
    (await this._sharedPreferences).setBool(key, value);
  }
87
  
88
  /// Resets a specific [SharedPreferences] value to it's default value.
89
  Future<void> resetPreference(String key) async {
90
91
92
    Function setter;
    PreferenceKeys pref = PreferenceKeys.get(key);
    switch (pref.type) {
93
94
95
96
97
98
99
100
      case int: 
        setter = setPreferencesInt;
        break;
      case bool:
        setter = setPreferencesBool;
        break;
      case String:
        setter = setPreferencesString;
101
        break;
102
    }
103

104
    await setter(pref.key, pref.defaultValue);
105
106
  }

107
  /// This is a generic function to reset all preferences to their default values.
108
  Future<void> resetAllPreferences() async {
Nicolas Richard Walter Boeckh's avatar
Tweaks    
Nicolas Richard Walter Boeckh committed
109
    PreferenceKeys.list.where((PreferenceKeys k) => k != PreferenceKeys.GEN__SHARE).forEach(
110
      (PreferenceKeys e) async => await resetPreference(e.key)
111
    );
112
113
114
115
116
117
118
119
  }

  Stream<int> get currentDesiredGPSAccuracy async* {
    while (true) {
      yield await getPreferencesInt(PreferenceKeys.GPS__ACCURACY.key);
      await Future.delayed(Duration(milliseconds: 200));
    }
  }
120
}