/* * 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 import KeychainAccess @objcMembers final class WorkTimeStore : NSObject { private enum StoreKeys { static let WorkTimeEnabledKey = "WorkTimeEnabled" static let WorkTimeKey = "WorkTime" static let WorkTimeInRoomsKey = "WorkTimeInRooms" } private struct BwiSettingsConstants { static let bwiSettingsKeychainService: String = BuildSettings.baseBundleIdentifier + ".bwi-WorkTime-service" } private let vault: KeyValueVault private let userId: String init(userId : String) { self.userId = userId vault = KeychainVault(Keychain(service: BwiSettingsConstants.bwiSettingsKeychainService, accessGroup: BuildSettings.keychainAccessGroup)) super.init() } func reset() { do { try vault.removeObject(forKey: StoreKeys.WorkTimeKey) try vault.removeObject(forKey: StoreKeys.WorkTimeEnabledKey) try vault.removeObject(forKey: StoreKeys.WorkTimeInRoomsKey) } catch let error { NSLog("[WorkTime] Error when removing objects: \(error)") } } // MARK: Servers var workTimeEnabled : Bool { get { do { return try vault.bool(forKey: StoreKeys.WorkTimeEnabledKey) ?? false } catch { return false } } set { do { try vault.set(newValue, forKey: StoreKeys.WorkTimeEnabledKey) } catch let error { NSLog("[WorkTime] Error when storing rest time enabled to the vault: \(error)") } } } var workTime : WorkTime { get { do { guard let data = try vault.data(forKey: StoreKeys.WorkTimeKey) else { return WorkTime() } return try JSONDecoder().decode(WorkTimeUser.self, from: data).WorkTime } catch { return WorkTime() } } set { do { let userWorkTime = WorkTimeUser(WorkTime: newValue, userId: userId) let data = try JSONEncoder().encode(userWorkTime) try vault.set(data, forKey: StoreKeys.WorkTimeKey) } catch let error { NSLog("[WorkTime] Error when storing addional header to the vault: \(error)") } } } func activateWorkTimeInRoom(_ roomId: String) { let workTimeInRoom = WorkTimeInRoom(userId: userId, roomId: roomId, isWorkTimeActive: true) var roomSet = roomSetFromStore() roomSet.update(with: workTimeInRoom) saveRoomSetToStore(roomSet) } func deactivateWorkTimeInRoom(_ roomId: String) { let workTimeInRoom = WorkTimeInRoom(userId: userId, roomId: roomId, isWorkTimeActive: false) var roomSet = roomSetFromStore() roomSet.update(with: workTimeInRoom) saveRoomSetToStore(roomSet) } @objc func isWorkTimeRoom(_ roomId: String) -> Bool { let roomSet = roomSetFromStore() if roomSet.first(where: {$0.roomId == roomId && $0.userId == userId}) != nil { return true } return false } @objc func isWorkTimeInRoomActive(_ roomId: String) -> Bool { let roomSet = roomSetFromStore() if let WorkTimeInRoom = roomSet.first(where: {$0.roomId == roomId && $0.userId == userId}) { return WorkTimeInRoom.isWorkTimeActive } return false } private func roomSetFromStore() -> Set { do { guard let data = try vault.data(forKey: StoreKeys.WorkTimeInRoomsKey) else { return Set() } return try JSONDecoder().decode(Set.self, from: data) } catch { return Set() } } private func saveRoomSetToStore(_ roomSet: Set) { do { print("saveRoomSetToStore", roomSet) let dataOut = try JSONEncoder().encode(roomSet) try vault.set(dataOut, forKey: StoreKeys.WorkTimeInRoomsKey) } catch { } } }