wordeck-native/Sources/App/RootView.swift
Till JS edc60056ea icon: Icon-Composer-App-Icon + macOS-Build grün
- AppIcon.icon (Icon Composer, blaues W) als App-Icon integriert.
  In project.yml als Target-Source mit type:file → XcodeGen erkennt
  .icon nativ als wrapper.icon. Alter forest-grüner Platzhalter
  (AppIcon.appiconset) entfernt. actool baut Icon für iOS + macOS.
- macOS-Build repariert (war pre-existing rot seit β-3/β-5/β-6):
  iOS-only SwiftUI-Modifier mit #if os(iOS) gegated
  (textInputAutocapitalization, keyboardType, navigationBarDrawer,
  tabViewBottomAccessory, .buttonStyle(.glass)); .topBarTrailing →
  cross-platform .primaryAction; .bottomBar-Toolbar gekapselt;
  iOS-only Extensions mit platformFilter:iOS an den embed-Deps.
- Verifiziert: iOS-Sim + macOS BUILD SUCCEEDED.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-25 17:52:12 +02:00

201 lines
7.5 KiB
Swift

import ManaAuthUI
import ManaCore
import SwiftUI
/// Top-Level-View: TabBar mit drei Tabs (Decks / Entdecken / Account).
/// Kein harter Login-Gate mehr Wordeck läuft auch im Guest-Modus
/// (lokale Decks lernen, Marketplace browsen). Schreibende Server-
/// Aktionen werden über ``ManaAuthGate`` einzeln auf Login eskaliert.
struct RootView: View {
@Environment(AuthClient.self) private var auth
@Environment(ManaAuthGate.self) private var authGate
@Environment(\.modelContext) private var modelContext
@State private var selectedTab: AppTab = .decks
@State private var pendingDeepLinkSlug: String?
@State private var showCreateDeck = false
@State private var showSignUpSheet = false
@State private var showForgotSheet = false
@State private var resetPasswordToken: String?
private let sourceAppUrl = URL(string: "https://wordeck.com/auth/verify")!
private let resetUniversalLink = URL(string: "https://wordeck.com/auth/reset")!
var body: some View {
mainTabs
.onOpenURL { url in handle(url: url) }
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { activity in
if let url = activity.webpageURL { handle(url: url) }
}
.manaBrand(WordeckBrand.manaBrand)
.manaAuthGate(authGate) {
gateSignInContent
}
.sheet(item: Binding(
get: { resetPasswordToken.map(IdentifiedString.init) },
set: { resetPasswordToken = $0?.value }
)) { token in
ManaResetPasswordView(
token: token.value,
auth: auth,
onDone: { resetPasswordToken = nil }
)
.manaBrand(WordeckBrand.manaBrand)
}
.task {
// DEBUG: Auto-Login mit DebugCredentials, falls signedOut.
// Release: no-op. Danach in Guest-Mode wechseln, wenn weder
// signedIn noch eingebuchtet Wordeck soll *immer* nutzbar
// sein, auch ohne Account.
await auth.ensureSignedIn()
if case .signedOut = auth.status {
do {
_ = try auth.enterGuestMode()
} catch {
Log.auth.warning(
"Guest-Mode konnte nicht aktiviert werden: \(String(describing: error), privacy: .public)"
)
}
}
// Einmalige Legacy-CacheEvent-Migration (E-4.4b).
if let coordinator = WordeckEventCoordinator.shared {
await WordeckDataMigration.runIfNeeded(context: modelContext, coordinator: coordinator)
}
}
}
/// Content für das ``ManaAuthGate``-Sheet wenn ein gegateter Button
/// gedrückt wird, fliegt der User in den Sign-In-Flow. Sign-Up und
/// Forgot-Password werden als verschachtelte Sheets aufgeklappt,
/// damit aus dem Gate-Sheet alle Auth-Pfade erreichbar bleiben.
private var gateSignInContent: some View {
NavigationStack {
ManaLoginView(
auth: auth,
onSignUpTapped: { showSignUpSheet = true },
onForgotTapped: { showForgotSheet = true }
)
.manaBrand(WordeckBrand.manaBrand)
.sheet(isPresented: $showSignUpSheet) {
ManaSignUpView(
auth: auth,
sourceAppUrl: sourceAppUrl,
onDone: { showSignUpSheet = false }
)
.manaBrand(WordeckBrand.manaBrand)
}
.sheet(isPresented: $showForgotSheet) {
ManaForgotPasswordView(
auth: auth,
resetUniversalLink: resetUniversalLink,
onDone: { showForgotSheet = false }
)
.manaBrand(WordeckBrand.manaBrand)
}
}
}
private var mainTabs: some View {
TabView(selection: $selectedTab) {
DeckListView(showCreate: $showCreateDeck)
.tabItem { Label("Decks", systemImage: "rectangle.stack") }
.tag(AppTab.decks)
ExploreView(deepLinkSlug: $pendingDeepLinkSlug)
.tabItem { Label("Entdecken", systemImage: "sparkles") }
.tag(AppTab.explore)
NavigationStack {
AccountView()
}
.tabItem { Label("Account", systemImage: "person.crop.circle") }
.tag(AppTab.account)
}
.decksCreateAccessory(visible: selectedTab == .decks) {
authGate.require(reason: "deck-create-accessory") {
showCreateDeck = true
}
}
}
/// Universal-Link- und URL-Scheme-Handler:
/// - `https://wordeck.com/d/<slug>` Explore-Tab + PublicDeckView
/// - `https://wordeck.com/auth/reset?token=` ManaResetPasswordView
/// - `cards://study/<deckId>` später (β-6 Notifications)
private func handle(url: URL) {
Log.app.info("Open URL: \(url.absoluteString, privacy: .public)")
guard url.host == "wordeck.com" || url.scheme == "cards" else { return }
let parts = url.pathComponents.filter { $0 != "/" }
// Auth-Reset-Link aus der Passwort-Vergessen-Email.
if parts == ["auth", "reset"] {
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
if let token = components?.queryItems?.first(where: { $0.name == "token" })?.value {
resetPasswordToken = token
return
}
}
if parts.count >= 2, parts[0] == "d" {
pendingDeepLinkSlug = parts[1]
selectedTab = .explore
}
}
}
/// Helper für `.sheet(item:)` mit einem String-Value (Reset-Token).
private struct IdentifiedString: Identifiable {
let value: String
var id: String {
value
}
}
enum AppTab: Hashable {
case decks
case explore
case account
}
private extension View {
/// iOS 26: floating Neues Deck"-Pille via `.tabViewBottomAccessory`,
/// nur sichtbar wenn der Decks-Tab aktiv ist. iOS 18 fällt auf den
/// bestehenden `.bottomBar`-+"-Toolbar-Button in `DeckListView` zurück.
///
/// Den Modifier nur konditional anwenden sonst rendert das System
/// auch bei leerem Inhalt die leere Glass-Hülle (sichtbar als toter
/// Streifen über der TabBar auf Entdecken/Account).
@ViewBuilder
func decksCreateAccessory(visible: Bool, onTap: @escaping () -> Void) -> some View {
// `tabViewBottomAccessory` + `.buttonStyle(.glass)` sind iOS-26-only
// und auf macOS gar nicht verfügbar ganz ausklammern.
#if os(iOS)
if #available(iOS 26.0, *), visible {
tabViewBottomAccessory {
DeckCreateAccessoryPill(action: onTap)
}
} else {
self
}
#else
self
#endif
}
}
#if os(iOS)
@available(iOS 26.0, *)
private struct DeckCreateAccessoryPill: View {
let action: () -> Void
var body: some View {
Button(action: action) {
Label("Neues Deck", systemImage: "plus")
.font(.subheadline.weight(.semibold))
}
.buttonStyle(.glass)
.tint(WordeckTheme.primary)
.accessibilityLabel("Neues Deck erstellen")
}
}
#endif