Files
Woka_Native_iOS/WOKA/Home/Model/TimePeriod.swift
BilalKhanWDI 33180a55ab - Icon setup on home screen in auto layout
- Added the LiveTv Moving View. Converted the timer into animation for the moving view.
- Made a Enum with custom functions to handle the time.
- Added logic to handle the dark grass for night time
- Rescaled the home grass so dark grass and light grass has identical dimensions
- Handled the night mode with white text for clear text
2024-05-23 19:27:34 +05:30

81 lines
4.5 KiB
Swift

//
// TimePeriod.swift
// WOKA
//
// Created by MacBook Pro on 23/05/24.
//
import UIKit
// Enumeration representing different periods of the day, each with a start time, end time, and two colors.
enum TimePeriod : Equatable{
// Enumeration cases for different periods of the day with associated values.
case morning(startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor)
case afternoon(startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor)
case evening(startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor)
case night(startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor)
// Computed property to get the details of each time period as a tuple.
var details: (startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor) {
switch self {
case .morning(let startTime, let endTime, let color1, let color2, let grass, let textColor):
return (startTime, endTime, color1, color2, grass, textColor)
case .afternoon(let startTime, let endTime, let color1, let color2, let grass, let textColor):
return (startTime, endTime, color1, color2, grass, textColor)
case .evening(let startTime, let endTime, let color1, let color2, let grass, let textColor):
return (startTime, endTime, color1, color2, grass, textColor)
case .night(let startTime, let endTime, let color1, let color2, let grass, let textColor):
return (startTime, endTime, color1, color2, grass, textColor)
}
}
// Function to check if a given current time falls within the time period.
func contains(currentTime: Date) -> Bool {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
guard let start = formatter.date(from: details.startTime),
let end = formatter.date(from: details.endTime) else { return false }
return isTime(currentTime, between: start, and: end)
}
// Equatable protocol conformance to compare two TimePeriod instances.
static func ==(lhs: TimePeriod, rhs: TimePeriod) -> Bool {
switch (lhs, rhs) {
case let (.morning(startTime1, endTime1, color11, color12, grass1, textColor1), .morning(startTime2, endTime2, color21, color22, grass2, textColor2)),
let (.afternoon(startTime1, endTime1, color11, color12, grass1, textColor1), .afternoon(startTime2, endTime2, color21, color22, grass2, textColor2)),
let (.evening(startTime1, endTime1, color11, color12, grass1, textColor1), .evening(startTime2, endTime2, color21, color22, grass2, textColor2)),
let (.night(startTime1, endTime1, color11, color12, grass1, textColor1), .night(startTime2, endTime2, color21, color22, grass2, textColor2)):
return startTime1 == startTime2 && endTime1 == endTime2 && color11 == color21 && color12 == color22 && grass1 == grass2 && textColor1 == textColor2
default:
return false
}
}
// Helper function to check if a given time falls between a start and end time.
func isTime(_ time: Date, between startTime: Date, and endTime: Date) -> Bool {
let calendar = Calendar.current
let currentComponents = calendar.dateComponents([.hour, .minute], from: time)
let startComponents = calendar.dateComponents([.hour, .minute], from: startTime)
let endComponents = calendar.dateComponents([.hour, .minute], from: endTime)
guard let currentHour = currentComponents.hour, let currentMinute = currentComponents.minute,
let startHour = startComponents.hour, let startMinute = startComponents.minute,
let endHour = endComponents.hour, let endMinute = endComponents.minute else {
return false
}
let currentTotalMinutes = currentHour * 60 + currentMinute
let startTotalMinutes = startHour * 60 + startMinute
let endTotalMinutes = endHour * 60 + endMinute
if startTotalMinutes <= endTotalMinutes {
// Regular case where the time period does not cross midnight.
return currentTotalMinutes >= startTotalMinutes && currentTotalMinutes <= endTotalMinutes
} else {
// Special case where the time period crosses midnight.
return currentTotalMinutes >= startTotalMinutes || currentTotalMinutes <= endTotalMinutes
}
}
}