Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Uplift/Models/Gym.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,66 @@ struct Gym: Hashable {
facilities.filter { $0.facilityType != .fitness }
}

/**
Determine whether at least one fitness center is open at this `Gym` depending on its fitness centers' hours.

- Parameters:
- currentTime: The current time to compare determine the status. Default is now.

- Returns: A `Bool` representing whether at least one of its fitness centers is open.
*/
func fitnessCenterIsOpen(currentTime: Date = Date.now) -> Bool {
fitnessCenters.contains {
switch $0.hours.getStatus(currentTime: currentTime) {
case .open:
return true
default:
return false
}
}
}

/**
Retrieve the status of the `Gym` depending on the fitness centers' hours.

- Parameters:
- currentTime: The current time to compare determine the status. Default is now.

- Returns: A `Status` object based on its fitness centers' hours. `nil`if there are no open or close hours in the future.
*/
func determineStatus(currentTime: Date = Date.now) -> Status? {
if fitnessCenterIsOpen(currentTime: currentTime) {
// Get all possible close times
let closeTimes = fitnessCenters.compactMap {
switch $0.hours.getStatus(currentTime: currentTime) {
case .open(let closeTime):
return closeTime
default:
return nil
}
}

// Get the latest closing time
if let closeTime = closeTimes.max() {
return Status.open(closeTime: closeTime)
}
} else {
// Get all possible open times
let openTimes = fitnessCenters.compactMap {
switch $0.hours.getStatus(currentTime: currentTime) {
case .closed(let openTime):
return openTime
default:
return nil
}
}

// Get the earliest open time
if let openTime = openTimes.min() {
return Status.closed(openTime: openTime)
}
}
return nil
}

}
Loading