feat: Add feature to save seismic event in calendar
This commit is contained in:
+64
-1
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,5 +72,9 @@
|
||||
</array>
|
||||
<key>UIUserInterfaceStyle</key>
|
||||
<string>Light</string>
|
||||
<key>NSCalendarsUsageDescription</key>
|
||||
<string>L'accesso al calendario è richiesto per poter salvare le informazioni dei sismi di interesse</string>
|
||||
<key>NSContactsUsageDescription</key>
|
||||
<string>L'accesso ai contatti è richiesto per poter aggiungere persone agli eventi creati</string>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -761,3 +761,4 @@
|
||||
"weather_humidity" = "Humidity: %@%%";
|
||||
"weather_clouds" = "Cloud cover: %@%%";
|
||||
"official_prelimiary" = "Preliminary";
|
||||
"calendar_description_nogeo_km" = "Earthquake M%@, depth %@ km.";
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -754,3 +754,4 @@
|
||||
"weather_humidity" = "Humedad: %@%%";
|
||||
"weather_clouds" = "Cubierta de nubes: %@%%";
|
||||
"official_prelimiary" = "Preliminar";
|
||||
"calendar_description_nogeo_km" = "Sismo M%@, profundidad %@ km.";
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -747,3 +747,4 @@
|
||||
"weather_humidity" = "Umidità: %@%%";
|
||||
"weather_clouds" = "Copertura nuvolosa: %@%%";
|
||||
"official_prelimiary" = "Preliminare";
|
||||
"calendar_description_nogeo_km" = "Sisma M%@, profondità %@ km.";
|
||||
|
||||
Reference in New Issue
Block a user