Files
hermes/mobile/ios-app/App/RootView.swift
T

84 lines
3.1 KiB
Swift

import SwiftUI
struct RootView: View {
@StateObject private var localization = LocalizationStore()
@EnvironmentObject private var analytics: HermesAnalyticsClient
@EnvironmentObject private var repository: HermesRepository
let onStartSession: (String) -> Void
var body: some View {
NavigationStack {
ScrollView {
VStack(alignment: .leading, spacing: 24) {
header
OnboardingView(onStartSession: { onStartSession(localization.localeCode) })
FeedView(onWatchPreview: {}, onRetry: { onStartSession(localization.localeCode) })
RoundView(onRetry: { onStartSession(localization.localeCode) })
SessionView(onRetry: { onStartSession(localization.localeCode) })
}
.padding(.horizontal, HermesTheme.screenPadding)
.padding(.vertical, 24)
}
.background(HermesTheme.background.ignoresSafeArea())
.navigationTitle(localization.string(for: "app.name"))
.navigationBarTitleDisplayMode(.inline)
}
.environmentObject(localization)
.onAppear {
analytics.track("app_opened", attributes: ["screen_name": "home"])
analytics.track("screen_viewed", attributes: ["screen_name": "home"])
}
.task(id: localization.localeCode) {
onStartSession(localization.localeCode)
}
.task {
while !Task.isCancelled {
try? await Task.sleep(nanoseconds: 5_000_000_000)
await analytics.flush(using: repository)
}
}
}
private var header: some View {
HStack(alignment: .top) {
VStack(alignment: .leading, spacing: 8) {
Text(localization.string(for: "app.name"))
.font(.largeTitle.bold())
.foregroundStyle(HermesTheme.textPrimary)
Text(localization.string(for: "app.subtitle"))
.font(.callout)
.foregroundStyle(HermesTheme.textSecondary)
}
Spacer(minLength: 12)
localeToggle
}
}
private var localeToggle: some View {
HStack(spacing: 8) {
localeButton(title: localization.localeName(for: "en"), localeCode: "en")
localeButton(title: localization.localeName(for: "sv"), localeCode: "sv")
}
}
private func localeButton(title: String, localeCode: String) -> some View {
let isSelected = localization.localeCode == localeCode
return Button {
localization.setLocale(localeCode)
analytics.track("locale_changed", attributes: ["locale_code": localeCode])
} label: {
Text(title)
.font(.caption.weight(.bold))
.padding(.horizontal, 12)
.padding(.vertical, 8)
.foregroundStyle(isSelected ? HermesTheme.background : HermesTheme.textPrimary)
.background(isSelected ? HermesTheme.accent : HermesTheme.surfaceElevated)
.clipShape(Capsule())
}
}
}