diff --git a/lib/src/common/constants/enums/generic_entity.dart b/lib/src/common/constants/enums/generic_entity.dart new file mode 100644 index 0000000..0702b78 --- /dev/null +++ b/lib/src/common/constants/enums/generic_entity.dart @@ -0,0 +1,5 @@ +import '../../utils/json_abstract.dart'; + +abstract class GenericEntity extends JsonAbstract { + T get id; +} diff --git a/lib/src/common/utils/app_utils.dart b/lib/src/common/utils/app_utils.dart index 609ef7b..a8ea7ef 100644 --- a/lib/src/common/utils/app_utils.dart +++ b/lib/src/common/utils/app_utils.dart @@ -91,6 +91,10 @@ abstract class AppUtils { (List>> event) => event.map(convertSnapNonNull(constructor)).toList(); + static T? Function(JsonObject? value) convertGet( + T Function(JsonObject) fromJson) => + (value) => value != null ? fromJson(value) : null; + static int currentDay(AsyncValue dob) { if (dob.valueOrNull == null) return 0; return DateTime.now().difference(dob.valueOrNull!).inDays; diff --git a/lib/src/common/utils/custom_types.dart b/lib/src/common/utils/custom_types.dart index 4a06c34..c044cc0 100644 --- a/lib/src/common/utils/custom_types.dart +++ b/lib/src/common/utils/custom_types.dart @@ -6,8 +6,13 @@ import 'dart:async'; +import '../constants/enums/generic_entity.dart'; + typedef ConvertFunction = R Function(S); typedef JsonObject = Map; typedef AsyncConvertFunction = FutureOr Function(S event); + +typedef GenericFromJson = T Function( + Map); diff --git a/lib/src/common/utils/extensions/custom_extensions/store_ref_extensions.dart b/lib/src/common/utils/extensions/custom_extensions/store_ref_extensions.dart deleted file mode 100644 index 34ce920..0000000 --- a/lib/src/common/utils/extensions/custom_extensions/store_ref_extensions.dart +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2024 Panta Dattatreya Reddy -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at https://mozilla.org/MPL/2.0/. - -import 'package:sembast/sembast.dart'; - -import '../../custom_types.dart'; -import '../../json_abstract.dart'; - -extension StoreRefExtensions on StoreRef { - Future> addAllWithKey( - Database db, - List values, - ConvertFunction keyGenerator, - ) async { - var keys = []; - await db.transaction((txn) async { - for (final value in values) { - K genKey = keyGenerator(value); - final key = await record(genKey).add(txn, value.toJson()); - if (key != null) keys.add(key); - } - }); - return keys; - } - - Future> addAllWithKeyAndTransaction( - DatabaseClient txn, - List values, - ConvertFunction keyGenerator, - ) async { - var keys = []; - for (final value in values) { - K genKey = keyGenerator(value); - final key = await record(genKey).add(txn, value.toJson()); - if (key != null) keys.add(key); - } - return keys; - } -} diff --git a/lib/src/common/utils/json_abstract.dart b/lib/src/common/utils/json_abstract.dart index 80f5d59..0c0d843 100644 --- a/lib/src/common/utils/json_abstract.dart +++ b/lib/src/common/utils/json_abstract.dart @@ -6,6 +6,4 @@ abstract class JsonAbstract { Map toJson(); - - JsonAbstract.fromJson(Map json); } diff --git a/lib/src/common/utils/repository/generic_repository.dart b/lib/src/common/utils/repository/generic_repository.dart new file mode 100644 index 0000000..675f63e --- /dev/null +++ b/lib/src/common/utils/repository/generic_repository.dart @@ -0,0 +1,53 @@ +import 'package:tekartik_app_flutter_sembast/setup/sembast_flutter.dart'; + +import '../../constants/enums/generic_entity.dart'; +import '../app_utils.dart'; +import '../custom_types.dart'; + +abstract class GenericRepository> { + final Database database; + final StoreRef store; + final GenericFromJson fromJson; + + GenericRepository(this.database, String _storeName, this.fromJson) + : store = StoreRef(_storeName); + + Stream> watchAll() => + store.query().onSnapshots(database).map(AppUtils.convertSnaps(fromJson)); + + Stream watchById(S id) => + store.record(id).onSnapshot(database).map(AppUtils.convertSnap(fromJson)); + + Future getCount([DatabaseClient? dbClient]) => + wrap(dbClient, (txn) => store.count(txn)); + + Future getById(S id, [DatabaseClient? dbClient]) async { + return wrap( + dbClient, + (txn) => store.record(id).get(txn).then(AppUtils.convertGet(fromJson)), + ); + } + + Future save(T entity, [DatabaseClient? dbClient]) => wrap( + dbClient, + (txn) => store.record(entity.id).put(txn, entity.toJson()), + ); + + Future saveAll(List entities, [DatabaseClient? dbClient]) async { + if (entities.isEmpty) return; + return wrap( + dbClient, + (txn) => Future.wait([for (final entry in entities) save(entry, txn)]), + ); + } + + Future wrap( + DatabaseClient? dbClient, + Future Function(DatabaseClient) call, + ) async { + if (dbClient == null) { + return await database.transaction((txn) => call(txn)); + } + return await call(dbClient); + } +} diff --git a/lib/src/features/home/controller/home_controller.dart b/lib/src/features/home/controller/home_controller.dart index 6bf79a4..f23f4b3 100644 --- a/lib/src/features/home/controller/home_controller.dart +++ b/lib/src/features/home/controller/home_controller.dart @@ -22,15 +22,15 @@ Stream>?> dayBoxMap(Ref ref) => @riverpod Stream dayBox(Ref ref, int boxNumber) => - ref.watch(dayBoxRepositoryProvider).getDayBoxByNumber(boxNumber); + ref.watch(dayBoxRepositoryProvider).watchById(boxNumber); @riverpod Stream weekBox(Ref ref, int boxNumber) => - ref.watch(weekBoxRepositoryProvider).getWeekBoxByWeekNumber(boxNumber); + ref.watch(weekBoxRepositoryProvider).watchById(boxNumber); @riverpod Stream yearBox(Ref ref, int boxNumber) => - ref.watch(yearBoxRepositoryProvider).getYearBoxByYearNumber(boxNumber); + ref.watch(yearBoxRepositoryProvider).watchById(boxNumber); @riverpod Stream>?> weekBoxMap(Ref ref) => ref.watch(weekBoxRepositoryProvider).getWeekBoxListGroupByYearNumber(); diff --git a/lib/src/features/home/controller/home_controller.g.dart b/lib/src/features/home/controller/home_controller.g.dart index c342725..f599325 100644 --- a/lib/src/features/home/controller/home_controller.g.dart +++ b/lib/src/features/home/controller/home_controller.g.dart @@ -24,7 +24,7 @@ final dayBoxMapProvider = // ignore: unused_element typedef DayBoxMapRef = AutoDisposeStreamProviderRef>?>; -String _$dayBoxHash() => r'93c0afdb80f0edbcd0e80db7b44d7e8a63542083'; +String _$dayBoxHash() => r'a03491494ae70644dcbd41bc90a0a7bf7f93649d'; /// Copied from Dart SDK class _SystemHash { @@ -174,7 +174,7 @@ class _DayBoxProviderElement extends AutoDisposeStreamProviderElement int get boxNumber => (origin as DayBoxProvider).boxNumber; } -String _$weekBoxHash() => r'bdd4650ae543d1884cb4822458e75e1068b0d847'; +String _$weekBoxHash() => r'9f662058a54370445316112f75ddaa032e23aa65'; /// See also [weekBox]. @ProviderFor(weekBox) @@ -303,7 +303,7 @@ class _WeekBoxProviderElement extends AutoDisposeStreamProviderElement int get boxNumber => (origin as WeekBoxProvider).boxNumber; } -String _$yearBoxHash() => r'e9366888ff438839695fda33c12e4819427a821f'; +String _$yearBoxHash() => r'79e96e09ede8fb3ee11a5cd51b67c3dd782e9448'; /// See also [yearBox]. @ProviderFor(yearBox) diff --git a/lib/src/features/home/domain/box.dart b/lib/src/features/home/domain/box.dart index 87bf0bc..96cb7d6 100644 --- a/lib/src/features/home/domain/box.dart +++ b/lib/src/features/home/domain/box.dart @@ -7,9 +7,9 @@ import 'package:flutter/material.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; +import '../../../common/constants/enums/generic_entity.dart'; import '../../../common/utils/converter/epoch_date_time_converter.dart'; import '../../../common/utils/extensions/custom_extensions.dart'; -import '../../../common/utils/json_abstract.dart'; import '../enums/box_status.dart'; part 'box.freezed.dart'; @@ -42,7 +42,7 @@ enum YearBoxFields { } sealed class Box { - int get boxNumber; + int get id; BoxStatus get boxStatus; bool get hasLandMark; String get chatGroupId; @@ -51,10 +51,10 @@ sealed class Box { } @freezed -class DayBox with _$DayBox implements JsonAbstract, Box { +class DayBox with _$DayBox implements GenericEntity, Box { const DayBox._(); factory DayBox({ - required int boxNumber, + @JsonKey(name: 'boxNumber') required int id, @EpochDateTimeConverter() required DateTime date, required BoxStatus boxStatus, required int weekNumber, @@ -70,14 +70,14 @@ class DayBox with _$DayBox implements JsonAbstract, Box { /// For Chat group name @override - String get chatGroupId => "DAY_$boxNumber"; + String get chatGroupId => "DAY_$id"; } @freezed -class WeekBox with _$WeekBox implements JsonAbstract, Box { +class WeekBox with _$WeekBox implements GenericEntity, Box { const WeekBox._(); factory WeekBox({ - required int boxNumber, + @JsonKey(name: 'boxNumber') required int id, @EpochDateTimeConverter() required DateTime startDate, @EpochDateTimeConverter() required DateTime endDate, required BoxStatus boxStatus, @@ -93,15 +93,15 @@ class WeekBox with _$WeekBox implements JsonAbstract, Box { /// For Chat group name @override - String get chatGroupId => "WEEK_$boxNumber"; + String get chatGroupId => "WEEK_$id"; } @freezed -class YearBox with _$YearBox implements JsonAbstract, Box { +class YearBox with _$YearBox implements GenericEntity, Box { const YearBox._(); factory YearBox({ - required int boxNumber, + @JsonKey(name: 'boxNumber') required int id, @EpochDateTimeConverter() required DateTime startDate, @EpochDateTimeConverter() required DateTime endDate, required BoxStatus boxStatus, @@ -116,5 +116,5 @@ class YearBox with _$YearBox implements JsonAbstract, Box { /// For Chat group name @override - String get chatGroupId => "YEAR_$boxNumber"; + String get chatGroupId => "YEAR_$id"; } diff --git a/lib/src/features/home/domain/box.freezed.dart b/lib/src/features/home/domain/box.freezed.dart index 5603efd..a5c5872 100644 --- a/lib/src/features/home/domain/box.freezed.dart +++ b/lib/src/features/home/domain/box.freezed.dart @@ -20,7 +20,8 @@ DayBox _$DayBoxFromJson(Map json) { /// @nodoc mixin _$DayBox { - int get boxNumber => throw _privateConstructorUsedError; + @JsonKey(name: 'boxNumber') + int get id => throw _privateConstructorUsedError; @EpochDateTimeConverter() DateTime get date => throw _privateConstructorUsedError; BoxStatus get boxStatus => throw _privateConstructorUsedError; @@ -43,7 +44,7 @@ abstract class $DayBoxCopyWith<$Res> { _$DayBoxCopyWithImpl<$Res, DayBox>; @useResult $Res call( - {int boxNumber, + {@JsonKey(name: 'boxNumber') int id, @EpochDateTimeConverter() DateTime date, BoxStatus boxStatus, int weekNumber, @@ -66,7 +67,7 @@ class _$DayBoxCopyWithImpl<$Res, $Val extends DayBox> @pragma('vm:prefer-inline') @override $Res call({ - Object? boxNumber = null, + Object? id = null, Object? date = null, Object? boxStatus = null, Object? weekNumber = null, @@ -74,9 +75,9 @@ class _$DayBoxCopyWithImpl<$Res, $Val extends DayBox> Object? hasLandMark = null, }) { return _then(_value.copyWith( - boxNumber: null == boxNumber - ? _value.boxNumber - : boxNumber // ignore: cast_nullable_to_non_nullable + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable as int, date: null == date ? _value.date @@ -110,7 +111,7 @@ abstract class _$$DayBoxImplCopyWith<$Res> implements $DayBoxCopyWith<$Res> { @override @useResult $Res call( - {int boxNumber, + {@JsonKey(name: 'boxNumber') int id, @EpochDateTimeConverter() DateTime date, BoxStatus boxStatus, int weekNumber, @@ -131,7 +132,7 @@ class __$$DayBoxImplCopyWithImpl<$Res> @pragma('vm:prefer-inline') @override $Res call({ - Object? boxNumber = null, + Object? id = null, Object? date = null, Object? boxStatus = null, Object? weekNumber = null, @@ -139,9 +140,9 @@ class __$$DayBoxImplCopyWithImpl<$Res> Object? hasLandMark = null, }) { return _then(_$DayBoxImpl( - boxNumber: null == boxNumber - ? _value.boxNumber - : boxNumber // ignore: cast_nullable_to_non_nullable + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable as int, date: null == date ? _value.date @@ -171,7 +172,7 @@ class __$$DayBoxImplCopyWithImpl<$Res> @JsonSerializable() class _$DayBoxImpl extends _DayBox { _$DayBoxImpl( - {required this.boxNumber, + {@JsonKey(name: 'boxNumber') required this.id, @EpochDateTimeConverter() required this.date, required this.boxStatus, required this.weekNumber, @@ -183,7 +184,8 @@ class _$DayBoxImpl extends _DayBox { _$$DayBoxImplFromJson(json); @override - final int boxNumber; + @JsonKey(name: 'boxNumber') + final int id; @override @EpochDateTimeConverter() final DateTime date; @@ -199,7 +201,7 @@ class _$DayBoxImpl extends _DayBox { @override String toString() { - return 'DayBox(boxNumber: $boxNumber, date: $date, boxStatus: $boxStatus, weekNumber: $weekNumber, yearNumber: $yearNumber, hasLandMark: $hasLandMark)'; + return 'DayBox(id: $id, date: $date, boxStatus: $boxStatus, weekNumber: $weekNumber, yearNumber: $yearNumber, hasLandMark: $hasLandMark)'; } @override @@ -207,8 +209,7 @@ class _$DayBoxImpl extends _DayBox { return identical(this, other) || (other.runtimeType == runtimeType && other is _$DayBoxImpl && - (identical(other.boxNumber, boxNumber) || - other.boxNumber == boxNumber) && + (identical(other.id, id) || other.id == id) && (identical(other.date, date) || other.date == date) && (identical(other.boxStatus, boxStatus) || other.boxStatus == boxStatus) && @@ -222,8 +223,8 @@ class _$DayBoxImpl extends _DayBox { @JsonKey(includeFromJson: false, includeToJson: false) @override - int get hashCode => Object.hash(runtimeType, boxNumber, date, boxStatus, - weekNumber, yearNumber, hasLandMark); + int get hashCode => Object.hash( + runtimeType, id, date, boxStatus, weekNumber, yearNumber, hasLandMark); /// Create a copy of DayBox /// with the given fields replaced by the non-null parameter values. @@ -243,7 +244,7 @@ class _$DayBoxImpl extends _DayBox { abstract class _DayBox extends DayBox { factory _DayBox( - {required final int boxNumber, + {@JsonKey(name: 'boxNumber') required final int id, @EpochDateTimeConverter() required final DateTime date, required final BoxStatus boxStatus, required final int weekNumber, @@ -254,7 +255,8 @@ abstract class _DayBox extends DayBox { factory _DayBox.fromJson(Map json) = _$DayBoxImpl.fromJson; @override - int get boxNumber; + @JsonKey(name: 'boxNumber') + int get id; @override @EpochDateTimeConverter() DateTime get date; @@ -281,7 +283,8 @@ WeekBox _$WeekBoxFromJson(Map json) { /// @nodoc mixin _$WeekBox { - int get boxNumber => throw _privateConstructorUsedError; + @JsonKey(name: 'boxNumber') + int get id => throw _privateConstructorUsedError; @EpochDateTimeConverter() DateTime get startDate => throw _privateConstructorUsedError; @EpochDateTimeConverter() @@ -305,7 +308,7 @@ abstract class $WeekBoxCopyWith<$Res> { _$WeekBoxCopyWithImpl<$Res, WeekBox>; @useResult $Res call( - {int boxNumber, + {@JsonKey(name: 'boxNumber') int id, @EpochDateTimeConverter() DateTime startDate, @EpochDateTimeConverter() DateTime endDate, BoxStatus boxStatus, @@ -328,7 +331,7 @@ class _$WeekBoxCopyWithImpl<$Res, $Val extends WeekBox> @pragma('vm:prefer-inline') @override $Res call({ - Object? boxNumber = null, + Object? id = null, Object? startDate = null, Object? endDate = null, Object? boxStatus = null, @@ -336,9 +339,9 @@ class _$WeekBoxCopyWithImpl<$Res, $Val extends WeekBox> Object? hasLandMark = null, }) { return _then(_value.copyWith( - boxNumber: null == boxNumber - ? _value.boxNumber - : boxNumber // ignore: cast_nullable_to_non_nullable + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable as int, startDate: null == startDate ? _value.startDate @@ -372,7 +375,7 @@ abstract class _$$WeekBoxImplCopyWith<$Res> implements $WeekBoxCopyWith<$Res> { @override @useResult $Res call( - {int boxNumber, + {@JsonKey(name: 'boxNumber') int id, @EpochDateTimeConverter() DateTime startDate, @EpochDateTimeConverter() DateTime endDate, BoxStatus boxStatus, @@ -393,7 +396,7 @@ class __$$WeekBoxImplCopyWithImpl<$Res> @pragma('vm:prefer-inline') @override $Res call({ - Object? boxNumber = null, + Object? id = null, Object? startDate = null, Object? endDate = null, Object? boxStatus = null, @@ -401,9 +404,9 @@ class __$$WeekBoxImplCopyWithImpl<$Res> Object? hasLandMark = null, }) { return _then(_$WeekBoxImpl( - boxNumber: null == boxNumber - ? _value.boxNumber - : boxNumber // ignore: cast_nullable_to_non_nullable + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable as int, startDate: null == startDate ? _value.startDate @@ -433,7 +436,7 @@ class __$$WeekBoxImplCopyWithImpl<$Res> @JsonSerializable() class _$WeekBoxImpl extends _WeekBox { _$WeekBoxImpl( - {required this.boxNumber, + {@JsonKey(name: 'boxNumber') required this.id, @EpochDateTimeConverter() required this.startDate, @EpochDateTimeConverter() required this.endDate, required this.boxStatus, @@ -445,7 +448,8 @@ class _$WeekBoxImpl extends _WeekBox { _$$WeekBoxImplFromJson(json); @override - final int boxNumber; + @JsonKey(name: 'boxNumber') + final int id; @override @EpochDateTimeConverter() final DateTime startDate; @@ -462,7 +466,7 @@ class _$WeekBoxImpl extends _WeekBox { @override String toString() { - return 'WeekBox(boxNumber: $boxNumber, startDate: $startDate, endDate: $endDate, boxStatus: $boxStatus, yearNumber: $yearNumber, hasLandMark: $hasLandMark)'; + return 'WeekBox(id: $id, startDate: $startDate, endDate: $endDate, boxStatus: $boxStatus, yearNumber: $yearNumber, hasLandMark: $hasLandMark)'; } @override @@ -470,8 +474,7 @@ class _$WeekBoxImpl extends _WeekBox { return identical(this, other) || (other.runtimeType == runtimeType && other is _$WeekBoxImpl && - (identical(other.boxNumber, boxNumber) || - other.boxNumber == boxNumber) && + (identical(other.id, id) || other.id == id) && (identical(other.startDate, startDate) || other.startDate == startDate) && (identical(other.endDate, endDate) || other.endDate == endDate) && @@ -485,8 +488,8 @@ class _$WeekBoxImpl extends _WeekBox { @JsonKey(includeFromJson: false, includeToJson: false) @override - int get hashCode => Object.hash(runtimeType, boxNumber, startDate, endDate, - boxStatus, yearNumber, hasLandMark); + int get hashCode => Object.hash( + runtimeType, id, startDate, endDate, boxStatus, yearNumber, hasLandMark); /// Create a copy of WeekBox /// with the given fields replaced by the non-null parameter values. @@ -506,7 +509,7 @@ class _$WeekBoxImpl extends _WeekBox { abstract class _WeekBox extends WeekBox { factory _WeekBox( - {required final int boxNumber, + {@JsonKey(name: 'boxNumber') required final int id, @EpochDateTimeConverter() required final DateTime startDate, @EpochDateTimeConverter() required final DateTime endDate, required final BoxStatus boxStatus, @@ -517,7 +520,8 @@ abstract class _WeekBox extends WeekBox { factory _WeekBox.fromJson(Map json) = _$WeekBoxImpl.fromJson; @override - int get boxNumber; + @JsonKey(name: 'boxNumber') + int get id; @override @EpochDateTimeConverter() DateTime get startDate; @@ -545,7 +549,8 @@ YearBox _$YearBoxFromJson(Map json) { /// @nodoc mixin _$YearBox { - int get boxNumber => throw _privateConstructorUsedError; + @JsonKey(name: 'boxNumber') + int get id => throw _privateConstructorUsedError; @EpochDateTimeConverter() DateTime get startDate => throw _privateConstructorUsedError; @EpochDateTimeConverter() @@ -568,7 +573,7 @@ abstract class $YearBoxCopyWith<$Res> { _$YearBoxCopyWithImpl<$Res, YearBox>; @useResult $Res call( - {int boxNumber, + {@JsonKey(name: 'boxNumber') int id, @EpochDateTimeConverter() DateTime startDate, @EpochDateTimeConverter() DateTime endDate, BoxStatus boxStatus, @@ -590,16 +595,16 @@ class _$YearBoxCopyWithImpl<$Res, $Val extends YearBox> @pragma('vm:prefer-inline') @override $Res call({ - Object? boxNumber = null, + Object? id = null, Object? startDate = null, Object? endDate = null, Object? boxStatus = null, Object? hasLandMark = null, }) { return _then(_value.copyWith( - boxNumber: null == boxNumber - ? _value.boxNumber - : boxNumber // ignore: cast_nullable_to_non_nullable + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable as int, startDate: null == startDate ? _value.startDate @@ -629,7 +634,7 @@ abstract class _$$YearBoxImplCopyWith<$Res> implements $YearBoxCopyWith<$Res> { @override @useResult $Res call( - {int boxNumber, + {@JsonKey(name: 'boxNumber') int id, @EpochDateTimeConverter() DateTime startDate, @EpochDateTimeConverter() DateTime endDate, BoxStatus boxStatus, @@ -649,16 +654,16 @@ class __$$YearBoxImplCopyWithImpl<$Res> @pragma('vm:prefer-inline') @override $Res call({ - Object? boxNumber = null, + Object? id = null, Object? startDate = null, Object? endDate = null, Object? boxStatus = null, Object? hasLandMark = null, }) { return _then(_$YearBoxImpl( - boxNumber: null == boxNumber - ? _value.boxNumber - : boxNumber // ignore: cast_nullable_to_non_nullable + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable as int, startDate: null == startDate ? _value.startDate @@ -684,7 +689,7 @@ class __$$YearBoxImplCopyWithImpl<$Res> @JsonSerializable() class _$YearBoxImpl extends _YearBox { _$YearBoxImpl( - {required this.boxNumber, + {@JsonKey(name: 'boxNumber') required this.id, @EpochDateTimeConverter() required this.startDate, @EpochDateTimeConverter() required this.endDate, required this.boxStatus, @@ -695,7 +700,8 @@ class _$YearBoxImpl extends _YearBox { _$$YearBoxImplFromJson(json); @override - final int boxNumber; + @JsonKey(name: 'boxNumber') + final int id; @override @EpochDateTimeConverter() final DateTime startDate; @@ -710,7 +716,7 @@ class _$YearBoxImpl extends _YearBox { @override String toString() { - return 'YearBox(boxNumber: $boxNumber, startDate: $startDate, endDate: $endDate, boxStatus: $boxStatus, hasLandMark: $hasLandMark)'; + return 'YearBox(id: $id, startDate: $startDate, endDate: $endDate, boxStatus: $boxStatus, hasLandMark: $hasLandMark)'; } @override @@ -718,8 +724,7 @@ class _$YearBoxImpl extends _YearBox { return identical(this, other) || (other.runtimeType == runtimeType && other is _$YearBoxImpl && - (identical(other.boxNumber, boxNumber) || - other.boxNumber == boxNumber) && + (identical(other.id, id) || other.id == id) && (identical(other.startDate, startDate) || other.startDate == startDate) && (identical(other.endDate, endDate) || other.endDate == endDate) && @@ -731,8 +736,8 @@ class _$YearBoxImpl extends _YearBox { @JsonKey(includeFromJson: false, includeToJson: false) @override - int get hashCode => Object.hash( - runtimeType, boxNumber, startDate, endDate, boxStatus, hasLandMark); + int get hashCode => + Object.hash(runtimeType, id, startDate, endDate, boxStatus, hasLandMark); /// Create a copy of YearBox /// with the given fields replaced by the non-null parameter values. @@ -752,7 +757,7 @@ class _$YearBoxImpl extends _YearBox { abstract class _YearBox extends YearBox { factory _YearBox( - {required final int boxNumber, + {@JsonKey(name: 'boxNumber') required final int id, @EpochDateTimeConverter() required final DateTime startDate, @EpochDateTimeConverter() required final DateTime endDate, required final BoxStatus boxStatus, @@ -762,7 +767,8 @@ abstract class _YearBox extends YearBox { factory _YearBox.fromJson(Map json) = _$YearBoxImpl.fromJson; @override - int get boxNumber; + @JsonKey(name: 'boxNumber') + int get id; @override @EpochDateTimeConverter() DateTime get startDate; diff --git a/lib/src/features/home/domain/box.g.dart b/lib/src/features/home/domain/box.g.dart index beb799b..5e24a32 100644 --- a/lib/src/features/home/domain/box.g.dart +++ b/lib/src/features/home/domain/box.g.dart @@ -7,7 +7,7 @@ part of 'box.dart'; // ************************************************************************** _$DayBoxImpl _$$DayBoxImplFromJson(Map json) => _$DayBoxImpl( - boxNumber: (json['boxNumber'] as num).toInt(), + id: (json['boxNumber'] as num).toInt(), date: const EpochDateTimeConverter() .fromJson((json['date'] as num).toInt()), boxStatus: $enumDecode(_$BoxStatusEnumMap, json['boxStatus']), @@ -18,7 +18,7 @@ _$DayBoxImpl _$$DayBoxImplFromJson(Map json) => _$DayBoxImpl( Map _$$DayBoxImplToJson(_$DayBoxImpl instance) => { - 'boxNumber': instance.boxNumber, + 'boxNumber': instance.id, 'date': const EpochDateTimeConverter().toJson(instance.date), 'boxStatus': _$BoxStatusEnumMap[instance.boxStatus]!, 'weekNumber': instance.weekNumber, @@ -38,7 +38,7 @@ const _$BoxStatusEnumMap = { _$WeekBoxImpl _$$WeekBoxImplFromJson(Map json) => _$WeekBoxImpl( - boxNumber: (json['boxNumber'] as num).toInt(), + id: (json['boxNumber'] as num).toInt(), startDate: const EpochDateTimeConverter() .fromJson((json['startDate'] as num).toInt()), endDate: const EpochDateTimeConverter() @@ -50,7 +50,7 @@ _$WeekBoxImpl _$$WeekBoxImplFromJson(Map json) => Map _$$WeekBoxImplToJson(_$WeekBoxImpl instance) => { - 'boxNumber': instance.boxNumber, + 'boxNumber': instance.id, 'startDate': const EpochDateTimeConverter().toJson(instance.startDate), 'endDate': const EpochDateTimeConverter().toJson(instance.endDate), 'boxStatus': _$BoxStatusEnumMap[instance.boxStatus]!, @@ -60,7 +60,7 @@ Map _$$WeekBoxImplToJson(_$WeekBoxImpl instance) => _$YearBoxImpl _$$YearBoxImplFromJson(Map json) => _$YearBoxImpl( - boxNumber: (json['boxNumber'] as num).toInt(), + id: (json['boxNumber'] as num).toInt(), startDate: const EpochDateTimeConverter() .fromJson((json['startDate'] as num).toInt()), endDate: const EpochDateTimeConverter() @@ -71,7 +71,7 @@ _$YearBoxImpl _$$YearBoxImplFromJson(Map json) => Map _$$YearBoxImplToJson(_$YearBoxImpl instance) => { - 'boxNumber': instance.boxNumber, + 'boxNumber': instance.id, 'startDate': const EpochDateTimeConverter().toJson(instance.startDate), 'endDate': const EpochDateTimeConverter().toJson(instance.endDate), 'boxStatus': _$BoxStatusEnumMap[instance.boxStatus]!, diff --git a/lib/src/features/home/presentation/widgets/check_box_section/day_check_box.dart b/lib/src/features/home/presentation/widgets/check_box_section/day_check_box.dart index 0ad36bc..6df8361 100644 --- a/lib/src/features/home/presentation/widgets/check_box_section/day_check_box.dart +++ b/lib/src/features/home/presentation/widgets/check_box_section/day_check_box.dart @@ -27,10 +27,10 @@ class DayCheckBox extends ConsumerWidget { status: dayBox.boxStatus, heroTag: dayBox.chatGroupId, highlight: dayBox.hasLandMark, - isCurrent: current == dayBox.boxNumber, + isCurrent: current == dayBox.id, isPast: dayBox.date.isBefore(DateTime.now()), - tooltip: (dayBox.boxNumber + 1).formatNumber(), - onTap: () => DayChatRoute(dayBox.boxNumber).go(context), + tooltip: (dayBox.id + 1).formatNumber(), + onTap: () => DayChatRoute(dayBox.id).go(context), onLongPress: () => ref .read(dayBoxRepositoryProvider) .save(dayBox.copyWith(boxStatus: dayBox.boxStatus.getNextState())), diff --git a/lib/src/features/home/presentation/widgets/check_box_section/week_check_box.dart b/lib/src/features/home/presentation/widgets/check_box_section/week_check_box.dart index 1a0c961..2ca5e1f 100644 --- a/lib/src/features/home/presentation/widgets/check_box_section/week_check_box.dart +++ b/lib/src/features/home/presentation/widgets/check_box_section/week_check_box.dart @@ -26,11 +26,11 @@ class WeekCheckBox extends ConsumerWidget { return StatusCheckBox( heroTag: weekBox.chatGroupId, status: weekBox.boxStatus, - highlight: current == weekBox.boxNumber || weekBox.hasLandMark, - isCurrent: current == weekBox.boxNumber, + highlight: current == weekBox.id || weekBox.hasLandMark, + isCurrent: current == weekBox.id, isPast: weekBox.endDate.isBefore(DateTime.now()), - tooltip: (weekBox.boxNumber + 1).formatNumber(), - onTap: () => WeekChatRoute(weekBox.boxNumber).go(context), + tooltip: (weekBox.id + 1).formatNumber(), + onTap: () => WeekChatRoute(weekBox.id).go(context), onLongPress: () => ref .read(weekBoxRepositoryProvider) .save(weekBox.copyWith(boxStatus: weekBox.boxStatus.getNextState())), diff --git a/lib/src/features/home/presentation/widgets/check_box_section/year_check_box.dart b/lib/src/features/home/presentation/widgets/check_box_section/year_check_box.dart index c78d388..013ccb1 100644 --- a/lib/src/features/home/presentation/widgets/check_box_section/year_check_box.dart +++ b/lib/src/features/home/presentation/widgets/check_box_section/year_check_box.dart @@ -26,11 +26,11 @@ class YearCheckBox extends ConsumerWidget { return StatusCheckBox( heroTag: yearBox.chatGroupId, status: yearBox.boxStatus, - highlight: yearBox.boxNumber == current || yearBox.hasLandMark, - isCurrent: yearBox.boxNumber == current, + highlight: yearBox.id == current || yearBox.hasLandMark, + isCurrent: yearBox.id == current, isPast: yearBox.endDate.isBefore(DateTime.now()), - tooltip: (yearBox.boxNumber + 1).formatNumber(), - onTap: () => YearChatRoute(yearBox.boxNumber).go(context), + tooltip: (yearBox.id + 1).formatNumber(), + onTap: () => YearChatRoute(yearBox.id).go(context), onLongPress: () => ref .read(yearBoxRepositoryProvider) .save(yearBox.copyWith(boxStatus: yearBox.boxStatus.getNextState())), diff --git a/lib/src/features/home/repository/day_box_repository.dart b/lib/src/features/home/repository/day_box_repository.dart index 7ae4abf..3f9d1e9 100644 --- a/lib/src/features/home/repository/day_box_repository.dart +++ b/lib/src/features/home/repository/day_box_repository.dart @@ -12,57 +12,30 @@ import 'package:sembast/sembast.dart'; import '../../../common/controller/global_controller.dart'; import '../../../common/utils/app_utils.dart'; -import '../../../common/utils/custom_types.dart'; import '../../../common/utils/extensions/custom_extensions.dart'; -import '../../../common/utils/extensions/custom_extensions/store_ref_extensions.dart'; +import '../../../common/utils/repository/generic_repository.dart'; import '../domain/box.dart'; part 'day_box_repository.g.dart'; -class DayBoxRepository { - final Database _db; - late final StoreRef _store; - - DayBoxRepository(this._db) { - _store = StoreRef("DayBox"); - } - - Stream getDayBoxByNumber(int dayNumber) => _store - .record(dayNumber) - .onSnapshot(_db) - .map(AppUtils.convertSnap(DayBox.fromJson)); - - Stream?> getDayBoxList() => _store - .query() - .onSnapshots(_db) - .map(AppUtils.convertSnaps(DayBox.fromJson)); +class DayBoxRepository extends GenericRepository { + DayBoxRepository(Database db) : super(db, "DayBox", DayBox.fromJson); Stream>?> getDayBoxListGroupByWeekNumber() => - _store + store .query() - .onSnapshots(_db) + .onSnapshots(database) .map(AppUtils.convertSnaps(DayBox.fromJson)) .map((snaps) => snaps.groupBy((snap) => snap.weekNumber)); - Stream?> getDayBoxListByWeekNumber(int weekNumber) => _store + Stream?> getDayBoxListByWeekNumber(int weekNumber) => store .query( finder: Finder( filter: Filter.equals(DayBoxFields.weekNumber.name, weekNumber), ), ) - .onSnapshots(_db) + .onSnapshots(database) .map(AppUtils.convertSnaps(DayBox.fromJson)); - - Future> saveAll(List days) => - _store.addAllWithKey(_db, days, (day) => day.boxNumber); - - Future save(DayBox day) => - _store.record(day.boxNumber).update(_db, day.toJson()); - - Future> saveInTransaction( - DatabaseClient transaction, List days) => - _store.addAllWithKeyAndTransaction( - transaction, days, (day) => day.boxNumber); } @riverpod diff --git a/lib/src/features/home/repository/week_box_repository.dart b/lib/src/features/home/repository/week_box_repository.dart index 20ba846..7c70545 100644 --- a/lib/src/features/home/repository/week_box_repository.dart +++ b/lib/src/features/home/repository/week_box_repository.dart @@ -12,56 +12,30 @@ import 'package:sembast/sembast.dart'; import '../../../common/controller/global_controller.dart'; import '../../../common/utils/app_utils.dart'; -import '../../../common/utils/custom_types.dart'; import '../../../common/utils/extensions/custom_extensions.dart'; -import '../../../common/utils/extensions/custom_extensions/store_ref_extensions.dart'; +import '../../../common/utils/repository/generic_repository.dart'; import '../domain/box.dart'; part 'week_box_repository.g.dart'; -class WeekBoxRepository { - final Database _db; - late final StoreRef _store; - - WeekBoxRepository(this._db) { - _store = StoreRef("WeekBox"); - } - Stream?> getWeekBoxList() => _store - .query() - .onSnapshots(_db) - .map(AppUtils.convertSnaps(WeekBox.fromJson)); - - Stream getWeekBoxByWeekNumber(int weekNumber) => _store - .record(weekNumber) - .onSnapshot(_db) - .map(AppUtils.convertSnap(WeekBox.fromJson)); +class WeekBoxRepository extends GenericRepository { + WeekBoxRepository(Database db) : super(db, "WeekBox", WeekBox.fromJson); Stream>?> - getWeekBoxListGroupByYearNumber() => _store + getWeekBoxListGroupByYearNumber() => store .query() - .onSnapshots(_db) + .onSnapshots(database) .map(AppUtils.convertSnaps(WeekBox.fromJson)) .map((snaps) => snaps.groupBy((snap) => snap.yearNumber)); - Stream?> getWeekBoxListByYearNumber(int yearNumber) => _store + Stream?> getWeekBoxListByYearNumber(int yearNumber) => store .query( finder: Finder( filter: Filter.equals(WeekBoxFields.yearNumber.name, yearNumber), ), ) - .onSnapshots(_db) + .onSnapshots(database) .map(AppUtils.convertSnaps(WeekBox.fromJson)); - - Future saveAll(List weeks) => - _store.addAllWithKey(_db, weeks, (week) => week.boxNumber); - - Future save(WeekBox week) => - _store.record(week.boxNumber).update(_db, week.toJson()); - - Future> saveInTransaction( - DatabaseClient transaction, List weeks) => - _store.addAllWithKeyAndTransaction( - transaction, weeks, (week) => week.boxNumber); } @riverpod diff --git a/lib/src/features/home/repository/year_box_repository.dart b/lib/src/features/home/repository/year_box_repository.dart index b6c829e..aa0a9c5 100644 --- a/lib/src/features/home/repository/year_box_repository.dart +++ b/lib/src/features/home/repository/year_box_repository.dart @@ -12,49 +12,21 @@ import 'package:sembast/sembast.dart'; import '../../../common/controller/global_controller.dart'; import '../../../common/utils/app_utils.dart'; -import '../../../common/utils/custom_types.dart'; import '../../../common/utils/extensions/custom_extensions.dart'; -import '../../../common/utils/extensions/custom_extensions/store_ref_extensions.dart'; +import '../../../common/utils/repository/generic_repository.dart'; import '../domain/box.dart'; part 'year_box_repository.g.dart'; -class YearBoxRepository { - final Database _db; - late final StoreRef _store; - - YearBoxRepository(this._db) { - _store = StoreRef("YearBox"); - } - Stream?> getYearBoxList() { - return _store - .query() - .onSnapshots(_db) - .map(AppUtils.convertSnaps(YearBox.fromJson)); - } +class YearBoxRepository extends GenericRepository { + YearBoxRepository(Database db) : super(db, "YearBox", YearBox.fromJson); Stream>?> - getYearBoxListGroupByDecadeNumber() => _store + getYearBoxListGroupByDecadeNumber() => store .query() - .onSnapshots(_db) + .onSnapshots(database) .map(AppUtils.convertSnaps(YearBox.fromJson)) - .map((snaps) => snaps.groupBy((snap) => snap.boxNumber ~/ 10)); - - Stream getYearBoxByYearNumber(int yearNumber) => _store - .record(yearNumber) - .onSnapshot(_db) - .map(AppUtils.convertSnap(YearBox.fromJson)); - - Future> saveAll(List years, {DatabaseClient? db}) => - _store.addAllWithKey(_db, years, (year) => year.boxNumber); - - Future save(YearBox year) => - _store.record(year.boxNumber).update(_db, year.toJson()); - - Future> saveInTransaction( - DatabaseClient transaction, List years) => - _store.addAllWithKeyAndTransaction( - transaction, years, (year) => year.boxNumber); + .map((snaps) => snaps.groupBy((snap) => snap.id ~/ 10)); } @riverpod diff --git a/lib/src/features/home/service/home_service.dart b/lib/src/features/home/service/home_service.dart index c7e1db3..96c56a3 100644 --- a/lib/src/features/home/service/home_service.dart +++ b/lib/src/features/home/service/home_service.dart @@ -22,23 +22,28 @@ part 'home_service.g.dart'; class HomeService { final Database db; - late final SetupRepository setupRepository; - late final DayBoxRepository dayBoxRepository; - late final WeekBoxRepository weekBoxRepository; - late final YearBoxRepository yearBoxRepository; - HomeService(Ref ref, this.db) { - dayBoxRepository = ref.read(dayBoxRepositoryProvider); - weekBoxRepository = ref.read(weekBoxRepositoryProvider); - yearBoxRepository = ref.read(yearBoxRepositoryProvider); - setupRepository = ref.read(setupRepositoryProvider); - } + final SetupRepository setupRepository; + final DayBoxRepository dayBoxRepository; + final WeekBoxRepository weekBoxRepository; + final YearBoxRepository yearBoxRepository; + + const HomeService({ + required this.db, + required this.setupRepository, + required this.dayBoxRepository, + required this.weekBoxRepository, + required this.yearBoxRepository, + }); + Future initialSetup(DateTime dob) async { final boxList = await compute(_getBoxesFromDateTime, dob); await db.transaction((tnx) async { - await yearBoxRepository.saveInTransaction(tnx, boxList.years); - await weekBoxRepository.saveInTransaction(tnx, boxList.weeks); - await dayBoxRepository.saveInTransaction(tnx, boxList.days); - await setupRepository.saveDateOfBirthInTransaction(tnx, dob); + await Future.wait([ + yearBoxRepository.saveAll(boxList.years, tnx), + weekBoxRepository.saveAll(boxList.weeks, tnx), + dayBoxRepository.saveAll(boxList.days, tnx), + setupRepository.saveDateOfBirthInTransaction(tnx, dob), + ]); }); } @@ -49,7 +54,7 @@ class HomeService { List dayBoxList = []; for (int year = 0; year < Constants.lifeInYears; year++) { yearBoxList.add(YearBox( - boxNumber: year, + id: year, startDate: dob.add(Duration(days: year * Constants.daysInYear)), endDate: dob.add(Duration(days: (year + 1) * Constants.daysInYear - 1)), boxStatus: BoxStatus.unknown, @@ -62,7 +67,7 @@ class HomeService { final weekEndDay = (week + 1) * Constants.daysInWeek - 1; weekBoxList.add(WeekBox( - boxNumber: week, + id: week, startDate: dob.add(Duration(days: weekStartDay)), endDate: dob.add(Duration(days: weekEndDay)), boxStatus: BoxStatus.unknown, @@ -74,7 +79,7 @@ class HomeService { day++) { dayBoxList.add( DayBox( - boxNumber: day, + id: day, date: dob.add(Duration(days: day)), boxStatus: BoxStatus.unknown, weekNumber: week, @@ -89,4 +94,10 @@ class HomeService { } @riverpod -HomeService homeService(Ref ref) => HomeService(ref, ref.watch(dbProvider)); +HomeService homeService(Ref ref) => HomeService( + db: ref.watch(dbProvider), + dayBoxRepository: ref.watch(dayBoxRepositoryProvider), + weekBoxRepository: ref.watch(weekBoxRepositoryProvider), + yearBoxRepository: ref.watch(yearBoxRepositoryProvider), + setupRepository: ref.watch(setupRepositoryProvider), + ); diff --git a/lib/src/features/home/service/home_service.g.dart b/lib/src/features/home/service/home_service.g.dart index f819f61..d2a8d85 100644 --- a/lib/src/features/home/service/home_service.g.dart +++ b/lib/src/features/home/service/home_service.g.dart @@ -6,7 +6,7 @@ part of 'home_service.dart'; // RiverpodGenerator // ************************************************************************** -String _$homeServiceHash() => r'25540b333b21cdce371a3f38d9ce0c319629ae38'; +String _$homeServiceHash() => r'b9f6608435ab987cd9fcf12ade3f4e20375f7a26'; /// See also [homeService]. @ProviderFor(homeService) diff --git a/lib/src/features/notes/domain/message.dart b/lib/src/features/notes/domain/message.dart index ca02a11..14e21ee 100644 --- a/lib/src/features/notes/domain/message.dart +++ b/lib/src/features/notes/domain/message.dart @@ -6,16 +6,16 @@ import 'package:freezed_annotation/freezed_annotation.dart'; +import '../../../common/constants/enums/generic_entity.dart'; import '../../../common/utils/converter/epoch_date_time_converter.dart'; -import '../../../common/utils/json_abstract.dart'; part 'message.freezed.dart'; part 'message.g.dart'; @freezed -class Message with _$Message implements JsonAbstract { +class Message with _$Message implements GenericEntity { factory Message({ - required String recordId, + @JsonKey(name: 'recordId') required String id, required String? groupId, @EpochDateTimeConverter() required DateTime dateCreated, @EpochDateTimeConverter() required DateTime lastModified, diff --git a/lib/src/features/notes/domain/message.freezed.dart b/lib/src/features/notes/domain/message.freezed.dart index 9c18eb4..a4c6902 100644 --- a/lib/src/features/notes/domain/message.freezed.dart +++ b/lib/src/features/notes/domain/message.freezed.dart @@ -20,7 +20,8 @@ Message _$MessageFromJson(Map json) { /// @nodoc mixin _$Message { - String get recordId => throw _privateConstructorUsedError; + @JsonKey(name: 'recordId') + String get id => throw _privateConstructorUsedError; String? get groupId => throw _privateConstructorUsedError; @EpochDateTimeConverter() DateTime get dateCreated => throw _privateConstructorUsedError; @@ -43,7 +44,7 @@ abstract class $MessageCopyWith<$Res> { _$MessageCopyWithImpl<$Res, Message>; @useResult $Res call( - {String recordId, + {@JsonKey(name: 'recordId') String id, String? groupId, @EpochDateTimeConverter() DateTime dateCreated, @EpochDateTimeConverter() DateTime lastModified, @@ -65,16 +66,16 @@ class _$MessageCopyWithImpl<$Res, $Val extends Message> @pragma('vm:prefer-inline') @override $Res call({ - Object? recordId = null, + Object? id = null, Object? groupId = freezed, Object? dateCreated = null, Object? lastModified = null, Object? message = null, }) { return _then(_value.copyWith( - recordId: null == recordId - ? _value.recordId - : recordId // ignore: cast_nullable_to_non_nullable + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable as String, groupId: freezed == groupId ? _value.groupId @@ -104,7 +105,7 @@ abstract class _$$MessageImplCopyWith<$Res> implements $MessageCopyWith<$Res> { @override @useResult $Res call( - {String recordId, + {@JsonKey(name: 'recordId') String id, String? groupId, @EpochDateTimeConverter() DateTime dateCreated, @EpochDateTimeConverter() DateTime lastModified, @@ -124,16 +125,16 @@ class __$$MessageImplCopyWithImpl<$Res> @pragma('vm:prefer-inline') @override $Res call({ - Object? recordId = null, + Object? id = null, Object? groupId = freezed, Object? dateCreated = null, Object? lastModified = null, Object? message = null, }) { return _then(_$MessageImpl( - recordId: null == recordId - ? _value.recordId - : recordId // ignore: cast_nullable_to_non_nullable + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable as String, groupId: freezed == groupId ? _value.groupId @@ -159,7 +160,7 @@ class __$$MessageImplCopyWithImpl<$Res> @JsonSerializable() class _$MessageImpl implements _Message { _$MessageImpl( - {required this.recordId, + {@JsonKey(name: 'recordId') required this.id, required this.groupId, @EpochDateTimeConverter() required this.dateCreated, @EpochDateTimeConverter() required this.lastModified, @@ -169,7 +170,8 @@ class _$MessageImpl implements _Message { _$$MessageImplFromJson(json); @override - final String recordId; + @JsonKey(name: 'recordId') + final String id; @override final String? groupId; @override @@ -183,7 +185,7 @@ class _$MessageImpl implements _Message { @override String toString() { - return 'Message(recordId: $recordId, groupId: $groupId, dateCreated: $dateCreated, lastModified: $lastModified, message: $message)'; + return 'Message(id: $id, groupId: $groupId, dateCreated: $dateCreated, lastModified: $lastModified, message: $message)'; } @override @@ -191,8 +193,7 @@ class _$MessageImpl implements _Message { return identical(this, other) || (other.runtimeType == runtimeType && other is _$MessageImpl && - (identical(other.recordId, recordId) || - other.recordId == recordId) && + (identical(other.id, id) || other.id == id) && (identical(other.groupId, groupId) || other.groupId == groupId) && (identical(other.dateCreated, dateCreated) || other.dateCreated == dateCreated) && @@ -203,8 +204,8 @@ class _$MessageImpl implements _Message { @JsonKey(includeFromJson: false, includeToJson: false) @override - int get hashCode => Object.hash( - runtimeType, recordId, groupId, dateCreated, lastModified, message); + int get hashCode => + Object.hash(runtimeType, id, groupId, dateCreated, lastModified, message); /// Create a copy of Message /// with the given fields replaced by the non-null parameter values. @@ -224,7 +225,7 @@ class _$MessageImpl implements _Message { abstract class _Message implements Message { factory _Message( - {required final String recordId, + {@JsonKey(name: 'recordId') required final String id, required final String? groupId, @EpochDateTimeConverter() required final DateTime dateCreated, @EpochDateTimeConverter() required final DateTime lastModified, @@ -233,7 +234,8 @@ abstract class _Message implements Message { factory _Message.fromJson(Map json) = _$MessageImpl.fromJson; @override - String get recordId; + @JsonKey(name: 'recordId') + String get id; @override String? get groupId; @override diff --git a/lib/src/features/notes/domain/message.g.dart b/lib/src/features/notes/domain/message.g.dart index f789ecd..a2c3535 100644 --- a/lib/src/features/notes/domain/message.g.dart +++ b/lib/src/features/notes/domain/message.g.dart @@ -8,7 +8,7 @@ part of 'message.dart'; _$MessageImpl _$$MessageImplFromJson(Map json) => _$MessageImpl( - recordId: json['recordId'] as String, + id: json['recordId'] as String, groupId: json['groupId'] as String?, dateCreated: const EpochDateTimeConverter() .fromJson((json['dateCreated'] as num).toInt()), @@ -19,7 +19,7 @@ _$MessageImpl _$$MessageImplFromJson(Map json) => Map _$$MessageImplToJson(_$MessageImpl instance) => { - 'recordId': instance.recordId, + 'recordId': instance.id, 'groupId': instance.groupId, 'dateCreated': const EpochDateTimeConverter().toJson(instance.dateCreated), diff --git a/lib/src/features/notes/presentation/widget/box_chat_title.dart b/lib/src/features/notes/presentation/widget/box_chat_title.dart index 6fa2558..f29307f 100644 --- a/lib/src/features/notes/presentation/widget/box_chat_title.dart +++ b/lib/src/features/notes/presentation/widget/box_chat_title.dart @@ -51,7 +51,7 @@ class _DayBoxChatTitle extends ConsumerWidget { .save(box.copyWith(hasLandMark: value)), ), title: Text( - context.l10n.dayN((box.boxNumber + 1).formatNumber()!), + context.l10n.dayN((box.id + 1).formatNumber()!), style: TextStyle(fontWeight: FontWeight.w600), ), subtitle: Text(box.date.toDateString), @@ -75,7 +75,7 @@ class _WeekBoxChatTitle extends ConsumerWidget { .save(box.copyWith(hasLandMark: value)), ), title: Text( - context.l10n.weekN((box.boxNumber + 1).formatNumber()!), + context.l10n.weekN((box.id + 1).formatNumber()!), style: TextStyle(fontWeight: FontWeight.w600), ), subtitle: Text(AppUtils.formatDates(box.startDate, box.endDate)), @@ -99,7 +99,7 @@ class _YearBoxChatTitle extends ConsumerWidget { .save(box.copyWith(hasLandMark: value)), ), title: Text( - context.l10n.yearN((box.boxNumber + 1).formatNumber()!), + context.l10n.yearN((box.id + 1).formatNumber()!), style: TextStyle(fontWeight: FontWeight.w600), ), subtitle: Text(AppUtils.formatDates(box.startDate, box.endDate)), diff --git a/lib/src/features/notes/presentation/widget/chat_screen.dart b/lib/src/features/notes/presentation/widget/chat_screen.dart index 3648ccf..8c7ea63 100644 --- a/lib/src/features/notes/presentation/widget/chat_screen.dart +++ b/lib/src/features/notes/presentation/widget/chat_screen.dart @@ -72,8 +72,7 @@ class ChatScreen extends HookConsumerWidget { StringBuffer message = StringBuffer(); for (var msgId in selectedMsgSet.value) { String? newMsg = asyncMessageList.valueOrNull - .firstWhereOrNull( - (msg) => msgId == msg.message.recordId) + .firstWhereOrNull((msg) => msgId == msg.message.id) ?.message .message; if (newMsg.isNotBlank) { @@ -110,7 +109,9 @@ class ChatScreen extends HookConsumerWidget { ), ), SendTextField( - onSend: ref.read(messageRepositoryProvider(box.chatGroupId)).save, + onSend: ref + .read(messageRepositoryProvider(box.chatGroupId)) + .saveMessage, ), Gap(8), ], diff --git a/lib/src/features/notes/presentation/widget/conversation_view.dart b/lib/src/features/notes/presentation/widget/conversation_view.dart index 7506c8b..870233f 100644 --- a/lib/src/features/notes/presentation/widget/conversation_view.dart +++ b/lib/src/features/notes/presentation/widget/conversation_view.dart @@ -61,10 +61,10 @@ class ConversationView extends HookConsumerWidget { child: MessageCard( processedMessage: processedMessageData, anySelected: selectedMessages.isNotBlank, - isSelected: selectedMessages - .contains(processedMessageData.message.recordId), + isSelected: + selectedMessages.contains(processedMessageData.message.id), onToggleSelection: () => - onToggleSelection(processedMessageData.message.recordId), + onToggleSelection(processedMessageData.message.id), ), wrap: (child) => Column( mainAxisSize: MainAxisSize.min, diff --git a/lib/src/features/notes/repository/message_repository.dart b/lib/src/features/notes/repository/message_repository.dart index 5723db2..da638f7 100644 --- a/lib/src/features/notes/repository/message_repository.dart +++ b/lib/src/features/notes/repository/message_repository.dart @@ -10,40 +10,37 @@ import 'package:sembast/sembast.dart'; import '../../../common/controller/global_controller.dart'; import '../../../common/utils/app_utils.dart'; -import '../../../common/utils/custom_types.dart'; +import '../../../common/utils/repository/generic_repository.dart'; import '../domain/message.dart'; part 'message_repository.g.dart'; -class DayBoxRepository { - final Database _db; +class DayBoxRepository extends GenericRepository { final String groupId; - late final StoreRef _store; - - DayBoxRepository(this._db, this.groupId) { - _store = StoreRef(groupId); - } - - Future save(String text) async { - String recordId = await _store.generateKey(_db); - DateTime now = DateTime.now(); - Message message = Message( - recordId: recordId, - groupId: groupId, - dateCreated: now, - lastModified: now, - message: text, - ); - await _store.record(recordId).add(_db, message.toJson()); - } - - Stream?> getMessageList() => _store + + DayBoxRepository(Database db, this.groupId) + : super(db, groupId, Message.fromJson); + + Future saveMessage(String text) => database.transaction((txn) async { + String recordId = await store.generateKey(txn); + DateTime now = DateTime.now(); + Message message = Message( + id: recordId, + groupId: groupId, + dateCreated: now, + lastModified: now, + message: text, + ); + await save(message, txn); + }); + + Stream?> getMessageList() => store .query(finder: Finder(sortOrders: [SortOrder(Field.key, false)])) - .onSnapshots(_db) + .onSnapshots(database) .map(AppUtils.convertSnaps(Message.fromJson)); Future deleteAll(Set recordIds) => - _store.records(recordIds).delete(_db); + store.records(recordIds).delete(database); } @riverpod diff --git a/macos/Podfile.lock b/macos/Podfile.lock index a453453..55707d8 100644 --- a/macos/Podfile.lock +++ b/macos/Podfile.lock @@ -32,9 +32,9 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24 - package_info_plus: f5790acc797bf17c3e959e9d6cf162cc68ff7523 + package_info_plus: 12f1c5c2cfe8727ca46cbd0b26677728972d9a5b path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46 - sqflite_darwin: a553b1fd6fe66f53bbb0fe5b4f5bab93f08d7a13 + sqflite_darwin: 5a7236e3b501866c1c9befc6771dfd73ffb8702d url_launcher_macos: c82c93949963e55b228a30115bd219499a6fe404 PODFILE CHECKSUM: 9ebaf0ce3d369aaa26a9ea0e159195ed94724cf3 diff --git a/pubspec.lock b/pubspec.lock index 9bf9bc3..ddb8ce5 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -380,10 +380,10 @@ packages: dependency: "direct main" description: name: flutter_native_splash - sha256: ee5c9bd2b74ea8676442fd4ab876b5d41681df49276488854d6c81a5377c0ef1 + sha256: "1152ab0067ca5a2ebeb862fe0a762057202cceb22b7e62692dcbabf6483891bb" url: "https://pub.dev" source: hosted - version: "2.4.2" + version: "2.4.3" flutter_riverpod: dependency: transitive description: @@ -462,10 +462,10 @@ packages: dependency: "direct main" description: name: go_router - sha256: ce89c5a993ca5eea74535f798478502c30a625ecb10a1de4d7fef5cd1bcac2a4 + sha256: "8ae664a70174163b9f65ea68dd8673e29db8f9095de7b5cd00e167c621f4fef5" url: "https://pub.dev" source: hosted - version: "14.4.1" + version: "14.6.0" go_router_builder: dependency: "direct dev" description: @@ -598,10 +598,10 @@ packages: dependency: "direct dev" description: name: json_serializable - sha256: ea1432d167339ea9b5bb153f0571d0039607a873d6e04e0117af043f14a1fd4b + sha256: c2fcb3920cf2b6ae6845954186420fca40bc0a8abcc84903b7801f17d7050d7c url: "https://pub.dev" source: hosted - version: "6.8.0" + version: "6.9.0" leak_tracker: dependency: transitive description: @@ -838,10 +838,10 @@ packages: dependency: transitive description: name: riverpod_analyzer_utils - sha256: dc53a659cb543b203cdc35cd4e942ed08ea893eb6ef12029301323bdf18c5d95 + sha256: c6b8222b2b483cb87ae77ad147d6408f400c64f060df7a225b127f4afef4f8c8 url: "https://pub.dev" source: hosted - version: "0.5.7" + version: "0.5.8" riverpod_annotation: dependency: "direct main" description: @@ -854,18 +854,18 @@ packages: dependency: "direct dev" description: name: riverpod_generator - sha256: "54458dac2fea976990dc9ed379060db6ae5c8790143f1963fedd0fb99980a326" + sha256: "63546d70952015f0981361636bf8f356d9cfd9d7f6f0815e3c07789a41233188" url: "https://pub.dev" source: hosted - version: "2.6.2" + version: "2.6.3" riverpod_lint: dependency: "direct dev" description: name: riverpod_lint - sha256: "326efc199b87f21053b9a2afbf2aea26c41b3bf6f8ba346ce69126ee17d16ebd" + sha256: "83e4caa337a9840469b7b9bd8c2351ce85abad80f570d84146911b32086fbd99" url: "https://pub.dev" source: hosted - version: "2.6.2" + version: "2.6.3" rxdart: dependency: transitive description: @@ -979,26 +979,26 @@ packages: dependency: transitive description: name: sqflite_common - sha256: "4468b24876d673418a7b7147e5a08a715b4998a7ae69227acafaab762e0e5490" + sha256: "761b9740ecbd4d3e66b8916d784e581861fd3c3553eda85e167bc49fdb68f709" url: "https://pub.dev" source: hosted - version: "2.5.4+5" + version: "2.5.4+6" sqflite_common_ffi: dependency: transitive description: name: sqflite_common_ffi - sha256: d316908f1537725427ff2827a5c5f3b2c1bc311caed985fe3c9b10939c9e11ca + sha256: b8ba78c1b72a9ee6c2323b06af95d43fd13e03d90c8369cb454fd7f629a72588 url: "https://pub.dev" source: hosted - version: "2.3.4" + version: "2.3.4+3" sqflite_common_ffi_web: dependency: transitive description: name: sqflite_common_ffi_web - sha256: f540ad769e5fd31aabe77bfa6e774fdd36145a83e33cdc39239f310c5f8559c3 + sha256: "61ea702e7aba727f28be7ead00b84c19c745cd4a4934d0c41473303df11ac9ea" url: "https://pub.dev" source: hosted - version: "0.4.5+3" + version: "0.4.5+4" sqflite_darwin: dependency: transitive description: @@ -1076,7 +1076,7 @@ packages: description: path: app_sembast ref: dart3a - resolved-ref: "15cf0c194714c37e36c402f7d24aa7c3d02a3965" + resolved-ref: "78da29a5df1b1ab8ec20124425f7372f42dc3af3" url: "https://github.com/tekartik/app_flutter_utils.dart" source: git version: "0.5.1" @@ -1085,7 +1085,7 @@ packages: description: path: app_sqflite ref: dart3a - resolved-ref: "15cf0c194714c37e36c402f7d24aa7c3d02a3965" + resolved-ref: "78da29a5df1b1ab8ec20124425f7372f42dc3af3" url: "https://github.com/tekartik/app_flutter_utils.dart" source: git version: "0.6.1" @@ -1127,10 +1127,10 @@ packages: dependency: transitive description: name: time - sha256: ad8e018a6c9db36cb917a031853a1aae49467a93e0d464683e029537d848c221 + sha256: "370572cf5d1e58adcb3e354c47515da3f7469dac3a95b447117e728e7be6f461" url: "https://pub.dev" source: hosted - version: "2.1.4" + version: "2.1.5" timing: dependency: transitive description: @@ -1183,10 +1183,10 @@ packages: dependency: transitive description: name: url_launcher_linux - sha256: e2b9622b4007f97f504cd64c0128309dfb978ae66adbe944125ed9e1750f06af + sha256: "4e9ba368772369e3e08f231d2301b4ef72b9ff87c31192ef471b380ef29a4935" url: "https://pub.dev" source: hosted - version: "3.2.0" + version: "3.2.1" url_launcher_macos: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index a51d4c7..b480b8a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: yliw description: "A new Flutter project." publish_to: "none" -version: 0.1.3 +version: 0.1.4 environment: sdk: ^3.5.4