Skip to content

Commit

Permalink
refactor: use appropriate variable names for DateRanger
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Jan 28, 2023
1 parent f7fc78f commit 5b54942
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 136 deletions.
38 changes: 17 additions & 21 deletions lib/src/model/booking_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ class BookingCollection with ChangeNotifier implements Serializable {
...singleBookingsFromRecurring,
});

Set<SingleBooking> singleBookingsBetween(DateRanger dateRange) =>
Set<SingleBooking> singleBookingsBetween(DateRanger dateRanger) =>
SplayTreeSet.of(
bookings.where((booking) => booking.overlapsWith(dateRange)),
bookings.where((booking) => booking.overlapsWith(dateRanger)),
);

Set<SingleBooking> recurringBookingsBetween(DateRanger dateRange) =>
Set<SingleBooking> recurringBookingsBetween(DateRanger dateRanger) =>
SplayTreeSet.of(
singleBookingsFromRecurring.where(
(recurringBooking) => recurringBooking.overlapsWith(dateRange),
(recurringBooking) => recurringBooking.overlapsWith(dateRanger),
),
);

Set<SingleBooking> allBookingsBetween(DateRanger dateRange) =>
Set<SingleBooking> allBookingsBetween(DateRanger dateRanger) =>
SplayTreeSet.of({
...singleBookingsBetween(dateRange),
...recurringBookingsBetween(dateRange),
...singleBookingsBetween(dateRanger),
...recurringBookingsBetween(dateRanger),
});

