-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix opening hours to match the cafe Uses the flutter-native TimeOfDay class, which has localized time formatting. This has been extended with comparison methods <= and isInTimeslot. Timeslot class simplifies with this change. Simplify DateService to only contain one getter. Make sure of fpdart's Option instead of null values. Use None for closed days. Clean up tests, add more tests, and use NiceMocks for mocking. --------- Co-authored-by: Omid Marfavi <[email protected]>
- Loading branch information
1 parent
57e5565
commit 0ee2343
Showing
11 changed files
with
181 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
class DateService { | ||
int currentWeekday() => DateTime.now().weekday; | ||
int currentHour() => DateTime.now().hour; | ||
DateTime get currentDateTime => DateTime.now(); | ||
} |
22 changes: 12 additions & 10 deletions
22
lib/features/opening_hours/data/datasources/opening_hours_local_data_source.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
import 'package:coffeecard/features/opening_hours/domain/entities/timeslot.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class OpeningHoursLocalDataSource { | ||
Map<int, Timeslot> getOpeningHours() { | ||
const normalOperation = Timeslot(start: 8, end: 16); | ||
const shortDayOperation = Timeslot(start: 8, end: 14); | ||
const closed = Timeslot(); | ||
const openTime = TimeOfDay(hour: 8, minute: 0); | ||
const normalDayCloseTime = TimeOfDay(hour: 15, minute: 30); | ||
const shortDayCloseTime = TimeOfDay(hour: 13, minute: 30); | ||
|
||
const normalDayOpeningHours = Timeslot(openTime, normalDayCloseTime); | ||
const shortDayOpeningHours = Timeslot(openTime, shortDayCloseTime); | ||
|
||
return { | ||
DateTime.monday: normalOperation, | ||
DateTime.tuesday: normalOperation, | ||
DateTime.wednesday: normalOperation, | ||
DateTime.thursday: normalOperation, | ||
DateTime.friday: shortDayOperation, | ||
DateTime.saturday: closed, | ||
DateTime.sunday: closed, | ||
DateTime.monday: normalDayOpeningHours, | ||
DateTime.tuesday: normalDayOpeningHours, | ||
DateTime.wednesday: normalDayOpeningHours, | ||
DateTime.thursday: normalDayOpeningHours, | ||
DateTime.friday: shortDayOpeningHours, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,37 @@ | ||
import 'package:coffeecard/core/strings.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
/// A timeslot with a start and end time. | ||
class Timeslot extends Equatable { | ||
final int? start; | ||
final int? end; | ||
final TimeOfDay startTime; | ||
final TimeOfDay endTime; | ||
|
||
const Timeslot({this.start, this.end}); | ||
const Timeslot(this.startTime, this.endTime); | ||
|
||
bool get isClosed => start == null || end == null; | ||
String format(BuildContext context) { | ||
final start = startTime.format(context); | ||
final end = endTime.format(context); | ||
return '$start-$end'; | ||
} | ||
|
||
@override | ||
String toString() => isClosed | ||
? Strings.closed | ||
: '${start.toString().padLeft(2, '0')}:00 - $end:00'; | ||
List<Object?> get props => [startTime, endTime]; | ||
} | ||
|
||
@override | ||
List<Object?> get props => [start, end]; | ||
/// Operators for comparing [TimeOfDay]s. | ||
extension TimeOfDayOperators on TimeOfDay { | ||
/// Returns true if [other] is before [this]. | ||
bool operator <=(TimeOfDay other) { | ||
if (hour < other.hour) { | ||
return true; | ||
} else if (hour == other.hour) { | ||
return minute <= other.minute; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
bool isInTimeslot(Timeslot timeslot) { | ||
return timeslot.startTime <= this && this <= timeslot.endTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.