frzi / swiftui-router

Path-based routing in SwiftUI
MIT License
919 stars 44 forks source link

NSUserActivity integration? #11

Closed emma-k-alexandra closed 4 years ago

emma-k-alexandra commented 4 years ago

Since routes know exactly what the user is doing at any given time, it would be pretty awesome to have some automatic integration with NSUserActivity to provide apps using SwiftUIRouter with some low effort integration with Siri and such on platforms that support NSUserActivity

frzi commented 4 years ago

That's a really neat idea and definitely where SwiftUI Router could shine. I am however wondering whether SwiftUI Router should be responsible for handling any NSUserActivity logic. I'd rather keep SwiftUI Router pretty basic (and abstract) so developers can build and attach their needs easily around it.

A possible way to use NSUserActivity is using onChange(of:) while keeping an eye on HistoryData.path:

struct RootView: View {
    var body: some View {
        Router {
            ContentView()
        }
    }
}

struct ContentView: View {
    @EnvironmentObject private var historyData: HistoryData

    var body: some View {
        Group {
            // Routes etc.
        }
        .onChange(of: historyData.path) { newPath in
            let activity = NSUserActivity()
            activity.userInfo = ["path": newPath]
            activity.becomeCurrent()
        }
    }
}

Thanks for your comment!

emma-k-alexandra commented 4 years ago

Very cool idea. Looking forward to seeing this project grow.