Set<SingleBooking> singleBookingsOn(DateTime dateTime) => SplayTreeSet.of(
Expand All @@ -82,7 +82,6 @@ class BookingCollection with ChangeNotifier implements Serializable {

Set<SingleBooking> recurringBookingsOn(DateTime dateTime) {
final filteredBookings = SplayTreeSet<SingleBooking>();

for (final recurringBooking in recurringBookings) {
final booking = recurringBooking.bookingOn(dateTime);

Expand Down Expand Up @@ -174,11 +173,10 @@ class BookingCollection with ChangeNotifier implements Serializable {
return runPercent;
}

Set<DateTime> datesWithBookings([DateRanger? dateRange]) {
Set<DateTime> datesWithBookings([DateRanger? dateRanger]) {
final dates = SplayTreeSet<DateTime>();

final bookingsList =
dateRange != null ? allBookingsBetween(dateRange) : allBookings;
dateRanger != null ? allBookingsBetween(dateRanger) : allBookings;

for (final booking in bookingsList) {
final shouldAddDate = dates.firstWhereOrNull(
Expand All @@ -196,7 +194,6 @@ class BookingCollection with ChangeNotifier implements Serializable {

Map<DateTime, int> get allBookingsCountPerDay {
final bookingsPerDay = SplayTreeMap<DateTime, int>();

for (final booking in allBookings) {
bookingsPerDay.update(
booking.dateOnly!,
Expand All @@ -208,11 +205,12 @@ class BookingCollection with ChangeNotifier implements Serializable {
return bookingsPerDay;
}

Map<DateTime, Duration> occupiedDurationPerWeek([DateRanger? dateRange]) {
Map<DateTime, Duration> occupiedDurationPerWeek([DateRanger? dateRanger]) {
final bookingsPerDay = SplayTreeMap<DateTime, Duration>();

for (final booking in allBookings) {
if (dateRange != null && !dateRange.includes(booking.dateOnly!)) continue;
if (dateRanger != null && !dateRanger.includes(booking.dateOnly!)) {
continue;
}

bookingsPerDay.update(
booking.dateOnly!.firstDayOfWeek,
Expand All @@ -225,14 +223,13 @@ class BookingCollection with ChangeNotifier implements Serializable {
}

Map<TimeOfDay, Duration> accumulatedTimeRangesOccupancy([
DateRanger? dateRange,
DateRanger? dateRanger,
]) {
final timeRanges =
SplayTreeMap<TimeOfDay, Duration>(TimeOfDayExtension.compare);

final bookingsSet =
dateRange != null ? allBookingsBetween(dateRange) : allBookings;

dateRanger != null ? allBookingsBetween(dateRanger) : allBookings;
for (final booking in bookingsSet) {
for (final bookingTimeRange in booking.hoursSpan.entries) {
timeRanges.update(
Expand All @@ -256,7 +253,6 @@ class BookingCollection with ChangeNotifier implements Serializable {
accumulatedTimeRangesOccupancy.entries,
(a, b) => a.value.compareTo(b.value),
);

final highestOccupancyDuration = timeRangesSortedByDuration.first.value;

return SplayTreeSet.of(
Expand All @@ -267,9 +263,9 @@ class BookingCollection with ChangeNotifier implements Serializable {
);
}

Set<TimeOfDay> mostOccupiedTimeRange([DateRanger? dateRange]) =>
Set<TimeOfDay> mostOccupiedTimeRange([DateRanger? dateRanger]) =>
mostOccupiedTimeRangeFromAccumulated(
accumulatedTimeRangesOccupancy(dateRange),
accumulatedTimeRangesOccupancy(dateRanger),
);

SingleBooking singleBookingFromId(String id) =>
Expand Down
34 changes: 12 additions & 22 deletions lib/src/model/cabin_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ class CabinCollection extends WritableManager<Set<Cabin>>

int get lastCabinNumber => cabins.isEmpty ? 0 : cabins.last.number;

Set<DateTime> allCabinsDatesWithBookings([DateRanger? dateRange]) =>
Set<DateTime> allCabinsDatesWithBookings([DateRanger? dateRanger]) =>
SplayTreeSet.of({
for (final cabin in cabins)
...cabin.bookingCollection.datesWithBookings(dateRange),
...cabin.bookingCollection.datesWithBookings(dateRanger),
});

Map<DateTime, int> get allCabinsBookingsCountPerDay {
final bookingsPerDay = <DateTime, int>{};

for (final cabin in cabins) {
for (final bookingsCount
in cabin.bookingCollection.allBookingsCountPerDay.entries) {
Expand All @@ -75,15 +74,13 @@ class CabinCollection extends WritableManager<Set<Cabin>>
}

Map<TimeOfDay, Duration> accumulatedTimeRangesOccupancy([
DateRanger? dateRange,
DateRanger? dateRanger,
]) {
final timeRanges =
SplayTreeMap<TimeOfDay, Duration>(TimeOfDayExtension.compare);

for (final cabin in cabins) {
final accumulatedTimeRanges =
cabin.bookingCollection.accumulatedTimeRangesOccupancy(dateRange);

cabin.bookingCollection.accumulatedTimeRangesOccupancy(dateRanger);
for (final bookingTimeRange in accumulatedTimeRanges.entries) {
timeRanges.update(
bookingTimeRange.key,
Expand All @@ -96,14 +93,13 @@ class CabinCollection extends WritableManager<Set<Cabin>>
return timeRanges;
}

Set<TimeOfDay> mostOccupiedTimeRange([DateRanger? dateRange]) =>
Set<TimeOfDay> mostOccupiedTimeRange([DateRanger? dateRanger]) =>
BookingCollection.mostOccupiedTimeRangeFromAccumulated(
accumulatedTimeRangesOccupancy(dateRange),
accumulatedTimeRangesOccupancy(dateRanger),
);

int get allBookingsCount {
var count = 0;

for (final cabin in cabins) {
count += cabin.bookingCollection.allBookings.length;
}
Expand All @@ -113,7 +109,6 @@ class CabinCollection extends WritableManager<Set<Cabin>>

int get bookingsCount {
var count = 0;

for (final cabin in cabins) {
count += cabin.bookingCollection.bookings.length;
}
Expand All @@ -123,7 +118,6 @@ class CabinCollection extends WritableManager<Set<Cabin>>

int get recurringBookingsCount {
var count = 0;

for (final cabin in cabins) {
count += cabin.bookingCollection.singleBookingsFromRecurring.length;
}
Expand All @@ -144,13 +138,11 @@ class CabinCollection extends WritableManager<Set<Cabin>>
return duration;
}

Map<DateTime, Duration> occupiedDurationPerWeek([DateRanger? dateRange]) {
Map<DateTime, Duration> occupiedDurationPerWeek([DateRanger? dateRanger]) {
final bookingsPerDay = SplayTreeMap<DateTime, Duration>();

for (final cabin in cabins) {
final occupiedDuration =
cabin.bookingCollection.occupiedDurationPerWeek(dateRange);

cabin.bookingCollection.occupiedDurationPerWeek(dateRanger);
for (final durationPerWeek in occupiedDuration.entries) {
bookingsPerDay.update(
durationPerWeek.key,
Expand Down Expand Up @@ -183,22 +175,20 @@ class CabinCollection extends WritableManager<Set<Cabin>>
percents.length;
}

int bookingsCountBetween(DateRanger dateRange) {
int bookingsCountBetween(DateRanger dateRanger) {
var count = 0;

for (final cabin in cabins) {
count += cabin.bookingCollection.singleBookingsBetween(dateRange).length;
count += cabin.bookingCollection.singleBookingsBetween(dateRanger).length;
}

return count;
}

int recurringBookingsCountBetween(DateRanger dateRange) {
int recurringBookingsCountBetween(DateRanger dateRanger) {
var count = 0;

for (final cabin in cabins) {
count +=
cabin.bookingCollection.recurringBookingsBetween(dateRange).length;
cabin.bookingCollection.recurringBookingsBetween(dateRanger).length;
}

return count;
Expand Down
8 changes: 4 additions & 4 deletions lib/widgets/item/activity_line_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import 'package:flutter/material.dart';

class ActivityLineChart extends StatelessWidget {
final Map<DateTime, Duration> occupiedDurationPerWeek;
final DateRanger dateRange;
final DateRanger dateRanger;
final String? tooltipMessage;

const ActivityLineChart({
super.key,
this.occupiedDurationPerWeek = const {},
required this.dateRange,
required this.dateRanger,
this.tooltipMessage,
});

Expand All @@ -35,8 +35,8 @@ class ActivityLineChart extends StatelessWidget {
for (final entry in occupiedDurationPerWeek.entries)
FlSpot(entry.key.toDouble(), entry.value.inMinutes.toDouble()),
],
minX: dateRange.startDate?.toDouble(),
maxX: dateRange.endDate?.toDouble(),
minX: dateRanger.startDate?.toDouble(),
maxX: dateRanger.endDate?.toDouble(),
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/item/items_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class _ItemsTableState<T extends Item> extends State<ItemsTable<T>> {
DataCell(
ActivityLineChart(
occupiedDurationPerWeek: row.occupiedDurationPerWeek,
dateRange: row.item is DateRanger
dateRanger: row.item is DateRanger
? row.item as DateRanger
: DateRange(
startDate: DateTime.now()
Expand Down
15 changes: 9 additions & 6 deletions test/model/date/date_range_item_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,30 @@ void main() {

group('.copyWith()', () {
test('should create a copy of this DateRangeItem', () {
final dateRange = DateRangeItem(
final dateRangeItem = DateRangeItem(
startDate: DateTime.utc(2022, 9, 11),
endDate: DateTime.utc(2023, 6, 23),
);
expect(dateRange, dateRange.copyWith());
expect(identical(dateRange, dateRange.copyWith()), isFalse);
expect(identical(dateRange.copyWith(), dateRange.copyWith()), isFalse);
expect(dateRangeItem, dateRangeItem.copyWith());
expect(identical(dateRangeItem, dateRangeItem.copyWith()), isFalse);
expect(
identical(dateRangeItem.copyWith(), dateRangeItem.copyWith()),
isFalse,
);
});

test(
'should create a copy of this DateRangeItem replacing old values',
() {
final startDate = DateTime(2022, 12, 4);
final endDate = DateTime(2022, 12, 5);
final dateRange = DateRangeItem(
final dateRangeItem = DateRangeItem(
id: 'date-range-item-id',
startDate: startDate,
endDate: endDate,
);
expect(
dateRange,
dateRangeItem,
DateRangeItem(
id: 'date-range-item-id',
startDate: DateTime.utc(2022, 9, 11),
Expand Down
Loading

0 comments on commit 5b54942

Please sign in to comment.