diff --git a/Sources/ScrollkeeperUI/Views/LibraryWindow.swift b/Sources/ScrollkeeperUI/Views/LibraryWindow.swift index 2863d10..53d7aec 100644 --- a/Sources/ScrollkeeperUI/Views/LibraryWindow.swift +++ b/Sources/ScrollkeeperUI/Views/LibraryWindow.swift @@ -84,6 +84,8 @@ struct LibraryWindow: View { #endif #if os(macOS) .modifier(HandoffHandler(selection: $selection)) + #else + .modifier(PadHandoffHandler(selection: $selection)) #endif #if !os(macOS) // On the iPad the details cover part of the library, so they open when a title is chosen. diff --git a/Sources/ScrollkeeperUI/Views/PadHandoffHandler.swift b/Sources/ScrollkeeperUI/Views/PadHandoffHandler.swift new file mode 100644 index 0000000..1b34a6a --- /dev/null +++ b/Sources/ScrollkeeperUI/Views/PadHandoffHandler.swift @@ -0,0 +1,47 @@ +#if os(iOS) +import ScrollkeeperKit +import SwiftUI + +/// Takes the `scrollkeeper://` links other Tableside Toolkit apps open on the iPad (app-handoff 2.9). A download +/// or open request shows the title and downloads it when needed. Nothing is sent back: the requesting app finds +/// the title in the shared library when the user returns to it. A link that names no request, such as a bare +/// `scrollkeeper://`, and an index request only bring the app forward. +struct PadHandoffHandler: ViewModifier { + @Environment(LibraryStore.self) private var store + @Binding var selection: LibraryTitle.ID? + @State private var failure: HandoffError? + + func body(content: Content) -> some View { + content + // The window already open takes the link, rather than a new one opening for it. + .handlesExternalEvents(preferring: [HandoffRequest.scheme], allowing: [HandoffRequest.scheme]) + .onOpenURL { url in Task { await handle(url) } } + .alert(isPresented: isShowingFailure, error: failure) { + Button("OK") { failure = nil } + } + } + + private func handle(_ url: URL) async { + let request: HandoffRequest + do { + request = try HandoffRequest(url: url) + } catch .unrecognizedLink { + return + } catch { + failure = error + return + } + guard request.action != .index else { return } + selection = request.sku + do { + _ = try await store.handoffFiles(for: request) + } catch { + failure = error + } + } + + private var isShowingFailure: Binding { + Binding(get: { failure != nil }, set: { if !$0 { failure = nil } }) + } +} +#endif diff --git a/docs/specs/app-handoff/design.md b/docs/specs/app-handoff/design.md index 812c883..028ece6 100644 --- a/docs/specs/app-handoff/design.md +++ b/docs/specs/app-handoff/design.md @@ -106,8 +106,12 @@ the state of the download the request waits for. While `handoffFiles` waits, - `scripts/build-app.sh` declares the `scrollkeeper` URL scheme in `CFBundleURLTypes`. - The iPad app declares the same scheme in `ios/Info.plist`, which Xcode - merges into the Info.plist it generates (2.9). Nothing there reads the link: - the system brings the app forward and SwiftUI opens no window for it. + merges into the Info.plist it generates (2.9). `PadHandoffHandler`, on the + library window, reads it with `onOpenURL`; its + `handlesExternalEvents(preferring:allowing:)` has the open window take the + link, so none opens for it. It ignores an unrecognized link and an index + request, selects the requested title and awaits `handoffFiles`, discarding + the files, and shows a `HandoffError` in an alert. - The Mac `AppDelegate` owns the `LibraryStore`, starts it at launch and takes the links in `application(_:open:)`, so a request is answered whether or not a library window is open, including the one that launched the app. The diff --git a/docs/specs/app-handoff/requirements.md b/docs/specs/app-handoff/requirements.md index f9b006d..55ba1fd 100644 --- a/docs/specs/app-handoff/requirements.md +++ b/docs/specs/app-handoff/requirements.md @@ -58,10 +58,14 @@ does the same for any title with a PDF. to download, the download failed, the requesting app is not installed, or the download holds no file the requesting app can open. 2.8 One request handles one title (5.3b). -2.9 The iPad app does not handle open requests. It does register the - `scrollkeeper` link, so another toolkit app can see that it is installed - and bring it forward for the user to download a title there; whatever the - link asks for, it only brings the app forward, in the window it has. +2.9 The iPad app registers the `scrollkeeper` link, so another toolkit app + can see that it is installed and bring it forward, in the window it has. + It handles open and download requests (2.1, 2.10) alike: it checks the app + key (2.2), shows the title (2.6), downloads it when needed as 2.3 and 2.4 + describe, and says why in an alert when it can't (2.7). It opens nothing in + the other app and sends no status links: the other app finds the title in + the shared library once it is on disk. Any other link, such as a bare + `scrollkeeper://` or an index request, only brings the app forward. 2.10 The Mac app also handles `scrollkeeper://download?sku=&app=`: the same as an open request, except that nothing is opened and the library window's selection is left alone. The requesting app learns that diff --git a/ios/UITests/RequestLinkUITests.swift b/ios/UITests/RequestLinkUITests.swift index 12ab806..1e394ae 100644 --- a/ios/UITests/RequestLinkUITests.swift +++ b/ios/UITests/RequestLinkUITests.swift @@ -1,6 +1,7 @@ import XCTest -/// The `scrollkeeper` link another toolkit app opens to bring Scrollkeeper forward (app-handoff 2.9). +/// The `scrollkeeper` links another toolkit app opens to bring Scrollkeeper forward, or to have it download a +/// title (app-handoff 2.9). final class RequestLinkUITests: XCTestCase { override func setUp() { continueAfterFailure = false @@ -24,4 +25,33 @@ final class RequestLinkUITests: XCTestCase { // A window opened for the link would show All Titles, as a new one does. XCTAssertTrue(library.waitForExistence(timeout: 10), "the link opened a window of its own") } + + func testADownloadRequestShowsTheTitleAndDownloadsIt() throws { + let app = launchDemo(downloadSeconds: 300) + + XCUIDevice.shared.system.open(try XCTUnwrap(URL(string: "scrollkeeper://download?sku=DEMO1003E&app=mapsmith"))) + + let downloads = app.buttons["downloads-button"].firstMatch + let isDownloading = NSPredicate(format: "label == %@", "Downloads, 1 in progress") + let started = expectation(for: isDownloading, evaluatedWith: downloads) + wait(for: [started], timeout: 30) + XCTAssertTrue(app.staticTexts["Sample Creature Rulebook"].firstMatch.exists, "the title isn't shown") + } + + func testARequestFromAnUnknownAppIsRefused() throws { + let app = launchDemo() + + XCUIDevice.shared.system.open(try XCTUnwrap(URL(string: "scrollkeeper://download?sku=DEMO1003E&app=other"))) + + XCTAssertTrue(app.alerts.firstMatch.waitForExistence(timeout: 10), "no alert said why") + XCTAssertEqual(app.buttons["downloads-button"].firstMatch.label, "Downloads") + } + + private func launchDemo(downloadSeconds: Int = 8) -> XCUIApplication { + let app = XCUIApplication() + app.launchArguments = ["-ScrollkeeperDemo", "-demoDownloadSeconds", "\(downloadSeconds)"] + app.launch() + XCTAssertTrue(app.staticTexts["Downloaded"].firstMatch.waitForExistence(timeout: 30), "the library didn't load") + return app + } }