67 lines
1.9 KiB
Swift
67 lines
1.9 KiB
Swift
//
|
|
// 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 ?? []
|
|
}
|
|
}
|