Reputation: 1
I'm trying to use NavSplitView and have a home screen or Lock Screen widget open the app to the tapped tool but my widget/navigation structure clearly needs to be adjusted.
@main
struct SparkysCircKitApp: App {
@StateObject private var navManager = NavigationManager.shared
@Namespace var animationNamespace
@State private var selectedTool: Tool?
let deviceType = UIDevice.current.userInterfaceIdiom
var body: some Scene {
WindowGroup {
if deviceType == .pad {
iPadListView(animationNamespace: animationNamespace, selectedTool: selectedTool?.route)
.environmentObject(accentColorManager)
.environmentObject(calculatorViewModel)
.environmentObject(navManager)
.environmentObject(settingsViewModel)
.environmentObject(toolStore)
} else {
\\\ iPHONE VIEWS
}
}
private func handleDeepLink(_ url: URL) {
print("Handling deep link: \(url)")
guard url.scheme == "sparky" else {
print("Invalid URL scheme: \(url.scheme ?? "nil")")
return
}
guard let host = url.host else {
print("No host in URL")
return
}
// Convert the hyphenated URL format back to a tool name
let toolName: String
if host == "nema-configurations" {
toolName = "configurations"
} else {
toolName = host
.replacingOccurrences(of: "-", with: " ")
.localizedCapitalized
}
DispatchQueue.main.async {
if toolName.lowercased() == "calculator" {
navManager.isCalculatorPresented = true
} else if toolName.lowercased() == "codeoftheday", let ruleNumber = url.pathComponents.last {
NotificationCenter.default.post(
name: .showCodeOfTheDay,
object: ruleNumber
)
} else if let tool = toolStore.findTool(byName: toolName) {
navManager.popToRoot()
navManager.navigate(to: tool.route)
selectedTool = tool
} else {
print("Could not find tool for name: \(toolName)")
}
}
}
}
struct iPadListView: View {
@EnvironmentObject var navManager: NavigationManager
@EnvironmentObject var toolStore: ToolStore
@State private var selectedTool: ToolRoute? = nil
var animationNamespace: Namespace.ID
init(
animationNamespace: Namespace.ID,
selectedTool: ToolRoute?
) {
self.animationNamespace = animationNamespace
self.selectedTool = selectedTool
}
var body: some View {
NavigationSplitView {
List(selection: $selectedTool) {
\\\ LIST VIEW
} detail: {
if let toolRoute = selectedTool {
ToolRouteView(route: toolRoute)
.environmentObject(accentColorManager)
.environmentObject(calculatorViewModel)
.environmentObject(navManager)
.environmentObject(settingsViewModel)
.environmentObject(toolStore)
} else {
ContentUnavailableView("Select a Tool",
systemImage: "hammer.circle",
description: Text("Choose a tool from the sidebar to get started"))
}
}
.onChange(of: selectedTool) {
navManager.activeRoute = selectedTool
}
.onOpenURL { url in
handleWidgetDeepLink(url)
}
}
private func handleWidgetDeepLink(_ url: URL) {
print("Handling iPad widget deep link: \(url)")
guard url.scheme == "sparky" else {
print("Invalid URL scheme: \(url.scheme ?? "nil")")
return
}
guard let host = url.host else {
print("No host in URL")
return
}
let toolName: String
if host == "nema-configurations" {
toolName = "configurations"
} else {
toolName = host
.replacingOccurrences(of: "-", with: " ")
.localizedCapitalized
}
DispatchQueue.main.async {
if toolName.lowercased() == "calculator" {
showingCalc = true
} else if toolName.lowercased() == "codeoftheday", let ruleNumber = url.pathComponents.last {
if let code = codes.first(where: { $0.ruleNumber == ruleNumber }) {
codeOfTheDay = code
}
}
}
}
}
I've tried using Notifications, passing the tool as a parameter and a few other variations but I can't seem to figure it out.
I've verified the URL scheme.
Upvotes: 0
Views: 18