From 730da61f485ed40f45d55943f02ea92f53942b42 Mon Sep 17 00:00:00 2001 From: zin- Date: Mon, 16 Sep 2024 15:39:45 +0900 Subject: [PATCH] drop: Mem v1 drop: EntityV1 --- .../habit/repeat_by_n_day_habit_scenario.dart | 885 +++++++++--------- lib/act_counter/act_counter.dart | 8 +- lib/act_counter/act_counter_client.dart | 4 +- .../single_selectable_mem_list_item.dart | 4 +- lib/acts/list/act_list.dart | 11 +- lib/acts/list/item/builder.dart | 4 +- lib/acts/list/item/total_act_time_item.dart | 4 +- .../created_and_updated_at_texts.dart | 6 +- lib/components/mem/list/actions.dart | 14 +- lib/components/mem/list/states.dart | 32 +- lib/components/mem/list/view.dart | 4 +- lib/components/mem/mem_done_checkbox.dart | 8 +- lib/components/mem/mem_name.dart | 4 +- lib/core/mem.dart | 84 +- lib/core/mem_detail.dart | 4 +- .../repository/database_tuple_entity.dart | 29 +- lib/framework/repository/entity.dart | 2 - lib/mems/actions.dart | 4 +- lib/mems/detail/actions.dart | 39 +- lib/mems/detail/body.dart | 6 +- lib/mems/detail/page.dart | 8 +- lib/mems/detail/states.dart | 13 +- lib/mems/list/item/actions.dart | 33 +- lib/mems/list/item/view.dart | 6 +- lib/mems/mem_client.dart | 25 +- lib/mems/mem_service.dart | 34 +- lib/mems/states.dart | 24 +- lib/notifications/mem_notifications.dart | 4 +- lib/notifications/notification_client.dart | 17 +- lib/repositories/mem.dart | 19 - lib/repositories/mem_entity.dart | 61 +- test/act_counter/act_counter_test.dart | 6 +- test/mems/mem_list_body_test.dart | 8 +- 33 files changed, 633 insertions(+), 781 deletions(-) delete mode 100644 lib/repositories/mem.dart diff --git a/integration_test/scenarios/habit/repeat_by_n_day_habit_scenario.dart b/integration_test/scenarios/habit/repeat_by_n_day_habit_scenario.dart index 59d274e5c..108ea350b 100644 --- a/integration_test/scenarios/habit/repeat_by_n_day_habit_scenario.dart +++ b/integration_test/scenarios/habit/repeat_by_n_day_habit_scenario.dart @@ -25,146 +25,138 @@ import '../helpers.dart'; const _name = 'Repeat by n day habit scenario'; -void main() => group( - _name, - () { - const insertedMemName = "$_name - mem name - inserted"; - const insertedMemRepeatByNDay = 2; - const withoutActMemName = "$insertedMemName - without act"; - const withOldActMemName = "$insertedMemName - with old act"; - const withCurrentActMemName = "$insertedMemName - with current act"; - const insertedRepeatNotificationMessage = - "$_name - inserted - mem notification message - repeat"; - - late final DatabaseAccessor dbA; - - int insertedMemId = 0; - int withoutActMemId = 0; - int withOldActMemId = 0; - int withCurrentActMemId = 0; - - setUpAll(() async { - dbA = await openTestDatabase(databaseDefinition); - - await clearAllTestDatabaseRows(databaseDefinition); - - insertedMemId = await dbA.insert( - defTableMems, - { - defColMemsName.name: insertedMemName, - defColCreatedAt.name: zeroDate, - }, - ); - await dbA.insert( - defTableMemNotifications, - { - defFkMemNotificationsMemId.name: insertedMemId, - defColMemNotificationsType.name: MemNotificationType.repeat.name, - defColMemNotificationsTime.name: 1, - defColMemNotificationsMessage.name: "never", - defColCreatedAt.name: zeroDate, - }, - ); - await dbA.insert( - defTableMemNotifications, - { - defFkMemNotificationsMemId.name: insertedMemId, - defColMemNotificationsType.name: - MemNotificationType.repeatByNDay.name, - defColMemNotificationsTime.name: insertedMemRepeatByNDay, - defColMemNotificationsMessage.name: "never", - defColCreatedAt.name: zeroDate, - }, - ); - - withoutActMemId = await dbA.insert(defTableMems, { - defColMemsName.name: withoutActMemName, - defColMemsDoneAt.name: null, +void main() => group(': $_name', () { + const insertedMemName = "$_name - mem name - inserted"; + const insertedMemRepeatByNDay = 2; + const withoutActMemName = "$insertedMemName - without act"; + const withOldActMemName = "$insertedMemName - with old act"; + const withCurrentActMemName = "$insertedMemName - with current act"; + const insertedRepeatNotificationMessage = + "$_name - inserted - mem notification message - repeat"; + + late final DatabaseAccessor dbA; + + int insertedMemId = 0; + int withoutActMemId = 0; + int withOldActMemId = 0; + int withCurrentActMemId = 0; + + setUpAll(() async { + dbA = await openTestDatabase(databaseDefinition); + + await clearAllTestDatabaseRows(databaseDefinition); + + insertedMemId = await dbA.insert( + defTableMems, + { + defColMemsName.name: insertedMemName, defColCreatedAt.name: zeroDate, - }); - withOldActMemId = await dbA.insert(defTableMems, { - defColMemsName.name: withOldActMemName, - defColMemsDoneAt.name: null, - defColCreatedAt.name: zeroDate, - }); - withCurrentActMemId = await dbA.insert(defTableMems, { - defColMemsName.name: withCurrentActMemName, - defColMemsDoneAt.name: null, + }, + ); + await dbA.insert( + defTableMemNotifications, + { + defFkMemNotificationsMemId.name: insertedMemId, + defColMemNotificationsType.name: MemNotificationType.repeat.name, + defColMemNotificationsTime.name: 1, + defColMemNotificationsMessage.name: "never", defColCreatedAt.name: zeroDate, - }); + }, + ); + await dbA.insert(defTableMemNotifications, { + defFkMemNotificationsMemId.name: insertedMemId, + defColMemNotificationsType.name: + MemNotificationType.repeatByNDay.name, + defColMemNotificationsTime.name: insertedMemRepeatByNDay, + defColMemNotificationsMessage.name: "never", + defColCreatedAt.name: zeroDate + }); - [withoutActMemId, withOldActMemId, withCurrentActMemId] - .forEachIndexed((index, insertedMemId) async { - await dbA.insert(defTableMemNotifications, { - defFkMemNotificationsMemId.name: insertedMemId, - defColMemNotificationsTime.name: 0, - defColMemNotificationsType.name: MemNotificationType.repeat.name, - defColMemNotificationsMessage.name: - insertedRepeatNotificationMessage, - defColCreatedAt.name: zeroDate, - }); - await dbA.insert(defTableMemNotifications, { - defFkMemNotificationsMemId.name: insertedMemId, - defColMemNotificationsTime.name: - index + insertedMemRepeatByNDay + 1, - defColMemNotificationsType.name: - MemNotificationType.repeatByNDay.name, - defColMemNotificationsMessage.name: - "$_name - inserted - mem notification message - after act started", - defColCreatedAt.name: zeroDate, - }); - }); + withoutActMemId = await dbA.insert(defTableMems, { + defColMemsName.name: withoutActMemName, + defColMemsDoneAt.name: null, + defColCreatedAt.name: zeroDate, + }); + withOldActMemId = await dbA.insert(defTableMems, { + defColMemsName.name: withOldActMemName, + defColMemsDoneAt.name: null, + defColCreatedAt.name: zeroDate + }); + withCurrentActMemId = await dbA.insert(defTableMems, { + defColMemsName.name: withCurrentActMemName, + defColMemsDoneAt.name: null, + defColCreatedAt.name: zeroDate + }); - await dbA.insert(defTableActs, { - defFkActsMemId.name: withOldActMemId, - defColActsStart.name: zeroDate.toIso8601String(), - defColActsStartIsAllDay.name: 0, - defColCreatedAt.name: zeroDate, + [withoutActMemId, withOldActMemId, withCurrentActMemId] + .forEachIndexed((index, insertedMemId) async { + await dbA.insert(defTableMemNotifications, { + defFkMemNotificationsMemId.name: insertedMemId, + defColMemNotificationsTime.name: 0, + defColMemNotificationsType.name: MemNotificationType.repeat.name, + defColMemNotificationsMessage.name: + insertedRepeatNotificationMessage, + defColCreatedAt.name: zeroDate }); - await dbA.insert(defTableActs, { - defFkActsMemId.name: withCurrentActMemId, - defColActsStart.name: DateTime.now().toIso8601String(), - defColActsStartIsAllDay.name: 0, - defColCreatedAt.name: zeroDate, + await dbA.insert(defTableMemNotifications, { + defFkMemNotificationsMemId.name: insertedMemId, + defColMemNotificationsTime.name: + index + insertedMemRepeatByNDay + 1, + defColMemNotificationsType.name: + MemNotificationType.repeatByNDay.name, + defColMemNotificationsMessage.name: + "$_name - inserted - mem notification message - after act started", + defColCreatedAt.name: zeroDate }); }); - setUp(() async { - NotificationClient.resetSingleton(); + await dbA.insert(defTableActs, { + defFkActsMemId.name: withOldActMemId, + defColActsStart.name: zeroDate.toIso8601String(), + defColActsStartIsAllDay.name: 0, + defColCreatedAt.name: zeroDate }); + await dbA.insert(defTableActs, { + defFkActsMemId.name: withCurrentActMemId, + defColActsStart.name: DateTime.now().toIso8601String(), + defColActsStartIsAllDay.name: 0, + defColCreatedAt.name: zeroDate + }); + }); - testWidgets( - 'show saved.', - (widgetTester) async { - widgetTester.ignoreMockMethodCallHandler( - MethodChannelMock.flutterLocalNotifications); + setUp(() async { + NotificationClient.resetSingleton(); + }); - const repeatText = "12:00 AM every $insertedMemRepeatByNDay days"; + testWidgets( + 'show saved.', + (widgetTester) async { + widgetTester.ignoreMockMethodCallHandler( + MethodChannelMock.flutterLocalNotifications); - await runApplication(); - await widgetTester.pumpAndSettle(defaultTransitionDuration); + const repeatText = "12:00 AM every $insertedMemRepeatByNDay days"; - expect(find.text(repeatText), findsOneWidget); - await widgetTester.tap(find.text(insertedMemName)); - await widgetTester.pumpAndSettle(defaultTransitionDuration); + await runApplication(); + await widgetTester.pumpAndSettle(defaultTransitionDuration); - expect( - widgetTester - .widget(find.descendant( - of: find.byKey(keyMemNotificationsView), - matching: find.byType(Text))) - .data, - repeatText); - - await widgetTester.tap( - find.descendant( - of: find.byKey(keyMemNotificationsView), - matching: find.byIcon(Icons.edit), - ), - ); - await widgetTester.pumpAndSettle(defaultTransitionDuration); + expect(find.text(repeatText), findsOneWidget); + await widgetTester.tap(find.text(insertedMemName)); + await widgetTester.pumpAndSettle(defaultTransitionDuration); - expect( + expect( + widgetTester + .widget(find.descendant( + of: find.byKey(keyMemNotificationsView), + matching: find.byType(Text))) + .data, + repeatText); + + await widgetTester.tap(find.descendant( + of: find.byKey(keyMemNotificationsView), + matching: find.byIcon(Icons.edit))); + await widgetTester.pumpAndSettle(defaultTransitionDuration); + + expect( widgetTester .widget( find.descendant( @@ -173,348 +165,311 @@ void main() => group( ), ) .initialValue, - insertedMemRepeatByNDay.toString(), - ); + insertedMemRepeatByNDay.toString()); + }, + ); + + testWidgets('save.', (widgetTester) async { + widgetTester.ignoreMockMethodCallHandler( + MethodChannelMock.flutterLocalNotifications); + + final testStart = DateTime.now(); + var expectedSavedMemId = + ((await dbA.select(defTableMems, orderBy: "id DESC", limit: 1)) + .single[defPkId.name] as int) + + 1; + + int checkPermissionStatusCount = 0; + widgetTester.setMockMethodCallHandler( + MethodChannelMock.permissionHandler, + List.generate( + 3, + (i) => (m) async { + expect(m.method, 'checkPermissionStatus'); + checkPermissionStatusCount++; + return 1; + })); + int alarmServiceStartCount = 0; + int alarmCancelCount = 0; + int alarmPeriodicCount = 0; + widgetTester + .setMockMethodCallHandler(MethodChannelMock.androidAlarmManager, [ + (message) async { + expect(message.method, equals('AlarmService.start')); + expect(message.arguments, orderedEquals([isNotNull])); + alarmServiceStartCount++; + return true; }, - ); + (message) async { + expect(message.method, equals('Alarm.cancel')); + expect( + message.arguments, + orderedEquals( + [equals(memStartNotificationId(expectedSavedMemId))])); + alarmCancelCount++; + return false; + }, + (message) async { + expect(message.method, equals('Alarm.cancel')); + expect( + message.arguments, + orderedEquals( + [equals(memEndNotificationId(expectedSavedMemId))])); + alarmCancelCount++; + return false; + }, + (message) async { + expect(message.method, equals('Alarm.periodic')); + expect(message.arguments[0], + equals(memRepeatedNotificationId(expectedSavedMemId))); + expect(message.arguments[1], isFalse); + expect(message.arguments[2], isFalse); + expect(message.arguments[3], isFalse); + expect( + DateTime.fromMillisecondsSinceEpoch(message.arguments[4]), + equals(testStart + .copyWith( + hour: defaultStartOfDay.hour, + minute: defaultStartOfDay.minute, + second: 0, + millisecond: 0, + microsecond: 0) + .add(const Duration(days: 1)))); + expect( + message.arguments[5], const Duration(days: 1).inMilliseconds); + expect(message.arguments[6], isFalse); + expect(message.arguments[7], isNotNull); + expect( + message.arguments[8], + equals({ + memIdKey: expectedSavedMemId, + notificationTypeKey: NotificationType.repeat.name + })); + alarmPeriodicCount++; + return false; + }, + ]); + + await runApplication(); + await widgetTester.pumpAndSettle(); + await widgetTester.tap(newMemFabFinder); + await widgetTester.pumpAndSettle(); + const enteringMemName = "$_name: Save - entering - mem name"; + await widgetTester.enterText(find.byKey(keyMemName), enteringMemName); + + final notificationAddFinder = find.descendant( + of: find.byKey(keyMemNotificationsView), + matching: find.byIcon(Icons.notification_add)); + await widgetTester.tap(notificationAddFinder); + await widgetTester.pumpAndSettle(defaultTransitionDuration); + + await widgetTester.tap(timeIconFinder); + await widgetTester.pump(); + await widgetTester.tap(okFinder); + await widgetTester.pump(); + + const enteringNDay = 3; + await widgetTester.enterText( + find.descendant( + of: find.byKey(keyMemRepeatByNDayNotification), + matching: find.byType(TextFormField)), + enteringNDay.toString()); + + await widgetTester.pageBack(); + await widgetTester.pumpAndSettle(); + await widgetTester.tap(find.byKey(keySaveMemFab)); + await widgetTester.pumpAndSettle(); + + final savedMem = (await dbA.select( + defTableMems, + where: "${defColMemsName.name} = ?", + whereArgs: [enteringMemName], + )) + .single; + final savedMemNotification = (await dbA.select(defTableMemNotifications, + where: "${defFkMemNotificationsMemId.name} = ?" + " AND ${defColMemNotificationsType.name} = ?" + " AND ${defColMemNotificationsTime.name} = ?", + whereArgs: [ + savedMem[defPkId.name], + MemNotificationType.repeatByNDay.name, + enteringNDay + ])) + .single; + expect(savedMemNotification[defColMemNotificationsTime.name], + equals(enteringNDay), + reason: 'enteringNDay'); + + if (defaultTargetPlatform == TargetPlatform.android) { + expect(checkPermissionStatusCount, equals(3), + reason: 'checkPermissionStatusCount'); + expect(alarmServiceStartCount, equals(1), + reason: 'alarmServiceStartCount'); + expect(alarmCancelCount, equals(2), reason: 'alarmCancelCount'); + expect(alarmPeriodicCount, equals(1), reason: 'alarmPeriodicCount'); + } else { + expect(checkPermissionStatusCount, equals(3), + reason: 'checkPermissionStatusCount'); + expect(alarmServiceStartCount, equals(0), + reason: 'alarmServiceStartCount'); + expect(alarmCancelCount, equals(0), reason: 'alarmCancelCount'); + expect(alarmPeriodicCount, equals(0), reason: 'alarmPeriodicCount'); + } + + widgetTester.clearAllMockMethodCallHandler(); + }); + + group('notify repeatByNDay', () { + testWidgets('withoutAct.', (widgetTester) async { + int checkPermissionStatusCount = 0; + widgetTester + .setMockMethodCallHandler(MethodChannelMock.permissionHandler, [ + (m) async { + expect(m.method, 'checkPermissionStatus'); + checkPermissionStatusCount++; + return 1; + } + ]); + int initializeCount = 0; + int cancelCount = 0; + int showCount = 0; + widgetTester.setMockMethodCallHandler( + MethodChannelMock.flutterLocalNotifications, [ + (message) async { + expect(message.method, equals('initialize')); + initializeCount++; + return true; + }, + ...[ + memStartNotificationId(withoutActMemId), + memEndNotificationId(withoutActMemId), + pausedActNotificationId(withoutActMemId), + afterActStartedNotificationId(withoutActMemId) + ].map((e) => (message) async { + expect(message.method, equals('cancel')); + expect(message.arguments['id'], equals(e)); + cancelCount++; + return false; + }), + (message) async { + expect(message.method, equals('show')); + expect(message.arguments['id'], + equals(memRepeatedNotificationId(withoutActMemId))); + expect(message.arguments['title'], equals(withoutActMemName)); + expect(message.arguments['body'], + equals(insertedRepeatNotificationMessage)); + expect(message.arguments['payload'], + equals("{\"$memIdKey\":$withoutActMemId}")); + showCount++; + return false; + } + ]); + + final params = { + memIdKey: withoutActMemId, + notificationTypeKey: NotificationType.repeat.name, + }; + + await scheduleCallback(0, params); + + if (defaultTargetPlatform == TargetPlatform.android) { + expect(checkPermissionStatusCount, equals(1), + reason: 'checkPermissionStatusCount'); + expect(initializeCount, equals(1), reason: 'initializeCount'); + expect(cancelCount, equals(4), reason: 'cancelCount'); + expect(showCount, equals(1), reason: 'showCount'); + } else { + expect(checkPermissionStatusCount, equals(1), + reason: 'checkPermissionStatusCount'); + expect(initializeCount, equals(0), reason: 'initializeCount'); + expect(cancelCount, equals(0), reason: 'cancelCount'); + expect(showCount, equals(0), reason: 'showCount'); + } + + widgetTester.clearAllMockMethodCallHandler(); + }); - testWidgets( - 'save.', - (widgetTester) async { - widgetTester.ignoreMockMethodCallHandler( - MethodChannelMock.flutterLocalNotifications); - - final testStart = DateTime.now(); - var expectedSavedMemId = - ((await dbA.select(defTableMems, orderBy: "id DESC", limit: 1)) - .single[defPkId.name] as int) + - 1; - - int checkPermissionStatusCount = 0; - widgetTester.setMockMethodCallHandler( - MethodChannelMock.permissionHandler, - List.generate( - 3, - (i) => (m) async { - expect(m.method, 'checkPermissionStatus'); - checkPermissionStatusCount++; - return 1; - })); - int alarmServiceStartCount = 0; - int alarmCancelCount = 0; - int alarmPeriodicCount = 0; - widgetTester.setMockMethodCallHandler( - MethodChannelMock.androidAlarmManager, [ - (message) async { - expect(message.method, equals('AlarmService.start')); - expect( - message.arguments, - orderedEquals([ - isNotNull, - ])); - alarmServiceStartCount++; - return true; - }, - (message) async { - expect(message.method, equals('Alarm.cancel')); - expect( - message.arguments, - orderedEquals( - [equals(memStartNotificationId(expectedSavedMemId))])); - alarmCancelCount++; - return false; - }, - (message) async { - expect(message.method, equals('Alarm.cancel')); - expect( - message.arguments, - orderedEquals( - [equals(memEndNotificationId(expectedSavedMemId))])); - alarmCancelCount++; - return false; - }, - (message) async { - expect(message.method, equals('Alarm.periodic')); - expect(message.arguments[0], - equals(memRepeatedNotificationId(expectedSavedMemId))); - expect(message.arguments[1], isFalse); - expect(message.arguments[2], isFalse); - expect(message.arguments[3], isFalse); - expect( - DateTime.fromMillisecondsSinceEpoch(message.arguments[4]), - equals(testStart - .copyWith( - hour: defaultStartOfDay.hour, - minute: defaultStartOfDay.minute, - second: 0, - millisecond: 0, - microsecond: 0) - .add(const Duration(days: 1)))); - expect(message.arguments[5], - const Duration(days: 1).inMilliseconds); - expect(message.arguments[6], isFalse); - expect(message.arguments[7], isNotNull); - expect( - message.arguments[8], - equals({ - memIdKey: expectedSavedMemId, - notificationTypeKey: NotificationType.repeat.name - })); - alarmPeriodicCount++; - return false; - }, - ]); - - await runApplication(); - await widgetTester.pumpAndSettle(); - await widgetTester.tap(newMemFabFinder); - await widgetTester.pumpAndSettle(); - const enteringMemName = "$_name: Save - entering - mem name"; - await widgetTester.enterText( - find.byKey(keyMemName), enteringMemName); - - final notificationAddFinder = find.descendant( - of: find.byKey(keyMemNotificationsView), - matching: find.byIcon(Icons.notification_add)); - await widgetTester.tap(notificationAddFinder); - await widgetTester.pumpAndSettle(defaultTransitionDuration); - - await widgetTester.tap(timeIconFinder); - await widgetTester.pump(); - await widgetTester.tap(okFinder); - await widgetTester.pump(); - - const enteringNDay = 3; - await widgetTester.enterText( - find.descendant( - of: find.byKey(keyMemRepeatByNDayNotification), - matching: find.byType(TextFormField)), - enteringNDay.toString(), - ); - - await widgetTester.pageBack(); - await widgetTester.pumpAndSettle(); - await widgetTester.tap(find.byKey(keySaveMemFab)); - await widgetTester.pumpAndSettle(); - - final savedMem = (await dbA.select( - defTableMems, - where: "${defColMemsName.name} = ?", - whereArgs: [enteringMemName], - )) - .single; - final savedMemNotification = (await dbA.select( - defTableMemNotifications, - where: "${defFkMemNotificationsMemId.name} = ?" - " AND ${defColMemNotificationsType.name} = ?" - " AND ${defColMemNotificationsTime.name} = ?", - whereArgs: [ - savedMem[defPkId.name], - MemNotificationType.repeatByNDay.name, - enteringNDay, - ], - )) - .single; - expect(savedMemNotification[defColMemNotificationsTime.name], - equals(enteringNDay), - reason: 'enteringNDay'); - - if (defaultTargetPlatform == TargetPlatform.android) { - expect(checkPermissionStatusCount, equals(3), - reason: 'checkPermissionStatusCount'); - expect(alarmServiceStartCount, equals(1), - reason: 'alarmServiceStartCount'); - expect(alarmCancelCount, equals(2), reason: 'alarmCancelCount'); - expect(alarmPeriodicCount, equals(1), - reason: 'alarmPeriodicCount'); - } else { - expect(checkPermissionStatusCount, equals(3), - reason: 'checkPermissionStatusCount'); - expect(alarmServiceStartCount, equals(0), - reason: 'alarmServiceStartCount'); - expect(alarmCancelCount, equals(0), reason: 'alarmCancelCount'); - expect(alarmPeriodicCount, equals(0), - reason: 'alarmPeriodicCount'); + testWidgets('withOldAct', (widgetTester) async { + int checkPermissionStatusCount = 0; + widgetTester + .setMockMethodCallHandler(MethodChannelMock.permissionHandler, [ + (m) async { + expect(m.method, 'checkPermissionStatus'); + checkPermissionStatusCount++; + return 1; + } + ]); + int initializeCount = 0; + int cancelCount = 0; + int showCount = 0; + widgetTester.setMockMethodCallHandler( + MethodChannelMock.flutterLocalNotifications, [ + (message) async { + expect(message.method, equals('initialize')); + initializeCount++; + return true; + }, + ...[ + memStartNotificationId(withOldActMemId), + memEndNotificationId(withOldActMemId), + pausedActNotificationId(withOldActMemId), + afterActStartedNotificationId(withOldActMemId), + ].map((e) => (message) async { + expect(message.method, equals('cancel')); + expect(message.arguments['id'], equals(e)); + cancelCount++; + return false; + }), + (message) async { + expect(message.method, equals('show')); + expect(message.arguments['id'], + equals(memRepeatedNotificationId(withOldActMemId))); + expect(message.arguments['title'], equals(withOldActMemName)); + expect(message.arguments['body'], + equals(insertedRepeatNotificationMessage)); + expect(message.arguments['payload'], + equals("{\"$memIdKey\":$withOldActMemId}")); + showCount++; + return false; } + ]); + + final params = { + memIdKey: withOldActMemId, + notificationTypeKey: NotificationType.repeat.name + }; + + await scheduleCallback(0, params); + + if (defaultTargetPlatform == TargetPlatform.android) { + expect(checkPermissionStatusCount, equals(1), + reason: 'checkPermissionStatusCount'); + expect(initializeCount, equals(1), reason: 'initializeCount'); + expect(cancelCount, equals(4), reason: 'cancelCount'); + expect(showCount, equals(1), reason: 'showCount'); + } else { + expect(checkPermissionStatusCount, equals(1), + reason: 'checkPermissionStatusCount'); + expect(initializeCount, equals(0), reason: 'initializeCount'); + expect(cancelCount, equals(0), reason: 'cancelCount'); + expect(showCount, equals(0), reason: 'showCount'); + } + + widgetTester.clearAllMockMethodCallHandler(); + }); - widgetTester.clearAllMockMethodCallHandler(); - }, - ); + testWidgets('withCurrentAct', (widgetTester) async { + widgetTester.setMockMethodCallHandler( + MethodChannelMock.flutterLocalNotifications, []); - group( - 'notify repeatByNDay', - () { - testWidgets( - 'withoutAct.', - (widgetTester) async { - int checkPermissionStatusCount = 0; - widgetTester.setMockMethodCallHandler( - MethodChannelMock.permissionHandler, [ - (m) async { - expect(m.method, 'checkPermissionStatus'); - checkPermissionStatusCount++; - return 1; - } - ]); - int initializeCount = 0; - int cancelCount = 0; - int showCount = 0; - widgetTester.setMockMethodCallHandler( - MethodChannelMock.flutterLocalNotifications, - [ - (message) async { - expect(message.method, equals('initialize')); - initializeCount++; - return true; - }, - ...[ - memStartNotificationId(withoutActMemId), - memEndNotificationId(withoutActMemId), - pausedActNotificationId(withoutActMemId), - afterActStartedNotificationId(withoutActMemId), - ].map( - (e) => (message) async { - expect(message.method, equals('cancel')); - expect(message.arguments['id'], equals(e)); - cancelCount++; - return false; - }, - ), - (message) async { - expect(message.method, equals('show')); - expect(message.arguments['id'], - equals(memRepeatedNotificationId(withoutActMemId))); - expect(message.arguments['title'], - equals(withoutActMemName)); - expect(message.arguments['body'], - equals(insertedRepeatNotificationMessage)); - expect(message.arguments['payload'], - equals("{\"$memIdKey\":$withoutActMemId}")); - showCount++; - return false; - }, - ], - ); - - final params = { - memIdKey: withoutActMemId, - notificationTypeKey: NotificationType.repeat.name, - }; - - await scheduleCallback(0, params); - - if (defaultTargetPlatform == TargetPlatform.android) { - expect(checkPermissionStatusCount, equals(1), - reason: 'checkPermissionStatusCount'); - expect(initializeCount, equals(1), reason: 'initializeCount'); - expect(cancelCount, equals(4), reason: 'cancelCount'); - expect(showCount, equals(1), reason: 'showCount'); - } else { - expect(checkPermissionStatusCount, equals(1), - reason: 'checkPermissionStatusCount'); - expect(initializeCount, equals(0), reason: 'initializeCount'); - expect(cancelCount, equals(0), reason: 'cancelCount'); - expect(showCount, equals(0), reason: 'showCount'); - } - - widgetTester.clearAllMockMethodCallHandler(); - }, - ); - - testWidgets( - 'withOldAct', - (widgetTester) async { - int checkPermissionStatusCount = 0; - widgetTester.setMockMethodCallHandler( - MethodChannelMock.permissionHandler, [ - (m) async { - expect(m.method, 'checkPermissionStatus'); - checkPermissionStatusCount++; - return 1; - } - ]); - int initializeCount = 0; - int cancelCount = 0; - int showCount = 0; - widgetTester.setMockMethodCallHandler( - MethodChannelMock.flutterLocalNotifications, - [ - (message) async { - expect(message.method, equals('initialize')); - initializeCount++; - return true; - }, - ...[ - memStartNotificationId(withOldActMemId), - memEndNotificationId(withOldActMemId), - pausedActNotificationId(withOldActMemId), - afterActStartedNotificationId(withOldActMemId), - ].map( - (e) => (message) async { - expect(message.method, equals('cancel')); - expect(message.arguments['id'], equals(e)); - cancelCount++; - return false; - }, - ), - (message) async { - expect(message.method, equals('show')); - expect(message.arguments['id'], - equals(memRepeatedNotificationId(withOldActMemId))); - expect(message.arguments['title'], - equals(withOldActMemName)); - expect(message.arguments['body'], - equals(insertedRepeatNotificationMessage)); - expect(message.arguments['payload'], - equals("{\"$memIdKey\":$withOldActMemId}")); - showCount++; - return false; - }, - ], - ); - - final params = { - memIdKey: withOldActMemId, - notificationTypeKey: NotificationType.repeat.name, - }; - - await scheduleCallback(0, params); - - if (defaultTargetPlatform == TargetPlatform.android) { - expect(checkPermissionStatusCount, equals(1), - reason: 'checkPermissionStatusCount'); - expect(initializeCount, equals(1), reason: 'initializeCount'); - expect(cancelCount, equals(4), reason: 'cancelCount'); - expect(showCount, equals(1), reason: 'showCount'); - } else { - expect(checkPermissionStatusCount, equals(1), - reason: 'checkPermissionStatusCount'); - expect(initializeCount, equals(0), reason: 'initializeCount'); - expect(cancelCount, equals(0), reason: 'cancelCount'); - expect(showCount, equals(0), reason: 'showCount'); - } - - widgetTester.clearAllMockMethodCallHandler(); - }, - ); - - testWidgets( - 'withCurrentAct', - (widgetTester) async { - widgetTester.setMockMethodCallHandler( - MethodChannelMock.flutterLocalNotifications, []); - - final params = { - memIdKey: withCurrentActMemId, - notificationTypeKey: NotificationType.repeat.name, - }; - - await scheduleCallback(0, params); - - widgetTester.clearAllMockMethodCallHandler(); - }, - ); - }, - ); - }, - ); + final params = { + memIdKey: withCurrentActMemId, + notificationTypeKey: NotificationType.repeat.name + }; + + await scheduleCallback(0, params); + + widgetTester.clearAllMockMethodCallHandler(); + }); + }); + }); diff --git a/lib/act_counter/act_counter.dart b/lib/act_counter/act_counter.dart index fdcbf76b3..184de859f 100644 --- a/lib/act_counter/act_counter.dart +++ b/lib/act_counter/act_counter.dart @@ -4,7 +4,7 @@ import 'package:mem/act_counter/home_widget.dart'; import 'package:mem/core/date_and_time/date_and_time.dart'; import 'package:mem/core/date_and_time/date_and_time_period.dart'; import 'package:mem/repositories/act_entity.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; class ActCounter implements HomeWidget { @override @@ -23,8 +23,10 @@ class ActCounter implements HomeWidget { ActCounter(this.memId, this.name, this.actCount, this.updatedAt); - ActCounter.from(SavedMemV1 savedMem, Iterable savedActs) - : memId = savedMem.id, + ActCounter.from( + SavedMemEntity savedMem, + Iterable savedActs, + ) : memId = savedMem.id, name = savedMem.name, actCount = savedActs.length, updatedAt = savedActs diff --git a/lib/act_counter/act_counter_client.dart b/lib/act_counter/act_counter_client.dart index 63fb9d4a0..4469b2a1c 100644 --- a/lib/act_counter/act_counter_client.dart +++ b/lib/act_counter/act_counter_client.dart @@ -18,7 +18,7 @@ class ActCounterClient { () async { await _actCounterRepository.receive( ActCounterEntity.from( - await _memRepository.ship(id: memId).then((v) => v.single.toV1()), + await _memRepository.ship(id: memId).then((v) => v.single), await _actRepository.ship( memId: memId, period: ActCounter.period(DateAndTime.now()), @@ -44,7 +44,7 @@ class ActCounterClient { await _actCounterRepository.replace( ActCounterEntity.from( - await _memRepository.ship(id: memId).then((v) => v.single.toV1()), + await _memRepository.ship(id: memId).then((v) => v.single), await _actRepository.ship( memId: memId, period: ActCounter.period(when), diff --git a/lib/act_counter/single_selectable_mem_list_item.dart b/lib/act_counter/single_selectable_mem_list_item.dart index b270d0116..2d2d416e3 100644 --- a/lib/act_counter/single_selectable_mem_list_item.dart +++ b/lib/act_counter/single_selectable_mem_list_item.dart @@ -4,7 +4,7 @@ import 'package:mem/components/mem/list/states.dart'; import 'package:mem/components/mem/mem_name.dart'; import 'package:mem/components/mem/mem_period.dart'; import 'package:mem/logger/log_service.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'states.dart'; @@ -31,7 +31,7 @@ class SingleSelectableMemListItem extends ConsumerWidget { class _SingleSelectableMemListItemComponent extends ListTile { _SingleSelectableMemListItemComponent( - SavedMemV1 mem, + SavedMemEntity mem, bool isSelected, void Function(int? memId) onSelected, ) : super( diff --git a/lib/acts/list/act_list.dart b/lib/acts/list/act_list.dart index c99712a1d..56d9a7b59 100644 --- a/lib/acts/list/act_list.dart +++ b/lib/acts/list/act_list.dart @@ -6,7 +6,7 @@ import 'package:mem/acts/states.dart'; import 'package:mem/components/mem/list/states.dart'; import 'package:mem/core/act.dart'; import 'package:mem/logger/log_service.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'app_bar.dart'; import 'item/builder.dart'; @@ -66,7 +66,7 @@ class _ActList extends StatelessWidget { final bool _isDateView; final bool _isTimeView; final List _actList; - final List _memList; + final List _memList; final ScrollController? _scrollController; const _ActList( @@ -100,8 +100,11 @@ class _ActList extends StatelessWidget { header: ActListSubHeader(e, _isDateView), sliver: SliverList( delegate: () { - final builder = - ActListItemBuilder(e, _memList, _isTimeView); + final builder = ActListItemBuilder( + e, + _memList, + _isTimeView, + ); return SliverChildBuilderDelegate( builder.build, diff --git a/lib/acts/list/item/builder.dart b/lib/acts/list/item/builder.dart index 9b605d872..500570a60 100644 --- a/lib/acts/list/item/builder.dart +++ b/lib/acts/list/item/builder.dart @@ -3,14 +3,14 @@ import 'package:flutter/material.dart'; import 'package:mem/core/act.dart'; import 'package:mem/logger/log_service.dart'; import 'package:mem/repositories/act_entity.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'total_act_time_item.dart'; import 'view.dart'; class ActListItemBuilder { final MapEntry> _actListWithDatetime; - final List _memList; + final List _memList; final bool _isTimeView; late final Map _actListGroupedByMemId; diff --git a/lib/acts/list/item/total_act_time_item.dart b/lib/acts/list/item/total_act_time_item.dart index c3490879b..8d21f7a55 100644 --- a/lib/acts/list/item/total_act_time_item.dart +++ b/lib/acts/list/item/total_act_time_item.dart @@ -3,11 +3,11 @@ import 'package:mem/core/act.dart'; import 'package:mem/core/date_and_time/duration.dart'; import 'package:mem/logger/log_service.dart'; import 'package:mem/mems/transitions.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; class TotalActTimeListItem extends StatelessWidget { final List _actList; - final SavedMemV1? _mem; + final SavedMemEntity? _mem; const TotalActTimeListItem(this._actList, this._mem, {super.key}); diff --git a/lib/components/created_and_updated_at_texts.dart b/lib/components/created_and_updated_at_texts.dart index aa7c6504a..835c35e91 100644 --- a/lib/components/created_and_updated_at_texts.dart +++ b/lib/components/created_and_updated_at_texts.dart @@ -3,18 +3,18 @@ import 'package:mem/components/date_and_time/date_and_time_view.dart'; import 'package:mem/core/date_and_time/date_and_time.dart'; import 'package:mem/core/mem.dart'; import 'package:mem/logger/log_service.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/values/colors.dart'; class CreatedAndUpdatedAtTexts extends StatelessWidget { - final MemV1 _entity; + final Mem _entity; const CreatedAndUpdatedAtTexts(this._entity, {super.key}); @override Widget build(BuildContext context) => v( () { - if (_entity is SavedMemV1) { + if (_entity is SavedMemEntity) { return Wrap( direction: Axis.horizontal, children: [ diff --git a/lib/components/mem/list/actions.dart b/lib/components/mem/list/actions.dart index 139c0e008..3e7c3108a 100644 --- a/lib/components/mem/list/actions.dart +++ b/lib/components/mem/list/actions.dart @@ -2,7 +2,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:mem/components/mem/list/states.dart'; import 'package:mem/logger/log_service.dart'; import 'package:mem/mems/mem_item_repository.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_item_entity.dart'; import 'package:mem/repositories/mem_repository.dart'; import 'package:mem/mems/states.dart'; @@ -16,12 +16,10 @@ final loadMemList = FutureProvider( final showDone = ref.watch(showDoneProvider); final mems = await v( - () => MemRepository() - .ship( - archived: showNotArchived == showArchived ? null : showArchived, - done: showNotDone == showDone ? null : showDone, - ) - .then((value) => value.map((e) => e.toV1())), + () => MemRepository().ship( + archived: showNotArchived == showArchived ? null : showArchived, + done: showNotDone == showDone ? null : showDone, + ), { 'showNotArchived': showNotArchived, 'showArchived': showArchived, @@ -32,7 +30,7 @@ final loadMemList = FutureProvider( ref.read(memsProvider.notifier).upsertAll( mems, - (tmp, item) => tmp is SavedMemV1 && item is SavedMemV1 + (tmp, item) => tmp is SavedMemEntity && item is SavedMemEntity ? tmp.id == item.id : false, ); diff --git a/lib/components/mem/list/states.dart b/lib/components/mem/list/states.dart index 16aa4dad7..570306645 100644 --- a/lib/components/mem/list/states.dart +++ b/lib/components/mem/list/states.dart @@ -11,7 +11,7 @@ import 'package:mem/mems/states.dart'; import 'package:mem/notifications/mem_notifications.dart'; import 'package:mem/repositories/act_entity.dart'; import 'package:mem/repositories/act_repository.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_notification_entity.dart'; import 'package:mem/repositories/mem_notification_repository.dart'; import 'package:mem/settings/states.dart'; @@ -40,9 +40,9 @@ final showDoneProvider = StateNotifierProvider, bool>( ), ); final _filteredMemsProvider = StateNotifierProvider.autoDispose< - ListValueStateNotifier, List>( + ListValueStateNotifier, List>( (ref) { - final savedMems = ref.watch(memsProvider).map((e) => e as SavedMemV1); + final savedMems = ref.watch(memsProvider).whereType(); final showNotArchived = ref.watch(showNotArchivedProvider); final showArchived = ref.watch(showArchivedProvider); @@ -86,7 +86,7 @@ final _filteredMemsProvider = StateNotifierProvider.autoDispose< ); final memListProvider = StateNotifierProvider.autoDispose< - ValueStateNotifier>, List>((ref) { + ValueStateNotifier>, List>((ref) { final filtered = ref.watch(_filteredMemsProvider); final latestActsByMem = ref.watch(latestActsByMemProvider); final savedMemNotifications = ref.watch(savedMemNotificationsProvider); @@ -199,15 +199,15 @@ final latestActsByMemProvider = StateNotifierProvider.autoDispose< initializer: (current, notifier) => v( () async { if (current.isEmpty) { - final memIds = - ref.read(memsProvider).whereType().map((e) => e.id); - - final actsByMemIds = await ActRepository().ship( - memIdsIn: memIds, - latestByMemIds: true, - ); - - ref.read(actsProvider.notifier).addAll(actsByMemIds); + ref.read(actsProvider.notifier).addAll( + await ActRepository().ship( + memIdsIn: ref + .read(memsProvider) + .whereType() + .map((e) => e.id), + latestByMemIds: true, + ), + ); } }, {'current': current}, @@ -227,8 +227,10 @@ final savedMemNotificationsProvider = StateNotifierProvider.autoDispose< initializer: (current, notifier) => v( () async { if (current.isEmpty) { - final memIds = - ref.read(memsProvider).whereType().map((e) => e.id); + final memIds = ref + .read(memsProvider) + .whereType() + .map((e) => e.id); final actsByMemIds = await MemNotificationRepository().ship( memIdsIn: memIds, diff --git a/lib/components/mem/list/view.dart b/lib/components/mem/list/view.dart index 1b07daa5c..c256af235 100644 --- a/lib/components/mem/list/view.dart +++ b/lib/components/mem/list/view.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:mem/components/async_value_view.dart'; import 'package:mem/logger/log_service.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'actions.dart'; import 'states.dart'; @@ -32,7 +32,7 @@ class MemListView extends ConsumerWidget { } class _MemListViewComponent extends StatelessWidget { - final List _memList; + final List _memList; final Widget _appBar; final Widget Function(int memId) _itemBuilder; final ScrollController? _scrollController; diff --git a/lib/components/mem/mem_done_checkbox.dart b/lib/components/mem/mem_done_checkbox.dart index cfe3e8217..a1f95a7a7 100644 --- a/lib/components/mem/mem_done_checkbox.dart +++ b/lib/components/mem/mem_done_checkbox.dart @@ -2,10 +2,10 @@ import 'package:flutter/material.dart'; import 'package:mem/core/mem.dart'; import 'package:mem/components/hero_view.dart'; import 'package:mem/logger/log_service.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; class MemDoneCheckbox extends StatelessWidget { - final MemV1 _mem; + final Mem _mem; final Function(bool? value) _onChanged; const MemDoneCheckbox( @@ -19,11 +19,11 @@ class MemDoneCheckbox extends StatelessWidget { () => HeroView( heroTag( 'mem-done', - _mem is SavedMemV1 ? _mem.id : null, + _mem is SavedMemEntity ? _mem.id : null, ), Checkbox( value: _mem.isDone, - onChanged: (_mem is SavedMemV1 ? _mem.isArchived : false) + onChanged: (_mem is SavedMemEntity ? _mem.isArchived : false) ? null : _onChanged, ), diff --git a/lib/components/mem/mem_name.dart b/lib/components/mem/mem_name.dart index 890b0c928..daa628382 100644 --- a/lib/components/mem/mem_name.dart +++ b/lib/components/mem/mem_name.dart @@ -4,14 +4,14 @@ import 'package:mem/components/hero_view.dart'; import 'package:mem/components/l10n.dart'; import 'package:mem/logger/log_service.dart'; import 'package:mem/mems/detail/states.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; Key keyMemName = const Key("mem-name"); String _memNameTag(int? memId) => heroTag('mem-name', memId); class MemNameText extends StatelessWidget { - final SavedMemV1 _mem; + final SavedMemEntity _mem; const MemNameText(this._mem, {super.key}); diff --git a/lib/core/mem.dart b/lib/core/mem.dart index 14f4602b6..adbc50222 100644 --- a/lib/core/mem.dart +++ b/lib/core/mem.dart @@ -1,11 +1,10 @@ // FIXME coreからflutterへの依存は排除したい import 'package:flutter/material.dart'; import 'package:mem/core/date_and_time/date_and_time_period.dart'; -import 'package:mem/framework/repository/entity.dart'; import 'package:mem/logger/log_service.dart'; import 'package:mem/notifications/notification/type.dart'; import 'package:mem/notifications/schedule.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; class Mem { final String name; @@ -14,74 +13,15 @@ class Mem { Mem(this.name, this.doneAt, this.period); -// factory Mem.defaultNew() => Mem("", null, null); -// -// bool get isDone => doneAt != null; -// -// Iterable periodSchedules( -// TimeOfDay startOfDay, -// ) => -// v( -// () { -// final id = this is SavedMemV1 ? (this as SavedMemV1).id : null; -// -// return id == null -// ? throw Exception() // coverage:ignore-line -// : [ -// Schedule.of( -// id, -// period?.start?.isAllDay == true -// ? DateTime( -// period!.start!.year, -// period!.start!.month, -// period!.start!.day, -// startOfDay.hour, -// startOfDay.minute, -// ) -// : period?.start, -// NotificationType.startMem, -// ), -// Schedule.of( -// id, -// period?.end?.isAllDay == true -// ? DateTime( -// period!.end!.year, -// period!.end!.month, -// period!.end!.day, -// startOfDay.hour, -// startOfDay.minute, -// ) -// .add(const Duration(days: 1)) -// .subtract(const Duration(minutes: 1)) -// : period?.end, -// NotificationType.endMem, -// ), -// ]; -// }, -// { -// 'this': this, -// 'startOfDay': startOfDay, -// }, -// ); -} - -class MemV1 extends EntityV1 { - final String name; - final DateTime? doneAt; - final DateAndTimePeriod? period; - - MemV1(this.name, this.doneAt, this.period); - bool get isDone => doneAt != null; - factory MemV1.defaultNew() => MemV1("", null, null); - Iterable periodSchedules( TimeOfDay startOfDay, ) => v( () { - final id = this is SavedMemV1 ? (this as SavedMemV1).id : null; + final id = + this is SavedMemEntity ? (this as SavedMemEntity).id : null; return id == null ? throw Exception() // coverage:ignore-line @@ -121,22 +61,4 @@ class MemV1 extends EntityV1 { 'startOfDay': startOfDay, }, ); - - MemV1 copiedWith({ - String Function()? name, - DateTime? Function()? doneAt, - DateAndTimePeriod? Function()? period, - }) => - MemV1( - name == null ? this.name : name(), - doneAt == null ? this.doneAt : doneAt(), - period == null ? this.period : period(), - ); - - @override - String toString() => "${super.toString()}: ${{ - "name": name, - "doneAt": doneAt, - "period": period, - }}"; } diff --git a/lib/core/mem_detail.dart b/lib/core/mem_detail.dart index c951a7954..b46678108 100644 --- a/lib/core/mem_detail.dart +++ b/lib/core/mem_detail.dart @@ -1,5 +1,5 @@ -import 'package:mem/core/mem.dart'; import 'package:mem/core/mem_notification.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_item_entity.dart'; // FIXME 定義するべきではない気がする @@ -8,7 +8,7 @@ import 'package:mem/repositories/mem_item_entity.dart'; // - DBのFK制約が絡むしEntityかも // - Repositoryも絡んでいくはず class MemDetail { - final MemV1 mem; + final MemEntity mem; final List memItems; final List? notifications; diff --git a/lib/framework/repository/database_tuple_entity.dart b/lib/framework/repository/database_tuple_entity.dart index b5b0b9c7c..5040c28ea 100644 --- a/lib/framework/repository/database_tuple_entity.dart +++ b/lib/framework/repository/database_tuple_entity.dart @@ -1,39 +1,14 @@ import 'package:mem/databases/table_definitions/base.dart'; import 'package:mem/framework/repository/entity.dart'; -mixin SavedDatabaseTupleMixinV1 on EntityV1 { - late T id; - late DateTime createdAt; - DateTime? updatedAt; - DateTime? archivedAt; - - bool get isArchived => archivedAt != null; - - Map unpack() { - return { - defPkId.name: id, - defColCreatedAt.name: createdAt, - defColUpdatedAt.name: updatedAt, - defColArchivedAt.name: archivedAt, - }; - } - - SavedDatabaseTupleMixinV1 copiedFrom(SavedDatabaseTupleMixinV1 origin) => this - ..id = origin.id - ..createdAt = origin.createdAt - ..updatedAt = origin.updatedAt - ..archivedAt = origin.archivedAt; - - @override - String toString() => "${super.toString()}${unpack()}"; -} - mixin DatabaseTupleEntity on Entity { late PrimaryKey id; late DateTime createdAt; late DateTime? updatedAt; late DateTime? archivedAt; + bool get isArchived => archivedAt != null; + DatabaseTupleEntity withMap(Map map) { id = map[defPkId.name]; createdAt = map[defColCreatedAt.name]; diff --git a/lib/framework/repository/entity.dart b/lib/framework/repository/entity.dart index facecb877..7e4fa82ab 100644 --- a/lib/framework/repository/entity.dart +++ b/lib/framework/repository/entity.dart @@ -4,8 +4,6 @@ // # 語源 // // 「存在するもの」、「実体」 -abstract class EntityV1 {} - mixin Entity { Map get toMap; diff --git a/lib/mems/actions.dart b/lib/mems/actions.dart index 7c0a91533..570ff4217 100644 --- a/lib/mems/actions.dart +++ b/lib/mems/actions.dart @@ -2,7 +2,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:mem/logger/log_service.dart'; import 'package:mem/mems/mem_service.dart'; import 'package:mem/mems/states.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_item_entity.dart'; final undoRemoveMem = FutureProvider.autoDispose.family( @@ -15,7 +15,7 @@ final undoRemoveMem = FutureProvider.autoDispose.family( await MemService().save(removedMemDetail, undo: true); final removeUndoneMem = removeUndone.mem; - if (removeUndoneMem is SavedMemV1) { + if (removeUndoneMem is SavedMemEntity) { ref.read(memsProvider.notifier).add(removeUndoneMem); for (var element in removeUndone.memItems) { ref.read(memItemsProvider.notifier).upsertAll( diff --git a/lib/mems/detail/actions.dart b/lib/mems/detail/actions.dart index 39938e487..4017022e5 100644 --- a/lib/mems/detail/actions.dart +++ b/lib/mems/detail/actions.dart @@ -3,8 +3,8 @@ import 'package:mem/core/mem_detail.dart'; import 'package:mem/logger/log_service.dart'; import 'package:mem/mems/detail/states.dart'; import 'package:mem/mems/mem_client.dart'; -import 'package:mem/repositories/mem.dart'; import 'package:mem/mems/states.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_notification_entity.dart'; final _memClient = MemClient(); @@ -19,8 +19,10 @@ final saveMem = ); ref.read(memsProvider.notifier).upsertAll( - [saved.mem], - (tmp, item) => tmp is SavedMemV1 && item is SavedMemV1 + [ + saved.mem, + ], + (tmp, item) => tmp is SavedMemEntity && item is SavedMemEntity ? tmp.id == item.id : false, ); @@ -53,13 +55,15 @@ final archiveMem = Provider.autoDispose.family, int?>( () async { final mem = ref.read(memByMemIdProvider(memId)); - final archived = await _memClient.archive(mem as SavedMemV1); + final archived = await _memClient.archive(mem!); ref .read(editingMemByMemIdProvider(memId).notifier) .updatedBy(archived.mem); ref.read(memsProvider.notifier).upsertAll( - [archived.mem], - (tmp, item) => tmp is SavedMemV1 && item is SavedMemV1 + [ + archived.mem, + ], + (tmp, item) => tmp is SavedMemEntity && item is SavedMemEntity ? tmp.id == item.id : false); @@ -72,16 +76,20 @@ final archiveMem = Provider.autoDispose.family, int?>( final unarchiveMem = Provider.autoDispose.family, int?>( (ref, memId) => v( () async { - final mem = ref.read(memByMemIdProvider(memId)); - - final unarchived = await _memClient.unarchive(mem as SavedMemV1); + final unarchived = await _memClient.unarchive( + ref.read( + memByMemIdProvider(memId), + )!, + ); ref .read(editingMemByMemIdProvider(memId).notifier) .updatedBy(unarchived.mem); ref.read(memsProvider.notifier).upsertAll( - [unarchived.mem], - (tmp, item) => tmp is SavedMemV1 && item is SavedMemV1 + [ + unarchived.mem, + ], + (tmp, item) => tmp is SavedMemEntity && item is SavedMemEntity ? tmp.id == item.id : false); @@ -97,16 +105,17 @@ final removeMem = Provider.autoDispose.family, int?>( if (memId != null) { final removeSuccess = await _memClient.remove(memId); - ref - .read(removedMemProvider(memId).notifier) - .updatedBy(ref.read(memByMemIdProvider(memId))); + final mem = ref.read(memByMemIdProvider(memId)); + ref.read(removedMemProvider(memId).notifier).updatedBy( + mem, + ); ref.read(removedMemItemsProvider(memId).notifier).updatedBy( ref.read(memItemsByMemIdProvider(memId)), ); // TODO mem notificationsにも同様の処理が必要では? ref.read(memsProvider.notifier).removeWhere( - (element) => element is SavedMemV1 && element.id == memId); + (element) => element is SavedMemEntity && element.id == memId); return removeSuccess; } diff --git a/lib/mems/detail/body.dart b/lib/mems/detail/body.dart index 8fec7d92e..a7afe67a4 100644 --- a/lib/mems/detail/body.dart +++ b/lib/mems/detail/body.dart @@ -38,7 +38,7 @@ class MemDetailBody extends ConsumerWidget { class _MemDetailBodyComponent extends StatelessWidget { final int? _memId; - final MemV1 _mem; + final Mem _mem; final Function(bool? memDone) _onMemDoneChanged; const _MemDetailBodyComponent( @@ -72,7 +72,9 @@ class _MemDetailBodyComponent extends StatelessWidget { ), Padding( padding: pageBottomPadding, - child: CreatedAndUpdatedAtTexts(_mem), + child: CreatedAndUpdatedAtTexts( + _mem, + ), ), ], ), diff --git a/lib/mems/detail/page.dart b/lib/mems/detail/page.dart index c53bae6b8..38d0f555d 100644 --- a/lib/mems/detail/page.dart +++ b/lib/mems/detail/page.dart @@ -9,7 +9,7 @@ import 'package:mem/mems/detail/app_bar/remove_mem_action.dart'; import 'package:mem/mems/detail/states.dart'; import 'package:mem/mems/detail/app_bar/transit_act_list_action.dart'; import 'package:mem/mems/detail/app_bar/transit_chart_action.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/values/colors.dart'; class MemDetailPage extends ConsumerWidget { @@ -21,13 +21,13 @@ class MemDetailPage extends ConsumerWidget { Widget build(BuildContext context, WidgetRef ref) => v( () => _MemDetailPage( ref.watch(editingMemByMemIdProvider(_memId).select( - (value) => value is SavedMemV1 ? value.id : null, + (value) => value is SavedMemEntity ? value.id : null, )), ref.watch(editingMemByMemIdProvider(_memId).select( - (value) => value is SavedMemV1, + (value) => value is SavedMemEntity, )), ref.watch(editingMemByMemIdProvider(_memId).select( - (value) => value is SavedMemV1 ? value.isArchived : false, + (value) => value is SavedMemEntity ? value.isArchived : false, )), ), { diff --git a/lib/mems/detail/states.dart b/lib/mems/detail/states.dart index 90c1114f1..6dfdd6189 100644 --- a/lib/mems/detail/states.dart +++ b/lib/mems/detail/states.dart @@ -1,6 +1,5 @@ import 'package:collection/collection.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:mem/core/mem.dart'; import 'package:mem/core/mem_item.dart'; import 'package:mem/core/mem_notification.dart'; import 'package:mem/components/list_value_state_notifier.dart'; @@ -8,16 +7,20 @@ import 'package:mem/logger/log_service.dart'; import 'package:mem/components/value_state_notifier.dart'; import 'package:mem/mems/mem_item_repository.dart'; import 'package:mem/mems/states.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_item_entity.dart'; import 'package:mem/repositories/mem_notification_entity.dart'; import 'package:mem/repositories/mem_notification_repository.dart'; final editingMemByMemIdProvider = StateNotifierProvider.autoDispose - .family, MemV1, int?>( + .family, MemEntity, int?>( (ref, memId) => v( - () => ValueStateNotifier( - ref.watch(memByMemIdProvider(memId)) ?? MemV1.defaultNew(), - ), + () { + final mem = ref.watch(memByMemIdProvider(memId)); + return ValueStateNotifier( + mem ?? MemEntity("", null, null), + ); + }, {"memId": memId}, ), ); diff --git a/lib/mems/list/item/actions.dart b/lib/mems/list/item/actions.dart index a32231c28..ef91790a9 100644 --- a/lib/mems/list/item/actions.dart +++ b/lib/mems/list/item/actions.dart @@ -2,46 +2,47 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:mem/logger/log_service.dart'; import 'package:mem/mems/mem_service.dart'; import 'package:mem/mems/states.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; -final doneMem = Provider.autoDispose.family( +final doneMem = Provider.autoDispose.family( (ref, memId) => v( () { - final mem = ref - .read(memByMemIdProvider(memId))! - .copiedWith(doneAt: () => DateTime.now()); - MemService().doneByMemId(memId).then( (doneMemDetail) => ref.read(memsProvider.notifier).upsertAll( - [doneMemDetail.mem], - (tmp, item) => tmp is SavedMemV1 && item is SavedMemV1 + [ + doneMemDetail.mem, + ], + (tmp, item) => tmp is SavedMemEntity && item is SavedMemEntity ? tmp.id == item.id : false, ), ); - return mem; + return ref + .read(memByMemIdProvider(memId))! + .copiedWith(doneAt: () => DateTime.now()); }, memId, ), ); -final undoneMem = Provider.autoDispose.family( +final undoneMem = Provider.autoDispose.family( (ref, memId) => v( () { - final mem = - ref.read(memByMemIdProvider(memId))!.copiedWith(doneAt: () => null); - MemService().undoneByMemId(memId).then( (undoneMemDetail) => ref.read(memsProvider.notifier).upsertAll( - [undoneMemDetail.mem], - (tmp, item) => tmp is SavedMemV1 && item is SavedMemV1 + [ + undoneMemDetail.mem, + ], + (tmp, item) => tmp is SavedMemEntity && item is SavedMemEntity ? tmp.id == item.id : false, ), ); - return mem; + return ref + .read(memByMemIdProvider(memId))! + .copiedWith(doneAt: () => null); }, memId, ), diff --git a/lib/mems/list/item/view.dart b/lib/mems/list/item/view.dart index 208f03809..478d6e61f 100644 --- a/lib/mems/list/item/view.dart +++ b/lib/mems/list/item/view.dart @@ -12,7 +12,7 @@ import 'package:mem/mems/detail/states.dart'; import 'package:mem/mems/list/item/subtitle.dart'; import 'package:mem/mems/states.dart'; import 'package:mem/repositories/act_entity.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_notification_entity.dart'; import 'package:mem/values/colors.dart'; @@ -40,7 +40,7 @@ class MemListItemView extends ConsumerWidget { ? ref.read(doneMem(_memId)) : ref.read(undoneMem(_memId)) ], - (tmp, item) => tmp is SavedMemV1 && item is SavedMemV1 + (tmp, item) => tmp is SavedMemEntity && item is SavedMemEntity ? tmp.id == item.id : false, ); @@ -65,7 +65,7 @@ class MemListItemView extends ConsumerWidget { class _MemListItemView extends ListTile { _MemListItemView( - SavedMemV1 mem, + SavedMemEntity mem, SavedActEntity? activeAct, Iterable memNotifications, void Function(int memId) onTap, diff --git a/lib/mems/mem_client.dart b/lib/mems/mem_client.dart index 86969dffc..3fa63d384 100644 --- a/lib/mems/mem_client.dart +++ b/lib/mems/mem_client.dart @@ -3,7 +3,7 @@ import 'package:mem/core/mem_detail.dart'; import 'package:mem/core/mem_notification.dart'; import 'package:mem/logger/log_service.dart'; import 'package:mem/notifications/notification_client.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_item_entity.dart'; import 'package:mem/repositories/mem_notification_entity.dart'; @@ -14,7 +14,7 @@ class MemClient { final NotificationClient _notificationClient; Future save( - MemV1 mem, + MemEntity mem, List memItemList, List memNotificationList, ) => @@ -29,8 +29,8 @@ class MemClient { ); _notificationClient.registerMemNotifications( - (saved.mem as SavedMemV1).id, - savedMem: saved.mem as SavedMemV1, + (saved.mem as SavedMemEntity).id, + savedMem: saved.mem as SavedMemEntity, savedMemNotifications: saved.notifications?.whereType(), ); @@ -44,15 +44,15 @@ class MemClient { }, ); - Future archive(MemV1 mem) => v( + Future archive(MemEntity mem) => v( () async { // FIXME MemServiceの責務 - if (mem is SavedMemV1) { + if (mem is SavedMemEntity) { final archived = await _memService.archive(mem); final archivedMem = archived.mem; // FIXME archive後のMemDetailなので、必ずSavedMemのはず - if (archivedMem is SavedMemV1) { + if (archivedMem is SavedMemEntity) { _notificationClient.cancelMemNotifications(archivedMem.id); } @@ -69,15 +69,16 @@ class MemClient { }, ); - Future unarchive(MemV1 mem) => v( + Future unarchive(Mem mem) => v( () async { - // FIXME MemServiceの責務 - if (mem is SavedMemV1) { + // FIXME 保存済みかどうかを判定するのはMemServiceの責務? + // Client sideで判定できるものではない気がする + if (mem is SavedMemEntity) { final unarchived = await _memService.unarchive(mem); _notificationClient.registerMemNotifications( - (unarchived.mem as SavedMemV1).id, - savedMem: unarchived.mem as SavedMemV1, + (unarchived.mem as SavedMemEntity).id, + savedMem: unarchived.mem as SavedMemEntity, savedMemNotifications: unarchived.notifications ?.whereType(), ); diff --git a/lib/mems/mem_service.dart b/lib/mems/mem_service.dart index 359d18def..28deedead 100644 --- a/lib/mems/mem_service.dart +++ b/lib/mems/mem_service.dart @@ -3,7 +3,6 @@ import 'package:mem/core/mem_detail.dart'; import 'package:mem/core/mem_notification.dart'; import 'package:mem/logger/log_service.dart'; import 'package:mem/mems/mem_item_repository.dart'; -import 'package:mem/repositories/mem.dart'; import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_item_entity.dart'; import 'package:mem/repositories/mem_notification_entity.dart'; @@ -23,10 +22,9 @@ class MemService { () async { final mem = memDetail.mem; - final savedMem = (mem is SavedMemV1 && !undo - ? await _memRepository.replace(SavedMemEntity.fromV1(mem)) - : await _memRepository.receive(MemEntity.fromV1(mem))) - .toV1(); + final savedMem = (mem is SavedMemEntity && !undo + ? await _memRepository.replace(mem) + : await _memRepository.receive(mem)); final savedMemItems = await Future.wait( memDetail.memItems.map((e) => (e is SavedMemItemEntity && !undo @@ -102,10 +100,9 @@ class MemService { Future doneByMemId(int memId) => i( () async { - final done = (await _memRepository - .ship(id: memId) - .then((v) => v.single.toV1())) - .copiedWith(doneAt: () => DateTime.now()); + final done = + (await _memRepository.ship(id: memId).then((v) => v.single)) + .copiedWith(doneAt: () => DateTime.now()); return save(MemDetail(done, [])); }, {'memId': memId}, @@ -113,20 +110,17 @@ class MemService { Future undoneByMemId(int memId) => i( () async { - final undone = (await _memRepository - .ship(id: memId) - .then((v) => v.single.toV1())) - .copiedWith(doneAt: () => null); + final undone = + (await _memRepository.ship(id: memId).then((v) => v.single)) + .copiedWith(doneAt: () => null); return save(MemDetail(undone, [])); }, {'memId': memId}, ); - Future archive(SavedMemV1 mem) => i( + Future archive(SavedMemEntity mem) => i( () async { - final archivedMem = await _memRepository - .archive(SavedMemEntity.fromV1(mem)) - .then((v) => v.toV1()); + final archivedMem = await _memRepository.archive(mem); final archivedMemItems = await _memItemRepository.archiveBy(memId: archivedMem.id); final archivedMemNotifications = @@ -143,11 +137,9 @@ class MemService { }, ); - Future unarchive(SavedMemV1 mem) => i( + Future unarchive(SavedMemEntity mem) => i( () async { - final unarchivedMem = await _memRepository - .unarchive(SavedMemEntity.fromV1(mem)) - .then((value) => value.toV1()); + final unarchivedMem = await _memRepository.unarchive(mem); final unarchivedMemItems = await _memItemRepository.unarchiveBy(memId: unarchivedMem.id); final unarchivedMemNotifications = await _memNotificationRepository diff --git a/lib/mems/states.dart b/lib/mems/states.dart index 6544cc429..1e93c2b7c 100644 --- a/lib/mems/states.dart +++ b/lib/mems/states.dart @@ -1,18 +1,17 @@ import 'package:collection/collection.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:mem/components/list_value_state_notifier.dart'; -import 'package:mem/core/mem.dart'; import 'package:mem/core/mem_detail.dart'; import 'package:mem/components/value_state_notifier.dart'; import 'package:mem/core/mem_notification.dart'; import 'package:mem/logger/log_service.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_item_entity.dart'; import 'package:mem/repositories/mem_repository.dart'; final memsProvider = - StateNotifierProvider, List>( - (ref) => v(() => ListValueStateNotifier([])), + StateNotifierProvider, List>( + (ref) => v(() => ListValueStateNotifier([])), ); final memItemsProvider = StateNotifierProvider< ListValueStateNotifier, List>( @@ -26,22 +25,23 @@ final memNotificationsProvider = StateNotifierProvider< ); final memByMemIdProvider = StateNotifierProvider.autoDispose - .family, SavedMemV1?, int?>( + .family, SavedMemEntity?, int?>( (ref, memId) => v( () => ValueStateNotifier( ref.watch(memsProvider).singleWhereOrNull( - (element) => element is SavedMemV1 ? element.id == memId : false, - ) as SavedMemV1?, + (element) => + element is SavedMemEntity ? element.id == memId : false, + ) as SavedMemEntity?, initializer: (current, notifier) => v( () async { if (memId != null) { final savedMem = await MemRepository() .ship(id: memId) - .then((value) => value.singleOrNull?.toV1()); + .then((value) => value.singleOrNull); ref.read(memsProvider.notifier).upsertAll( [if (savedMem != null) savedMem], (current, updating) => - (current is SavedMemV1 && updating is SavedMemV1) + (current is SavedMemEntity && updating is SavedMemEntity) ? current.id == updating.id : true, ); @@ -76,10 +76,10 @@ final removedMemDetailProvider = StateNotifierProvider.autoDispose memId, ), ); -final removedMemProvider = - StateNotifierProvider.family, MemV1?, int>( +final removedMemProvider = StateNotifierProvider.family< + ValueStateNotifier, MemEntity?, int>( (ref, memId) => v( - () => ValueStateNotifier(null), + () => ValueStateNotifier(null), memId, ), ); diff --git a/lib/notifications/mem_notifications.dart b/lib/notifications/mem_notifications.dart index 544fd28f7..44373573f 100644 --- a/lib/notifications/mem_notifications.dart +++ b/lib/notifications/mem_notifications.dart @@ -3,7 +3,7 @@ import 'package:flutter/material.dart'; import 'package:mem/core/act.dart'; import 'package:mem/core/mem_notification.dart'; import 'package:mem/logger/log_service.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_notification_entity.dart'; import 'notification_client.dart'; @@ -15,7 +15,7 @@ const memIdKey = 'memId'; class MemNotifications { static Schedule periodicScheduleOf( - SavedMemV1 savedMem, + SavedMemEntity savedMem, TimeOfDay startOfDay, Iterable memNotifications, Act? latestAct, diff --git a/lib/notifications/notification_client.dart b/lib/notifications/notification_client.dart index 6664fb57f..89230c8f2 100644 --- a/lib/notifications/notification_client.dart +++ b/lib/notifications/notification_client.dart @@ -5,7 +5,7 @@ import 'package:flutter/material.dart'; import 'package:mem/components/l10n.dart'; import 'package:mem/logger/log_service.dart'; import 'package:mem/repositories/act_repository.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import 'package:mem/repositories/mem_notification_entity.dart'; import 'package:mem/repositories/mem_notification_repository.dart'; import 'package:mem/repositories/mem_repository.dart'; @@ -73,9 +73,8 @@ class NotificationClient { ) => v( () async { - final savedMem = await _memRepository - .ship(id: memId) - .then((v) => v.singleOrNull?.toV1()); + final savedMem = + await _memRepository.ship(id: memId).then((v) => v.singleOrNull); if (savedMem == null || savedMem.isDone || savedMem.isArchived) { await cancelMemNotifications(memId); @@ -127,13 +126,19 @@ class NotificationClient { Future registerMemNotifications( int memId, { - SavedMemV1? savedMem, + SavedMemEntity? savedMem, Iterable? savedMemNotifications, }) => v( () async { final mem = savedMem ?? - await _memRepository.ship(id: memId).then((v) => v.single.toV1()); + await _memRepository + .ship( + id: memId, + ) + .then( + (v) => v.single, + ); if (mem!.isDone || mem.isArchived) { cancelMemNotifications(memId); } else { diff --git a/lib/repositories/mem.dart b/lib/repositories/mem.dart deleted file mode 100644 index d7ef37555..000000000 --- a/lib/repositories/mem.dart +++ /dev/null @@ -1,19 +0,0 @@ -import 'package:mem/core/date_and_time/date_and_time_period.dart'; -import 'package:mem/core/mem.dart'; -import 'package:mem/framework/repository/database_tuple_entity.dart'; - -class SavedMemV1 extends MemV1 with SavedDatabaseTupleMixinV1 { - SavedMemV1(super.name, super.doneAt, super.period); - - @override - SavedMemV1 copiedWith({ - String Function()? name, - DateTime? Function()? doneAt, - DateAndTimePeriod? Function()? period, - }) => - SavedMemV1( - name == null ? this.name : name(), - doneAt == null ? this.doneAt : doneAt(), - period == null ? this.period : period(), - )..copiedFrom(this); -} diff --git a/lib/repositories/mem_entity.dart b/lib/repositories/mem_entity.dart index 7f096b159..20892ea16 100644 --- a/lib/repositories/mem_entity.dart +++ b/lib/repositories/mem_entity.dart @@ -1,13 +1,11 @@ import 'package:mem/core/date_and_time/date_and_time.dart'; import 'package:mem/core/date_and_time/date_and_time_period.dart'; import 'package:mem/core/mem.dart'; -import 'package:mem/databases/table_definitions/base.dart'; import 'package:mem/databases/table_definitions/mems.dart'; import 'package:mem/framework/repository/database_tuple_entity.dart'; import 'package:mem/framework/repository/entity.dart'; -import 'package:mem/repositories/mem.dart'; -class MemEntity extends Mem with Entity { +class MemEntity extends Mem with Entity, Copyable { MemEntity(super.name, super.doneAt, super.period); MemEntity.fromMap(Map map) @@ -29,14 +27,17 @@ class MemEntity extends Mem with Entity { ), ); - MemEntity.fromV1(MemV1 savedMem) - : this.fromMap( - MemEntity( - savedMem.name, - savedMem.doneAt, - savedMem.period, - ).toMap, - ); + @override + MemEntity copiedWith({ + String Function()? name, + DateTime? Function()? doneAt, + DateAndTimePeriod? Function()? period, + }) => + MemEntity( + name == null ? this.name : name(), + doneAt == null ? this.doneAt : doneAt(), + period == null ? this.period : period(), + ); @override Map get toMap => { @@ -52,7 +53,7 @@ class MemEntity extends Mem with Entity { } class SavedMemEntity extends MemEntity with DatabaseTupleEntity { - // SavedMemEntity(super.name, super.doneAt, super.period); + SavedMemEntity(super.name, super.doneAt, super.period); SavedMemEntity.fromMap( Map map, @@ -60,22 +61,22 @@ class SavedMemEntity extends MemEntity with DatabaseTupleEntity { withMap(map); } - SavedMemEntity.fromV1(SavedMemV1 savedMem) - : this.fromMap( - MemEntity.fromV1(savedMem).toMap - ..addAll( - { - defPkId.name: savedMem.id, - defColCreatedAt.name: savedMem.createdAt, - defColUpdatedAt.name: savedMem.updatedAt, - defColArchivedAt.name: savedMem.archivedAt, - }, - ), - ); - - SavedMemV1 toV1() => SavedMemV1(name, doneAt, period) - ..id = id - ..createdAt = createdAt - ..updatedAt = updatedAt - ..archivedAt = archivedAt; + @override + SavedMemEntity copiedWith({ + String Function()? name, + DateTime? Function()? doneAt, + DateAndTimePeriod? Function()? period, + }) => + SavedMemEntity.fromMap( + toMap + ..addAll( + super + .copiedWith( + name: name, + doneAt: doneAt, + period: period, + ) + .toMap, + ), + ); } diff --git a/test/act_counter/act_counter_test.dart b/test/act_counter/act_counter_test.dart index c1bea7c9a..a20e6ce0c 100644 --- a/test/act_counter/act_counter_test.dart +++ b/test/act_counter/act_counter_test.dart @@ -4,7 +4,7 @@ import 'package:mem/core/date_and_time/date_and_time.dart'; import 'package:mem/core/date_and_time/date_and_time_period.dart'; import 'package:mem/databases/table_definitions/base.dart'; import 'package:mem/repositories/act_entity.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; void main() { group('ActCounter.from', () { @@ -13,7 +13,7 @@ void main() { final zeroDate = DateTime(0); final oneDate = DateTime(1); - final savedMem = SavedMemV1("constructor", null, null)..id = memId; + final savedMem = SavedMemEntity("constructor", null, null)..id = memId; final acts = [ SavedActEntity(memId, DateAndTimePeriod.startNow(), { defPkId.name: 1, @@ -33,7 +33,7 @@ void main() { final zeroDate = DateTime(0); final oneDate = DateTime(1); - final savedMem = SavedMemV1("constructor", null, null)..id = memId; + final savedMem = SavedMemEntity("constructor", null, null)..id = memId; final acts = [ SavedActEntity(memId, DateAndTimePeriod(end: DateAndTime.now()), { defPkId.name: 3, diff --git a/test/mems/mem_list_body_test.dart b/test/mems/mem_list_body_test.dart index fef805906..dcb1ab135 100644 --- a/test/mems/mem_list_body_test.dart +++ b/test/mems/mem_list_body_test.dart @@ -9,7 +9,7 @@ import 'package:mem/mems/detail/states.dart'; import 'package:mem/mems/list/body.dart'; import 'package:mem/mems/list/show_new_mem_fab.dart'; import 'package:mem/mems/states.dart'; -import 'package:mem/repositories/mem.dart'; +import 'package:mem/repositories/mem_entity.dart'; import '../../integration_test/scenarios/helpers.dart'; @@ -18,13 +18,15 @@ void main() { final scrollController = ScrollController(); final samples = List.generate( 20, - (index) => SavedMemV1( + (index) => SavedMemEntity( 'Hide & show ShowNewMemFab: mem name - $index', null, null, ) ..id = index - ..createdAt = zeroDate, + ..createdAt = zeroDate + ..updatedAt = null + ..archivedAt = null, ); await widgetTester.pumpWidget(ProviderScope(