Files
swiftGrammar/AIGrammar/AIGrammarApp.swift
2024-09-10 16:39:30 +08:00

110 lines
4.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// AIGrammarApp.swift
// AIGrammar
//
// Created by oscar on 2024/3/27.
//
import SwiftUI
import TrustDecision
import Combine
import Firebase
import FirebaseAnalytics
import FirebaseCrashlytics
@main
struct AIGrammarApp: App {
// AppDelegate
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
let persistenceController = PersistenceController.shared
@StateObject private var appState = NofifyState()
@State private var urlToOpen: String?
init() {
// AppDelegate
appDelegate.app = self
//
setupLogging()
_ = InitApp.shared
// firebase info线
FirebaseConfiguration.shared.setLoggerLevel(.error)
FirebaseApp.configure()
Crashlytics.crashlytics().setCrashlyticsCollectionEnabled(true)
//
// Crashlytics.crashlytics().log("Test crash")
// fatalError("Test crash from Crashlytics")
}
var body: some Scene {
WindowGroup {
AllTabView(selectedTab: $appState.selectedTab, showPromotion: $appState.showPromotion, promotionMode: $appState.promotionMode)
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.environmentObject(IAPManager()) // IAPManager
.environmentObject(globalEnvironment) // IAPManager
.environmentObject(appState)
.preferredColorScheme(.light) // 使
.sheet(isPresented: $appState.showPromotion) {
switch appState.promotionMode {
case .halfScreen:
PromotionView() //
.presentationDetents([.medium])
case .fullScreen:
PromotionView() //
.edgesIgnoringSafeArea(.all)
default:
EmptyView()
}
}
.onChange(of: urlToOpen) { url in
if let url = url, let link = URL(string: url) {
UIApplication.shared.open(link)
}
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
handlePushNotification()
}
}
}
// app
func openURL(urlString: String) {
urlToOpen = urlString
}
func handlePushNotification() {
// 线
DispatchQueue.main.async {
let pushInfo = globalEnvironment.pushSettings
// tab
if pushInfo.gotoTab >= 0 && pushInfo.gotoTab < 4 {
self.appState.selectedTab = pushInfo.gotoTab
}else {
logger.info("invalid goto Tab: \(pushInfo.gotoTab)")
}
//
if pushInfo.showPage && pushInfo.page != "" {
self.appState.showPromotion = true
switch pushInfo.showMode {
case "halfScreen" :
self.appState.promotionMode = PromotionDisplayType.halfScreen
case "fullScreen" :
self.appState.promotionMode = PromotionDisplayType.fullScreen
default:
break
}
}
//
if pushInfo.showPage && pushInfo.openURL != "" {
urlToOpen = pushInfo.openURL
}
logger.info("Push Values. gotoTab: \(pushInfo.gotoTab), showPage: \(pushInfo.showPage), page: \(pushInfo.page), showMode: \(pushInfo.showMode), url: \(pushInfo.openURL)")
}
}
}