Skip to content

Commit

Permalink
rx_storage: 50c711605b6aa9a185e7f3ca9b2c87f0d2b35187
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed Feb 26, 2022
1 parent 1116260 commit 78db985
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 38 deletions.
12 changes: 7 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,15 @@ packages:
path: ".."
relative: true
source: path
version: "2.3.0"
version: "3.0.0-dev.0"
rx_storage:
dependency: transitive
dependency: "direct overridden"
description:
name: rx_storage
url: "https://pub.dartlang.org"
source: hosted
path: "."
ref: "50c711605b6aa9a185e7f3ca9b2c87f0d2b35187"
resolved-ref: "50c711605b6aa9a185e7f3ca9b2c87f0d2b35187"
url: "https://github.com/Flutter-Dart-Open-Source/rx_storage.git"
source: git
version: "1.2.0"
rxdart:
dependency: transitive
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependency_overrides:
rx_storage:
git:
url: https://github.com/Flutter-Dart-Open-Source/rx_storage.git
ref: ef0da1677adb10a85a155297f2e037358abcecbb
ref: 50c711605b6aa9a185e7f3ca9b2c87f0d2b35187

dev_dependencies:
flutter_lints: ^1.0.4
Expand Down
50 changes: 33 additions & 17 deletions lib/src/impl/shared_preferences_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class SharedPreferencesAdapter implements SharedPreferencesLike {

static Future<T> _wrap<T>(T value) => SynchronousFuture<T>(value);

static Future<T> _wrapFutureOr<T>(FutureOr<T> value) =>
value is Future<T> ? value : SynchronousFuture<T>(value);

@override
Future<void> clear([void _]) =>
_prefs.clear().throwsIfNotSuccess('Cannot clear');
Expand Down Expand Up @@ -51,7 +54,7 @@ class SharedPreferencesAdapter implements SharedPreferencesLike {
if (val is List) {
val = _prefs.getStringList(key);
}
return _wrap(decoder(val));
return _wrapFutureOr(decoder(val));
}

@override
Expand All @@ -65,43 +68,56 @@ class SharedPreferencesAdapter implements SharedPreferencesLike {
Future<void> write<T extends Object>(
String key, T? value, Encoder<T?> encoder,
[void _]) {
final val = encoder(value);
final encodedOrFuture = encoder(value);
return encodedOrFuture is Future<Object?>
? encodedOrFuture.then((encoded) => _write(encoded, key, value))
: _write(encodedOrFuture, key, value);
}

Future<void> _write(Object? encoded, String key, Object? value) {
assert(encoded is! Future<dynamic>,
'The actual type of encoded value is ${encoded.runtimeType}');

if (val == null) {
if (encoded == null) {
return remove(key);
}
if (val is double) {
return _prefs.setDouble(key, val).throwsIfNotSuccess(
if (encoded is double) {
return _prefs.setDouble(key, encoded).throwsIfNotSuccess(
'Cannot set double value: key=$key, value=$value');
}
if (val is int) {
if (encoded is int) {
return _prefs
.setInt(key, val)
.setInt(key, encoded)
.throwsIfNotSuccess('Cannot set int value: key=$key, value=$value');
}
if (val is bool) {
if (encoded is bool) {
return _prefs
.setBool(key, val)
.setBool(key, encoded)
.throwsIfNotSuccess('Cannot set bool value: key=$key, value=$value');
}
if (val is String) {
return _prefs.setString(key, val).throwsIfNotSuccess(
if (encoded is String) {
return _prefs.setString(key, encoded).throwsIfNotSuccess(
'Cannot set String value: key=$key, value=$value');
}
if (val is List<String>) {
return _prefs.setStringList(key, val).throwsIfNotSuccess(
if (encoded is List<String>) {
return _prefs.setStringList(key, encoded).throwsIfNotSuccess(
'Cannot set List<String> value: key=$key, value=$value');
}

throw StateError('Value $val of type ${val.runtimeType} is not supported. '
'Encoder must return the value of a supported type, eg. double, int, bool, String or List<String>');
throw PlatformException(
code: SharedPreferencesLike.errorCode,
message:
'The encoded value of $value has the unsupported type (${encoded.runtimeType}). '
'Encoder must return a value of type FutureOr<T> '
'(where T is a supported type (double, int, bool, String or List<String>))',
);
}
}

extension _ThrowsIfNotSuccess on Future<bool> {
Future<void> throwsIfNotSuccess(String message) {
return then((success) {
if (!success) {
return then((isSuccessful) {
if (!isSuccessful) {
throw PlatformException(
code: SharedPreferencesLike.errorCode,
message: message,
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: ef0da1677adb10a85a155297f2e037358abcecbb
resolved-ref: ef0da1677adb10a85a155297f2e037358abcecbb
ref: "50c711605b6aa9a185e7f3ca9b2c87f0d2b35187"
resolved-ref: "50c711605b6aa9a185e7f3ca9b2c87f0d2b35187"
url: "https://github.com/Flutter-Dart-Open-Source/rx_storage.git"
source: git
version: "1.2.0"
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: rx_shared_preferences
description: Rxdart streaming shared preferences. Reactive stream rxdart wrapper around SharedPreferences. A stream based wrapper over shared_preferences, allowing reactive key-value storage.
version: 2.3.0
version: 3.0.0-dev.0
homepage: https://github.com/hoc081098/rx_shared_preferences.git
repository: https://github.com/hoc081098/rx_shared_preferences.git
issue_tracker: https://github.com/hoc081098/rx_shared_preferences/issues
Expand All @@ -27,4 +27,4 @@ dependency_overrides:
rx_storage:
git:
url: https://github.com/Flutter-Dart-Open-Source/rx_storage.git
ref: ef0da1677adb10a85a155297f2e037358abcecbb
ref: 50c711605b6aa9a185e7f3ca9b2c87f0d2b35187
40 changes: 34 additions & 6 deletions test/adapter_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -56,7 +55,14 @@ void main() {
expect(
await adapter.read<User>(
'User',
(s) => s == null ? null : User.fromJson(jsonDecode(s as String)),
userFromString,
),
user1,
);
expect(
await adapter.read<User>(
'User',
userFromStringFuture,
),
user1,
);
Expand All @@ -75,7 +81,12 @@ void main() {
adapter.write<User>(
'User',
user2,
(u) => jsonEncode(u),
userToString,
),
adapter.write<User>(
'User',
user2,
userToStringFuture,
),
]);
expect(
Expand Down Expand Up @@ -111,6 +122,11 @@ void main() {
'flutter.User',
kTestValues2['flutter.User'],
]),
isMethodCall('setValue', arguments: <dynamic>[
'String',
'flutter.User',
kTestValues2['flutter.User'],
]),
],
);
store.log.clear();
Expand All @@ -123,15 +139,22 @@ void main() {
expect(
await adapter.read<User>(
'User',
(s) => s == null ? null : User.fromJson(jsonDecode(s as String)),
userFromString,
),
user2,
);
expect(
await adapter.read<User>(
'User',
userFromStringFuture,
),
user2,
);
expect(store.log, equals(<MethodCall>[]));

await runZonedGuarded(
() => adapter.write('unsupported_type', 1, (v) => <String>{}),
(e, s) => expect(e, isA<StateError>()),
(e, s) => expect(e, isA<PlatformException>()),
);

store.failedMethod = const MethodCall('setValue');
Expand All @@ -145,7 +168,12 @@ void main() {
adapter.write<User>(
'User',
user2,
(u) => jsonEncode(u),
userToString,
),
adapter.write<User>(
'User',
user2,
userToStringFuture,
),
]) {
expect(f, throwsPlatformException);
Expand Down
39 changes: 34 additions & 5 deletions test/like_shared_prefs_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ void main() {
expect(
await rxPrefs.read<User>(
'User',
(s) => s == null ? null : User.fromJson(jsonDecode(s as String)),
userFromString,
),
user1,
);
expect(
await rxPrefs.read<User>(
'User',
userFromStringFuture,
),
user1,
);
Expand All @@ -73,7 +80,12 @@ void main() {
rxPrefs.write<User>(
'User',
user2,
(u) => jsonEncode(u),
userToString,
),
rxPrefs.write<User>(
'User',
user2,
userToStringFuture,
),
]);
expect(
Expand Down Expand Up @@ -109,6 +121,11 @@ void main() {
'flutter.User',
kTestValues2['flutter.User'],
]),
isMethodCall('setValue', arguments: <dynamic>[
'String',
'flutter.User',
kTestValues2['flutter.User'],
]),
],
);
store.log.clear();
Expand All @@ -121,15 +138,22 @@ void main() {
expect(
await rxPrefs.read<User>(
'User',
(s) => s == null ? null : User.fromJson(jsonDecode(s as String)),
userFromString,
),
user2,
);
expect(
await rxPrefs.read<User>(
'User',
userFromStringFuture,
),
user2,
);
expect(store.log, equals(<MethodCall>[]));

await expectLater(
rxPrefs.write('unsupported_type', 1, (v) => <String>{}),
throwsA(isA<StateError>()),
throwsA(isA<PlatformException>()),
);

store.failedMethod = const MethodCall('setValue');
Expand All @@ -143,7 +167,12 @@ void main() {
rxPrefs.write<User>(
'User',
user2,
(u) => jsonEncode(u),
userToString,
),
rxPrefs.write<User>(
'User',
user2,
userToStringFuture,
),
]) {
expect(f, throwsPlatformException);
Expand Down
13 changes: 13 additions & 0 deletions test/model/user.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

class User {
final String id;
final String name;
Expand Down Expand Up @@ -34,3 +36,14 @@ class User {

const user1 = User('1', 'Name 1', 20);
const user2 = User('2', 'Name 2', 30);

User? userFromString(Object? s) =>
s == null ? null : User.fromJson(jsonDecode(s as String));

Future<User?> userFromStringFuture(Object? s) async =>
s == null ? null : User.fromJson(jsonDecode(s as String));

String? userToString(User? u) => u == null ? null : jsonEncode(u);

Future<String?> userToStringFuture(User? u) async =>
u == null ? null : jsonEncode(u);

0 comments on commit 78db985

Please sign in to comment.