Skip to content

Commit

Permalink
data: entity: Add json factory
Browse files Browse the repository at this point in the history
Will be used to facilitate backup-restore functionality
Issue: #41
  • Loading branch information
nullxception committed Dec 10, 2022
1 parent 525ace4 commit 08ab6d8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 2 additions & 0 deletions lib/data/repository/booru/entity/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ class Post with _$Post {
@HiveField(19, defaultValue: []) @Default([]) List<String> tagsMeta,
}) = _Post;

factory Post.fromJson(Map<String, dynamic> json) => _$PostFromJson(json);

static const empty = Post();
}
22 changes: 12 additions & 10 deletions lib/data/repository/favorite_post/entity/favorite_post.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import 'package:boorusphere/data/repository/booru/entity/post.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hive/hive.dart';

part 'favorite_post.freezed.dart';
part 'favorite_post.g.dart';

@HiveType(typeId: 5, adapterName: 'FavoritePostAdapter')
class FavoritePost {
const FavoritePost({
this.post = Post.empty,
this.timestamp = 0,
});
@freezed
class FavoritePost with _$FavoritePost {
@HiveType(typeId: 5, adapterName: 'FavoritePostAdapter')
const factory FavoritePost({
@HiveField(0, defaultValue: Post.empty) @Default(Post.empty) Post post,
@HiveField(1, defaultValue: 0) @Default(0) int timestamp,
}) = _FavoritePost;
const FavoritePost._();

@HiveField(0, defaultValue: Post.empty)
final Post post;
@HiveField(1, defaultValue: 0)
final int timestamp;
factory FavoritePost.fromJson(Map<String, dynamic> json) =>
_$FavoritePostFromJson(json);

String get key => '${post.id}@${post.serverId}';
}
2 changes: 2 additions & 0 deletions lib/data/repository/search_history/entity/search_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ class SearchHistory with _$SearchHistory {
@HiveField(0, defaultValue: '') @Default('*') String query,
@HiveField(1, defaultValue: '') @Default('') String server,
}) = _SearchHistory;
factory SearchHistory.fromJson(Map<String, dynamic> json) =>
_$SearchHistoryFromJson(json);
}

0 comments on commit 08ab6d8

Please sign in to comment.