Skip to content

chore: refactor hive base #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .fvmrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"flutter": "3.22.1",
"flavors": {}
"flutter": "3.22.1"
}
14 changes: 7 additions & 7 deletions lib/core/source/common/hive_base_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import 'package:mutex/mutex.dart';
import 'package:rxdart/rxdart.dart';

abstract class HiveBaseSource<Key, Model> implements LocalStorage<Key, Model> {
final Map<String, dynamic> Function(Model) dbParser;
final Model Function(Map<String, dynamic>) modelParser;
final String Function(Model) dbParser;
final Model Function(String) modelParser;
final Mutex _mutex = Mutex();
Box<String>? _box;

Expand All @@ -35,7 +35,7 @@ abstract class HiveBaseSource<Key, Model> implements LocalStorage<Key, Model> {
withBox((box) async {
await box.put(
key,
jsonEncode(dbParser(response)),
dbParser(response),
);
return response;
});
Expand All @@ -45,7 +45,7 @@ abstract class HiveBaseSource<Key, Model> implements LocalStorage<Key, Model> {
withBox((box) async {
await box.putAll(
entries.mapValues(
(entry) => jsonEncode(dbParser(entry.value)),
(entry) => dbParser(entry.value),
),
);
return getElements();
Expand All @@ -63,7 +63,7 @@ abstract class HiveBaseSource<Key, Model> implements LocalStorage<Key, Model> {
) =>
withBox((box) async {
final data = box.get(key);
return data == null ? null : modelParser(jsonDecode(data));
return data == null ? null : modelParser(data);
});

@override
Expand All @@ -73,14 +73,14 @@ abstract class HiveBaseSource<Key, Model> implements LocalStorage<Key, Model> {
final box = await getBox();
yield await getElement(key);
yield* box.watch(key: key).map(
(event) => modelParser(jsonDecode(event.value)),
(event) => modelParser(event.value),
);
}

@override
Future<List<Model>> getElements() => withBox(
(box) => Future.value(
box.values.map((e) => modelParser(jsonDecode(e))).toList(),
box.values.map((e) => modelParser(e)).toList(),
),
);

Expand Down
6 changes: 4 additions & 2 deletions lib/core/source/project_local_source.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'dart:convert';

import 'package:flutter_template/core/model/project.dart';
import 'package:flutter_template/core/source/common/hive_base_source.dart';

class ProjectLocalSource extends HiveBaseSource<int, Project> {
ProjectLocalSource()
: super(
dbParser: (project) => project.toJson(),
modelParser: (project) => Project.fromJson(project),
dbParser: (project) => jsonEncode(project.toJson()),
modelParser: (project) => Project.fromJson(jsonDecode(project)),
);

Future<List<Project>> replaceProjects(List<Project> elements) async {
Expand Down
Loading