diff --git a/lib/components/organisms/calendar/utility.dart b/lib/components/organisms/calendar/utility.dart index 5bb23ec77b..4933056d34 100644 --- a/lib/components/organisms/calendar/utility.dart +++ b/lib/components/organisms/calendar/utility.dart @@ -96,7 +96,7 @@ List nextPillSheetDateRanges( return List.generate(count.toInt(), (groupPageIndex) { return pillSheetGroup.pillSheets.map((pillSheet) { final offset = groupPageIndex * pillSheetGroup.totalPillCountIntoGroup; - final begin = pillSheet.scheduledLastTakenDate.add(Duration(days: 1)); + final begin = pillSheet.estimatedLastTakenDate.add(Duration(days: 1)); final end = begin.add(Duration(days: Weekday.values.length - 1)); return DateRange( begin.add(Duration(days: offset)), end.add(Duration(days: offset))); diff --git a/lib/domain/calendar/components/pill_sheet_modified_history/components/pill_sheet_modified_history_taken_pill_action.dart b/lib/domain/calendar/components/pill_sheet_modified_history/components/pill_sheet_modified_history_taken_pill_action.dart index 93f06fa3c1..df8802481b 100644 --- a/lib/domain/calendar/components/pill_sheet_modified_history/components/pill_sheet_modified_history_taken_pill_action.dart +++ b/lib/domain/calendar/components/pill_sheet_modified_history/components/pill_sheet_modified_history_taken_pill_action.dart @@ -28,7 +28,7 @@ class PillSheetModifiedHistoryTakenPillAction extends StatelessWidget { if (value == null || afterPillSheet == null) { return Container(); } - final time = DateTimeFormatter.hourAndMinute(value.afterLastTakenDate); + final time = DateTimeFormatter.hourAndMinute(estimatedEventCausingDate); return GestureDetector( onTap: () { analytics.logEvent(name: "tapped_history_taken_action"); diff --git a/lib/domain/record/components/button/record_page_button.dart b/lib/domain/record/components/button/record_page_button.dart index c63adc53bb..43f5a53594 100644 --- a/lib/domain/record/components/button/record_page_button.dart +++ b/lib/domain/record/components/button/record_page_button.dart @@ -12,7 +12,7 @@ class RecordPageButton extends StatelessWidget { }) : super(key: key); Widget build(BuildContext context) { - if (currentPillSheet.allTaken) + if (currentPillSheet.isAllTaken) return CancelButton(currentPillSheet); else return TakenButton( diff --git a/lib/domain/record/util/take.dart b/lib/domain/record/util/take.dart index 9b117d55d8..6599e67508 100644 --- a/lib/domain/record/util/take.dart +++ b/lib/domain/record/util/take.dart @@ -71,12 +71,15 @@ Future take({ if (pillSheet.groupIndex > activedPillSheet.groupIndex) { return pillSheet; } - if (pillSheet.allTaken) { + if (pillSheet.isEnded) { return pillSheet; } - if (takenDate.isAfter(pillSheet.scheduledLastTakenDate)) { + + // takenDateよりも予測するピルシートが大きい場合はactivedPillSheetじゃないPillSheetと判断。 + // そのピルシートの最終日で予測する最終服用日を記録する + if (takenDate.isAfter(pillSheet.estimatedLastTakenDate)) { return pillSheet.copyWith( - lastTakenDate: pillSheet.scheduledLastTakenDate); + lastTakenDate: pillSheet.estimatedLastTakenDate); } else { return pillSheet.copyWith(lastTakenDate: takenDate); } diff --git a/lib/entity/pill_sheet.dart b/lib/entity/pill_sheet.dart index a707247817..88d5f6dfee 100644 --- a/lib/entity/pill_sheet.dart +++ b/lib/entity/pill_sheet.dart @@ -90,7 +90,8 @@ abstract class PillSheet implements _$PillSheet { ? 0 : lastTakenDate!.date().difference(beginingDate.date()).inDays + 1; - bool get allTaken => todayPillNumber == lastTakenPillNumber; + bool get isAllTaken => todayPillNumber == lastTakenPillNumber; + bool get isEnded => typeInfo.totalCount == lastTakenPillNumber; bool get isReached => beginingDate.date().toUtc().millisecondsSinceEpoch < now().toUtc().millisecondsSinceEpoch; @@ -104,6 +105,9 @@ abstract class PillSheet implements _$PillSheet { return DateRange(begin, end).inRange(n); } - DateTime get scheduledLastTakenDate => - beginingDate.add(Duration(days: pillSheetType.totalCount - 1)); + DateTime get estimatedLastTakenDate => beginingDate + .add(Duration(days: pillSheetType.totalCount - 1)) + .date() + .add(Duration(days: 1)) + .subtract(Duration(seconds: 1)); } diff --git a/test/domain/record/record_page_state_test.dart b/test/domain/record/record_page_state_test.dart index 7b7e47ffd3..6065827acd 100644 --- a/test/domain/record/record_page_state_test.dart +++ b/test/domain/record/record_page_state_test.dart @@ -109,7 +109,7 @@ void main() { ); await waitForResetStoreState(); - expect(state.pillSheetGroup?.pillSheets.first.allTaken, isTrue); + expect(state.pillSheetGroup?.pillSheets.first.isAllTaken, isTrue); expect( store.markFor(pillNumberIntoPillSheet: 1, pillSheet: pillSheetEntity), PillMarkType.done); @@ -184,7 +184,7 @@ void main() { ); await waitForResetStoreState(); - expect(state.pillSheetGroup?.pillSheets.first.allTaken, isFalse); + expect(state.pillSheetGroup?.pillSheets.first.isAllTaken, isFalse); expect( store.markFor(pillNumberIntoPillSheet: 1, pillSheet: pillSheetEntity), PillMarkType.done); @@ -261,7 +261,7 @@ void main() { ); await waitForResetStoreState(); - expect(state.pillSheetGroup?.pillSheets.first.allTaken, isTrue); + expect(state.pillSheetGroup?.pillSheets.first.isAllTaken, isTrue); for (int i = 1; i <= pillSheetEntity.pillSheetType.totalCount; i++) { expect( store.shouldPillMarkAnimation( @@ -333,7 +333,7 @@ void main() { ); await waitForResetStoreState(); - expect(state.pillSheetGroup?.pillSheets.first.allTaken, isFalse); + expect(state.pillSheetGroup?.pillSheets.first.isAllTaken, isFalse); expect( store.shouldPillMarkAnimation( pillNumberIntoPillSheet: 3, diff --git a/test/domain/record/record_page_store_test.dart b/test/domain/record/record_page_store_test.dart index 2e8220d69b..bed43c27a0 100644 --- a/test/domain/record/record_page_store_test.dart +++ b/test/domain/record/record_page_store_test.dart @@ -457,7 +457,7 @@ void main() { test("group has two pill sheet for first pillSheet.isFill pattern", () async { var mockTodayRepository = MockTodayService(); - final _today = DateTime.parse("2020-09-19"); + final _today = DateTime.parse("2022-05-29"); todayRepository = mockTodayRepository; when(mockTodayRepository.today()).thenReturn(_today); when(mockTodayRepository.now()).thenReturn(_today); @@ -474,9 +474,9 @@ void main() { final pillSheet = PillSheet( id: "sheet_id", typeInfo: PillSheetType.pillsheet_28_0.typeInfo, - beginingDate: _today.subtract(Duration(days: 28)), + beginingDate: DateTime.parse("2022-05-01"), groupIndex: 0, - lastTakenDate: _today.subtract(Duration(days: 1)), + lastTakenDate: DateTime.parse("2022-05-28"), ); final pillSheet2 = PillSheet( id: "sheet_id_2", @@ -608,7 +608,8 @@ void main() { ); final pillSheetService = MockPillSheetService(); when(pillSheetService.update(batch, [ - pillSheet.copyWith(lastTakenDate: DateTime.parse("2020-09-18")), + pillSheet.copyWith( + lastTakenDate: DateTime.parse("2020-09-18 23:59:59")), pillSheet2.copyWith(lastTakenDate: _today) ])).thenReturn(null); @@ -625,7 +626,8 @@ void main() { id: "group_id", pillSheetIDs: ["sheet_id", "sheet_id_2"], pillSheets: [ - pillSheet.copyWith(lastTakenDate: DateTime.parse("2020-09-18")), + pillSheet.copyWith( + lastTakenDate: DateTime.parse("2020-09-18 23:59:59")), pillSheet2.copyWith(lastTakenDate: _today), ], createdAt: _today, diff --git a/test/entity/pill_sheet_test.dart b/test/entity/pill_sheet_test.dart index 3cc270588b..529687f2c6 100644 --- a/test/entity/pill_sheet_test.dart +++ b/test/entity/pill_sheet_test.dart @@ -241,4 +241,20 @@ void main() { expect(model.isReached, false); }); }); + group("#estimatedLastTakenDate", () { + test("spec", () { + var sheetType = PillSheetType.pillsheet_21; + var pillSheet = PillSheet( + beginingDate: DateTime.parse("2022-05-01"), + typeInfo: PillSheetTypeInfo( + dosingPeriod: sheetType.dosingPeriod, + name: sheetType.fullName, + totalCount: sheetType.totalCount, + pillSheetTypeReferencePath: sheetType.rawPath, + ), + ); + expect(pillSheet.estimatedLastTakenDate, + DateTime.parse("2022-05-29").subtract(Duration(seconds: 1))); + }); + }); }