diff --git a/Sources/Earthquake Network.xcodeproj/project.pbxproj b/Sources/Earthquake Network.xcodeproj/project.pbxproj index 2087750..031a4ac 100644 --- a/Sources/Earthquake Network.xcodeproj/project.pbxproj +++ b/Sources/Earthquake Network.xcodeproj/project.pbxproj @@ -10,7 +10,7 @@ 6502470D2A612E4A001AC512 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6502470C2A612E4A001AC512 /* Constants.swift */; }; 6502470E2A6136F0001AC512 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6502470C2A612E4A001AC512 /* Constants.swift */; }; 6502470F2A613AD7001AC512 /* EQNUserData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65CB83422915720400EE1E35 /* EQNUserData.swift */; }; - 650247122A61832F001AC512 /* EQNDebugHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 650247112A61832F001AC512 /* EQNDebugHelper.swift */; }; + 650247122A61832F001AC512 /* EQNBackgroundPositionDebugHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 650247112A61832F001AC512 /* EQNBackgroundPositionDebugHelper.swift */; }; 650247152A618D7F001AC512 /* Foundation+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 650247142A618D7F001AC512 /* Foundation+Extensions.swift */; }; 650B23AB2632CCD3007AE752 /* UIView+EQNExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 650B23AA2632CCD3007AE752 /* UIView+EQNExtensions.swift */; }; 651901B925F5358700CAFF20 /* EQNMapAnnotationSeismic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 651901B825F5358700CAFF20 /* EQNMapAnnotationSeismic.swift */; }; @@ -302,7 +302,7 @@ 25A8BFFE29D46740E8A8A7A3 /* Pods_Earthquake_Network.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Earthquake_Network.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 40CD2E5581CF2FA3D52F392D /* Pods-Earthquake Network.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Earthquake Network.release.xcconfig"; path = "Pods/Target Support Files/Pods-Earthquake Network/Pods-Earthquake Network.release.xcconfig"; sourceTree = ""; }; 6502470C2A612E4A001AC512 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = ""; }; - 650247112A61832F001AC512 /* EQNDebugHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EQNDebugHelper.swift; sourceTree = ""; }; + 650247112A61832F001AC512 /* EQNBackgroundPositionDebugHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EQNBackgroundPositionDebugHelper.swift; sourceTree = ""; }; 650247142A618D7F001AC512 /* Foundation+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Foundation+Extensions.swift"; sourceTree = ""; }; 650B23AA2632CCD3007AE752 /* UIView+EQNExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+EQNExtensions.swift"; sourceTree = ""; }; 651901B825F5358700CAFF20 /* EQNMapAnnotationSeismic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EQNMapAnnotationSeismic.swift; sourceTree = ""; }; @@ -700,6 +700,7 @@ 65F9B49B2A8CA22800F13260 /* BackgroundTaskManager.swift */, 65F9B49F2A8CC58200F13260 /* UpdateUserLocationTask.swift */, 65F9B49D2A8CA2AC00F13260 /* EQNBackgroundPosition.swift */, + 650247112A61832F001AC512 /* EQNBackgroundPositionDebugHelper.swift */, ); path = Background; sourceTree = ""; @@ -1127,7 +1128,6 @@ 8CF4F4DA216D44930057110B /* EQNPastquakes.m */, 65EA58812A60360D0038EE9D /* EQNRealtimePushNotification.swift */, 6552C1452926DBA1008E723C /* AppPreferences.swift */, - 650247112A61832F001AC512 /* EQNDebugHelper.swift */, ); path = Models; sourceTree = ""; @@ -1650,7 +1650,7 @@ 652C37BD26092B3C0068EC3B /* FiltersViewModel.swift in Sources */, DCF9E14F24F6EA07002B6B1D /* EQNSeismicNetwork.swift in Sources */, DC2814302519C24400C1AFF7 /* SeismicNetworkTableViewCell.swift in Sources */, - 650247122A61832F001AC512 /* EQNDebugHelper.swift in Sources */, + 650247122A61832F001AC512 /* EQNBackgroundPositionDebugHelper.swift in Sources */, 65CB83432915720400EE1E35 /* EQNUserData.swift in Sources */, 654D18C425F93C0600BB6DB0 /* PasquakesMapViewController.swift in Sources */, 8C14113721EE502800A59729 /* EQNAllertaSismica.m in Sources */, diff --git a/Sources/Earthquake Network/Controllers/Alerts/AllerteViewController.m b/Sources/Earthquake Network/Controllers/Alerts/AllerteViewController.m index f11e8e1..4810bb9 100644 --- a/Sources/Earthquake Network/Controllers/Alerts/AllerteViewController.m +++ b/Sources/Earthquake Network/Controllers/Alerts/AllerteViewController.m @@ -104,6 +104,10 @@ typedef NS_ENUM(NSInteger, AllerteTableRow) { self.title = [NSLocalizedString(@"tab_network", nil) capitalizedString]; self.tableView.estimatedRowHeight = 200.0; self.tableView.rowHeight = UITableViewAutomaticDimension; + + if (EQNBackgroundPositionDebugHelper.shared.isEnabled) { + self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(backgroundPositionDebugTapped:)]; + } } - (void)refreshUI diff --git a/Sources/Earthquake Network/Controllers/Debug/EQNBackgrounPositionDebugViewController.swift b/Sources/Earthquake Network/Controllers/Debug/EQNBackgrounPositionDebugViewController.swift index 94bb2f3..7defacf 100644 --- a/Sources/Earthquake Network/Controllers/Debug/EQNBackgrounPositionDebugViewController.swift +++ b/Sources/Earthquake Network/Controllers/Debug/EQNBackgrounPositionDebugViewController.swift @@ -19,6 +19,7 @@ class EQNBackgroundPositionDebugViewController: UITableViewController { formatter.dateStyle = .medium return formatter }() + private let helper = EQNBackgroundPositionDebugHelper() // MARK: - Init @@ -44,7 +45,7 @@ class EQNBackgroundPositionDebugViewController: UITableViewController { } private func loadData() { - positions = EQNBackgroundPosition.load() + positions = helper.loadPosition() tableView.reloadData() } @@ -77,14 +78,7 @@ class EQNBackgroundPositionDebugViewController: UITableViewController { // MARK: - Actions @objc private func onTapDeleteButton(_ sender: UIBarButtonItem) { - EQNBackgroundPosition.reset() + helper.resetPositions() loadData() } - - // MARK: - Class - - @objc - class func resetAll() { - EQNBackgroundPosition.reset() - } } diff --git a/Sources/Earthquake Network/Models/Background/EQNBackgroundPosition.swift b/Sources/Earthquake Network/Models/Background/EQNBackgroundPosition.swift index 47cb338..5aed862 100644 --- a/Sources/Earthquake Network/Models/Background/EQNBackgroundPosition.swift +++ b/Sources/Earthquake Network/Models/Background/EQNBackgroundPosition.swift @@ -19,30 +19,14 @@ struct EQNBackgroundPosition: Codable { } let request: Bool? - // MARK: - Class - - static func save( + init( + date: Date, coordinate: CLLocationCoordinate2D, - requestSuccess: Bool + request: Bool? ) { - var positions = load() - positions.append(.init(date: Date(), latitude: coordinate.latitude, longitude: coordinate.longitude, request: requestSuccess)) - - if let data = try? JSONEncoder().encode(positions) { - UserDefaults.standard.set(data, forKey: "BackgroundPositions") - } - } - - static func reset() { - UserDefaults.standard.removeObject(forKey: "BackgroundPositions") - } - - static func load() -> [EQNBackgroundPosition] { - guard let data = UserDefaults.standard.object(forKey: "BackgroundPositions") as? Data else { - return [] - } - - let positions = try? JSONDecoder().decode([EQNBackgroundPosition].self, from: data) - return positions ?? [] + self.date = date + self.latitude = coordinate.latitude + self.longitude = coordinate.longitude + self.request = request } } diff --git a/Sources/Earthquake Network/Models/Background/EQNBackgroundPositionDebugHelper.swift b/Sources/Earthquake Network/Models/Background/EQNBackgroundPositionDebugHelper.swift new file mode 100644 index 0000000..71c8265 --- /dev/null +++ b/Sources/Earthquake Network/Models/Background/EQNBackgroundPositionDebugHelper.swift @@ -0,0 +1,66 @@ +// +// EQNDebugHelper.swift +// Earthquake Network +// +// Created by Andrea Busi on 14/07/23. +// Copyright © 2023 Earthquake Network. All rights reserved. +// + +import Foundation + + +@objc +class EQNBackgroundPositionDebugHelper: NSObject { + + @objc + static let shared = EQNBackgroundPositionDebugHelper() + + private var timers: [String: Timer] = [:] + + @objc + var isEnabled: Bool { false } + + // MARK: - Public + + @objc + func printPositions( + interval: TimeInterval = 2.0, + repeats: Bool = true + ) { + let timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { timer in + let current = EQNUserData.shared.lastLocation?.coordinate + print("[EQNDebugHelper] Current | lat: \(current?.latitude ?? 0) - lon: \(current?.longitude ?? 0)") + + let saved = EQNUser.default().lastPosition?.coordinate + print("[EQNDebugHelper] Saved | lat: \(saved?.latitude ?? 0) - lon: \(saved?.longitude ?? 0)") + } + timers["positions"] = timer + } + + // MARK: - Class + + func savePosition( + coordinate: CLLocationCoordinate2D, + requestSuccess: Bool + ) { + var positions = loadPosition() + positions.append(.init(date: Date(), coordinate: coordinate, request: requestSuccess)) + + if let data = try? JSONEncoder().encode(positions) { + UserDefaults.standard.set(data, forKey: "BackgroundPositions") + } + } + + func resetPositions() { + UserDefaults.standard.removeObject(forKey: "BackgroundPositions") + } + + func loadPosition() -> [EQNBackgroundPosition] { + guard let data = UserDefaults.standard.object(forKey: "BackgroundPositions") as? Data else { + return [] + } + + let positions = try? JSONDecoder().decode([EQNBackgroundPosition].self, from: data) + return positions ?? [] + } +} diff --git a/Sources/Earthquake Network/Models/Background/UpdateUserLocationTask.swift b/Sources/Earthquake Network/Models/Background/UpdateUserLocationTask.swift index f45b221..fc86eef 100644 --- a/Sources/Earthquake Network/Models/Background/UpdateUserLocationTask.swift +++ b/Sources/Earthquake Network/Models/Background/UpdateUserLocationTask.swift @@ -22,6 +22,7 @@ final class UpdateUserLocationTask: NSObject, BackgroundTaskIdentifiable { } static let shared = UpdateUserLocationTask() + private let debugHelper = EQNBackgroundPositionDebugHelper() // MARK: - Internal @@ -58,10 +59,14 @@ final class UpdateUserLocationTask: NSObject, BackgroundTaskIdentifiable { // send position to cloud let url = EQNGeneratoreURLServer.urlPosizione(withLocation: location.coordinate) ServerRequest.default().inviaInformazioniAlServer(with: url, richiesta: .posizione) { result in - EQNBackgroundPosition.save(coordinate: location.coordinate, requestSuccess: true) + if self.debugHelper.isEnabled { + self.debugHelper.savePosition(coordinate: location.coordinate, requestSuccess: true) + } self.appTaskCompletion?(true) } failure: { error in - EQNBackgroundPosition.save(coordinate: location.coordinate, requestSuccess: false) + if self.debugHelper.isEnabled { + self.debugHelper.savePosition(coordinate: location.coordinate, requestSuccess: false) + } self.appTaskCompletion?(false) } } diff --git a/Sources/Earthquake Network/Models/EQNDebugHelper.swift b/Sources/Earthquake Network/Models/EQNDebugHelper.swift deleted file mode 100644 index 4ef6d76..0000000 --- a/Sources/Earthquake Network/Models/EQNDebugHelper.swift +++ /dev/null @@ -1,36 +0,0 @@ -// -// EQNDebugHelper.swift -// Earthquake Network -// -// Created by Andrea Busi on 14/07/23. -// Copyright © 2023 Earthquake Network. All rights reserved. -// - -import Foundation - - -@objc -class EQNDebugHelper: NSObject { - - @objc - static let shared = EQNDebugHelper() - - private var timers: [String: Timer] = [:] - - // MARK: - Public - - @objc - func printPositions( - interval: TimeInterval = 2.0, - repeats: Bool = true - ) { - let timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { timer in - let current = EQNUserData.shared.lastLocation?.coordinate - print("[EQNDebugHelper] Current | lat: \(current?.latitude ?? 0) - lon: \(current?.longitude ?? 0)") - - let saved = EQNUser.default().lastPosition?.coordinate - print("[EQNDebugHelper] Saved | lat: \(saved?.latitude ?? 0) - lon: \(saved?.longitude ?? 0)") - } - timers["positions"] = timer - } -} diff --git a/Sources/Earthquake Network/Storyboards/Base.lproj/Main.storyboard b/Sources/Earthquake Network/Storyboards/Base.lproj/Main.storyboard index 72e8dfe..36856de 100644 --- a/Sources/Earthquake Network/Storyboards/Base.lproj/Main.storyboard +++ b/Sources/Earthquake Network/Storyboards/Base.lproj/Main.storyboard @@ -2369,11 +2369,6 @@ Sisma rilevato da 10 smartphone - - - - -