Files
Woka_Native_iOS/WOKA/Theme/Model/TimePeriod.swift
BilalKhanWDI 0e921c3113 - Handled the star and moon to show only at the night time.
- Added a star above the grass and added it to the animation.
- Woke Client call 1:15 - 1:40
- Added Sidebar theme clicks and added userdefaults with checks for default loading. Also added to logout
- Handled the Theme change from sidebar, also handled the default theme selected and loading it at the time of startup
- Fixed issue of sidebar showing up, left side blacktint was not coming. It was the issue of ViewLifeCycle
- Theme 2  Explore woka items size ratio updated for auto layouts
- Worked on RootView Navigation.
2024-05-27 19:42:56 +05:30

89 lines
4.9 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, dayCycle: DayCycle)
case afternoon(startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor, dayCycle: DayCycle)
case evening(startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor, dayCycle: DayCycle)
case night(startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor, dayCycle: DayCycle)
// 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, dayCycle: DayCycle) {
switch self {
case .morning(let startTime, let endTime, let color1, let color2, let grass, let textColor, let dayCycle):
return (startTime, endTime, color1, color2, grass, textColor, dayCycle)
case .afternoon(let startTime, let endTime, let color1, let color2, let grass, let textColor, let dayCycle):
return (startTime, endTime, color1, color2, grass, textColor, dayCycle)
case .evening(let startTime, let endTime, let color1, let color2, let grass, let textColor, let dayCycle):
return (startTime, endTime, color1, color2, grass, textColor, dayCycle)
case .night(let startTime, let endTime, let color1, let color2, let grass, let textColor, let dayCycle):
return (startTime, endTime, color1, color2, grass, textColor, dayCycle)
}
}
// 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, dayCycle1), .morning(startTime2, endTime2, color21, color22, grass2, textColor2, dayCycle2)),
let (.afternoon(startTime1, endTime1, color11, color12, grass1, textColor1, dayCycle1), .afternoon(startTime2, endTime2, color21, color22, grass2, textColor2, dayCycle2)),
let (.evening(startTime1, endTime1, color11, color12, grass1, textColor1, dayCycle1), .evening(startTime2, endTime2, color21, color22, grass2, textColor2, dayCycle2)),
let (.night(startTime1, endTime1, color11, color12, grass1, textColor1, dayCycle1), .night(startTime2, endTime2, color21, color22, grass2, textColor2, dayCycle2)):
return startTime1 == startTime2 && endTime1 == endTime2 && color11 == color21 && color12 == color22 && grass1 == grass2 && textColor1 == textColor2 && dayCycle1 == dayCycle2
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
}
}
}
enum DayCycle{
case morning
case afternoon
case evening
case night
}