diff --git a/TraccarClient.xcodeproj/project.pbxproj b/TraccarClient.xcodeproj/project.pbxproj index 027bfbe6c4d2103e0e5878b836c00384a9603877..e54a3fcba7045cef1bfe2e85048a5467e0e3a36c 100644 --- a/TraccarClient.xcodeproj/project.pbxproj +++ b/TraccarClient.xcodeproj/project.pbxproj @@ -124,6 +124,9 @@ 53F10AF52AF0F306004D0529 /* ConnectViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 53F10AF32AF0F306004D0529 /* ConnectViewController.xib */; }; 53F10AF72AF101DB004D0529 /* UIColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53F10AF62AF101DB004D0529 /* UIColor.swift */; }; 53F10AF92AF101EC004D0529 /* Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53F10AF82AF101EC004D0529 /* Color.swift */; }; + 53FFDB9B2AF8FD1D0071F396 /* API+Permissions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53FFDB9A2AF8FD1D0071F396 /* API+Permissions.swift */; }; + 53FFDBA02AFA1E6C0071F396 /* PermissionsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53FFDB9F2AFA1E6C0071F396 /* PermissionsModel.swift */; }; + 53FFDBA22AFA200E0071F396 /* TrackingParametersModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53FFDBA12AFA200E0071F396 /* TrackingParametersModel.swift */; }; 5E394EBE28A9CC7600396F33 /* BatteryStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E394EBD28A9CC7600396F33 /* BatteryStatus.swift */; }; 5E716A271F63A0B100A2DBC3 /* DistanceCalculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E716A261F63A0B100A2DBC3 /* DistanceCalculator.swift */; }; 5E716A291F63A45A00A2DBC3 /* RequestManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E716A281F63A45A00A2DBC3 /* RequestManager.swift */; }; @@ -290,6 +293,9 @@ 53F10AF32AF0F306004D0529 /* ConnectViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ConnectViewController.xib; sourceTree = "<group>"; }; 53F10AF62AF101DB004D0529 /* UIColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColor.swift; sourceTree = "<group>"; }; 53F10AF82AF101EC004D0529 /* Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; }; + 53FFDB9A2AF8FD1D0071F396 /* API+Permissions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "API+Permissions.swift"; sourceTree = "<group>"; }; + 53FFDB9F2AFA1E6C0071F396 /* PermissionsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PermissionsModel.swift; sourceTree = "<group>"; }; + 53FFDBA12AFA200E0071F396 /* TrackingParametersModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrackingParametersModel.swift; sourceTree = "<group>"; }; 5E394EBD28A9CC7600396F33 /* BatteryStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatteryStatus.swift; sourceTree = "<group>"; }; 5E716A261F63A0B100A2DBC3 /* DistanceCalculator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DistanceCalculator.swift; sourceTree = "<group>"; }; 5E716A281F63A45A00A2DBC3 /* RequestManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestManager.swift; sourceTree = "<group>"; }; @@ -427,6 +433,8 @@ 53157EC52AEE4076003C9B6A /* UserModel.swift */, 53157EC72AEE4D6E003C9B6A /* SignInModel.swift */, 53157EC92AEE4DFE003C9B6A /* DeviceModel.swift */, + 53FFDB9F2AFA1E6C0071F396 /* PermissionsModel.swift */, + 53FFDBA12AFA200E0071F396 /* TrackingParametersModel.swift */, ); name = Models; sourceTree = "<group>"; @@ -564,6 +572,7 @@ 5326838A2AE92CA500A364C0 /* JSONField.swift */, 532683B42AE98FB600A364C0 /* UIString.swift */, 53157EC22AEE3FF9003C9B6A /* API+Auth.swift */, + 53FFDB9A2AF8FD1D0071F396 /* API+Permissions.swift */, ); name = Network; sourceTree = "<group>"; @@ -657,8 +666,8 @@ 53D62E3C2AEFA2F800C80BAC /* InitialViewController */ = { isa = PBXGroup; children = ( - 53D62E3D2AEFA31200C80BAC /* InitialViewController.swift */, - 53D62E3E2AEFA31200C80BAC /* InitialViewController.xib */, + 53FFDB9E2AFA1E560071F396 /* Controller */, + 53FFDB9D2AFA1E4E0071F396 /* View */, ); path = InitialViewController; sourceTree = "<group>"; @@ -720,6 +729,22 @@ path = MainViewController; sourceTree = "<group>"; }; + 53FFDB9D2AFA1E4E0071F396 /* View */ = { + isa = PBXGroup; + children = ( + 53D62E3E2AEFA31200C80BAC /* InitialViewController.xib */, + ); + path = View; + sourceTree = "<group>"; + }; + 53FFDB9E2AFA1E560071F396 /* Controller */ = { + isa = PBXGroup; + children = ( + 53D62E3D2AEFA31200C80BAC /* InitialViewController.swift */, + ); + path = Controller; + sourceTree = "<group>"; + }; CED4870F17DB1BF6007FCF57 = { isa = PBXGroup; children = ( @@ -1001,6 +1026,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 53FFDBA22AFA200E0071F396 /* TrackingParametersModel.swift in Sources */, 53157EC32AEE3FF9003C9B6A /* API+Auth.swift in Sources */, 5326833C2AE9175900A364C0 /* LoginViewController.swift in Sources */, 530080382AF15CE000A05E04 /* TransactionsViewController.swift in Sources */, @@ -1019,6 +1045,7 @@ 53EBC6062AF1A43900601AA7 /* SettingsSwitchTableViewCell.swift in Sources */, CB7ED0821F661B4F00A33FCF /* PositionProvider.swift in Sources */, 5300803D2AF1803100A05E04 /* TransactionTableViewCell.swift in Sources */, + 53FFDBA02AFA1E6C0071F396 /* PermissionsModel.swift in Sources */, 53554AE52AED1BF10018BAEE /* UIButton.swift in Sources */, 53554AE02AED1B480018BAEE /* KNButtonViewHelper.swift in Sources */, 53157EDC2AEE5841003C9B6A /* UIWindow.swift in Sources */, @@ -1055,6 +1082,7 @@ 532683652AE91D0800A364C0 /* UIFont.swift in Sources */, CBCE82F21B8D265800A7318B /* TraccarClient.xcdatamodeld in Sources */, 532683762AE923A500A364C0 /* KNTextFieldViewHelper.swift in Sources */, + 53FFDB9B2AF8FD1D0071F396 /* API+Permissions.swift in Sources */, 53F10AEC2AF06556004D0529 /* UIImage.swift in Sources */, 532683A72AE94ADC00A364C0 /* KNObject.swift in Sources */, 532683A12AE948FF00A364C0 /* AppManager.swift in Sources */, diff --git a/TraccarClient.xcodeproj/project.xcworkspace/xcuserdata/g.makhoul.xcuserdatad/UserInterfaceState.xcuserstate b/TraccarClient.xcodeproj/project.xcworkspace/xcuserdata/g.makhoul.xcuserdatad/UserInterfaceState.xcuserstate index 4ea608581c47e9d65c3c58fc1a48a69a8b3f6327..ea9252c735be2e5812623d170c6ff2795900089d 100644 Binary files a/TraccarClient.xcodeproj/project.xcworkspace/xcuserdata/g.makhoul.xcuserdatad/UserInterfaceState.xcuserstate and b/TraccarClient.xcodeproj/project.xcworkspace/xcuserdata/g.makhoul.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/TraccarClient/API+Auth.swift b/TraccarClient/API+Auth.swift index cbde6380c2de110258be39bb1346bf8cc321bc6a..f86f7cbf8199b46763b175598afbf7f06211140e 100644 --- a/TraccarClient/API+Auth.swift +++ b/TraccarClient/API+Auth.swift @@ -10,8 +10,12 @@ import Foundation extension NetworkRequest { // MARK: - Signin - func signin(params: Parameters, _ completion: @escaping ResultClosure) { - request(endpoint: endpoint(.signin), method: .post, params: params) { result in + func signin(params: Parameters, force: Bool = false , _ completion: @escaping ResultClosure) { + var parameters: Parameters = params + if force == true { + parameters.add("force", true) + } + request(endpoint: endpoint(.signin), method: .post, params: parameters) { result in let saveUser: Bool = result.success let user = SignInModel(result.object, saveUser: saveUser) result.data = user diff --git a/TraccarClient/API+Permissions.swift b/TraccarClient/API+Permissions.swift new file mode 100644 index 0000000000000000000000000000000000000000..8b0895c0dca4f8e79e2a1d0bace6cb85a0577812 --- /dev/null +++ b/TraccarClient/API+Permissions.swift @@ -0,0 +1,33 @@ +// +// API+Permissions.swift +// TraccarClient +// +// Created by George Makhoul on 06/11/2023. +// Copyright © 2023 Traccar. All rights reserved. +// + +import Alamofire +import Foundation + +extension NetworkRequest { + // MARK: - Signin + func permissions(_ completion: @escaping ResultClosure) { + request(endpoint: endpoint(.permissions), method: .get) { result in + let save: Bool = result.success + let permissions = PermissionsModel(result.object, save: save) + result.data = permissions + print(result) + completion(result) + } + } + + func parameters(_ completion: @escaping ResultClosure) { + request(endpoint: endpoint(.parameters), method: .get) { result in + let save: Bool = result.success + let parameters = TrackingParametersModel(result.object, save: save) + result.data = parameters + print(result) + completion(result) + } + } +} diff --git a/TraccarClient/Custom/CustomHeaderView/CustomHeaderView.swift b/TraccarClient/Custom/CustomHeaderView/CustomHeaderView.swift index 1727ba51fa93da04542c1ae6181cc9e2d360a9f7..5834a853aa30a8f4961e68f3b984f3cdb6331ed6 100644 --- a/TraccarClient/Custom/CustomHeaderView/CustomHeaderView.swift +++ b/TraccarClient/Custom/CustomHeaderView/CustomHeaderView.swift @@ -88,7 +88,7 @@ final class CustomHeaderView: KNComponentView { button.set(title: "") rightButton.set(title: "") - + rightButton.isHidden = !UserDefaults.standard.bool(forKey: Keys.User.updateSettings) button.set(icon: .back) rightButton.set(icon: .apply) } diff --git a/TraccarClient/InitialViewController/InitialViewController.swift b/TraccarClient/InitialViewController/Controller/InitialViewController.swift similarity index 71% rename from TraccarClient/InitialViewController/InitialViewController.swift rename to TraccarClient/InitialViewController/Controller/InitialViewController.swift index 41ed071998a168f65eeea71be6cc35ed99334837..f38220b86558df18f234f7570fb11fbbeef260af 100644 --- a/TraccarClient/InitialViewController/InitialViewController.swift +++ b/TraccarClient/InitialViewController/Controller/InitialViewController.swift @@ -52,14 +52,20 @@ extension InitialViewController { window?.makeKeyAndVisible() } else { - - let connect = ConnectViewController() - let navigation = KNNavigationController(rootViewController: connect) - navigation.modalPresentationStyle = .overFullScreen - - window?.rootViewController = navigation - window?.makeKeyAndVisible() + api.permissions { result in + if result.success { + api.parameters { result in + if result.success { + let connect = ConnectViewController() + let navigation = KNNavigationController(rootViewController: connect) + navigation.modalPresentationStyle = .overFullScreen + self.window?.rootViewController = navigation + self.window?.makeKeyAndVisible() + } + } + } + } } } } diff --git a/TraccarClient/InitialViewController/InitialViewController.xib b/TraccarClient/InitialViewController/View/InitialViewController.xib similarity index 100% rename from TraccarClient/InitialViewController/InitialViewController.xib rename to TraccarClient/InitialViewController/View/InitialViewController.xib diff --git a/TraccarClient/JSONField.swift b/TraccarClient/JSONField.swift index b0fe04295250b13609d2d623a0a199ca2597eef8..9bffe706b81a6d840c79e535330099a5053bf456 100644 --- a/TraccarClient/JSONField.swift +++ b/TraccarClient/JSONField.swift @@ -195,4 +195,10 @@ enum JSONField: String { case full_name case categories + case updateSettings = "user_can_update_his_settings" + case autoTracking = "turn_on_tracking_automatically" + case frequency = "frequency_in_seconds" + case distance = "distance_in_meters" + case angle = "angle_in_degrees" + case offline_buffering = "offline_buffering" } diff --git a/TraccarClient/Keys.swift b/TraccarClient/Keys.swift index 85b21aa9cc28dca855df4bd63f5ad4af3c31b14d..2d9b22881cc1d8952117913525df783ecb0794c2 100644 --- a/TraccarClient/Keys.swift +++ b/TraccarClient/Keys.swift @@ -45,7 +45,8 @@ struct Keys { static let status = tag + "_status" static let deviceName = tag + "_device_name" static let device = tag + "_device" - + static let updateSettings = tag + "_update_settings" + static let autoTracking = tag + "auto_tracking" } struct Badge { diff --git a/TraccarClient/LoginViewController/Model/LoginModelController.swift b/TraccarClient/LoginViewController/Model/LoginModelController.swift index 42880f8c09618f90b59baea72d4e993f99e566f8..bc87bc4f2e4cdaa80d686d7093048ca997a12cde 100644 --- a/TraccarClient/LoginViewController/Model/LoginModelController.swift +++ b/TraccarClient/LoginViewController/Model/LoginModelController.swift @@ -61,11 +61,7 @@ final class LoginModelController { AppDelegate.instance.managedObjectContext = NSManagedObjectContext.init(concurrencyType: .mainQueueConcurrencyType) AppDelegate.instance.managedObjectContext?.persistentStoreCoordinator = AppDelegate.instance.persistentStoreCoordinator } -} - -// MARK: - Request -extension LoginModelController { - func signin() { + private func signinRequest(forcing: Bool) { var params: PARAMS = [:] for item in fields { @@ -79,16 +75,23 @@ extension LoginModelController { } let spinner = viewController.spin() - api.signin(params: params) { [weak self] result in - spinner.stop() - + api.signin(params: params, force: forcing) { [weak self] result in guard let strongSelf = self else {return} - if result.success { - let connect = ConnectViewController() - strongSelf.viewController.navigationController?.pushViewController(connect, animated: true) + api.permissions { result in + if result.success { + api.parameters { result in + spinner.stop() + if result.success { + let connect = ConnectViewController() + strongSelf.viewController.navigationController?.pushViewController(connect, animated: true) + } + } + } + } } else { + spinner.stop() if result.statusCode == 401 { do { let value = try JSONSerialization.jsonObject(with: result.dataResponse?.data ?? Data()) @@ -127,6 +130,12 @@ extension LoginModelController { } else if result.statusCode == 404 { ok("Domain is not valid!") } + else if result.statusCode == 403 { + alert(confirm: "Continue here", destructive: "Cancel", message: result.displayError) { + strongSelf.viewController.dismiss(animated: true) + strongSelf.signinRequest(forcing: true) + } + } else { ok(result) @@ -134,6 +143,14 @@ extension LoginModelController { } } } + +} + +// MARK: - Request +extension LoginModelController { + func signin() { + signinRequest(forcing: false) + } } diff --git a/TraccarClient/Network+Endpoint.swift b/TraccarClient/Network+Endpoint.swift index 1eea81fec26d6b27c6617208aec77910b0de485c..05661add6d1d8bb3950903eb6c5b7413579d8e07 100644 --- a/TraccarClient/Network+Endpoint.swift +++ b/TraccarClient/Network+Endpoint.swift @@ -31,6 +31,9 @@ enum Endpoint: String, CaseIterable { case all case signin = "fleet-login" case logout = "logout" + case permissions = "nextcloud/fleet/permissions" + case parameters = "nextcloud/fleet/tracking-parameters" + // /////////////// // get endpoint with resource diff --git a/TraccarClient/PermissionsModel.swift b/TraccarClient/PermissionsModel.swift new file mode 100644 index 0000000000000000000000000000000000000000..f28d8b935e46cdc39674a2b2fda8871450941484 --- /dev/null +++ b/TraccarClient/PermissionsModel.swift @@ -0,0 +1,57 @@ +// +// PermissionModel.swift +// TraccarClient +// +// Created by George Makhoul on 07/11/2023. +// Copyright © 2023 Traccar. All rights reserved. +// + +import Foundation +final class PermissionsModel: KNObject { + + // MARK: - Properties + var update_settings: Bool = false + var tracking_auto: Bool = false + + // MARK: - Init + override init() { + super.init() + } + + init(_ dict: JSON, save: Bool = false) { + super.init() + update_settings = dict["user_can_update_his_settings"] as? Bool ?? false + tracking_auto = dict["turn_on_tracking_automatically"] as? Bool ?? false + + if save { + saveToDefaults() + } + } + //MARK: - Methods + private func saveToDefaults() { + let ud = UserDefaults.standard + ud.set(update_settings, forKey: Keys.User.updateSettings) + ud.set(tracking_auto, forKey: Keys.User.autoTracking) + } + + class func loadFromDefaults() -> PermissionsModel { + let ud = UserDefaults.standard + var params: PARAMS = [:] + + if let updateSettings = ud.value(forKey: Keys.User.updateSettings) as? Bool { + params.add(key: .updateSettings, updateSettings) + } + + if let autoTracking = ud.value(forKey: Keys.User.autoTracking) as? Bool { + params.add(key: .autoTracking, autoTracking) + } + + return PermissionsModel(params) + } + + class func clearUserFromDefaults() { + let ud = UserDefaults.standard + ud.removeObject(forKey: Keys.User.updateSettings) + ud.removeObject(forKey: Keys.User.autoTracking) + } +} diff --git a/TraccarClient/SettingsViewController/SettingsViewController.swift b/TraccarClient/SettingsViewController/SettingsViewController.swift index d803f9abba0bd4288d0ceb32f227f7df9d1d3d8f..9704321952b476ec5a1929489b72a07386f25cfd 100644 --- a/TraccarClient/SettingsViewController/SettingsViewController.swift +++ b/TraccarClient/SettingsViewController/SettingsViewController.swift @@ -182,71 +182,82 @@ extension SettingsViewController: CustomHeaderViewDelegate { extension SettingsViewController: UITableViewDelegate, UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { - 4 + UserDefaults.standard.bool(forKey: Keys.User.updateSettings) ? 4 : 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - switch section { - case 0: - return textDefaults.count - - case 1: - return multiChoicesDefaults.count - - case 2: - return switchDefaults.count - - case 3: + if UserDefaults.standard.bool(forKey: Keys.User.updateSettings) { + switch section { + case 0: + return textDefaults.count + + case 1: + return multiChoicesDefaults.count + + case 2: + return switchDefaults.count + + case 3: + return 1 + + default: + return 0 + } + } else { return 1 - - default: - return 0 } } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - if section == 0 { - return "Tracking Parameters" - } - - if section == 3 { - return "Settings" + if UserDefaults.standard.bool(forKey: Keys.User.updateSettings) { + if section == 0 { + return "Tracking Parameters" + } + + if section == 3 { + return "Settings" + } } - + return "" } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - if indexPath.section == 0 { - let cell = tableView.dequeue(withCell: SettingsTextTableViewCell.self) - cell.set(defaultValue: textDefaults[indexPath.row], index: indexPath.row) - cell.delegate = self + if UserDefaults.standard.bool(forKey: Keys.User.updateSettings) { + if indexPath.section == 0 { + let cell = tableView.dequeue(withCell: SettingsTextTableViewCell.self) + cell.set(defaultValue: textDefaults[indexPath.row], index: indexPath.row) + cell.delegate = self + + return cell + } - return cell - } - - if indexPath.section == 1 { - let cell = tableView.dequeue(withCell: SettingsSegmentTableViewCell.self) - cell.set(defaultValue: multiChoicesDefaults[indexPath.row], index: indexPath.row) - cell.delegate = self - return cell - } - - if indexPath.section == 2 { - let cell = tableView.dequeue(withCell: SettingsSwitchTableViewCell.self) - cell.set(defaultValue: switchDefaults[indexPath.row], index: indexPath.row) - cell.delegate = self - return cell - } - - if indexPath.section == 3 { - let cell = tableView.dequeue(withCell: SettingsLogoutTableViewCell.self) - cell.set(title: "Logout") - cell.delegate = self - return cell + if indexPath.section == 1 { + let cell = tableView.dequeue(withCell: SettingsSegmentTableViewCell.self) + cell.set(defaultValue: multiChoicesDefaults[indexPath.row], index: indexPath.row) + cell.delegate = self + return cell + } + + if indexPath.section == 2 { + let cell = tableView.dequeue(withCell: SettingsSwitchTableViewCell.self) + cell.set(defaultValue: switchDefaults[indexPath.row], index: indexPath.row) + cell.delegate = self + return cell + } + + if indexPath.section == 3 { + let cell = tableView.dequeue(withCell: SettingsLogoutTableViewCell.self) + cell.set(title: "Logout") + cell.delegate = self + return cell + } } - return UITableViewCell() + let cell = tableView.dequeue(withCell: SettingsLogoutTableViewCell.self) + cell.set(title: "Logout") + cell.delegate = self + return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { @@ -277,7 +288,7 @@ extension SettingsViewController: SettingsLogoutTableViewCellDelegate { func logout() { if AppDelegate.instance.trackingController != nil { - alert(presentingBy: self, confirm: "Yes" , destructive: "Cancel", message: "It seems that you are on the map, do you want to stop the location update before loging out ?") { + alert(presentingBy: self, confirm: "Yes" , destructive: "Cancel", message: "It seems that you are on the map, do you want to stop the location update before logging out?") { UserDefaults.standard.set(false, forKey: "service_status_preference") TransactionsViewController.addMessage(NSLocalizedString("Service destroyed", comment: "")) AppDelegate.instance.trackingController?.stop() diff --git a/TraccarClient/TrackingParametersModel.swift b/TraccarClient/TrackingParametersModel.swift new file mode 100644 index 0000000000000000000000000000000000000000..54e232cec76b3c3c21655b212b0ba322e3e86180 --- /dev/null +++ b/TraccarClient/TrackingParametersModel.swift @@ -0,0 +1,72 @@ +// +// TrackingParametersModel.swift +// TraccarClient +// +// Created by George Makhoul on 07/11/2023. +// Copyright © 2023 Traccar. All rights reserved. +// + +import Foundation +final class TrackingParametersModel: KNObject { + + // MARK: - Properties + var frequency: Int = 0 + var distance: Int = 0 + var angle: Int = 0 + var offline_buffering: Bool = false + + // MARK: - Init + override init() { + super.init() + } + + init(_ dict: JSON, save: Bool = false) { + super.init() + frequency = dict["frequency_in_seconds"] as? Int ?? 0 + distance = dict["distance_in_meters"] as? Int ?? 0 + angle = dict["angle_in_degrees"] as? Int ?? 0 + offline_buffering = dict["offline_buffering"] as? Bool ?? false + + if save { + saveToDefaults() + } + } + //MARK: - Methods + private func saveToDefaults() { + let ud = UserDefaults.standard + ud.set(frequency, forKey: "frequency_preference") + ud.set(distance, forKey: "distance_preference") + ud.set(angle, forKey: "angle_preference") + ud.set(offline_buffering, forKey: "buffer_preference") + } + + class func loadFromDefaults() -> TrackingParametersModel { + let ud = UserDefaults.standard + var params: PARAMS = [:] + + if let frequecy = ud.value(forKey: "frequency_preference") as? Int { + params.add(key: .frequency, frequecy) + } + + if let distance = ud.value(forKey: "distance_preference") as? Int { + params.add(key: .distance, distance) + } + if let angle = ud.value(forKey: "angle_preference") as? Int { + params.add(key: .angle, angle) + } + if let offlineBuffering = ud.value(forKey: "buffer_preference") as? Bool { + params.add(key: .offline_buffering, offlineBuffering) + } + + return TrackingParametersModel(params) + } + + class func clearUserFromDefaults() { + let ud = UserDefaults.standard + ud.removeObject(forKey: "frequency_preference") + ud.removeObject(forKey: "distance_preference") + ud.removeObject(forKey: "angle_preference") + ud.removeObject(forKey: "buffer_preference") + } +} +