Skip to content
This repository was archived by the owner on Jul 9, 2020. It is now read-only.

Commit 4238c7a

Browse files
authored
fix(hydrated_cubit): retain data during migration (#61)
1 parent de62630 commit 4238c7a

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

packages/hydrated_cubit/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.1.2
2+
3+
- fix: reintroduce migration code to ensure no data loss ([#67](https://github.com/felangel/hydrated_bloc/issues/67))
4+
15
# 0.1.1
26

37
- fix: support use alongside hive ([#53](https://github.com/felangel/cubit/pull/53))

packages/hydrated_cubit/lib/src/hydrated_storage.dart

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:convert';
23
import 'dart:io';
34
import 'package:flutter/foundation.dart';
45
import 'package:hive/hive.dart';
@@ -62,10 +63,32 @@ class HydratedStorage implements Storage {
6263
'hydrated_box',
6364
encryptionCipher: encryptionCipher,
6465
);
66+
67+
await _migrate(directory, box);
68+
6569
return _instance = HydratedStorage(box);
6670
});
6771
}
6872

73+
static Future _migrate(Directory directory, Box box) async {
74+
final file = File('${directory.path}/.hydrated_bloc.json');
75+
if (await file.exists()) {
76+
try {
77+
final dynamic storageJson = json.decode(await file.readAsString());
78+
final cache = (storageJson as Map).cast<String, String>();
79+
for (final key in cache.keys) {
80+
try {
81+
final string = cache[key];
82+
final dynamic object = json.decode(string);
83+
await box.put(key, object);
84+
} on dynamic catch (_) {}
85+
}
86+
} on dynamic catch (_) {} finally {
87+
await file.delete();
88+
}
89+
}
90+
}
91+
6992
/// Internal instance of [HiveImpl].
7093
/// It should only be used for testing.
7194
@visibleForTesting

packages/hydrated_cubit/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: hydrated_cubit
22
description: An extension to the cubit state management library which automatically persists and restores cubit states.
3-
version: 0.1.1
3+
version: 0.1.2
44
homepage: https://github.com/felangel/cubit
55

66
environment:

packages/hydrated_cubit/test/hydrated_storage_test.dart

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:convert';
12
import 'dart:io';
23

34
import 'package:flutter/services.dart';
@@ -33,6 +34,18 @@ void main() {
3334
await storage?.clear();
3435
});
3536

37+
group('migration', () {
38+
test('returns correct value when file exists', () async {
39+
final directory = await getTemporaryDirectory();
40+
File('${directory.path}/.hydrated_bloc.json')
41+
..writeAsStringSync(json.encode({
42+
'CounterBloc': json.encode({'value': 4})
43+
}));
44+
storage = await HydratedStorage.build();
45+
expect(storage.read('CounterBloc')['value'] as int, 4);
46+
});
47+
});
48+
3649
group('build', () {
3750
setUp(() async {
3851
await (await HydratedStorage.build()).clear();

0 commit comments

Comments
 (0)