-
-
Notifications
You must be signed in to change notification settings - Fork 50
Add material_ui support to hive_ce_flutter #333
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,67 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter/material.dart' as flutter show TimeOfDay; | ||
| import 'package:hive_ce/hive_ce.dart'; | ||
| import 'package:material_ui/material_ui.dart' as material_ui show TimeOfDay; | ||
|
|
||
| /// TODO: Document this! | ||
| class TimeOfDayAdapter extends TypeAdapter<TimeOfDay> { | ||
| abstract class _TimeOfDayAdapter<T> extends TypeAdapter<T> { | ||
| static const _defaultTypeId = 201; | ||
|
|
||
| /// Constructor | ||
| const TimeOfDayAdapter({int? typeId}) : typeId = typeId ?? _defaultTypeId; | ||
| const _TimeOfDayAdapter({int? typeId}) : typeId = typeId ?? _defaultTypeId; | ||
|
|
||
| @override | ||
| final int typeId; | ||
|
|
||
| T _create(int hour, int minute); | ||
|
|
||
| int _hour(T obj); | ||
|
|
||
| int _minute(T obj); | ||
|
|
||
| @override | ||
| TimeOfDay read(BinaryReader reader) { | ||
| T read(BinaryReader reader) { | ||
| final totalMinutes = reader.readInt(); | ||
| return TimeOfDay(hour: totalMinutes ~/ 60, minute: totalMinutes % 60); | ||
| return _create(totalMinutes ~/ 60, totalMinutes % 60); | ||
| } | ||
|
|
||
| @override | ||
| void write(BinaryWriter writer, TimeOfDay obj) { | ||
| writer.writeInt(obj.hour * 60 + obj.minute); | ||
| void write(BinaryWriter writer, T obj) { | ||
| writer.writeInt(_hour(obj) * 60 + _minute(obj)); | ||
| } | ||
| } | ||
|
|
||
| /// A [TypeAdapter] for the `TimeOfDay` class from `package:flutter/material.dart` | ||
| /// | ||
| /// Uses the same binary format and default type id as | ||
| /// [MaterialUiTimeOfDayAdapter] | ||
| class TimeOfDayAdapter extends _TimeOfDayAdapter<flutter.TimeOfDay> { | ||
| /// Constructor | ||
| const TimeOfDayAdapter({super.typeId}); | ||
|
|
||
| @override | ||
| flutter.TimeOfDay _create(int hour, int minute) => | ||
| flutter.TimeOfDay(hour: hour, minute: minute); | ||
|
|
||
| @override | ||
| int _hour(flutter.TimeOfDay obj) => obj.hour; | ||
|
|
||
| @override | ||
| int _minute(flutter.TimeOfDay obj) => obj.minute; | ||
| } | ||
|
|
||
| /// A [TypeAdapter] for the `TimeOfDay` class from `package:material_ui` | ||
| /// | ||
| /// Uses the same binary format and default type id as [TimeOfDayAdapter] | ||
| class MaterialUiTimeOfDayAdapter | ||
| extends _TimeOfDayAdapter<material_ui.TimeOfDay> { | ||
| /// Constructor | ||
| const MaterialUiTimeOfDayAdapter({super.typeId}); | ||
|
|
||
| @override | ||
| material_ui.TimeOfDay _create(int hour, int minute) => | ||
| material_ui.TimeOfDay(hour: hour, minute: minute); | ||
|
|
||
| @override | ||
| int _hour(material_ui.TimeOfDay obj) => obj.hour; | ||
|
|
||
| @override | ||
| int _minute(material_ui.TimeOfDay obj) => obj.minute; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,35 @@ | ||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter/widgets.dart'; | ||
| import 'package:hive_ce_flutter/adapters.dart' | ||
| hide IsolatedHive, IsolateNameServer; | ||
| import 'package:hive_ce_flutter/src/isolate/isolate_name_server.dart'; | ||
| import 'package:hive_ce_flutter/src/type_registry_extensions.dart'; | ||
| import 'package:hive_ce_flutter/src/wrapper/path_provider.dart'; | ||
| import 'package:hive_ce_flutter/src/wrapper/path.dart' as path_helper; | ||
|
|
||
| /// Flutter extensions for [IsolatedHiveInterface] | ||
| extension IsolatedHiveX on IsolatedHiveInterface { | ||
| /// Initializes [IsolatedHive] with the path from | ||
| /// [getApplicationDocumentsDirectory] and the Flutter [IsolateNameServer] | ||
| /// | ||
| /// You can provide a [subDirectory] where the boxes should be stored | ||
| /// You can provide a [subDirectory] where the boxes should be stored. | ||
| /// Relative paths are resolved against [getApplicationDocumentsDirectory]. | ||
| /// Absolute paths are used as-is. | ||
| /// | ||
| /// Also registers the flutter type adapters | ||
| /// | ||
| /// If [useMaterialUi] is true, [MaterialUiTimeOfDayAdapter] is registered | ||
| /// instead of [TimeOfDayAdapter] | ||
| Future<void> initFlutter({ | ||
| String? subDirectory, | ||
| int? colorAdapterTypeId, | ||
| int? timeOfDayAdapterTypeId, | ||
| bool useMaterialUi = false, | ||
| }) async { | ||
| try { | ||
| WidgetsFlutterBinding.ensureInitialized(); | ||
| } catch (_) { | ||
| // This will fail if the Flutter engine is not available | ||
| } | ||
|
|
||
| String? path; | ||
| if (!kIsWeb) { | ||
| final appDir = await getApplicationDocumentsDirectory(); | ||
| path = path_helper.join(appDir.path, subDirectory); | ||
| } | ||
|
|
||
| await init(path, isolateNameServer: const IsolateNameServer()); | ||
|
|
||
| final colorAdapter = ColorAdapter(typeId: colorAdapterTypeId); | ||
| if (!isAdapterRegistered(colorAdapter.typeId)) { | ||
| registerAdapter(colorAdapter); | ||
| } | ||
|
|
||
| final timeOfDayAdapter = TimeOfDayAdapter(typeId: timeOfDayAdapterTypeId); | ||
| if (!isAdapterRegistered(timeOfDayAdapter.typeId)) { | ||
| registerAdapter(timeOfDayAdapter); | ||
| } | ||
| await initFlutterCommon( | ||
| dir: subDirectory, | ||
| initHive: (path) => | ||
| init(path, isolateNameServer: const IsolateNameServer()), | ||
| colorAdapterTypeId: colorAdapterTypeId, | ||
| timeOfDayAdapterTypeId: timeOfDayAdapterTypeId, | ||
| useMaterialUi: useMaterialUi, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter/widgets.dart'; | ||
| import 'package:hive_ce_flutter/adapters.dart'; | ||
| import 'package:hive_ce_flutter/src/wrapper/path_provider.dart'; | ||
| import 'package:hive_ce_flutter/src/wrapper/path.dart' as path_helper; | ||
|
|
||
| /// Flutter extensions for [TypeRegistry] | ||
| extension TypeRegistryX on TypeRegistry { | ||
| /// Common Flutter initialization code | ||
| Future<void> initFlutterCommon({ | ||
| String? dir, | ||
| required FutureOr<void> Function(String? path) initHive, | ||
| int? colorAdapterTypeId, | ||
| int? timeOfDayAdapterTypeId, | ||
| bool useMaterialUi = false, | ||
| }) async { | ||
| try { | ||
| WidgetsFlutterBinding.ensureInitialized(); | ||
| } catch (_) { | ||
| // This will fail if the Flutter engine is not available | ||
| } | ||
|
|
||
| String? path; | ||
| if (!kIsWeb) { | ||
| // Root-relative Windows paths still take the drive from the app directory | ||
| if (dir != null && | ||
| path_helper.isAbsolute(dir) && | ||
| !path_helper.isRootRelative(dir)) { | ||
| path = dir; | ||
| } else { | ||
| final appDir = await getApplicationDocumentsDirectory(); | ||
| path = path_helper.join(appDir.path, dir); | ||
| } | ||
| } | ||
|
|
||
| await initHive(path); | ||
|
|
||
| _registerAdapter(ColorAdapter(typeId: colorAdapterTypeId)); | ||
| if (useMaterialUi) { | ||
| _registerAdapter( | ||
| MaterialUiTimeOfDayAdapter(typeId: timeOfDayAdapterTypeId), | ||
| ); | ||
| } else { | ||
| _registerAdapter(TimeOfDayAdapter(typeId: timeOfDayAdapterTypeId)); | ||
| } | ||
| } | ||
|
|
||
| void _registerAdapter<T>(TypeAdapter<T> adapter) { | ||
| if (isAdapterRegistered(adapter.typeId)) return; | ||
| registerAdapter(adapter); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import 'package:hive_ce/src/registry/type_registry_impl.dart'; | ||
| import 'package:hive_ce_flutter/src/type_registry_extensions.dart'; | ||
| import 'package:path/path.dart' as p; | ||
| import 'package:path_provider_platform_interface/path_provider_platform_interface.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| final _documentsPath = p.absolute('documents'); | ||
|
|
||
| class _FakePathProviderPlatform extends PathProviderPlatform { | ||
| var documentsPathCalls = 0; | ||
|
|
||
| @override | ||
| Future<String?> getApplicationDocumentsPath() async { | ||
| documentsPathCalls++; | ||
| return _documentsPath; | ||
| } | ||
| } | ||
|
|
||
| void main() { | ||
| late _FakePathProviderPlatform pathProvider; | ||
|
|
||
| setUp(() { | ||
| pathProvider = _FakePathProviderPlatform(); | ||
| PathProviderPlatform.instance = pathProvider; | ||
| }); | ||
|
|
||
| Future<String?> initPath(String? dir) async { | ||
| String? initPath; | ||
| await TypeRegistryImpl().initFlutterCommon( | ||
| dir: dir, | ||
| initHive: (path) => initPath = path, | ||
| ); | ||
| return initPath; | ||
| } | ||
|
|
||
| group('TypeRegistryX.initFlutterCommon()', () { | ||
| test('uses the documents directory when dir is null', () async { | ||
| expect(await initPath(null), _documentsPath); | ||
| expect(pathProvider.documentsPathCalls, 1); | ||
| }); | ||
|
|
||
| test('resolves a relative dir against the documents directory', () async { | ||
| expect(await initPath('boxes'), p.join(_documentsPath, 'boxes')); | ||
| expect(pathProvider.documentsPathCalls, 1); | ||
| }); | ||
|
|
||
| test('uses an absolute dir as-is', () async { | ||
| final dir = p.absolute('boxes'); | ||
| expect(await initPath(dir), dir); | ||
| expect(pathProvider.documentsPathCalls, 0); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.