1
1
import 'dart:async' ;
2
- import 'dart:convert' ;
3
- import 'dart:io' ;
4
2
5
3
import 'package:hive_ce/hive.dart' ;
6
4
// ignore: implementation_imports
7
5
import 'package:hive_ce/src/hive_impl.dart' ;
6
+ import 'package:hydrated_bloc/src/_migration/_migration_stub.dart'
7
+ if (dart.library.io) 'package:hydrated_bloc/src/_migration/_migration_io.dart' ;
8
8
import 'package:hydrated_bloc/src/hydrated_cipher.dart' ;
9
9
import 'package:meta/meta.dart' ;
10
10
import 'package:synchronized/synchronized.dart' ;
@@ -28,29 +28,40 @@ abstract class Storage {
28
28
Future <void > close ();
29
29
}
30
30
31
- /// {@template hydrated_storage}
32
- /// Implementation of [Storage] which uses [package:hive] (https://pub.dev/packages/hive)
33
- /// to persist and retrieve state changes from the local device.
31
+ /// {@template hydrated_storage_directory}
32
+ /// A platform-agnostic storage directory representation.
34
33
/// {@endtemplate}
35
- class HydratedStorage implements Storage {
36
- /// {@macro hydrated_storage}
37
- @visibleForTesting
38
- HydratedStorage (this ._box);
34
+ class HydratedStorageDirectory {
35
+ /// {@macro hydrated_storage_directory}
36
+ const HydratedStorageDirectory (this .path);
37
+
38
+ /// The path to the storage directory.
39
+ final String path;
39
40
40
41
/// Sentinel directory used to determine that web storage should be used
41
42
/// when initializing [HydratedStorage] .
42
43
///
43
44
/// ```dart
44
45
/// await HydratedStorage.build(
45
- /// storageDirectory: HydratedStorage.webStorageDirectory ,
46
+ /// storageDirectory: HydratedStorageDirectory.web ,
46
47
/// );
47
48
/// ```
48
- static final webStorageDirectory = Directory ('' );
49
+ static const web = HydratedStorageDirectory ('' );
50
+ }
51
+
52
+ /// {@template hydrated_storage}
53
+ /// Implementation of [Storage] which uses [package:hive_ce] (https://pub.dev/packages/hive_ce)
54
+ /// to persist and retrieve state changes from the local device.
55
+ /// {@endtemplate}
56
+ class HydratedStorage implements Storage {
57
+ /// {@macro hydrated_storage}
58
+ @visibleForTesting
59
+ HydratedStorage (this ._box);
49
60
50
61
/// Returns an instance of [HydratedStorage] .
51
62
/// [storageDirectory] is required.
52
63
///
53
- /// For web, use [webStorageDirectory ] as the `storageDirectory`
64
+ /// For web, use [HydratedStorageDirectory.web ] as the `storageDirectory`
54
65
///
55
66
/// ```dart
56
67
/// import 'package:flutter/foundation.dart';
@@ -63,8 +74,8 @@ class HydratedStorage implements Storage {
63
74
/// WidgetsFlutterBinding.ensureInitialized();
64
75
/// HydratedBloc.storage = await HydratedStorage.build(
65
76
/// storageDirectory: kIsWeb
66
- /// ? HydratedStorage.webStorageDirectory
67
- /// : await getTemporaryDirectory(),
77
+ /// ? HydratedStorageDirectory.web
78
+ /// : HydratedStorageDirectory(( await getTemporaryDirectory()).path ),
68
79
/// );
69
80
/// runApp(App());
70
81
/// }
@@ -81,7 +92,7 @@ class HydratedStorage implements Storage {
81
92
/// return HydratedAesCipher(byteskey);
82
93
/// ```
83
94
static Future <HydratedStorage > build ({
84
- required Directory storageDirectory,
95
+ required HydratedStorageDirectory storageDirectory,
85
96
HydratedCipher ? encryptionCipher,
86
97
}) {
87
98
return _lock.synchronized (() async {
@@ -91,7 +102,7 @@ class HydratedStorage implements Storage {
91
102
hive = HiveImpl ();
92
103
Box <dynamic > box;
93
104
94
- if (storageDirectory == webStorageDirectory ) {
105
+ if (storageDirectory == HydratedStorageDirectory .web ) {
95
106
box = await hive.openBox <dynamic >(
96
107
'hydrated_box' ,
97
108
encryptionCipher: encryptionCipher,
@@ -102,31 +113,13 @@ class HydratedStorage implements Storage {
102
113
'hydrated_box' ,
103
114
encryptionCipher: encryptionCipher,
104
115
);
105
- await _migrate (storageDirectory, box);
116
+ await migrate (storageDirectory.path , box);
106
117
}
107
118
108
119
return _instance = HydratedStorage (box);
109
120
});
110
121
}
111
122
112
- static Future <dynamic > _migrate (Directory directory, Box <dynamic > box) async {
113
- final file = File ('${directory .path }/.hydrated_bloc.json' );
114
- if (file.existsSync ()) {
115
- try {
116
- final dynamic storageJson = json.decode (await file.readAsString ());
117
- final cache = (storageJson as Map ).cast <String , String >();
118
- for (final key in cache.keys) {
119
- try {
120
- final string = cache[key];
121
- final dynamic object = json.decode (string ?? '' );
122
- await box.put (key, object);
123
- } catch (_) {}
124
- }
125
- } catch (_) {}
126
- await file.delete ();
127
- }
128
- }
129
-
130
123
/// Internal instance of [HiveImpl] .
131
124
/// It should only be used for testing.
132
125
@visibleForTesting
0 commit comments