Skip to content
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

Updates to package:hive_flutter #698

Merged
merged 5 commits into from Jun 18, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions hive_flutter/lib/adapters.dart
@@ -0,0 +1,7 @@
import 'package:flutter/material.dart' show Color, TimeOfDay;
import 'package:hive/hive.dart' show TypeAdapter, BinaryReader, BinaryWriter;

export 'hive_flutter.dart';

part 'src/adapters/color_adapter.dart';
part 'src/adapters/time_adapter.dart';
6 changes: 4 additions & 2 deletions hive_flutter/lib/hive_flutter.dart
Expand Up @@ -5,8 +5,10 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path_helper;
import 'package:path_provider/path_provider.dart'
if (dart.library.html) 'src/stub/path_provider.dart';
import 'package:path/path.dart' if (dart.library.html) 'src/stub/path.dart'
as path_helper;

export 'package:hive/hive.dart';

Expand Down
12 changes: 3 additions & 9 deletions hive_flutter/lib/src/adapters/color_adapter.dart
@@ -1,17 +1,11 @@
import 'package:flutter/painting.dart';
import 'package:hive/hive.dart';
part of '../../adapters.dart';

class ColorAdapter extends TypeAdapter<Color> {
@override
Color read(BinaryReader reader) {
final colorValue = reader.readInt();
return Color(colorValue);
}
Color read(BinaryReader reader) => Color(reader.readInt());

@override
void write(BinaryWriter writer, Color obj) {
writer.writeInt(obj.value);
}
void write(BinaryWriter writer, Color obj) => writer.writeInt(obj.value);

@override
int get typeId => 200;
Expand Down
11 changes: 5 additions & 6 deletions hive_flutter/lib/src/adapters/time_adapter.dart
@@ -1,16 +1,15 @@
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
part of '../../adapters.dart';

class TimeAdapter extends TypeAdapter<TimeOfDay> {
class TimeOfDayAdapter extends TypeAdapter<TimeOfDay> {
@override
TimeOfDay read(BinaryReader reader) {
final time = reader.read() as TimeOfDay;
return time;
final totalMinutes = reader.readInt();
return TimeOfDay(hour: totalMinutes ~/ 60, minute: totalMinutes % 60);
}

@override
void write(BinaryWriter writer, TimeOfDay obj) {
writer.write(obj);
writer.writeInt(obj.hour * 60 + obj.minute);
}

@override
Expand Down
10 changes: 5 additions & 5 deletions hive_flutter/lib/src/hive_extensions.dart
Expand Up @@ -5,11 +5,11 @@ extension HiveX on HiveInterface {
/// Initializes Hive with the path from [getApplicationDocumentsDirectory].
///
/// You can provide a [subDir] where the boxes should be stored.
Future initFlutter([String? subDir]) async {
Future<void> initFlutter([String? subDir]) async {
WidgetsFlutterBinding.ensureInitialized();
if (!kIsWeb) {
var appDir = await getApplicationDocumentsDirectory();
init(path_helper.join(appDir.path, subDir));
}
if (kIsWeb) return;

var appDir = await getApplicationDocumentsDirectory();
init(path_helper.join(appDir.path, subDir));
}
}
14 changes: 14 additions & 0 deletions hive_flutter/lib/src/stub/path.dart
@@ -0,0 +1,14 @@
String join(
String part1, [
String? part2,
String? part3,
String? part4,
String? part5,
String? part6,
String? part7,
String? part8,
]) {
throw UnimplementedError(
'[Hive Error] Tried to use the `path` package from Flutter Web.',
);
}
9 changes: 9 additions & 0 deletions hive_flutter/lib/src/stub/path_provider.dart
@@ -0,0 +1,9 @@
abstract class Directory {
String get path;
}

Future<Directory> getApplicationDocumentsDirectory() {
throw UnimplementedError(
'[Hive Error] Tried to use the `path_provider` package from Flutter Web.',
);
}
10 changes: 5 additions & 5 deletions hive_flutter/pubspec.yaml
Expand Up @@ -11,13 +11,13 @@ dependencies:
flutter:
sdk: flutter

hive: ^2.0.0
path_provider: ^2.0.0
hive: ^2.0.4
path_provider: ^2.0.2
path: ^1.8.0

dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.8.0
mockito: ^5.0.7
build_runner: ^1.10.0
pedantic: ^1.11.1
mockito: ^5.0.10
build_runner: ^2.0.4
5 changes: 3 additions & 2 deletions hive_flutter/test/adapters/color_adapter_test.dart
@@ -1,7 +1,8 @@
import 'package:flutter/material.dart';
import 'dart:ui' show Color;

import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/src/adapters/color_adapter.dart';
import 'package:hive_flutter/adapters.dart';
import 'package:mockito/mockito.dart';

import '../mocks.dart';
Expand Down
24 changes: 15 additions & 9 deletions hive_flutter/test/adapters/time_adapter_test.dart
@@ -1,29 +1,35 @@
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' show TimeOfDay;
import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/src/adapters/time_adapter.dart';
import 'package:hive_flutter/adapters.dart';
import 'package:mockito/mockito.dart';

import '../mocks.dart';

void main() {
group('TimeOfDayAdapter', () {
late TimeOfDay time;
late int totalMinutes;

setUp(() {
time = TimeOfDay(hour: 8, minute: 0);
totalMinutes = time.hour * 60 + time.minute;
});

test('.read()', () {
final time = TimeOfDay(hour: 8, minute: 0);
final BinaryReader binaryReader = MockBinaryReader();
when(binaryReader.read()).thenReturn(time);
when(binaryReader.readInt()).thenReturn(totalMinutes);

final readTime = TimeAdapter().read(binaryReader);
verify(binaryReader.read());
final readTime = TimeOfDayAdapter().read(binaryReader);
verify(binaryReader.readInt()).called(1);
expect(readTime, time);
});

test('.write()', () {
final time = TimeOfDay(hour: 8, minute: 0);
final BinaryWriter binaryWriter = MockBinaryWriter();

TimeAdapter().write(binaryWriter, time);
verify(binaryWriter.write(time));
TimeOfDayAdapter().write(binaryWriter, time);
verify(binaryWriter.writeInt(totalMinutes));
});
});
}
46 changes: 23 additions & 23 deletions hive_flutter/test/mocks.mocks.dart
@@ -1,26 +1,27 @@
// Mocks generated by Mockito 5.0.0-nullsafety.7 from annotations
// Mocks generated by Mockito 5.0.10 from annotations
// in hive_flutter/test/mocks.dart.
// Do not manually edit this file.

import 'dart:convert' as _i5;
import 'dart:typed_data' as _i2;
import 'dart:typed_data' as _i4;

import 'package:hive/hive.dart' as _i4;
import 'package:hive/src/object/hive_object.dart' as _i3;
import 'package:mockito/mockito.dart' as _i1;
import 'package:hive/hive.dart' as _i3;
import 'package:hive/src/object/hive_object.dart' as _i1;
import 'package:mockito/mockito.dart' as _i2;

// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: comment_references
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis

class _FakeUint8List extends _i1.Fake implements _i2.Uint8List {}

class _FakeHiveList<E extends _i3.HiveObject> extends _i1.Fake
implements _i4.HiveList<E> {}
class _FakeHiveList<E extends _i1.HiveObjectMixin> extends _i2.Fake
implements _i3.HiveList<E> {}

/// A class which mocks [BinaryReader].
///
/// See the documentation for Mockito's code generation for more information.
class MockBinaryReader extends _i1.Mock implements _i4.BinaryReader {
class MockBinaryReader extends _i2.Mock implements _i3.BinaryReader {
@override
int get availableBytes =>
(super.noSuchMethod(Invocation.getter(#availableBytes), returnValue: 0)
Expand All @@ -37,13 +38,13 @@ class MockBinaryReader extends _i1.Mock implements _i4.BinaryReader {
(super.noSuchMethod(Invocation.method(#readByte, []), returnValue: 0)
as int);
@override
_i2.Uint8List viewBytes(int? bytes) =>
_i4.Uint8List viewBytes(int? bytes) =>
(super.noSuchMethod(Invocation.method(#viewBytes, [bytes]),
returnValue: _FakeUint8List()) as _i2.Uint8List);
returnValue: _i4.Uint8List(0)) as _i4.Uint8List);
@override
_i2.Uint8List peekBytes(int? bytes) =>
_i4.Uint8List peekBytes(int? bytes) =>
(super.noSuchMethod(Invocation.method(#peekBytes, [bytes]),
returnValue: _FakeUint8List()) as _i2.Uint8List);
returnValue: _i4.Uint8List(0)) as _i4.Uint8List);
@override
int readWord() =>
(super.noSuchMethod(Invocation.method(#readWord, []), returnValue: 0)
Expand Down Expand Up @@ -76,9 +77,9 @@ class MockBinaryReader extends _i1.Mock implements _i4.BinaryReader {
(super.noSuchMethod(Invocation.method(#readString, [byteCount, decoder]),
returnValue: '') as String);
@override
_i2.Uint8List readByteList([int? length]) =>
_i4.Uint8List readByteList([int? length]) =>
(super.noSuchMethod(Invocation.method(#readByteList, [length]),
returnValue: _FakeUint8List()) as _i2.Uint8List);
returnValue: _i4.Uint8List(0)) as _i4.Uint8List);
@override
List<int> readIntList([int? length]) =>
(super.noSuchMethod(Invocation.method(#readIntList, [length]),
Expand Down Expand Up @@ -107,16 +108,16 @@ class MockBinaryReader extends _i1.Mock implements _i4.BinaryReader {
(super.noSuchMethod(Invocation.method(#readMap, [length]),
returnValue: <dynamic, dynamic>{}) as Map<dynamic, dynamic>);
@override
_i4.HiveList<_i3.HiveObject> readHiveList([int? length]) =>
_i3.HiveList<_i1.HiveObjectMixin> readHiveList([int? length]) =>
(super.noSuchMethod(Invocation.method(#readHiveList, [length]),
returnValue: _FakeHiveList<_i3.HiveObject>())
as _i4.HiveList<_i3.HiveObject>);
returnValue: _FakeHiveList<_i1.HiveObjectMixin>())
as _i3.HiveList<_i1.HiveObjectMixin>);
}

/// A class which mocks [BinaryWriter].
///
/// See the documentation for Mockito's code generation for more information.
class MockBinaryWriter extends _i1.Mock implements _i4.BinaryWriter {
class MockBinaryWriter extends _i2.Mock implements _i3.BinaryWriter {
@override
void writeByte(int? byte) =>
super.noSuchMethod(Invocation.method(#writeByte, [byte]),
Expand Down Expand Up @@ -197,13 +198,12 @@ class MockBinaryWriter extends _i1.Mock implements _i4.BinaryWriter {
Invocation.method(#writeMap, [map], {#writeLength: writeLength}),
returnValueForMissingStub: null);
@override
void writeHiveList(covariant _i4.HiveList<_i3.HiveObject> list,
{bool writeLength = true}) =>
void writeHiveList(_i3.HiveList<_i1.HiveObjectMixin>? list,
{bool? writeLength = true}) =>
super.noSuchMethod(
Invocation.method(
#writeHiveList, [list], {#writeLength: writeLength}),
returnValueForMissingStub: null);

@override
void write<T>(T? value, {bool? writeTypeId = true}) => super.noSuchMethod(
Invocation.method(#write, [value], {#writeTypeId: writeTypeId}),
Expand Down