diff --git a/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksViewController.swift b/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksViewController.swift index 7cd08c6..b2648e8 100644 --- a/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksViewController.swift +++ b/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksViewController.swift @@ -7,6 +7,7 @@ // import UIKit +import EventKitUI class SeismicNetworksViewController: EQNBaseViewController, UITableViewDelegate, UITableViewDataSource { @@ -105,6 +106,58 @@ class SeismicNetworksViewController: EQNBaseViewController, UITableViewDelegate, cell.delegate = self return cell } + + // MARK: - Private + + private func openCalendar(for seismic: EQNSisma) { + checkCalendarPermission { + self.createCalendarEvent(for: seismic) + } failure: { + let alert = UIAlertController(title: "Attenzione", message: "Non è possibile aprire il calendario, assicurarsi di aver impostato i permessi corretti", preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "Ok", style: .default)) + self.present(alert, animated: true) + } + } + + private func checkCalendarPermission(success: @escaping () -> Void, failure: @escaping () -> Void) { + let authorization = EKEventStore.authorizationStatus(for: .event) + switch authorization { + case .notDetermined: + let eventStore = EKEventStore() + eventStore.requestAccess(to: .event) { (granted, error) in + DispatchQueue.main.async { + if granted { + success() + } else { + failure() + } + } + } + case .authorized: + success() + default: + failure() + } + } + + private func createCalendarEvent(for seismic: EQNSisma) { + let eventStore = EKEventStore() + + // calendar event + let event = EKEvent(eventStore: eventStore) + event.title = seismic.place + event.startDate = seismic.date + event.endDate = seismic.date + event.notes = String(format: NSLocalizedString("calendar_description_nogeo_km", comment: ""), seismic.magnitude, seismic.depth) + + // controller to present + let eventVC = EKEventEditViewController() + eventVC.editViewDelegate = self + eventVC.eventStore = eventStore + eventVC.event = event + + present(eventVC, animated: true) + } } extension SeismicNetworksViewController: SeismicNetworkTableViewCellDelegate { @@ -137,7 +190,10 @@ extension SeismicNetworksViewController: SeismicNetworkTableViewCellDelegate { } func seismicNetworkCellDidTapCalendar(_ cell: SeismicNetworkTableViewCell) { - // todo + guard let index = tableView?.indexPath(for: cell) else { return } + + let seismic = seismics[index.row] + openCalendar(for: seismic) } func seismicNetworkCellDidTapSettings(_ cell: SeismicNetworkTableViewCell) { @@ -154,3 +210,10 @@ extension SeismicNetworksViewController: SeismicNetworkTableViewCellDelegate { tableView?.reloadRows(at: indexToReloads, with: .automatic) } } + + +extension SeismicNetworksViewController: EKEventEditViewDelegate { + func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) { + dismiss(animated: true, completion: nil) + } +} diff --git a/Sources/Earthquake Network/Info.plist b/Sources/Earthquake Network/Info.plist index ac897e1..3e79361 100644 --- a/Sources/Earthquake Network/Info.plist +++ b/Sources/Earthquake Network/Info.plist @@ -72,5 +72,9 @@ UIUserInterfaceStyle Light + NSCalendarsUsageDescription + L'accesso al calendario è richiesto per poter salvare le informazioni dei sismi di interesse + NSContactsUsageDescription + L'accesso ai contatti è richiesto per poter aggiungere persone agli eventi creati diff --git a/Sources/Earthquake Network/en.lproj/InfoPlist.strings b/Sources/Earthquake Network/en.lproj/InfoPlist.strings index 96e7eb2..b55154e 100644 --- a/Sources/Earthquake Network/en.lproj/InfoPlist.strings +++ b/Sources/Earthquake Network/en.lproj/InfoPlist.strings @@ -2,4 +2,6 @@ "NSLocationAlwaysUsageDescription" = "Change to 'Always allow' so we can alert you in real time when an earthquake occurs nearby."; "NSLocationWhenInUseUsageDescription" = "We need your location to send you real time seismic alerts."; "NSPhotoLibraryAddUsageDescription" = "Access to the library is required in order to save the images generated by the app"; +"NSCalendarsUsageDescription" = "Access to the calendar is required in order to save the information of the earthquakes of interest"; +"NSContactsUsageDescription" = "Access to contacts is required in order to add people to the created events"; "CFBundleDisplayName" = "Earthquake Network"; diff --git a/Sources/Earthquake Network/en.lproj/Localizable.strings b/Sources/Earthquake Network/en.lproj/Localizable.strings index 8e758b4..0a9be61 100644 --- a/Sources/Earthquake Network/en.lproj/Localizable.strings +++ b/Sources/Earthquake Network/en.lproj/Localizable.strings @@ -761,3 +761,4 @@ "weather_humidity" = "Humidity: %@%%"; "weather_clouds" = "Cloud cover: %@%%"; "official_prelimiary" = "Preliminary"; +"calendar_description_nogeo_km" = "Earthquake M%@, depth %@ km."; diff --git a/Sources/Earthquake Network/es.lproj/InfoPlist.strings b/Sources/Earthquake Network/es.lproj/InfoPlist.strings index fc34411..bc72cc9 100644 --- a/Sources/Earthquake Network/es.lproj/InfoPlist.strings +++ b/Sources/Earthquake Network/es.lproj/InfoPlist.strings @@ -2,4 +2,6 @@ "NSLocationAlwaysUsageDescription" = "Cambiar a 'Permitir siempre' para que podamos alertarte en tiempo real cuando se produce un sismo cercano."; "NSLocationWhenInUseUsageDescription" = "Necesitamos tu ubicación para enviarte alertas sísmicas en tiempo real"; "NSPhotoLibraryAddUsageDescription" = "Se requiere acceso a la biblioteca para guardar las imágenes generadas por la aplicación"; +"NSCalendarsUsageDescription" = "Se requiere acceso al calendario para guardar la información de los terremotos de interés"; +"NSContactsUsageDescription" = "Se requiere acceso a los contactos para agregar personas a los eventos creados"; "CFBundleDisplayName" = "Sismos Detector"; diff --git a/Sources/Earthquake Network/es.lproj/Localizable.strings b/Sources/Earthquake Network/es.lproj/Localizable.strings index df644c9..031a8da 100644 --- a/Sources/Earthquake Network/es.lproj/Localizable.strings +++ b/Sources/Earthquake Network/es.lproj/Localizable.strings @@ -754,3 +754,4 @@ "weather_humidity" = "Humedad: %@%%"; "weather_clouds" = "Cubierta de nubes: %@%%"; "official_prelimiary" = "Preliminar"; +"calendar_description_nogeo_km" = "Sismo M%@, profundidad %@ km."; diff --git a/Sources/Earthquake Network/it.lproj/InfoPlist.strings b/Sources/Earthquake Network/it.lproj/InfoPlist.strings index a0aa43b..07ddfee 100644 --- a/Sources/Earthquake Network/it.lproj/InfoPlist.strings +++ b/Sources/Earthquake Network/it.lproj/InfoPlist.strings @@ -2,4 +2,6 @@ "NSLocationAlwaysUsageDescription" = "Passa a 'Consenti sempre' in modo che possiamo allertarti in tempo reale quando accade un sisma nelle tue vicinanze."; "NSLocationWhenInUseUsageDescription" = "La tua posizione è necessaria per ricevere le allerte sismiche in tempo reale."; "NSPhotoLibraryAddUsageDescription" = "L'accesso alla libreria è richiesto per poter salvare le immagini generate dall'app"; +"NSCalendarsUsageDescription" = "L'accesso al calendario è richiesto per poter salvare le informazioni dei sismi di interesse"; +"NSContactsUsageDescription" = "L'accesso ai contatti è richiesto per poter aggiungere persone agli eventi creati"; "CFBundleDisplayName" = "Rilevatore Terremoto"; diff --git a/Sources/Earthquake Network/it.lproj/Localizable.strings b/Sources/Earthquake Network/it.lproj/Localizable.strings index 77d7d93..e05a734 100644 --- a/Sources/Earthquake Network/it.lproj/Localizable.strings +++ b/Sources/Earthquake Network/it.lproj/Localizable.strings @@ -747,3 +747,4 @@ "weather_humidity" = "Umidità: %@%%"; "weather_clouds" = "Copertura nuvolosa: %@%%"; "official_prelimiary" = "Preliminare"; +"calendar_description_nogeo_km" = "Sisma M%@, profondità %@ km.";