Skip to content

Commit 5593095

Browse files
authored
feat: create JsonCacheEncPrefs class (#45)
* feat: create JsonCacheEncPrefs class Closes #30 * test: append the '_test' prefix to the test file of JsonCacheEncPref.
1 parent b7b7dd4 commit 5593095

File tree

8 files changed

+172
-8
lines changed

8 files changed

+172
-8
lines changed

CHANGELOG.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- JsonCacheEncPrefs: an implementation on top of the
13+
[encrypted_shared_preferences](https://pub.dev/packages/encrypted_shared_preferences)
14+
package — [30](https://github.com/dartoos-dev/json_cache/issues/30).
15+
1016
## [0.3.2] - 2021-08-25
1117

1218
### Changed
@@ -17,7 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1723

1824
### Added
1925

20-
- JsonCacheCrossLocalStorage: implementation on top of the cross_local_storage
26+
- JsonCacheCrossLocalStorage: an implementation on top of the cross_local_storage
2127
package — [32](https://github.com/dartoos-dev/json_cache/issues/32).
2228

2329
### Changed
@@ -47,7 +53,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4753

4854
### Added
4955

50-
- JsonCacheLocalStorage: implementation on top of the localstorage package —
56+
- JsonCacheLocalStorage: an implementation on top of the localstorage package —
5157
[29](https://github.com/dartoos-dev/json_cache/issues/29).
5258

5359
### Added
@@ -58,7 +64,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5864

5965
### Added
6066

61-
- JsonCachePrefs: implementation on top of the shared_preferences package —
67+
- JsonCachePrefs: an implementation on top of the shared_preferences package —
6268
[26](https://github.com/dartoos-dev/json_cache/issues/26).
6369

6470
### Changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Rultor.com](https://www.rultor.com/b/dartoos-dev/json_cache)](https://www.rultor
2424
- [Implementations](#implementations)
2525
- [JsonCacheMem — Thread-safe In-memory cache](#jsoncachemem)
2626
- [JsonCachePrefs — SharedPreferences](#jsoncacheprefs)
27+
- [JsonCacheEncPrefs — EncryptedSharedPreferences](#jsoncacheencprefs)
2728
- [JsonCacheLocalStorage — LocalStorage](#jsoncachelocalstorage)
2829
- [JsonCacheCrossLocalStorage — CrossLocalStorage](#jsoncachecrosslocalstorage)
2930
- [Demo application](#demo-application)
@@ -180,6 +181,20 @@ is an implementation on top of the
180181
181182
```
182183

184+
### JsonCacheEncPrefs
185+
186+
[JsonCacheEncPrefs](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCacheEncPrefs-class.html)
187+
is an implementation on top of the
188+
[encrypted_shared_preferences](https://pub.dev/packages/encrypted_shared_preferences)
189+
package.
190+
191+
```dart
192+
193+
final encPrefs = EncryptedSharedPreferences();
194+
final JsonCache jsonCache = JsonCacheMem(JsonCacheEncPrefs(encPrefs));
195+
196+
```
197+
183198
### JsonCacheLocalStorage
184199

185200
[JsonCacheLocalStorage](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCacheLocalStorage-class.html)

lib/json_cache.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ library json_cache;
33

44
export 'src/json_cache.dart';
55
export 'src/json_cache_cross_local_storage.dart';
6+
export 'src/json_cache_enc_prefs.dart';
67
export 'src/json_cache_fake.dart';
78
export 'src/json_cache_hollow.dart';
89
export 'src/json_cache_local_storage.dart';

lib/src/json_cache_enc_prefs.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'dart:convert';
2+
3+
import 'package:encrypted_shared_preferences/encrypted_shared_preferences.dart';
4+
5+
import 'json_cache.dart';
6+
7+
/// Shared Preferences as encrypted values.
8+
///
9+
/// See also:
10+
/// -
11+
/// [encrypted_shared_preferences](https://pub.dev/packages/encrypted_shared_preferences)
12+
class JsonCacheEncPrefs implements JsonCache {
13+
/// [_encPrefs] an [EncryptedSharedPreferences] instance.
14+
JsonCacheEncPrefs(this._encPrefs);
15+
16+
// The encrypted preferences file object.
17+
final EncryptedSharedPreferences _encPrefs;
18+
19+
@override
20+
Future<void> clear() async {
21+
await _encPrefs.clear();
22+
}
23+
24+
@override
25+
Future<void> refresh(String key, Map<String, dynamic> value) async {
26+
await _encPrefs.setString(key, json.encode(value));
27+
}
28+
29+
@override
30+
Future<void> remove(String key) async {
31+
await (await _encPrefs.getInstance()).remove(key);
32+
}
33+
34+
@override
35+
Future<Map<String, dynamic>?> value(String key) async {
36+
final shrPrefs = await _encPrefs.getInstance();
37+
if (shrPrefs.containsKey(key)) {
38+
return json.decode(await _encPrefs.getString(key))
39+
as Map<String, dynamic>;
40+
}
41+
return null;
42+
}
43+
}

pubspec.lock

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
# Generated by pub
22
# See https://dart.dev/tools/pub/glossary#lockfile
33
packages:
4+
args:
5+
dependency: transitive
6+
description:
7+
name: args
8+
url: "https://pub.dartlang.org"
9+
source: hosted
10+
version: "2.2.0"
11+
asn1lib:
12+
dependency: transitive
13+
description:
14+
name: asn1lib
15+
url: "https://pub.dartlang.org"
16+
source: hosted
17+
version: "1.0.2"
418
async:
519
dependency: transitive
620
description:
721
name: async
822
url: "https://pub.dartlang.org"
923
source: hosted
10-
version: "2.6.1"
24+
version: "2.8.1"
1125
boolean_selector:
1226
dependency: transitive
1327
description:
@@ -28,7 +42,7 @@ packages:
2842
name: charcode
2943
url: "https://pub.dartlang.org"
3044
source: hosted
31-
version: "1.2.0"
45+
version: "1.3.1"
3246
clock:
3347
dependency: transitive
3448
description:
@@ -43,13 +57,41 @@ packages:
4357
url: "https://pub.dartlang.org"
4458
source: hosted
4559
version: "1.15.0"
60+
convert:
61+
dependency: transitive
62+
description:
63+
name: convert
64+
url: "https://pub.dartlang.org"
65+
source: hosted
66+
version: "3.0.1"
4667
cross_local_storage:
4768
dependency: "direct main"
4869
description:
4970
name: cross_local_storage
5071
url: "https://pub.dartlang.org"
5172
source: hosted
5273
version: "2.0.1"
74+
crypto:
75+
dependency: transitive
76+
description:
77+
name: crypto
78+
url: "https://pub.dartlang.org"
79+
source: hosted
80+
version: "3.0.1"
81+
encrypt:
82+
dependency: transitive
83+
description:
84+
name: encrypt
85+
url: "https://pub.dartlang.org"
86+
source: hosted
87+
version: "5.0.1"
88+
encrypted_shared_preferences:
89+
dependency: "direct main"
90+
description:
91+
name: encrypted_shared_preferences
92+
url: "https://pub.dartlang.org"
93+
source: hosted
94+
version: "2.0.0"
5395
fake_async:
5496
dependency: transitive
5597
description:
@@ -120,7 +162,7 @@ packages:
120162
name: meta
121163
url: "https://pub.dartlang.org"
122164
source: hosted
123-
version: "1.3.0"
165+
version: "1.7.0"
124166
mutex:
125167
dependency: "direct main"
126168
description:
@@ -184,6 +226,13 @@ packages:
184226
url: "https://pub.dartlang.org"
185227
source: hosted
186228
version: "2.0.1"
229+
pointycastle:
230+
dependency: transitive
231+
description:
232+
name: pointycastle
233+
url: "https://pub.dartlang.org"
234+
source: hosted
235+
version: "3.3.1"
187236
process:
188237
dependency: transitive
189238
description:
@@ -279,7 +328,7 @@ packages:
279328
name: test_api
280329
url: "https://pub.dartlang.org"
281330
source: hosted
282-
version: "0.3.0"
331+
version: "0.4.2"
283332
typed_data:
284333
dependency: transitive
285334
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ environment:
1010

1111
dependencies:
1212
cross_local_storage: ^2.0.1
13+
encrypted_shared_preferences: ^2.0.0
1314
flutter:
1415
sdk: flutter
1516
localstorage: ^4.0.0+1

test/json_cache_enc_prefs_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:encrypted_shared_preferences/encrypted_shared_preferences.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:json_cache/json_cache.dart';
4+
import 'package:shared_preferences/shared_preferences.dart';
5+
6+
void main() {
7+
SharedPreferences.setMockInitialValues({});
8+
group('JsonCachePrefs', () {
9+
test('clear, value, refresh', () async {
10+
const profKey = 'profile';
11+
const profData = <String, Object>{'id': 1, 'name': 'John Due'};
12+
final JsonCacheEncPrefs encPrefsCache =
13+
JsonCacheEncPrefs(EncryptedSharedPreferences());
14+
await encPrefsCache.refresh(profKey, profData);
15+
var prof = await encPrefsCache.value(profKey);
16+
expect(prof, profData);
17+
await encPrefsCache.clear();
18+
prof = await encPrefsCache.value(profKey);
19+
expect(prof, isNull);
20+
});
21+
22+
test('remove', () async {
23+
const profKey = 'profile';
24+
const prefKey = 'preferences';
25+
final profData = <String, Object>{'id': 1, 'name': 'John Due'};
26+
final prefData = <String, Object>{
27+
'theme': 'dark',
28+
'notifications': {'enabled': true}
29+
};
30+
final JsonCacheEncPrefs encPrefsCache =
31+
JsonCacheEncPrefs(EncryptedSharedPreferences());
32+
await encPrefsCache.refresh(profKey, profData);
33+
await encPrefsCache.refresh(prefKey, prefData);
34+
35+
var prof = await encPrefsCache.value(profKey);
36+
expect(prof, profData);
37+
38+
await encPrefsCache.remove(profKey);
39+
prof = await encPrefsCache.value(profKey);
40+
expect(prof, isNull);
41+
42+
var pref = await encPrefsCache.value(prefKey);
43+
expect(pref, prefData);
44+
await encPrefsCache.remove(prefKey);
45+
pref = await encPrefsCache.value(prefKey);
46+
expect(pref, isNull);
47+
});
48+
});
49+
}

test/json_cache_prefs_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:shared_preferences/shared_preferences.dart';
55
void main() {
66
SharedPreferences.setMockInitialValues({});
77
group('JsonCachePrefs', () {
8-
test('clear, recover and refresh', () async {
8+
test('clear, value, refresh', () async {
99
const profKey = 'profile';
1010
const profData = <String, Object>{'id': 1, 'name': 'John Due'};
1111
final JsonCachePrefs prefsCache =

0 commit comments

Comments
 (0)