Skip to content

Commit

Permalink
Get coordinates from EKEvent if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
pakerwreah committed Sep 11, 2024
1 parent 0b4fdfb commit f316173
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
12 changes: 10 additions & 2 deletions Calendr/Events/EventDetails/EventDetailsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions Calendr/Mocks/Factories/EventModel+Factory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions Calendr/Models/EventModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Calendr/Providers/CalendarServiceProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions Calendr/Providers/GeocodeServiceProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f316173

Please sign in to comment.