Hive adapter + testing

This commit is contained in:
andrei
2021-12-02 10:27:02 -05:00
parent 98942b231b
commit 5e532a132a
14 changed files with 484 additions and 14 deletions

View File

@@ -1,13 +1,32 @@
import 'dart:core';
import 'dart:core' as core;
import 'package:hive/hive.dart';
part 'data_util.g.dart';
enum Page { lists, templates }
@HiveType(typeId: 1)
class List {
List(this.name, {this.id, this.description, this.isTemplate});
List(
this.name, {
this.id,
this.description,
this.isTemplate,
this.checks = const [],
});
@HiveField(0)
int? id;
@HiveField(1)
String name;
@HiveField(2)
String? description;
@HiveField(3)
bool? isTemplate;
@HiveField(4)
core.List<Check> checks;
Map<String, dynamic> toMap() {
var map = <String, dynamic>{};
if (id != null) map["id"] = id;
@@ -27,12 +46,17 @@ class List {
}
}
@HiveType(typeId: 2)
class Check {
Check(this.text, this.value, {this.id, this.listID});
@HiveField(5)
int? id;
@HiveField(6)
String text;
@HiveField(7)
bool value;
@HiveField(8)
int? listID;
Map<String, dynamic> toMap() {

96
lib/data_util.g.dart Normal file
View File

@@ -0,0 +1,96 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'data_util.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class ListAdapter extends TypeAdapter<List> {
@override
final int typeId = 1;
@override
List read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return List(
fields[1] as String,
id: fields[0] as int?,
description: fields[2] as String?,
isTemplate: fields[3] as bool?,
checks: (fields[4] as core.List).cast<Check>(),
);
}
@override
void write(BinaryWriter writer, List obj) {
writer
..writeByte(5)
..writeByte(0)
..write(obj.id)
..writeByte(1)
..write(obj.name)
..writeByte(2)
..write(obj.description)
..writeByte(3)
..write(obj.isTemplate)
..writeByte(4)
..write(obj.checks);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ListAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
class CheckAdapter extends TypeAdapter<Check> {
@override
final int typeId = 2;
@override
Check read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Check(
fields[6] as String,
fields[7] as bool,
id: fields[5] as int?,
listID: fields[8] as int?,
);
}
@override
void write(BinaryWriter writer, Check obj) {
writer
..writeByte(4)
..writeByte(5)
..write(obj.id)
..writeByte(6)
..write(obj.text)
..writeByte(7)
..write(obj.value)
..writeByte(8)
..write(obj.listID);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is CheckAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}