Skip to content

Commit

Permalink
Merge f5c020a into f7fc78f
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Jan 28, 2023
2 parents f7fc78f + f5c020a commit efa22ec
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 45 deletions.
42 changes: 12 additions & 30 deletions lib/src/model/booking_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter/material.dart';
import 'booking/booking.dart';
import 'booking/recurring_booking.dart';
import 'booking/single_booking.dart';
import 'date/date_range.dart';
import 'date/date_ranger.dart';
import 'serializable.dart';

Expand Down Expand Up @@ -114,41 +115,23 @@ class BookingCollection with ChangeNotifier implements Serializable {
null;
}

Duration occupiedDuration({DateTime? dateTime, DateRanger? dateRange}) {
if (dateTime != null && dateRange != null) {
throw ArgumentError(
'Either dateTime or dateRange must be given, but not both.',
);
}
Duration occupiedDuration([DateRanger? dateRanger]) {
final bookingsList =
dateRanger != null ? allBookingsBetween(dateRanger) : allBookings;

var runDuration = Duration.zero;

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

for (final booking in bookingsList) {
runDuration += booking.duration;
}

return runDuration;
}

double occupancyPercentOn(
DateTime? dateTime, {
required TimeOfDay startTime,
required TimeOfDay endTime,
}) {
final fallbackDateTime = dateTime ?? DateTime.now();
final startDate = fallbackDateTime.addLocalTimeOfDay(startTime);
final endDate = fallbackDateTime.addLocalTimeOfDay(endTime);
double occupancyPercentOn([DateRanger? dateRanger]) {
dateRanger ??= DateRange.today();

final maxViewDuration = endDate.difference(startDate);

return occupiedDuration(dateTime: fallbackDateTime).inMicroseconds /
maxViewDuration.inMicroseconds;
return occupiedDuration(dateRanger).inMicroseconds /
dateRanger.duration.inMicroseconds;
}

double occupancyPercent({
Expand All @@ -158,15 +141,14 @@ class BookingCollection with ChangeNotifier implements Serializable {
}) {
var runPercent = 0.0;
var count = 0;

for (final dateTime in dates ?? datesWithBookings()) {
count++;

final currentPercent = occupancyPercentOn(
dateTime,
startTime: startTime,
endTime: endTime,
final dateRanger = DateRange(
startDate: dateTime.addLocalTimeOfDay(startTime),
endDate: dateTime.addLocalTimeOfDay(endTime),
);
final currentPercent = occupancyPercentOn(dateRanger);

runPercent += (currentPercent - runPercent) / count;
}
Expand Down
8 changes: 2 additions & 6 deletions lib/src/model/cabin_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,10 @@ class CabinCollection extends WritableManager<Set<Cabin>>
return count;
}

Duration totalOccupiedDuration({DateTime? dateTime, DateRanger? dateRange}) {
Duration totalOccupiedDuration([DateRanger? dateRanger]) {
var duration = Duration.zero;

for (final cabin in cabins) {
duration += cabin.bookingCollection.occupiedDuration(
dateTime: dateTime,
dateRange: dateRange,
);
duration += cabin.bookingCollection.occupiedDuration(dateRanger);
}

return duration;
Expand Down
14 changes: 8 additions & 6 deletions lib/widgets/layout/scrollable_time_table.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:cabin_booking/constants.dart';
import 'package:cabin_booking/model.dart';
import 'package:cabin_booking/utils/date_time_extension.dart';
import 'package:cabin_booking/widgets/booking/booking_preview_panel_overlay.dart';
import 'package:cabin_booking/widgets/booking/bookings_table.dart';
import 'package:cabin_booking/widgets/cabin/cabin_icon.dart';
Expand Down Expand Up @@ -86,6 +87,11 @@ class _ScrollablePanelOverlayTimeTableState
Widget build(BuildContext context) {
final theme = Theme.of(context);

final dateRange = DateRange(
startDate: widget.dateTime.addLocalTimeOfDay(kTimeTableStartTime),
endDate: widget.dateTime.addLocalTimeOfDay(kTimeTableEndTime),
);

return LayoutBuilder(
builder: (context, constraints) {
final bookingStackWidth = _stackWidth(constraints.maxWidth);
Expand All @@ -110,12 +116,8 @@ class _ScrollablePanelOverlayTimeTableState
width: bookingStackWidth,
child: CabinIcon.progress(
number: cabin.number,
progress:
cabin.bookingCollection.occupancyPercentOn(
widget.dateTime,
startTime: kTimeTableStartTime,
endTime: kTimeTableEndTime,
),
progress: cabin.bookingCollection
.occupancyPercentOn(dateRange),
),
),
],
Expand Down
5 changes: 2 additions & 3 deletions lib/widgets/school_year/school_years_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ class SchoolYearsTable extends StatelessWidget {
bookingsCount: cabinCollection.bookingsCountBetween(schoolYear),
recurringBookingsCount:
cabinCollection.recurringBookingsCountBetween(schoolYear),
occupiedDuration: cabinCollection.totalOccupiedDuration(
dateRange: schoolYear,
),
occupiedDuration:
cabinCollection.totalOccupiedDuration(schoolYear),
occupiedDurationPerWeek:
cabinCollection.occupiedDurationPerWeek(schoolYear)
..fillEmptyKeyValues(
Expand Down

0 comments on commit efa22ec

Please sign in to comment.