Meshtastic-Apple/Meshtastic/MeshtasticApp.swift
Garth Vander Houwen 69c318a9e1
Message list performance fixes into 2.7.6 (#1475)
* Remove extra want config call when adding a contact

* App badge and unnecessary notification fixes (#1455)

* - Fix for app badge not going to zero if a message arrives while you have that chat open
- Fix for push notifications popping up when a message is received while that chat is open

* Fix for cancelling notifications, works now. And scroll to bottom of conversation upon new message

* Fix: Channels help grammer fix (#1452)

* remove outdated TCP not available on Apple devices (#1450)

* Update initial onboarding view

* remove toggle gating for mac

* Update crash reporting opt out in real time

* Update onboarding text

* Use mDNS text records for node name

* TCP IP and port on the connection screen

* Hide app icon chooser on mac

* Infinite loop hang bugfixes and performance improvements for both `UserMessageList` and `ChannelMessageList` (#1465)

* 2.7.5 Working Changes (#1460)

* Remove extra want config call when adding a contact

* App badge and unnecessary notification fixes (#1455)

* - Fix for app badge not going to zero if a message arrives while you have that chat open
- Fix for push notifications popping up when a message is received while that chat is open

* Fix for cancelling notifications, works now. And scroll to bottom of conversation upon new message

* Fix: Channels help grammer fix (#1452)

* remove outdated TCP not available on Apple devices (#1450)

* Update initial onboarding view

* remove toggle gating for mac

* Update crash reporting opt out in real time

* Update onboarding text

---------

Co-authored-by: Gnome Adrift <646322+gnomeadrift@users.noreply.github.com>
Co-authored-by: Zain Kergaye <62440012+ZainKergaye@users.noreply.github.com>
Co-authored-by: NillRudd <102033730+NillRudd@users.noreply.github.com>

* UserEntity: add mostRecentMessage and unreadMessages with early exit when lastMessage is nil, and fetch 1 row (not N) otherwise

* UserList: replace 5 slow calls to user.messageList with new fast calls

* NodeList: always put the connected node at the top of list (if it matches the node filters)

* ChannelEntity: add faster mostRecentPrivateMessage and unreadMessages which fetch 1 row (not N)

* ChannelList: replace 5 calls to channel.allPrivateMessage with new fast calls

* Fix incorrect appState.unreadDirectMessages calculations

* MyInfoEntity: also fix unreadMessages count here to be fast, and use it for appState.unreadChannelMessages

* UserMessageList: use @FetchRequest to prevent the N^2 behavior that was happening in calls to allPrivateMessages

* Refactor ChannelEntityExtension and MyInfoEntityExtension to be more similar to UserEntityExtension

* Remove SwiftUI-infinite-loop-causing `.id(redrawTapbacksTrigger)` in ChannelMessageList and UserMessageList (duplicate row ids)

* MyInfoEntityExtension: exclude emoji tapbacks (which never get marked as read anyway) from unread message count

* Add SaveChannelLinkData so MessageText and MeshtasticApp can use .sheet(item: ...) and avoid infinite loop hang due to Binding rebuild

* ChannelMessageList and UserMessageList: switch to stable messageId for ForEach SwiftUI row identity

* ChannelMessageList and UserMessageList: debouncedScrollToBottom; keyboardWillShowNotification/keyboardDidShowNotification

* ChannelMessageList and UserMessageList: scroll to bottom onFirstAppear

* ChannelMessageList and UserMessageList: block spurious markMessagesAsRead when this View is not active

---------

Co-authored-by: Garth Vander Houwen <garth@meshtastic.com>
Co-authored-by: Gnome Adrift <646322+gnomeadrift@users.noreply.github.com>
Co-authored-by: Zain Kergaye <62440012+ZainKergaye@users.noreply.github.com>
Co-authored-by: NillRudd <102033730+NillRudd@users.noreply.github.com>

* message-list-performance: revert scrolling changes (#1472)

* Revert e0f0b4a0f7 (ChannelMessageList and UserMessageList: scroll to bottom onFirstAppear)

* Revert "ChannelMessageList and UserMessageList: debouncedScrollToBottom; keyboardWillShowNotification/keyboardDidShowNotification"

This reverts commit ee1a7c4415.

---------

Co-authored-by: Gnome Adrift <646322+gnomeadrift@users.noreply.github.com>
Co-authored-by: Zain Kergaye <62440012+ZainKergaye@users.noreply.github.com>
Co-authored-by: NillRudd <102033730+NillRudd@users.noreply.github.com>
Co-authored-by: Jake-B <jake-b@users.noreply.github.com>
Co-authored-by: Mike Robbins <mrobbins@alum.mit.edu>
2025-10-17 18:16:00 -07:00

218 lines
7.5 KiB
Swift

// Copyright (C) 2022 Garth Vander Houwen
import SwiftUI
import CoreData
import OSLog
import TipKit
import MeshtasticProtobufs
import DatadogCore
import DatadogCrashReporting
import DatadogRUM
import DatadogTrace
import DatadogLogs
import DatadogSessionReplay
@main
struct MeshtasticAppleApp: App {
@UIApplicationDelegateAdaptor(MeshtasticAppDelegate.self) private var appDelegate
@StateObject var appState: AppState
private let persistenceController: PersistenceController
private let accessoryManager: AccessoryManager
@Environment(\.scenePhase) var scenePhase
@State var saveChannelLink: SaveChannelLinkData?
@State var incomingUrl: URL?
init() {
let persistenceController = PersistenceController.shared
let appState = AppState(
router: Router()
)
// Initialize Datadog
// RUM Client Tokens are NOT secret
let appID = "79fe92a9-74c9-4c8f-ba63-6308384ecfa9"
let clientToken = "pub4427bea20dbdb08a6af68034de22cd3b"
var environment = "AppStore"
#if DEBUG
environment = "Local"
#else
if Bundle.main.isTestFlight {
environment = "TestFlight"
}
#endif
#if false
Datadog.initialize(
with: Datadog.Configuration(
clientToken: clientToken,
env: environment,
site: .us5
),
trackingConsent: UserDefaults.usageDataAndCrashReporting ? .granted : .notGranted
)
DatadogCrashReporting.CrashReporting.enable()
Logs.enable()
Trace.enable(
with: Trace.Configuration(
sampleRate: 100, networkInfoEnabled: true // 100% sampling for development/testing, reduce for production
)
)
RUM.enable(
with: RUM.Configuration(
applicationID: appID,
swiftUIViewsPredicate: DefaultSwiftUIRUMViewsPredicate(),
swiftUIActionsPredicate: DefaultSwiftUIRUMActionsPredicate(isLegacyDetectionEnabled: true),
trackBackgroundEvents: true
)
)
if Bundle.main.isTestFlight {
SessionReplay.enable(
with: SessionReplay.Configuration(
replaySampleRate: 100,
textAndInputPrivacyLevel: .maskSensitiveInputs,
imagePrivacyLevel: .maskNone,
touchPrivacyLevel: .show,
startRecordingImmediately: true,
featureFlags: [.swiftui: true]
)
)
}
#endif
accessoryManager = AccessoryManager.shared
accessoryManager.appState = appState
self._appState = StateObject(wrappedValue: appState)
self.persistenceController = persistenceController
// Wire up router
self.appDelegate.router = appState.router
// Initialize map data manager
MapDataManager.shared.initialize()
#if DEBUG
// Show tips in development
try? Tips.resetDatastore()
#endif
if !UserDefaults.firstLaunch {
// If this is first launch, we will show onboarding screens which
// Step through the authorization process. Do not start discovery
// unitl this workflow completes, otherwise the discovery process
// may trigger permission dialogs too soon.
accessoryManager.startDiscovery()
}
}
var body: some Scene {
WindowGroup {
ContentView(
appState: appState,
router: appState.router
)
.sheet(item: $saveChannelLink
) { link in
SaveChannelQRCode(
channelSetLink: link.data,
addChannels: link.add,
accessoryManager: accessoryManager )
.presentationDetents([.large])
.presentationDragIndicator(.visible)
}
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
Logger.mesh.debug("URL received \(userActivity, privacy: .public)")
self.incomingUrl = userActivity.webpageURL
self.saveChannelLink = nil
var addChannels = false
if self.incomingUrl?.absoluteString.lowercased().contains("meshtastic.org/v/#") == true {
ContactURLHandler.handleContactUrl(url: self.incomingUrl!, accessoryManager: accessoryManager)
} else if self.incomingUrl?.absoluteString.lowercased().contains("meshtastic.org/e/") == true {
if let components = self.incomingUrl?.absoluteString.components(separatedBy: "#") {
addChannels = Bool(self.incomingUrl?["add"] ?? "false") ?? false
if (self.incomingUrl?.absoluteString.lowercased().contains("?")) != nil {
guard let cs = components.last!.components(separatedBy: "?").first else {
return
}
self.saveChannelLink = SaveChannelLinkData(data: cs, add: addChannels)
} else {
guard let cs = components.first else {
return
}
self.saveChannelLink = SaveChannelLinkData(data: cs, add: addChannels)
}
Logger.services.debug("Add Channel \(addChannels, privacy: .public)")
}
Logger.mesh.debug("User wants to open a Channel Settings URL: \(self.incomingUrl?.absoluteString ?? "No QR Code Link")")
}
if self.saveChannelLink != nil {
Logger.mesh.debug("User wants to open Channel Settings URL: \(String(describing: self.incomingUrl!.relativeString), privacy: .public)")
}
}
.onOpenURL(perform: { (url) in
Logger.mesh.debug("Some sort of URL was received \(url, privacy: .public)")
self.incomingUrl = url
var addChannels = false
if url.absoluteString.lowercased().contains("meshtastic.org/v/#") {
ContactURLHandler.handleContactUrl(url: url, accessoryManager: accessoryManager)
} else if url.absoluteString.lowercased().contains("meshtastic.org/e/") {
if let components = self.incomingUrl?.absoluteString.components(separatedBy: "#") {
addChannels = Bool(self.incomingUrl?["add"] ?? "false") ?? false
if self.incomingUrl?.absoluteString.lowercased().contains("?") != nil {
guard let cs = components.last!.components(separatedBy: "?").first else {
return
}
self.saveChannelLink = SaveChannelLinkData(data: cs, add: addChannels)
} else {
guard let cs = components.first else {
return
}
self.saveChannelLink = SaveChannelLinkData(data: cs, add: addChannels)
}
Logger.services.debug("Add Channel \(addChannels, privacy: .public)")
}
Logger.mesh.debug("User wants to open a Channel Settings URL: \(self.incomingUrl?.absoluteString ?? "No QR Code Link", privacy: .public)")
} else if url.absoluteString.lowercased().contains("meshtastic:///") {
appState.router.route(url: url)
}
})
.task {
try? Tips.configure(
[
// Reset which tips have been shown and what parameters have been tracked, useful during testing and for this sample project
.datastoreLocation(.applicationDefault),
// When should the tips be presented? If you use .immediate, they'll all be presented whenever a screen with a tip appears.
// You can adjust this on per tip level as well
.displayFrequency(.immediate)
]
)
}
}
.onChange(of: scenePhase) { (_, newScenePhase) in
switch newScenePhase {
case .background:
Logger.services.info("🎬 [App] Scene is in the background")
accessoryManager.appDidEnterBackground()
do {
try persistenceController.container.viewContext.save()
Logger.services.info("💾 [App] Saved CoreData ViewContext when the app went to the background.")
} catch {
Logger.services.error("💥 [App] Failed to save viewContext when the app goes to the background.")
}
case .inactive:
Logger.services.info("🎬 [App] Scene is inactive")
case .active:
Logger.services.info("🎬 [App] Scene is active")
accessoryManager.appDidBecomeActive()
@unknown default:
Logger.services.error("🍎 [App] Apple must have changed something")
}
}
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.environmentObject(appState)
.environmentObject(accessoryManager)
.environmentObject(appState.router)
}
}