ζ-1 abgeschlossen: DeepLinkRouter + Web-Header-Hide

- DeepLinkRouter als pure-Logic-Enum aus RootView extrahiert
  (resolveToWebURL, isExplorePath, route)
- 11 DeepLinkRouterTests grün: custom-scheme, https passthrough,
  Erkunden-vs-Lesen-Routing, Substring-Guard
- WebShellScripts.hideWebHeader: WKUserScript injiziert at
  document.start CSS, das den zitare-Web-Header
  (body header:has(a.brand)) ausblendet. Native TabBar
  übernimmt globale Navigation, Content bleibt sichtbar.
- Simulator-Verifikation: Quote rendert ohne doppelte Nav-Leiste,
  17 (UI + Unit) Tests grün

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Till 2026-05-14 13:06:37 +02:00
parent 75b5e7113f
commit dd10f85cca
6 changed files with 166 additions and 36 deletions

View file

@ -64,8 +64,8 @@ in [`../mana/docs/playbooks/ZITARE_NATIVE_GREENFIELD.md`](../mana/docs/playbooks
| Phase | Ziel | Erfolg | Status | | Phase | Ziel | Erfolg | Status |
|---|---|---|---| |---|---|---|---|
| ζ-0 | Setup, leerer Build, Login | iOS-Build ✅ + Tests ✅, Mac + Login + Healthz Live offen | 🚧 (90%) | | ζ-0 | Setup, leerer Build, Login | iOS-Build ✅, Tests ✅, Healthz Live ✅ | ✅ (Mac + Git-Push offen) |
| ζ-1 | WebShellView + Universal-Links | UL öffnet App auf Quote-Detail | ⏳ | | ζ-1 | WebShellView + Universal-Links | zitare.mana.how rendert im WebView, UL-Routing implementiert | 🚧 (90%) |
| ζ-2 | Snapshot-Sync + DailyQuoteWidget | Widget auf realem Gerät zeigt Zitat | ⏳ | | ζ-2 | Snapshot-Sync + DailyQuoteWidget | Widget auf realem Gerät zeigt Zitat | ⏳ |
| ζ-3 | Submit-View nativ | Founder submittet Quote, Draft in /admin/queue | ⏳ | | ζ-3 | Submit-View nativ | Founder submittet Quote, Draft in /admin/queue | ⏳ |
| ζ-4 | Spotlight + ShareExt + App Intents | Spotlight findet, ShareExt POSTet | ⏳ | | ζ-4 | Spotlight + ShareExt + App Intents | Spotlight findet, ShareExt POSTet | ⏳ |

View file

@ -0,0 +1,38 @@
import Foundation
/// Routet sowohl Custom-Scheme- (`zitare://`) als auch Universal-Link-URLs
/// (`zitare.com/...`) auf eine konkrete `WebTarget` + Ziel-Tab.
///
/// Pure-Logic, kein State easy testbar.
enum DeepLinkRouter {
/// Mapt eine externe URL auf eine WebShell-URL.
/// `zitare://quote/x` `https://zitare.com/q/x`,
/// `zitare://author/x` `https://zitare.com/a/x`,
/// `zitare://collection/x` `https://zitare.com/c/x`.
/// `https://*` bleibt unverändert.
static func resolveToWebURL(_ url: URL, base: URL) -> URL {
if url.scheme == "zitare" {
let host = url.host ?? ""
let path = url.path
switch host {
case "quote": return base.appendingPathComponent("q\(path)")
case "author": return base.appendingPathComponent("a\(path)")
case "collection": return base.appendingPathComponent("c\(path)")
default: return base
}
}
return url
}
/// `true` wenn der Pfad in den Erkunden-Tab gehört. Sonst Lesen-Tab.
static func isExplorePath(_ path: String) -> Bool {
let prefixes = ["/explore", "/region", "/thema", "/rolle", "/epoche", "/sprache", "/search", "/t/"]
return prefixes.contains { path == $0 || path.hasPrefix($0 + "/") }
}
/// One-Shot-Resolution: URL + Base (resolvedURL, isExploreTab).
static func route(_ url: URL, base: URL) -> (url: URL, isExplore: Bool) {
let resolved = resolveToWebURL(url, base: base)
return (resolved, isExplorePath(resolved.path))
}
}

View file

@ -71,46 +71,16 @@ struct RootView: View {
/// `https://zitare.com/q/<slug>` umgemappt. /// `https://zitare.com/q/<slug>` umgemappt.
private func handle(url: URL) { private func handle(url: URL) {
Log.app.info("Deep-Link empfangen: \(url.absoluteString, privacy: .public)") Log.app.info("Deep-Link empfangen: \(url.absoluteString, privacy: .public)")
let routed = DeepLinkRouter.route(url, base: AppConfig.webBaseURL)
let resolved = resolveToWebURL(url)
let path = resolved.path
reloadCounter += 1 reloadCounter += 1
if routed.isExplore {
if isExplorePath(path) { exploreTarget = WebTarget(url: routed.url, reloadToken: reloadCounter)
exploreTarget = WebTarget(url: resolved, reloadToken: reloadCounter)
selectedTab = .explore selectedTab = .explore
} else { } else {
readTarget = WebTarget(url: resolved, reloadToken: reloadCounter) readTarget = WebTarget(url: routed.url, reloadToken: reloadCounter)
selectedTab = .read selectedTab = .read
} }
} }
/// `zitare://quote/spitteler-...` `https://zitare.com/q/spitteler-...`.
/// `zitare://author/x` `https://zitare.com/a/x`.
/// `zitare://collection/x` `https://zitare.com/c/x`.
/// `https://zitare.com/<anything>` bleibt wie es ist.
private func resolveToWebURL(_ url: URL) -> URL {
if url.scheme == "zitare" {
let host = url.host ?? ""
let path = url.path
switch host {
case "quote":
return AppConfig.webBaseURL.appendingPathComponent("q\(path)")
case "author":
return AppConfig.webBaseURL.appendingPathComponent("a\(path)")
case "collection":
return AppConfig.webBaseURL.appendingPathComponent("c\(path)")
default:
return AppConfig.webBaseURL
}
}
return url
}
private func isExplorePath(_ path: String) -> Bool {
let prefixes = ["/explore", "/region", "/thema", "/rolle", "/epoche", "/sprache", "/search", "/t/"]
return prefixes.contains { path == $0 || path.hasPrefix($0 + "/") }
}
} }
enum AppTab: Hashable { enum AppTab: Hashable {

View file

@ -0,0 +1,30 @@
import Foundation
import WebKit
/// User-Scripts, die in jeden `WKWebView` injiziert werden.
///
/// `WKUserScript` ist MainActor-isolated; deshalb sind die Factory-
/// Methoden hier ebenfalls MainActor. Aufrufer leben sowieso auf Main
/// (SwiftUI `makeUIView`/`makeNSView` sind MainActor).
@MainActor
enum WebShellScripts {
/// Versteckt den zitare-Web-Header (`<header>` mit `<a class="brand">`),
/// weil die native TabBar bereits global navigiert. Footer und
/// Hauptinhalt bleiben sichtbar.
///
/// CSS wird at document.start als `<style>`-Tag injiziert vor dem
/// First Paint, kein Flicker.
static let hideWebHeader: WKUserScript = .init(
source: """
(function() {
var css = 'body header:has(a.brand) { display: none !important; }';
var style = document.createElement('style');
style.setAttribute('data-zitare-native', 'hide-web-header');
style.textContent = css;
(document.head || document.documentElement).appendChild(style);
})();
""",
injectionTime: .atDocumentStart,
forMainFrameOnly: true
)
}

View file

@ -102,6 +102,7 @@ final class WebNavState {
let config = WKWebViewConfiguration() let config = WKWebViewConfiguration()
config.websiteDataStore = .default() config.websiteDataStore = .default()
config.applicationNameForUserAgent = "ZitareNative/0.1 (iOS)" config.applicationNameForUserAgent = "ZitareNative/0.1 (iOS)"
config.userContentController.addUserScript(WebShellScripts.hideWebHeader)
let webView = WKWebView(frame: .zero, configuration: config) let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = context.coordinator webView.navigationDelegate = context.coordinator
webView.uiDelegate = context.coordinator webView.uiDelegate = context.coordinator
@ -149,6 +150,7 @@ final class WebNavState {
let config = WKWebViewConfiguration() let config = WKWebViewConfiguration()
config.websiteDataStore = .default() config.websiteDataStore = .default()
config.applicationNameForUserAgent = "ZitareNative/0.1 (macOS)" config.applicationNameForUserAgent = "ZitareNative/0.1 (macOS)"
config.userContentController.addUserScript(WebShellScripts.hideWebHeader)
let webView = WKWebView(frame: .zero, configuration: config) let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = context.coordinator webView.navigationDelegate = context.coordinator
webView.uiDelegate = context.coordinator webView.uiDelegate = context.coordinator

View file

@ -0,0 +1,90 @@
import XCTest
@testable import ZitareNative
final class DeepLinkRouterTests: XCTestCase {
let base = URL(string: "https://zitare.mana.how")!
// MARK: - resolveToWebURL
func test_resolve_customSchemeQuote() throws {
let url = try XCTUnwrap(URL(string: "zitare://quote/spitteler-schweizer-bleiben"))
let resolved = DeepLinkRouter.resolveToWebURL(url, base: base)
XCTAssertEqual(resolved.absoluteString, "https://zitare.mana.how/q/spitteler-schweizer-bleiben")
}
func test_resolve_customSchemeAuthor() throws {
let url = try XCTUnwrap(URL(string: "zitare://author/spitteler"))
let resolved = DeepLinkRouter.resolveToWebURL(url, base: base)
XCTAssertEqual(resolved.absoluteString, "https://zitare.mana.how/a/spitteler")
}
func test_resolve_customSchemeCollection() throws {
let url = try XCTUnwrap(URL(string: "zitare://collection/schweizer-stimmen"))
let resolved = DeepLinkRouter.resolveToWebURL(url, base: base)
XCTAssertEqual(resolved.absoluteString, "https://zitare.mana.how/c/schweizer-stimmen")
}
func test_resolve_unknownCustomSchemeFallsBackToBase() throws {
let url = try XCTUnwrap(URL(string: "zitare://unknown/foo"))
let resolved = DeepLinkRouter.resolveToWebURL(url, base: base)
XCTAssertEqual(resolved.absoluteString, "https://zitare.mana.how")
}
func test_resolve_httpsPassesThrough() throws {
let url = try XCTUnwrap(URL(string: "https://zitare.com/q/keller-werke"))
let resolved = DeepLinkRouter.resolveToWebURL(url, base: base)
XCTAssertEqual(resolved.absoluteString, "https://zitare.com/q/keller-werke")
}
// MARK: - isExplorePath
func test_explore_root() {
XCTAssertTrue(DeepLinkRouter.isExplorePath("/explore"))
}
func test_explore_subpaths() {
for path in [
"/region/schweiz",
"/thema/philosophie",
"/rolle/schriftsteller",
"/epoche/moderne",
"/sprache/de",
"/search",
"/t/eigenstaendigkeit"
] {
XCTAssertTrue(DeepLinkRouter.isExplorePath(path), "expected \(path) → explore")
}
}
func test_read_paths_are_not_explore() {
for path in ["/", "/q/spitteler-x", "/a/spitteler", "/c/schweizer", "/heute", "/random"] {
XCTAssertFalse(DeepLinkRouter.isExplorePath(path), "expected \(path) → read")
}
}
func test_explore_prefix_not_substring() {
// `/region` matcht, aber `/regions/...` darf NICHT matchen
XCTAssertTrue(DeepLinkRouter.isExplorePath("/region"))
XCTAssertTrue(DeepLinkRouter.isExplorePath("/region/schweiz"))
XCTAssertFalse(DeepLinkRouter.isExplorePath("/regions-overview"))
}
// MARK: - route (one-shot)
func test_route_customQuote_goesToRead() throws {
let result = try DeepLinkRouter.route(
XCTUnwrap(URL(string: "zitare://quote/x")),
base: base
)
XCTAssertEqual(result.url.path, "/q/x")
XCTAssertFalse(result.isExplore)
}
func test_route_httpsExplorePath_goesToExplore() throws {
let result = try DeepLinkRouter.route(
XCTUnwrap(URL(string: "https://zitare.mana.how/region/schweiz")),
base: base
)
XCTAssertTrue(result.isExplore)
}
}