AutoCat/AutoCatCore/Models/DTO/VehicleEventDto.swift

70 lines
2.0 KiB
Swift

//
// VehicleEventDto.swift
// AutoCatCore
//
// Created by Selim Mustafaev on 12.06.2024.
// Copyright © 2024 Selim Mustafaev. All rights reserved.
//
import Foundation
public struct VehicleEventDto: Codable, Sendable {
public var id: String = UUID().uuidString
public var date: TimeInterval = Date().timeIntervalSince1970
public var latitude: Double = 0
public var longitude: Double = 0
public var address: String? = nil
public var addedBy: String? = nil
public var number: String?
public init(lat: Double, lon: Double) {
self.latitude = lat
self.longitude = lon
self.addedBy = Settings.shared.user.email
}
public func getMapLink() -> URL? {
var urlString = "http://maps.apple.com/?sll=\(self.latitude),\(self.longitude)"
if let address = self.address?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
urlString = urlString + "&address=" + address
}
return URL(string: urlString)
}
public func getLocationString() -> String {
let coordinates = "Lat: \(self.latitude), Lon: \(self.longitude)"
if let addressString = self.address {
return "\(addressString) (\(coordinates)"
} else {
return coordinates
}
}
public var location: String {
if let address {
return address
} else {
return "\(latitude), \(longitude)"
}
}
public func findAddress() async throws {
guard address == nil else {
return
}
/*
return RxLocationManager
.getAddressForLocation(latitude: self.latitude, longitude: self.longitude)
.map { addr in
if let realm = self.realm {
try realm.write { self.address = addr }
} else {
self.address = addr
}
}
*/
}
}