open events in map apps

This commit is contained in:
Selim Mustafaev 2023-03-03 23:48:08 +03:00
parent 637c364be7
commit b2e53829e5
3 changed files with 103 additions and 44 deletions

View File

@ -145,42 +145,77 @@ class EventsController: UIViewController, UITableViewDataSource, UITableViewDele
// MARK: - UITableViewDelegate // MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
guard let vehicle = self.vehicle else {
HUD.flash(.labeledError(title: nil, subtitle: "Unknown vehicle"))
return nil
}
let event = vehicle.events[indexPath.row]
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
let copy = UIAction(title: NSLocalizedString("Copy", comment: ""), image: UIImage(systemName: "doc.on.doc")) { action in
self.copyEvent(index: indexPath.row) let copy = UIAction(title: NSLocalizedString("Copy", comment: ""),
image: UIImage(systemName: "doc.on.doc"),
handler: { _ in self.copyEvent(event: event) })
let share = UIAction(title: NSLocalizedString("Share", comment: ""),
image: UIImage(systemName: "square.and.arrow.up"),
handler: { _ in self.shareEvent(event: event) })
let edit = UIAction(title: NSLocalizedString("Edit", comment: ""),
image: UIImage(systemName: "pencil"),
handler: { _ in self.editEvent(event: event) })
let delete = UIAction(title: NSLocalizedString("Delete", comment: ""),
image: UIImage(systemName: "trash"),
attributes: .destructive,
handler: { _ in self.deleteEvent(event: event) })
let openApple = UIAction(title: NSLocalizedString("Apple Maps", comment: ""),
image: UIImage(systemName: "map"),
handler: { _ in self.openInAppleMaps(event: event) })
let openYandex = UIAction(title: NSLocalizedString("Yandex Maps", comment: ""),
image: UIImage(systemName: "map"),
handler: { _ in self.openInYandexMaps(event: event) })
let openMenu: UIMenuElement
if let yandexUrl = URL(string: "yandexmaps://"),
UIApplication.shared.canOpenURL(yandexUrl)
{
openMenu = UIMenu(title: NSLocalizedString("Open in ...", comment: ""),
children: [openApple, openYandex])
} else {
openApple.title = NSLocalizedString("Open in Apple Maps", comment: "")
openMenu = openApple
} }
let share = UIAction(title: NSLocalizedString("Share", comment: ""), image: UIImage(systemName: "square.and.arrow.up")) { action in return UIMenu(title: NSLocalizedString("Actions", comment: ""), children: [copy, share, edit, openMenu, delete])
self.shareEvent(index: indexPath.row)
}
let edit = UIAction(title: NSLocalizedString("Edit", comment: ""), image: UIImage(systemName: "pencil")) { action in
self.editEvent(index: indexPath.row)
}
let delete = UIAction(title: NSLocalizedString("Delete", comment: ""), image: UIImage(systemName: "trash"), attributes: .destructive) { action in
self.deleteEvent(index: indexPath.row)
}
return UIMenu(title: NSLocalizedString("Actions", comment: ""), children: [copy, share, edit, delete])
} }
} }
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
guard let vehicle = self.vehicle else {
HUD.flash(.labeledError(title: nil, subtitle: "Unknown vehicle"))
return nil
}
let event = vehicle.events[indexPath.row]
let copy = UIContextualAction(style: .normal, title: NSLocalizedString("Copy", comment: "")) { action, view, completion in let copy = UIContextualAction(style: .normal, title: NSLocalizedString("Copy", comment: "")) { action, view, completion in
self.copyEvent(index: indexPath.row) self.copyEvent(event: event)
completion(true) completion(true)
} }
copy.image = UIImage(systemName: "doc.on.doc") copy.image = UIImage(systemName: "doc.on.doc")
copy.backgroundColor = .systemBlue copy.backgroundColor = .systemBlue
let delete = UIContextualAction(style: .destructive, title: NSLocalizedString("Delete", comment: "")) { action, view, completion in let delete = UIContextualAction(style: .destructive, title: NSLocalizedString("Delete", comment: "")) { action, view, completion in
self.deleteEvent(index: indexPath.row, completion: completion) self.deleteEvent(event: event, completion: completion)
} }
delete.image = UIImage(systemName: "trash") delete.image = UIImage(systemName: "trash")
let edit = UIContextualAction(style: .normal, title: NSLocalizedString("Edit", comment: "")) { action, view, completion in let edit = UIContextualAction(style: .normal, title: NSLocalizedString("Edit", comment: "")) { action, view, completion in
self.editEvent(index: indexPath.row) self.editEvent(event: event)
completion(true) completion(true)
} }
edit.image = UIImage(systemName: "pencil") edit.image = UIImage(systemName: "pencil")
@ -206,13 +241,7 @@ class EventsController: UIViewController, UITableViewDataSource, UITableViewDele
// MARK: - Event actions // MARK: - Event actions
func deleteEvent(index: Int, completion: ((Bool) -> Void)? = nil) { func deleteEvent(event: VehicleEvent, completion: ((Bool) -> Void)? = nil) {
guard let vehicle = self.vehicle else {
HUD.flash(.labeledError(title: nil, subtitle: "Unknown vehicle"))
return
}
let event = vehicle.events[index]
HUD.show(.progress) HUD.show(.progress)
Api.remove(event: event.id).observe(on: MainScheduler.instance).subscribe(onSuccess: { vehicle in Api.remove(event: event.id).observe(on: MainScheduler.instance).subscribe(onSuccess: { vehicle in
let result = self.update(vehicle: vehicle) let result = self.update(vehicle: vehicle)
@ -224,13 +253,7 @@ class EventsController: UIViewController, UITableViewDataSource, UITableViewDele
}).disposed(by: self.bag) }).disposed(by: self.bag)
} }
func editEvent(index: Int) { func editEvent(event: VehicleEvent) {
guard let vehicle = self.vehicle else {
HUD.flash(.labeledError(title: nil, subtitle: "Unknown vehicle"))
return
}
let event = vehicle.events[index]
let sb = UIStoryboard(name: "Main", bundle: nil) let sb = UIStoryboard(name: "Main", bundle: nil)
let controller = sb.instantiateViewController(identifier: "LocationEditController") as LocationEditController let controller = sb.instantiateViewController(identifier: "LocationEditController") as LocationEditController
controller.title = NSLocalizedString("Edit event", comment: "") controller.title = NSLocalizedString("Edit event", comment: "")
@ -252,14 +275,8 @@ class EventsController: UIViewController, UITableViewDataSource, UITableViewDele
self.navigationController?.pushViewController(controller, animated: true) self.navigationController?.pushViewController(controller, animated: true)
} }
func copyEvent(index: Int) { func copyEvent(event: VehicleEvent) {
guard let vehicle = self.vehicle else {
HUD.flash(.labeledError(title: nil, subtitle: "Unknown vehicle"))
return
}
var items: [String: Any] = [:] var items: [String: Any] = [:]
let event = vehicle.events[index]
if let url = event.getMapLink() { if let url = event.getMapLink() {
items[kUTTypeURL as String] = url items[kUTTypeURL as String] = url
@ -278,17 +295,29 @@ class EventsController: UIViewController, UITableViewDataSource, UITableViewDele
self.setupBarButtonItems() self.setupBarButtonItems()
} }
func shareEvent(index: Int) { func shareEvent(event: VehicleEvent) {
guard let vehicle = self.vehicle else { guard let url = event.getMapLink() else {
HUD.flash(.labeledError(title: nil, subtitle: "Unknown vehicle"))
return return
} }
let event = vehicle.events[index]
if let url = event.getMapLink() {
let controller = UIActivityViewController(activityItems: [url], applicationActivities: nil) let controller = UIActivityViewController(activityItems: [url], applicationActivities: nil)
self.present(controller, animated: true) self.present(controller, animated: true)
} }
func openInAppleMaps(event: VehicleEvent) {
let coordinates = CLLocationCoordinate2D(latitude: event.latitude,
longitude: event.longitude)
let placemark = MKPlacemark(coordinate: coordinates)
let mapItem = MKMapItem(placemark: placemark)
mapItem.openInMaps()
}
func openInYandexMaps(event: VehicleEvent) {
guard let url = URL(string: "yandexmaps://maps.yandex.ru/?pt=\(event.longitude),\(event.latitude)&z=12") else {
return
}
UIApplication.shared.open(url)
} }
@objc func addEvent(_ sender: UIBarButtonItem) { @objc func addEvent(_ sender: UIBarButtonItem) {

View File

@ -4,6 +4,7 @@ import LinkPresentation
import RealmSwift import RealmSwift
import Eureka import Eureka
import AutoCatCore import AutoCatCore
import SwiftEntryKit
class ReportController: FormViewController, MediaBrowserViewControllerDataSource, MediaBrowserViewControllerDelegate, UIActivityItemSource { class ReportController: FormViewController, MediaBrowserViewControllerDataSource, MediaBrowserViewControllerDelegate, UIActivityItemSource {
@ -156,7 +157,15 @@ class ReportController: FormViewController, MediaBrowserViewControllerDataSource
for row in form.allRows { for row in form.allRows {
if let labelRow = row as? LabelRow { if let labelRow = row as? LabelRow {
let doubleTap = UITapGestureRecognizer { _ in let doubleTap = UITapGestureRecognizer { _ in
UIPasteboard.general.string = labelRow.value guard let text = labelRow.value else {
return
}
UIPasteboard.general.string = text
let generator = UIImpactFeedbackGenerator(style: .rigid)
generator.impactOccurred()
let toastMessage = NSLocalizedString("Copied: ", comment: "") + text
self.showToast(text: toastMessage)
} }
doubleTap.numberOfTapsRequired = 2 doubleTap.numberOfTapsRequired = 2
@ -165,6 +174,23 @@ class ReportController: FormViewController, MediaBrowserViewControllerDataSource
} }
} }
func showToast(text: String) {
let style = EKProperty.LabelStyle(
font: .systemFont(ofSize: 14),
color: .white, //.black,
alignment: .center
)
let labelContent = EKProperty.LabelContent(
text: text,
style: style
)
let contentView = EKNoteMessageView(with: labelContent)
var attributes: EKAttributes = .bottomFloat //.toast
attributes.entryBackground = .visualEffect(style: EKAttributes.BackgroundStyle.BlurStyle(light: .dark, dark: .light)) //.color(color: .init(red: 0, green: 196, blue: 0))
SwiftEntryKit.display(entry: contentView, using: attributes)
}
func update(row tag: String, with value: String) { func update(row tag: String, with value: String) {
if let row = self.form.rowBy(tag: tag) as? LabelRow { if let row = self.form.rowBy(tag: tag) as? LabelRow {
row.value = value row.value = value

View File

@ -2,6 +2,10 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>yandexmaps</string>
</array>
<key>ITSAppUsesNonExemptEncryption</key> <key>ITSAppUsesNonExemptEncryption</key>
<false/> <false/>
<key>CFBundleDevelopmentRegion</key> <key>CFBundleDevelopmentRegion</key>