/* * Copyright (c) 2021 BWI GmbH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import Foundation @objcMembers class WorkTimeService : NSObject { static let minutesInDay = 60 * 24 var workTime : WorkTime let store : WorkTimeStore static func workTimeService(_ userId: String) -> WorkTimeService { let store = WorkTimeStore(userId: userId) return WorkTimeService(store) } init(_ store: WorkTimeStore) { self.store = store self.workTime = store.workTime } func setStartTime(_ startTime: Int) { var fixedStartTime = startTime if fixedStartTime < 0 { fixedStartTime = 0 } if fixedStartTime > WorkTimeService.minutesInDay { fixedStartTime = WorkTimeService.minutesInDay } self.workTime.startTime = fixedStartTime store.workTime = self.workTime } func setFinishTime(_ finishTime: Int) { var fixedFinishTime = finishTime if fixedFinishTime > WorkTimeService.minutesInDay { fixedFinishTime = WorkTimeService.minutesInDay } if fixedFinishTime < 0 { fixedFinishTime = 0 } self.workTime.finishTime = fixedFinishTime store.workTime = self.workTime } func addWorkDay(_ day: WorkTime.Days) { self.workTime.workdays.insert(day) store.workTime = self.workTime } func removeWorkDay(_ day: WorkTime.Days) { self.workTime.workdays.remove(day) store.workTime = self.workTime } func isNowWorkTime() -> Bool { isWorkTime(Date()) } func isWorkTime(_ date: Date) -> Bool { let weekday = Calendar.current.component(.weekday, from: date) if self.workTime.workdays.contains(WorkTime.Days(weekday)) { let minutes = Calendar.current.component(.hour, from: date)*60 + Calendar.current.component(.minute, from: date) if self.workTime.startTime <= self.workTime.finishTime { if self.workTime.startTime <= minutes && self.workTime.finishTime >= minutes { return true } } else { if self.workTime.startTime <= minutes || self.workTime.finishTime >= minutes { return true } } } return false } func isWorkTimeGlobalyEnabled() -> Bool { return store.workTimeEnabled } func enableWorkTime(_ enable: Bool) { store.workTimeEnabled = enable } func activateWorkTimeInRoom(_ roomId: String) { self.store.activateWorkTimeInRoom(roomId); } func deactivateWorkTimeInRoom(_ roomId: String) { self.store.deactivateWorkTimeInRoom(roomId) } func localizedWorkTime() -> String { return WorkTimeModel().formatedWorkTime(store.workTime) } }