Skip to content
Merged
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"
}
15 changes: 7 additions & 8 deletions lib/core/source/common/hive_base_source.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:convert';

import 'package:dartx/dartx.dart';
import 'package:flutter_template/core/source/common/local_storage.dart';
Expand All @@ -8,8 +7,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 +34,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 +44,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 +62,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 +72,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)),
);

Stream<List<Project>> getProjects() => getElementsStream();
Expand Down
Loading