← Back to index

WatchEntry

SpotsComplication
CodeWhat It DoesHow It Does It
▶ IMPORTS
import FoundationFramework importsImports Foundation.
struct WatchEntry: Identifiable, Codable, Equatable { var id: UUID var playName: String var dateTime: Date var cuisine: String var address: String var neighborhood: String var rating: Int var beenThere: Bool var starRating: Int var latitude: Double? var longitude: Double? var isPast: Bool { dateTime < Date() } var formattedDate: String { let f = DateFormatter() f.dateFormat = "MM/dd/yy h:mma" f.amSymbol = "am" f.pmSymbol = "pm" return f.string(from: dateTime) } var daysUntil: Int? { guard !isPast else { return nil } return Calendar.current.dateComponents([.day], from: Calendar.current.startOfDay(for: Date()), to: Calendar.current.startOfDay(for: dateTime)).day } // MARK: - Codable (explicit so new fields degrade gracefully with old cached data) private enum CodingKeys: String, CodingKey { case id, playName, dateTime, cuisine, address, neighborhood, rating, beenThere, starRating, latitude, longitude } init(id: UUID, playName: String, dateTime: Date, cuisine: String, address: String, neighborhood: String, rating: Int, beenThere: Bool = false, starRating: Int = 0, latitude: Double? = nil, longitude: Double? = nil) { self.id = id self.playName = playName self.dateTime = dateTime self.cuisine = cuisine self.address = address self.neighborhood = neighborhood self.rating = rating self.beenThere = beenThere self.starRating = starRating self.latitude = latitude self.longitude = longitude } init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) id = try c.decode(UUID.self, forKey: .id) playName = try c.decode(String.self, forKey: .playName) dateTime = try c.decode(Date.self, forKey: .dateTime) cuisine = try c.decode(String.self, forKey: .cuisine) address = try c.decode(String.self, forKey: .address) neighborhood = try c.decodeIfPresent(String.self, forKey: .neighborhood) ?? "" rating = try c.decodeIfPresent(Int.self, forKey: .rating) ?? 0 beenThere = try c.decodeIfPresent(Bool.self, forKey: .beenThere) ?? false starRating = try c.decodeIfPresent(Int.self, forKey: .starRating) ?? 0 latitude = try c.decodeIfPresent(Double.self, forKey: .latitude) longitude = try c.decodeIfPresent(Double.self, forKey: .longitude) } }`WatchEntry` structDefines the `WatchEntry` struct. Conforms to Identifiable, Codable, Equatable.
▶ CONVERSION FROM SPOTENTRY (IOS TARGET ONLY)
#if !os(watchOS)Code blockSee source code for full implementation.
extension SpotEntry { var watchEntry: WatchEntry { WatchEntry( id: id, playName: playName, dateTime: dateTime, cuisine: cuisine, address: address, neighborhood: neighborhood, rating: rating, beenThere: beenThere, starRating: starRating ) } }`SpotEntry` extensionDefines the `SpotEntry` extension.
#endifCode blockSee source code for full implementation.