Models.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // Models.swift
  3. // Runner
  4. //
  5. // Created by Groot on 2025/5/9.
  6. //
  7. import Foundation
  8. import MapKit
  9. extension Decodable {
  10. static func fromJson(json: Any) -> Self? {
  11. do {
  12. let data = try JSONSerialization.data(withJSONObject: json, options: [])
  13. return try JSONDecoder().decode(Self.self, from: data)
  14. } catch {
  15. print(error.localizedDescription)
  16. return nil
  17. }
  18. }
  19. }
  20. extension Encodable {
  21. func toJson() -> [String: Any] {
  22. guard let data = try? JSONEncoder().encode(self) else {
  23. return [:]
  24. }
  25. return try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
  26. }
  27. }
  28. struct ATMapCameraPosition: Codable {
  29. var latitude: CGFloat
  30. var longitude: CGFloat
  31. // 地图缩放比例,对应span
  32. var zoom: CGFloat
  33. }
  34. struct ATMapPadding: Codable {
  35. var top: CGFloat
  36. var left: CGFloat
  37. var bottom: CGFloat
  38. var right: CGFloat
  39. var padding: UIEdgeInsets {
  40. return UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)
  41. }
  42. }
  43. class ATMapPolyline: NSObject, Codable {
  44. var id: String = UUID().uuidString
  45. var points: [CLLocationCoordinate2D]
  46. var mapPadding: ATMapPadding?
  47. var polyline: MKPolyline {
  48. return MKPolyline(coordinates: points, count: points.count)
  49. }
  50. enum CodingKeys: String, CodingKey {
  51. case points, mapPadding
  52. }
  53. required init(from decoder: Decoder) throws {
  54. let container = try decoder.container(keyedBy: CodingKeys.self)
  55. points = try container.decode([CLLocationCoordinate2D].self, forKey: .points)
  56. if container.contains(.mapPadding) {
  57. mapPadding = try container.decodeIfPresent(ATMapPadding.self, forKey: .mapPadding)
  58. }
  59. }
  60. func encode(to encoder: Encoder) throws {
  61. var container = encoder.container(keyedBy: CodingKeys.self)
  62. try container.encode(points, forKey: .points)
  63. try container.encodeIfPresent(mapPadding, forKey: .mapPadding)
  64. }
  65. }
  66. class ATMapMarker: NSObject, Codable {
  67. var id: String
  68. var markerName: String?
  69. var latitude: CGFloat
  70. var longitude: CGFloat
  71. var isSelected: Bool
  72. var markerType: any MapMarkerSupportType
  73. enum CodingKeys: String, CodingKey {
  74. case id, markerName, latitude, longitude, isSelected, markerType
  75. }
  76. required init(from decoder: Decoder) throws {
  77. let container = try decoder.container(keyedBy: CodingKeys.self)
  78. id = try container.decode(String.self, forKey: .id)
  79. if container.contains(.markerName) {
  80. markerName = try container.decodeIfPresent(String.self, forKey: .markerName)
  81. }
  82. latitude = try container.decode(CGFloat.self, forKey: .latitude)
  83. longitude = try container.decode(CGFloat.self, forKey: .longitude)
  84. isSelected = try container.decode(Bool.self, forKey: .isSelected)
  85. // 使用工厂方法创建正确类型的markerType
  86. let rawValue = try container.decode(Int.self, forKey: .markerType)
  87. markerType = MarkerTypeFactory.markerType(from: rawValue)
  88. }
  89. func encode(to encoder: Encoder) throws {
  90. var container = encoder.container(keyedBy: CodingKeys.self)
  91. try container.encode(id, forKey: .id)
  92. try container.encode(markerName, forKey: .markerName)
  93. try container.encode(latitude, forKey: .latitude)
  94. try container.encode(longitude, forKey: .longitude)
  95. try container.encode(isSelected, forKey: .isSelected)
  96. try container.encode(markerType.rawValue, forKey: .markerType)
  97. }
  98. }
  99. extension ATMapMarker: MKAnnotation {
  100. var coordinate: CLLocationCoordinate2D {
  101. return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  102. }
  103. }
  104. extension CLLocationCoordinate2D: Codable {
  105. enum CodingKeys: String, CodingKey {
  106. case latitude
  107. case longitude
  108. }
  109. public init(from decoder: Decoder) throws {
  110. let container = try decoder.container(keyedBy: CodingKeys.self)
  111. let latitude = try container.decode(Double.self, forKey: .latitude)
  112. let longitude = try container.decode(Double.self, forKey: .longitude)
  113. self.init(latitude: latitude, longitude: longitude)
  114. }
  115. public func encode(to encoder: Encoder) throws {
  116. var container = encoder.container(keyedBy: CodingKeys.self)
  117. try container.encode(latitude, forKey: .latitude)
  118. try container.encode(longitude, forKey: .longitude)
  119. }
  120. }