diff --git a/Calendr/Events/EventDetails/EventDetailsViewModel.swift b/Calendr/Events/EventDetails/EventDetailsViewModel.swift index dbde4e8f..6b25911c 100644 --- a/Calendr/Events/EventDetails/EventDetailsViewModel.swift +++ b/Calendr/Events/EventDetails/EventDetailsViewModel.swift @@ -166,11 +166,19 @@ class EventDetailsViewModel { coordinates = Maybe.create { observer in Task { - guard let coordinates = await geocoder.geocodeAddressString(event.location ?? "") else { + guard let address = event.location, !address.isEmpty else { observer(.completed) return } - observer(.success(coordinates)) + if let coordinates = event.coordinates { + observer(.success(coordinates)) + } + else if let coordinates = await geocoder.geocodeAddressString(address) { + observer(.success(coordinates)) + } + else { + observer(.completed) + } } return Disposables.create() } diff --git a/Calendr/Mocks/Factories/EventModel+Factory.swift b/Calendr/Mocks/Factories/EventModel+Factory.swift index 90e9808f..4fc9842e 100644 --- a/Calendr/Mocks/Factories/EventModel+Factory.swift +++ b/Calendr/Mocks/Factories/EventModel+Factory.swift @@ -33,6 +33,7 @@ extension EventModel { end: type.isReminder ? Calendar.gregorian.endOfDay(for: start) : end, title: title, location: location, + coordinates: nil, notes: notes, url: url, isAllDay: isAllDay || type.isBirthday, diff --git a/Calendr/Models/EventModel.swift b/Calendr/Models/EventModel.swift index 7615c6ab..246e0783 100644 --- a/Calendr/Models/EventModel.swift +++ b/Calendr/Models/EventModel.swift @@ -13,6 +13,7 @@ struct EventModel: Equatable { let end: Date let title: String let location: String? + let coordinates: Coordinates? let notes: String? let url: URL? let isAllDay: Bool diff --git a/Calendr/Providers/CalendarServiceProvider.swift b/Calendr/Providers/CalendarServiceProvider.swift index 4fb4ea8f..12af1191 100644 --- a/Calendr/Providers/CalendarServiceProvider.swift +++ b/Calendr/Providers/CalendarServiceProvider.swift @@ -437,6 +437,7 @@ private extension EventModel { end: event.endDate, title: event.title, location: event.location, + coordinates: (event.structuredLocation?.geoLocation?.coordinate).map(Coordinates.init), notes: event.notes, url: event.url, isAllDay: event.shouldBeAllDay(dateProvider), @@ -461,6 +462,7 @@ private extension EventModel { end: dateProvider.calendar.endOfDay(for: date), title: reminder.title, location: reminder.location, // doesn't work + coordinates: nil, notes: reminder.notes, url: reminder.url, // doesn't work isAllDay: dueDateComponents.hour == nil, diff --git a/Calendr/Providers/GeocodeServiceProvider.swift b/Calendr/Providers/GeocodeServiceProvider.swift index 0b746945..0024c7cf 100644 --- a/Calendr/Providers/GeocodeServiceProvider.swift +++ b/Calendr/Providers/GeocodeServiceProvider.swift @@ -43,13 +43,13 @@ where LocationCache.Key == String, LocationCache.Value == Coordinates? { func geocodeAddressString(_ address: String) async -> Coordinates? { - guard !address.isEmpty else { return nil } - if let location = cache.get(address) { print("Cache hit for address: \"\(address)\"") return location } + print("Geocoding \"\(address)\"") + let sanitized = address.replacingOccurrences(of: ["(", ")"], with: " ") do {