diff --git a/AutoCat/Screens/EventsScreen/EventModel.swift b/AutoCat/Screens/EventsScreen/EventModel.swift index 1ee9576..4892e20 100644 --- a/AutoCat/Screens/EventsScreen/EventModel.swift +++ b/AutoCat/Screens/EventsScreen/EventModel.swift @@ -8,11 +8,31 @@ import CoreLocation -struct EventModel: Identifiable { +struct EventModel: Identifiable, Hashable { let id: String var date: String var coordinate: CLLocationCoordinate2D var address: String var isMe: Bool + + func hash(into hasher: inout Hasher) { + + hasher.combine(id) + hasher.combine(date) + hasher.combine(coordinate.latitude) + hasher.combine(coordinate.longitude) + hasher.combine(address) + hasher.combine(isMe) + } + + static func == (lhs: EventModel, rhs: EventModel) -> Bool { + + lhs.id == rhs.id + && lhs.date == rhs.date + && lhs.coordinate.latitude == rhs.coordinate.latitude + && lhs.coordinate.longitude == rhs.coordinate.longitude + && lhs.address == rhs.address + && lhs.isMe == rhs.isMe + } } diff --git a/AutoCat/Screens/EventsScreen/EventsScreen.swift b/AutoCat/Screens/EventsScreen/EventsScreen.swift index 02ceec6..f83a98a 100644 --- a/AutoCat/Screens/EventsScreen/EventsScreen.swift +++ b/AutoCat/Screens/EventsScreen/EventsScreen.swift @@ -13,6 +13,7 @@ import MapKit struct EventsScreen: View { @State var viewModel: EventsViewModel + @State var selectedEvent: EventModel? init(viewModel: EventsViewModel) { self.viewModel = viewModel @@ -61,9 +62,10 @@ struct EventsScreen: View { } var map: some View { - Map { + Map(selection: $selectedEvent) { ForEach(viewModel.events) { Marker($0.date, coordinate: $0.coordinate) + .tag($0) } } } @@ -115,6 +117,14 @@ struct EventsScreen: View { } label: { Label(useLabels ? "Copy" : "", systemImage: "doc.on.doc") } + + if useLabels { + Button() { + viewModel.openMaps(event) + } label: { + Label("Open in Maps", systemImage: "map") + } + } } } diff --git a/AutoCat/Screens/EventsScreen/EventsViewModel.swift b/AutoCat/Screens/EventsScreen/EventsViewModel.swift index 230d950..138b1a8 100644 --- a/AutoCat/Screens/EventsScreen/EventsViewModel.swift +++ b/AutoCat/Screens/EventsScreen/EventsViewModel.swift @@ -11,6 +11,7 @@ import AutoCatCore import CoreLocation import UniformTypeIdentifiers import MobileCoreServices +import MapKit extension UTType { @@ -174,4 +175,19 @@ class EventsViewModel: ACHudContainer { return date + "\n" + location } + + func openMaps(_ event: EventModel) { + guard let url = URL(string: "yandexmaps://maps.yandex.ru/?pt=\(event.coordinate.longitude),\(event.coordinate.latitude)&z=12") else { + return + } + + if UIApplication.shared.canOpenURL(url) { + UIApplication.shared.open(url) + } else { + let placemark = MKPlacemark(coordinate: event.coordinate) + let mapItem = MKMapItem(placemark: placemark) + mapItem.name = event.date + mapItem.openInMaps() + } + } } diff --git a/AutoCat/ru.lproj/Localizable.strings b/AutoCat/ru.lproj/Localizable.strings index cf1a784..8cf4748 100644 --- a/AutoCat/ru.lproj/Localizable.strings +++ b/AutoCat/ru.lproj/Localizable.strings @@ -415,3 +415,5 @@ "Server" = "Сервер"; "Not updated" = "Не обновленные"; + +"Open in Maps" = "Открыть на карте";