Skip to content

Commit 61416f1

Browse files
chore: refactor hive base (#217)
Co-authored-by: nicolantean <[email protected]> Co-authored-by: Nicolás Lantean <[email protected]>
1 parent 6e7418a commit 61416f1

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

.fvmrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"flutter": "3.22.1",
3-
"flavors": {}
2+
"flutter": "3.22.1"
43
}

lib/core/source/common/hive_base_source.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:async';
2-
import 'dart:convert';
32

43
import 'package:dartx/dartx.dart';
54
import 'package:flutter_template/core/source/common/local_storage.dart';
@@ -8,8 +7,8 @@ import 'package:mutex/mutex.dart';
87
import 'package:rxdart/rxdart.dart';
98

109
abstract class HiveBaseSource<Key, Model> implements LocalStorage<Key, Model> {
11-
final Map<String, dynamic> Function(Model) dbParser;
12-
final Model Function(Map<String, dynamic>) modelParser;
10+
final String Function(Model) dbParser;
11+
final Model Function(String) modelParser;
1312
final Mutex _mutex = Mutex();
1413
Box<String>? _box;
1514

@@ -35,7 +34,7 @@ abstract class HiveBaseSource<Key, Model> implements LocalStorage<Key, Model> {
3534
withBox((box) async {
3635
await box.put(
3736
key,
38-
jsonEncode(dbParser(response)),
37+
dbParser(response),
3938
);
4039
return response;
4140
});
@@ -45,7 +44,7 @@ abstract class HiveBaseSource<Key, Model> implements LocalStorage<Key, Model> {
4544
withBox((box) async {
4645
await box.putAll(
4746
entries.mapValues(
48-
(entry) => jsonEncode(dbParser(entry.value)),
47+
(entry) => dbParser(entry.value),
4948
),
5049
);
5150
return getElements();
@@ -63,7 +62,7 @@ abstract class HiveBaseSource<Key, Model> implements LocalStorage<Key, Model> {
6362
) =>
6463
withBox((box) async {
6564
final data = box.get(key);
66-
return data == null ? null : modelParser(jsonDecode(data));
65+
return data == null ? null : modelParser(data);
6766
});
6867

6968
@override
@@ -73,14 +72,14 @@ abstract class HiveBaseSource<Key, Model> implements LocalStorage<Key, Model> {
7372
final box = await getBox();
7473
yield await getElement(key);
7574
yield* box.watch(key: key).map(
76-
(event) => modelParser(jsonDecode(event.value)),
75+
(event) => modelParser(event.value),
7776
);
7877
}
7978

8079
@override
8180
Future<List<Model>> getElements() => withBox(
8281
(box) => Future.value(
83-
box.values.map((e) => modelParser(jsonDecode(e))).toList(),
82+
box.values.map((e) => modelParser(e)).toList(),
8483
),
8584
);
8685

lib/core/source/project_local_source.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import 'dart:convert';
2+
13
import 'package:flutter_template/core/model/project.dart';
24
import 'package:flutter_template/core/source/common/hive_base_source.dart';
35

46
class ProjectLocalSource extends HiveBaseSource<int, Project> {
57
ProjectLocalSource()
68
: super(
7-
dbParser: (project) => project.toJson(),
8-
modelParser: (project) => Project.fromJson(project),
9+
dbParser: (project) => jsonEncode(project.toJson()),
10+
modelParser: (project) => Project.fromJson(jsonDecode(project)),
911
);
1012

1113
Stream<List<Project>> getProjects() => getElementsStream();

0 commit comments

Comments
 (0)