Skip to content

Commit

Permalink
Merge pull request #384 from bannzai/hotfix/function/record
Browse files Browse the repository at this point in the history
Fix taken method for straddle pill sheet
  • Loading branch information
bannzai authored Sep 30, 2021
2 parents c948a5f + ac2533e commit a59566f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/components/organisms/calendar/utility.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ List<DateRange> 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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 6 additions & 3 deletions lib/domain/record/util/take.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ Future<PillSheetGroup?> 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);
}
Expand Down
10 changes: 7 additions & 3 deletions lib/entity/pill_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
8 changes: 4 additions & 4 deletions test/domain/record/record_page_state_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions test/domain/record/record_page_store_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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",
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions test/entity/pill_sheet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
});
});
}

0 comments on commit a59566f

Please sign in to comment.