Move notification check to init

Clean up initial map centering
This commit is contained in:
Garth Vander Houwen 2023-03-26 09:08:08 -07:00
parent 002e6d6986
commit cfa4c03faa
5 changed files with 33 additions and 42 deletions

View file

@ -1,8 +1,8 @@
//
// Persistence.swift
// CoreDataSample
// Meshtastic
//
// Created by Garth Vander Houwen on 11/28/21.
// Copyright(c) Garth Vander Houwen 11/28/21.
//
import CoreData

View file

@ -1,3 +1,10 @@
//
// PersistenceEntityExtenstion.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 11/28/21.
//
import CoreData
import CoreLocation
import MapKit

View file

@ -1,6 +1,6 @@
//
// UserEntityExtension.swift
// MeshtasticApple
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 6/3/22.
//

View file

@ -26,6 +26,20 @@ struct Connect: View {
@State var presentingSwitchPreferredPeripheral = false
@State var selectedPeripherialId = ""
init () {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.getNotificationSettings(completionHandler: { (settings) in
if settings.authorizationStatus == .notDetermined {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
print("Notifications are all set!")
} else if let error = error {
print(error.localizedDescription)
}
}
}
})
}
var body: some View {
NavigationStack {
@ -279,18 +293,9 @@ struct Connect: View {
.onAppear(perform: {
self.bleManager.context = context
self.bleManager.userSettings = userSettings
// Ask for notification permission
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
print("Notifications are all set!")
} else if let error = error {
print(error.localizedDescription)
}
}
})
}
#if canImport(ActivityKit)
#if canImport(ActivityKit)
func startNodeActivity() {
if #available(iOS 16.2, *) {
liveActivityStarted = true
@ -330,29 +335,7 @@ struct Connect: View {
}
}
}
#endif
#if os(iOS)
func postNotification() {
let timerSeconds = 60
let content = UNMutableNotificationContent()
content.title = "Mesh Live Activity Over"
content.body = "Your timed mesh live activity is over."
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timerSeconds), repeats: false)
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString,
content: content, trigger: trigger)
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error) in
if error != nil {
// Handle any errors.
print("Error posting local notification: \(error?.localizedDescription ?? "no description")")
} else {
print("Posted local notification.")
}
}
}
#endif
#endif
func didDismissSheet() {
bleManager.disconnectPeripheral(reconnect: false)

View file

@ -43,8 +43,8 @@ struct MapViewSwiftUI: UIViewRepresentable {
let span = MKCoordinateSpan(latitudeDelta: 0.003, longitudeDelta: 0.003)
let center = (latest.count > 0 && userTrackingMode == MKUserTrackingMode.none) ? latest[0].coordinate : LocationHelper.currentLocation
let region = MKCoordinateRegion(center: center, span: span)
mapView.setRegion(region, animated: true)
mapView.addAnnotations(showNodeHistory ? positions : latest)
mapView.setRegion(region, animated: true)
// Set user (phone gps) tracking options
mapView.setUserTrackingMode(userTrackingMode, animated: true)
if userTrackingMode == MKUserTrackingMode.none {
@ -120,7 +120,7 @@ struct MapViewSwiftUI: UIViewRepresentable {
let latest = positions
.filter { $0.latest == true }
.sorted { $0.nodePosition?.num ?? 0 > $1.nodePosition?.num ?? -1 }
let annotationCount = waypoints.count + (showNodeHistory || showRouteLines ? positions.count : latest.count)
let annotationCount = waypoints.count + (showNodeHistory ? positions.count : latest.count)
if annotationCount > mapView.annotations.count {
print("Annotation Count: \(annotationCount) Map Annotations: \(mapView.annotations.count)")
if showRouteLines {
@ -150,23 +150,24 @@ struct MapViewSwiftUI: UIViewRepresentable {
}
mapView.removeAnnotations(mapView.annotations)
mapView.addAnnotations(waypoints)
mapView.setUserTrackingMode(userTrackingMode, animated: true)
if userTrackingMode == MKUserTrackingMode.none {
mapView.showsUserLocation = false
mapView.addAnnotations(showNodeHistory ? positions : latest)
if recenter {
if latest.count == 1 {
mapView.fit(annotations:showNodeHistory ? positions : latest, andShow: false)
mapView.fit(annotations:showNodeHistory ? positions : latest, andShow: true)
} else {
mapView.addAnnotations(showNodeHistory ? positions : latest)
mapView.fitAllAnnotations()
}
} else {
mapView.addAnnotations(showNodeHistory ? positions : latest)
}
} else {
// Centering Done by tracking mode
mapView.addAnnotations(showNodeHistory ? positions : latest)
mapView.showsUserLocation = true
}
mapView.setUserTrackingMode(userTrackingMode, animated: true)
}
}
}