From 5e532a132a10b8336bd82e5436b507ac678acfe5 Mon Sep 17 00:00:00 2001 From: andrei Date: Thu, 2 Dec 2021 10:27:02 -0500 Subject: [PATCH] Hive adapter + testing --- checks/checks.hive | 0 checks/checks.lock | 0 checks/test.hive | Bin 0 -> 42 bytes checks/test.lock | 0 lib/data_util.dart | 26 +++- lib/data_util.g.dart | 96 +++++++++++++ lists/lists.hive | Bin 0 -> 42 bytes lists/lists.lock | 0 pubspec.lock | 310 +++++++++++++++++++++++++++++++++++++++++- pubspec.yaml | 5 +- test/box_test.dart | 45 ++++++ test/test.hive | Bin 0 -> 42 bytes test/test.lock | 0 test/widget_test.dart | 16 +-- 14 files changed, 484 insertions(+), 14 deletions(-) create mode 100644 checks/checks.hive create mode 100644 checks/checks.lock create mode 100644 checks/test.hive create mode 100644 checks/test.lock create mode 100644 lib/data_util.g.dart create mode 100644 lists/lists.hive create mode 100644 lists/lists.lock create mode 100644 test/box_test.dart create mode 100644 test/test.hive create mode 100644 test/test.lock diff --git a/checks/checks.hive b/checks/checks.hive new file mode 100644 index 0000000..e69de29 diff --git a/checks/checks.lock b/checks/checks.lock new file mode 100644 index 0000000..e69de29 diff --git a/checks/test.hive b/checks/test.hive new file mode 100644 index 0000000000000000000000000000000000000000..a73744f0a6084198301fa3de609e4b909d43974c GIT binary patch literal 42 ocmdO5fC5EU1_nkJP9U!&wYVfcC$qSOiGi7cg$pD checks; + Map toMap() { var map = {}; 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 toMap() { diff --git a/lib/data_util.g.dart b/lib/data_util.g.dart new file mode 100644 index 0000000..4be82fb --- /dev/null +++ b/lib/data_util.g.dart @@ -0,0 +1,96 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'data_util.dart'; + +// ************************************************************************** +// TypeAdapterGenerator +// ************************************************************************** + +class ListAdapter extends TypeAdapter { + @override + final int typeId = 1; + + @override + List read(BinaryReader reader) { + final numOfFields = reader.readByte(); + final fields = { + 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(), + ); + } + + @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 { + @override + final int typeId = 2; + + @override + Check read(BinaryReader reader) { + final numOfFields = reader.readByte(); + final fields = { + 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; +} diff --git a/lists/lists.hive b/lists/lists.hive new file mode 100644 index 0000000000000000000000000000000000000000..a73744f0a6084198301fa3de609e4b909d43974c GIT binary patch literal 42 ocmdO5fC5EU1_nkJP9U!&wYVfcC$qSOiGi7cg$pD=2.12.0 <3.0.0" + dart: ">=2.14.0 <3.0.0" flutter: ">=1.10.0" diff --git a/pubspec.yaml b/pubspec.yaml index b705fc8..0f1b417 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,6 +30,7 @@ dependencies: flutter: sdk: flutter sqflite: ^2.0.0+4 + hive: ^2.0.4 # The following adds the Cupertino Icons font to your application. @@ -39,7 +40,8 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - + hive_generator: ^1.1.1 + build_runner: # The "flutter_lints" package below contains a set of recommended lints to # encourage good coding practices. The lint set provided by the package is # activated in the `analysis_options.yaml` file located at the root of your @@ -47,6 +49,7 @@ dev_dependencies: # rules and activating additional ones. flutter_lints: ^1.0.0 + # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec diff --git a/test/box_test.dart b/test/box_test.dart new file mode 100644 index 0000000..7b4af0d --- /dev/null +++ b/test/box_test.dart @@ -0,0 +1,45 @@ +import 'dart:math'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:boxchecker/data_util.dart' as data; +import 'package:hive/hive.dart'; + +void main() { + Hive.init("lists"); + Hive.registerAdapter(data.ListAdapter()); + Hive.registerAdapter(data.CheckAdapter()); + test('opening box', () async { + var box = await Hive.openBox("lists"); + expect(box, isNotNull); + box.close(); + }); + test('adding lists to box', () async { + var box = await Hive.openBox("lists"); + await box.clear(); + expect(box.length, 0); + final data.List list = data.List("test_list"); + box.add(list); + + expect(box.length, 1); + + var l = box.get(0); + expect(l, isNotNull); + box.close(); + }); + test('adding chekcs to lists', () async { + var box = await Hive.openBox("lists"); + var l = box.get(0); + expect(l, isNotNull); + + expect(l!.checks, isEmpty); + //print(l.checks); + + l.checks.add(data.Check("test Check", false)); + expect(l.checks, isNotEmpty); + + var l2 = box.get(0); + expect(l2, isNotNull); + expect(l2!.checks, isNotEmpty); + }); +} diff --git a/test/test.hive b/test/test.hive new file mode 100644 index 0000000000000000000000000000000000000000..a73744f0a6084198301fa3de609e4b909d43974c GIT binary patch literal 42 ocmdO5fC5EU1_nkJP9U!&wYVfcC$qSOiGi7cg$pD