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