44 */
55
66import 'dart:async' ;
7-
7+ import 'package:archive/archive.dart' ;
8+ import 'package:path/path.dart' as path;
9+ import 'package:hive/hive.dart' ;
10+ import 'package:webf/foundation.dart' ;
811import 'package:webf/src/module/module_manager.dart' ;
9- import 'package:shared_preferences/shared_preferences.dart' ;
1012
1113class AsyncStorageModule extends BaseModule {
1214 @override
1315 String get name => 'AsyncStorage' ;
1416
15- static Future <SharedPreferences >? _prefs;
17+ static String getBoxKey (ModuleManager moduleManager) {
18+ String origin = moduleManager.controller.origin + '_async' ;
19+ int fileCheckSum = getCrc32 (origin.codeUnits);
20+ return '_webf_$fileCheckSum ' ;
21+ }
1622
1723 AsyncStorageModule (ModuleManager ? moduleManager) : super (moduleManager);
1824
19- /// Loads and parses the [SharedPreferences] for this app from disk.
20- ///
21- /// Because this is reading from disk, it shouldn't be awaited in
22- /// performance-sensitive blocks.
23- static Future <SharedPreferences > _getPrefs () {
24- _prefs ?? = SharedPreferences .getInstance ();
25- return _prefs! ;
25+ late LazyBox <String > _lazyBox;
26+
27+ @override
28+ Future <void > initialize () async {
29+ final key = getBoxKey (moduleManager! );
30+ final tmpPath = await getWebFTemporaryPath ();
31+ final storagePath = path.join (tmpPath, 'AsyncStorage' );
32+ try {
33+ _lazyBox = await Hive .openLazyBox (key, path: storagePath);
34+ } catch (e) {
35+ // Try again to avoid resources are temporarily unavailable.
36+ _lazyBox = await Hive .openLazyBox (key, path: storagePath);
37+ }
2638 }
2739
28- static Future <bool > setItem (String key, String value) async {
29- final SharedPreferences prefs = await _getPrefs ();
30- return prefs.setString (key, value);
40+ Future <bool > setItem (String key, String value) async {
41+ try {
42+ await _lazyBox.put (key, value);
43+ return true ;
44+ } catch (e, stack) {
45+ return false ;
46+ }
3147 }
3248
33- static Future <String ?> getItem (String key) async {
34- final SharedPreferences prefs = await _getPrefs ();
35- return prefs.getString (key);
49+ Future <String ?> getItem (String key) async {
50+ return _lazyBox.get (key);
3651 }
3752
38- static Future <bool > removeItem (String key) async {
39- final SharedPreferences prefs = await _getPrefs ();
40- return prefs.remove (key);
53+ Future <bool > removeItem (String key) async {
54+ try {
55+ _lazyBox.delete (key);
56+ return true ;
57+ } catch (e, stack) {
58+ return false ;
59+ }
4160 }
4261
43- static Future <Set <String >> getAllKeys () async {
44- final SharedPreferences prefs = await _getPrefs ();
45- return prefs. getKeys () ;
62+ Future <Set <dynamic >> getAllKeys () async {
63+ Set < dynamic > keys = _lazyBox.keys. toSet ();
64+ return keys ;
4665 }
4766
48- static Future <bool > clear () async {
49- final SharedPreferences prefs = await _getPrefs ();
50- return prefs. clear () ;
67+ Future <bool > clear () async {
68+ await _lazyBox. clear ();
69+ return true ;
5170 }
5271
53- static Future <int > length () async {
54- final SharedPreferences prefs = await _getPrefs ();
55- final Set <String > keys = prefs.getKeys ();
56- return keys.length;
72+ Future <int > length () async {
73+ return _lazyBox.length;
5774 }
5875
5976 @override
@@ -63,7 +80,7 @@ class AsyncStorageModule extends BaseModule {
6380 String invoke (String method, params, InvokeModuleCallback callback) {
6481 switch (method) {
6582 case 'getItem' :
66- AsyncStorageModule . getItem (params).then ((String ? value) {
83+ getItem (params).then ((String ? value) {
6784 callback (data: value ?? '' );
6885 }).catchError ((e, stack) {
6986 callback (error: '$e \n $stack ' );
@@ -72,36 +89,36 @@ class AsyncStorageModule extends BaseModule {
7289 case 'setItem' :
7390 String key = params[0 ];
7491 String value = params[1 ];
75- AsyncStorageModule . setItem (key, value).then ((bool isSuccess) {
92+ setItem (key, value).then ((bool isSuccess) {
7693 callback (data: isSuccess.toString ());
7794 }).catchError ((e, stack) {
7895 callback (error: 'Error: $e \n $stack ' );
7996 });
8097 break ;
8198 case 'removeItem' :
82- AsyncStorageModule . removeItem (params).then ((bool isSuccess) {
99+ removeItem (params).then ((bool isSuccess) {
83100 callback (data: isSuccess.toString ());
84101 }).catchError ((e, stack) {
85102 callback (error: 'Error: $e \n $stack ' );
86103 });
87104 break ;
88105 case 'getAllKeys' :
89- AsyncStorageModule . getAllKeys ().then ((Set <String > set ) {
106+ getAllKeys ().then ((Set <dynamic > set ) {
90107 List <String > list = List .from (set );
91108 callback (data: list);
92109 }).catchError ((e, stack) {
93110 callback (error: 'Error: $e \n $stack ' );
94111 });
95112 break ;
96113 case 'clear' :
97- AsyncStorageModule . clear ().then ((bool isSuccess) {
114+ clear ().then ((bool isSuccess) {
98115 callback (data: isSuccess.toString ());
99116 }).catchError ((e, stack) {
100117 callback (error: 'Error: $e \n $stack ' );
101118 });
102119 break ;
103120 case 'length' :
104- AsyncStorageModule . length ().then ((int length) {
121+ length ().then ((int length) {
105122 callback (data: length);
106123 }).catchError ((e, stack) {
107124 callback (error: 'Error: $e \n $stack ' );
0 commit comments