Skip to content

Commit

Permalink
Merge branch 'DenchikBY-dbu-public-key'
Browse files Browse the repository at this point in the history
  • Loading branch information
roughike committed Apr 12, 2021
2 parents b4894eb + cf3de50 commit 5a31af4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,5 +1,6 @@
## 2.0.0
* [#16](https://github.com/roughike/streaming_shared_preferences/pull/16): Migrate to null-safety
* [#14](https://github.com/roughike/streaming_shared_preferences/pull/14): Make preference key public

**Breaking changes:**

Expand Down
27 changes: 14 additions & 13 deletions lib/src/preference/preference.dart
Expand Up @@ -21,13 +21,13 @@ class Preference<T> extends StreamView<T> {
@visibleForTesting
Preference.$$_private(
this._preferences,
this._key,
this.key,
this.defaultValue,
this._adapter,
this._keyChanges,
) : super(
_keyChanges.stream.transform(
_EmitValueChanges(_key, defaultValue, _adapter, _preferences),
_EmitValueChanges(key, defaultValue, _adapter, _preferences),
),
);

Expand All @@ -38,14 +38,14 @@ class Preference<T> extends StreamView<T> {
/// Get the latest value from the persistent storage synchronously.
///
/// If the returned value doesn't exist (=is `null`), returns [defaultValue].
T getValue() => _adapter.getValue(_preferences, _key) ?? defaultValue;
T getValue() => _adapter.getValue(_preferences, key) ?? defaultValue;

/// Update the value and notify all listeners about the new value.
///
/// Returns `true` if the [value] was successfully set, otherwise returns
/// `false`.
Future<bool> setValue(T value) async {
if (_key == $$_getKeysKey) {
if (key == $$_getKeysKey) {
/// This would not normally happen - it's a special case just for
/// `getKeys()`.
///
Expand All @@ -58,41 +58,42 @@ class Preference<T> extends StreamView<T> {
);
}

return _updateAndNotify(_adapter.setValue(_preferences, _key, value));
return _updateAndNotify(_adapter.setValue(_preferences, key, value));
}

/// Clear, or in other words, remove, the value. Effectively sets the [_key]
/// Clear, or in other words, remove, the value. Effectively sets the [key]
/// to a null value. After removing a value, the [Preference] will emit
/// [defaultValue] once.
///
/// Returns `true` if the clear operation was successful, otherwise returns
/// `false`.
Future<bool> clear() async {
if (_key == $$_getKeysKey) {
if (key == $$_getKeysKey) {
throw UnsupportedError(
'clear() not supported for Preference with a null key.',
);
}

return _updateAndNotify(_preferences.remove(_key));
return _updateAndNotify(_preferences.remove(key));
}

/// Invokes [fn] and captures the result, notifies all listeners about an
/// update to [_key], and then returns the previously captured result.
/// update to [key], and then returns the previously captured result.
Future<bool> _updateAndNotify(Future<bool> fn) async {
final isSuccessful = await fn;
_keyChanges.add(_key);
_keyChanges.add(key);

return isSuccessful;
}

final String key;

/// The fallback value to emit when there's no stored value associated
/// with the [key].
final T defaultValue;

// Private fields to not clutter autocompletion results for this class.
final SharedPreferences _preferences;
final String _key;
final PreferenceAdapter<T> _adapter;
final StreamController<String> _keyChanges;

Expand All @@ -101,10 +102,10 @@ class Preference<T> extends StreamView<T> {
identical(this, other) ||
other is Preference &&
runtimeType == other.runtimeType &&
_key == other._key;
key == other.key;

@override
int get hashCode => _key.hashCode;
int get hashCode => key.hashCode;
}

/// A [StreamTransformer] that starts with the current persisted value and emits
Expand Down

0 comments on commit 5a31af4

Please sign in to comment.