For API data for day(different timings for all days and Sunday closed) and date, get the entering time and leaving time, from that change the label as bold in available time and strike on unavailable times.
Before entering time all times are unavailable and after leaving all times are unavailable and also lunch time unavailable.
How to write the condition from API data to the label change (to bold/ strike)?
API response:
Optional(<__NSSingleObjectArrayI 0x600003873050>(
{
day = Saturday;
dArray = "<null>";
enteringTime = "09.00 am";
entryTime = "<null>";
exitTime = "<null>";
id = 13;
leavingTime = "06.00 pm";
lunchtimeFrom = "13:00";
lunchtimeTo = "14:00";
}
Code Snippet:
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "08:05 AM")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
time805Lbl.attributedText = attributeString
Define Date format in your viewController
-
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
Now, add a method to check available time
func checkAvailableTime(currentTime: String) -> NSMutableAttributedString {
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: currentTime)
var time = "\(times["lunchtimeFrom"]!)"
let lunchStartTime = dateFormatter.date(from: time)
time = "\(times["lunchtimeTo"]!)"
let lunchEndTime = dateFormatter.date(from: time)
let personCheckTime = dateFormatter.date(from: currentTime)
if (lunchStartTime!.compare(personCheckTime!) == .orderedAscending) && (lunchEndTime!.compare(personCheckTime!) == .orderedDescending){
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
}
return attributeString
}
Time to call that method -
time805Lbl.attributedText = checkAvailableTime(currentTime: time805Lbl.text!)
Where times
is your time response.
I hope you get response as below -
let times = [
"day":"Saturday",
"dArray":"<null>",
"enteringTime":"09:00 AM",
"entryTime":"<null>",
"exitTime":"<null>",
"id": 13,
"leavingTime":"06:00 PM",
"lunchtimeFrom":"1:00 PM",
"lunchtimeTo":"2:00 PM"
] as [String : Any]
Let me know if you are still having any issue.