Skip to content

Commit

Permalink
Tweak data model
Browse files Browse the repository at this point in the history
And some refactoring
  • Loading branch information
nighthawk committed Mar 21, 2024
1 parent cb5d8ed commit 28d87a7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 35 deletions.
33 changes: 17 additions & 16 deletions Sources/TripKit/model/API/LocationAPIModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,24 +378,25 @@ extension TKAPI {
public struct Group: Codable, Hashable {
public let key: String
public let hashCode: Int
public let stops: [TKStopCoordinate]?
public let bikePods: [TKBikePodLocation]?
public let carPods: [TKCarPodLocation]?
public let carParks: [TKCarParkLocation]?
public let carRentals: [TKCarRentalLocation]?
public let freeFloating: [TKFreeFloatingVehicleLocation]?
public let onStreetParking: [TKOnStreetParkingLocation]?
public let facilities: [TKFacilityLocation]?

@EmptyLossyArray @LossyArray public var stops: [TKStopCoordinate]
@EmptyLossyArray @LossyArray public var bikePods: [TKBikePodLocation]
@EmptyLossyArray @LossyArray public var carPods: [TKCarPodLocation]
@EmptyLossyArray @LossyArray public var carParks: [TKCarParkLocation]
@EmptyLossyArray @LossyArray public var carRentals: [TKCarRentalLocation]
@EmptyLossyArray @LossyArray public var freeFloating: [TKFreeFloatingVehicleLocation]
@EmptyLossyArray @LossyArray public var onStreetParking: [TKOnStreetParkingLocation]
@EmptyLossyArray @LossyArray public var facilities: [TKFacilityLocation]

public var all: [TKNamedCoordinate] {
return (stops ?? []) as [TKNamedCoordinate]
+ (bikePods ?? []) as [TKNamedCoordinate]
+ (carPods ?? []) as [TKNamedCoordinate]
+ (carParks ?? []) as [TKNamedCoordinate]
+ (carRentals ?? []) as [TKNamedCoordinate]
+ (freeFloating ?? []) as [TKNamedCoordinate]
+ (onStreetParking ?? []) as [TKNamedCoordinate]
+ (facilities ?? []) as [TKNamedCoordinate]
return stops as [TKNamedCoordinate]
+ bikePods as [TKNamedCoordinate]
+ carPods as [TKNamedCoordinate]
+ carParks as [TKNamedCoordinate]
+ carRentals as [TKNamedCoordinate]
+ freeFloating as [TKNamedCoordinate]
+ onStreetParking as [TKNamedCoordinate]
+ facilities as [TKNamedCoordinate]
}

}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TripKit/model/CoreData/SegmentTemplate+Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension SegmentTemplate {
set { encode(newValue, key: "miniInstruction") }
}

@objc var modeInfo: TKModeInfo? {
var modeInfo: TKModeInfo? {
get { decode(TKModeInfo.self, key: "modeInfo") }
set { encode(newValue, key: "modeInfo") }
}
Expand Down
28 changes: 19 additions & 9 deletions Sources/TripKit/model/TKLocationTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,20 +261,22 @@ public class TKOnStreetParkingLocation: TKModeCoordinate {

public class TKFacilityLocation: TKNamedCoordinate {

public enum FacilityType: String, Codable {
case parkAndRide = "Park-and-Ride"
}

public var facilityType: FacilityType
public var facilityType: String
public var primaryType: String?
public var secondaryType: String?

private enum CodingKeys: String, CodingKey {
case facilityID = "id"
case facilityType
case primaryType = "type"
case secondaryType = "subType"
}

public required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
facilityType = try values.decode(FacilityType.self, forKey: .facilityType)
facilityType = try values.decode(String.self, forKey: .facilityType)
primaryType = try values.decodeIfPresent(String.self, forKey: .primaryType)
secondaryType = try values.decodeIfPresent(String.self, forKey: .secondaryType)
try super.init(from: decoder)
locationID = try values.decode(String.self, forKey: .facilityID)
}
Expand All @@ -284,24 +286,32 @@ public class TKFacilityLocation: TKNamedCoordinate {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(facilityType, forKey: .facilityType)
try container.encode(locationID, forKey: .facilityID)
try container.encode(primaryType, forKey: .primaryType)
try container.encode(secondaryType, forKey: .secondaryType)
}

@objc public class override var supportsSecureCoding: Bool { true }

public required init?(coder aDecoder: NSCoder) {
guard
let info = try? aDecoder.decode(FacilityType.self, forKey: CodingKeys.facilityType.rawValue),
let facilityType = try? aDecoder.decode(String.self, forKey: CodingKeys.facilityType.rawValue),
let facilityID = try? aDecoder.decode(String.self, forKey: CodingKeys.facilityID.rawValue)
else { return nil }
facilityType = info

self.facilityType = facilityType
self.primaryType = try? aDecoder.decode(String.self, forKey: CodingKeys.primaryType.rawValue)
self.secondaryType = try? aDecoder.decode(String.self, forKey: CodingKeys.secondaryType.rawValue)

super.init(coder: aDecoder)
locationID = facilityID
}

public override func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
try? aCoder.encode(encodable: facilityType, forKey: CodingKeys.facilityType.rawValue)
try? aCoder.encode(encodable: locationID, forKey: CodingKeys.facilityType.rawValue)
try? aCoder.encode(encodable: locationID, forKey: CodingKeys.facilityID.rawValue)
try? aCoder.encode(encodable: primaryType, forKey: CodingKeys.primaryType.rawValue)
try? aCoder.encode(encodable: secondaryType, forKey: CodingKeys.secondaryType.rawValue)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ extension TKUIServiceViewModel.ServiceVisit: TKUIModeAnnotation {
var title: String? { return visit.title }
var subtitle: String? { return visit.subtitle }
var coordinate: CLLocationCoordinate2D { return visit.coordinate }
var modeInfo: TKModeInfo! { return visit.modeInfo }
var modeInfo: TKModeInfo? { return visit.modeInfo }
var clusterIdentifier: String? { return visit.clusterIdentifier }
}

Expand Down Expand Up @@ -147,7 +147,7 @@ extension TKUIServiceViewModel {
}

extension TKUIServiceViewModel.ServiceEmbarkation: TKUIModeAnnotation {
var modeInfo: TKModeInfo! {
var modeInfo: TKModeInfo? {
return visit.modeInfo
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import TripKit
// MARK: TKUIModeAnnotation

extension TKModeCoordinate: TKUIModeAnnotation {
public var modeInfo: TKModeInfo! {
public var modeInfo: TKModeInfo? {
return stopModeInfo
}
}
Expand Down Expand Up @@ -74,7 +74,7 @@ extension Alert: TKUIImageAnnotation {
// MARK: TKUIModeAnnotation

extension StopLocation: TKUIModeAnnotation {
public var modeInfo: TKModeInfo! {
public var modeInfo: TKModeInfo? {
return stopModeInfo
}

Expand All @@ -93,7 +93,7 @@ extension StopLocation: TKUIStopAnnotation {}
// MARK: TKUIModeAnnotation

extension StopVisits: TKUIModeAnnotation {
public var modeInfo: TKModeInfo! {
public var modeInfo: TKModeInfo? {
return service.findModeInfo() ?? .unknown
}

Expand Down
6 changes: 2 additions & 4 deletions Sources/TripKitUI/views/map annotations/TKUIAnnotations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ public protocol TKUISelectableOnMap {
}

/// For displaying an annotation in a `TKUIModeAnnotationView`
@objc
public protocol TKUIModeAnnotation: MKAnnotation {
var modeInfo: TKModeInfo! { get }
public protocol TKUIModeAnnotation: TKUIImageAnnotation {
var modeInfo: TKModeInfo? { get }
var clusterIdentifier: String? { get }
}

Expand All @@ -56,7 +55,6 @@ public extension TKUIModeAnnotation {
var imageIsTemplate: Bool { return modeInfo?.remoteImageIsTemplate ?? false }
}

@objc
public protocol TKUIStopAnnotation: TKUIModeAnnotation {
var stopCode: String { get }
var timeZone: TimeZone? { get }
Expand Down

0 comments on commit 28d87a7

Please sign in to comment.