Skip to content

Commit

Permalink
deon
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed Oct 12, 2023
1 parent 9ad5fcd commit 82032ed
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
4 changes: 2 additions & 2 deletions example/lib/dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension DialogExtensions on BuildContext {
}

try {
await rxPrefs.executeUpdateStringList(key, (currentList) {
await rxPrefs.updateStringList(key, (currentList) {
final list = currentList ?? const <String>[];
if (list.contains(string)) {
throw StateError('Duplicated $string!');
Expand Down Expand Up @@ -77,7 +77,7 @@ extension DialogExtensions on BuildContext {
}

try {
await rxPrefs.executeUpdateStringList(
await rxPrefs.updateStringList(
key,
(currentList) => [
for (final s in (currentList ?? const <String>[]))
Expand Down
2 changes: 1 addition & 1 deletion example/lib/snippet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ void main() async {
);

// Or read-modify-write style.
await rxPrefs.executeUpdateStringList(
await rxPrefs.updateStringList(
idsKey, (currentIds) => [...?currentIds, 'new id']);
}
70 changes: 70 additions & 0 deletions test/streams_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,24 @@ void main() {
emitsInOrder(<Object>[anything, expected]),
);
expect(await rxPrefs.getBool('bool'), initial);
// ignore: deprecated_member_use_from_same_package
await rxPrefs.executeUpdateBool('bool', (s) => !s!);
expect(await rxPrefs.getBool('bool'), expected);
});

test('updateBool', () async {
final initial = kTestValues['flutter.bool'] as bool;
final expected = !initial;

expect(
rxPrefs.getBoolStream('bool'),
emitsInOrder(<Object>[anything, expected]),
);
expect(await rxPrefs.getBool('bool'), initial);
await rxPrefs.updateBool('bool', (s) => !s!);
expect(await rxPrefs.getBool('bool'), expected);
});

test('executeUpdateDouble', () async {
final initial = kTestValues['flutter.double'] as double;
final expected = initial + 1;
Expand All @@ -411,10 +425,24 @@ void main() {
emitsInOrder(<Object>[anything, expected]),
);
expect(await rxPrefs.getDouble('double'), initial);
// ignore: deprecated_member_use_from_same_package
await rxPrefs.executeUpdateDouble('double', (s) => s! + 1);
expect(await rxPrefs.getDouble('double'), expected);
});

test('updateDouble', () async {
final initial = kTestValues['flutter.double'] as double;
final expected = initial + 1;

expect(
rxPrefs.getDoubleStream('double'),
emitsInOrder(<Object>[anything, expected]),
);
expect(await rxPrefs.getDouble('double'), initial);
await rxPrefs.updateDouble('double', (s) => s! + 1);
expect(await rxPrefs.getDouble('double'), expected);
});

test('executeUpdateInt', () async {
final initial = kTestValues['flutter.int'] as int;
final expected = initial + 1;
Expand All @@ -424,10 +452,24 @@ void main() {
emitsInOrder(<Object>[anything, expected]),
);
expect(await rxPrefs.getInt('int'), initial);
// ignore: deprecated_member_use_from_same_package
await rxPrefs.executeUpdateInt('int', (s) => s! + 1);
expect(await rxPrefs.getInt('int'), expected);
});

test('updateInt', () async {
final initial = kTestValues['flutter.int'] as int;
final expected = initial + 1;

expect(
rxPrefs.getIntStream('int'),
emitsInOrder(<Object>[anything, expected]),
);
expect(await rxPrefs.getInt('int'), initial);
await rxPrefs.updateInt('int', (s) => s! + 1);
expect(await rxPrefs.getInt('int'), expected);
});

test('executeUpdateString', () async {
final initial = kTestValues['flutter.String'] as String;
final expected = initial + '1';
Expand All @@ -437,10 +479,24 @@ void main() {
emitsInOrder(<Object>[anything, expected]),
);
expect(await rxPrefs.getString('String'), initial);
// ignore: deprecated_member_use_from_same_package
await rxPrefs.executeUpdateString('String', (s) => s! + '1');
expect(await rxPrefs.getString('String'), expected);
});

test('updateString', () async {
final initial = kTestValues['flutter.String'] as String;
final expected = initial + '1';

expect(
rxPrefs.getStringStream('String'),
emitsInOrder(<Object>[anything, expected]),
);
expect(await rxPrefs.getString('String'), initial);
await rxPrefs.updateString('String', (s) => s! + '1');
expect(await rxPrefs.getString('String'), expected);
});

test('executeUpdateStringList', () async {
final initial = kTestValues['flutter.List'] as List<String>;
final expected = [...initial, '1'];
Expand All @@ -450,8 +506,22 @@ void main() {
emitsInOrder(<Object>[anything, expected]),
);
expect(await rxPrefs.getStringList('List'), initial);
// ignore: deprecated_member_use_from_same_package
await rxPrefs.executeUpdateStringList('List', (s) => [...s!, '1']);
expect(await rxPrefs.getStringList('List'), expected);
});

test('updateStringList', () async {
final initial = kTestValues['flutter.List'] as List<String>;
final expected = [...initial, '1'];

expect(
rxPrefs.getStringListStream('List'),
emitsInOrder(<Object>[anything, expected]),
);
expect(await rxPrefs.getStringList('List'), initial);
await rxPrefs.updateStringList('List', (s) => [...s!, '1']);
expect(await rxPrefs.getStringList('List'), expected);
});
});
}

0 comments on commit 82032ed

Please sign in to comment.