scaffolding hermes flow and audit logging

This commit is contained in:
2026-04-09 18:54:10 +02:00
parent e401b6dbab
commit cf5316a2c1
59 changed files with 1830 additions and 593 deletions
+155 -37
View File
@@ -3,55 +3,173 @@ import SwiftUI
struct FeedView: View {
@EnvironmentObject private var localization: LocalizationStore
@EnvironmentObject private var analytics: HermesAnalyticsClient
@EnvironmentObject private var repository: HermesRepository
let onWatchPreview: () -> Void = {}
let onRetry: () -> Void
var body: some View {
VStack(alignment: .leading, spacing: HermesTheme.sectionSpacing) {
HermesSectionHeader(
title: localization.string(for: "feed.title"),
subtitle: localization.string(for: "feed.subtitle")
TimelineView(.periodic(from: Date(), by: 1)) { _ in
let round = repository.currentRound
let now = repository.serverNow()
let bannerMessage = hermesUserFacingErrorMessage(
localization: localization,
localeCode: localization.localeCode,
error: repository.errorCause
)
ZStack(alignment: .bottomLeading) {
RoundedRectangle(cornerRadius: HermesTheme.cornerRadius, style: .continuous)
.fill(
LinearGradient(
colors: [HermesTheme.surfaceElevated, HermesTheme.background],
startPoint: .topLeading,
endPoint: .bottomTrailing
VStack(alignment: .leading, spacing: HermesTheme.sectionSpacing) {
HermesSectionHeader(
title: localization.string(for: "feed.title"),
subtitle: localization.string(for: "feed.subtitle")
)
if round == nil {
if let bannerMessage {
feedErrorState(
message: bannerMessage,
retryText: localization.string(for: "common.retry"),
onRetry: onRetry
)
)
.frame(height: 220)
} else {
feedLoadingState(
title: localization.string(for: "common.loading"),
subtitle: localization.string(for: "feed.subtitle")
)
}
} else {
if let bannerMessage {
feedBanner(message: bannerMessage)
}
VStack(alignment: .leading, spacing: 8) {
Text(localization.string(for: "feed.hero_title"))
.font(.title2.weight(.bold))
.foregroundStyle(HermesTheme.textPrimary)
heroCard(round: round)
Text(localization.string(for: "feed.hero_subtitle"))
.font(.callout)
.foregroundStyle(HermesTheme.textSecondary)
.frame(maxWidth: 260, alignment: .leading)
HStack(spacing: 12) {
HermesMetricPill(
label: localization.string(for: "feed.lock_label"),
value: round.map { Self.countdownText(for: $0.event.lockAt.timeIntervalSince(now)) } ?? "--:--"
)
HermesMetricPill(
label: localization.string(for: "feed.odds_label"),
value: round.map { Self.formatOdds($0) } ?? "--"
)
}
Button {
analytics.track("next_round_requested", attributes: ["screen_name": "feed"])
analytics.track("cta_pressed", attributes: ["screen_name": "feed", "action": "watch_preview"])
onWatchPreview()
} label: {
Text(localization.string(for: "feed.cta"))
}
.buttonStyle(HermesPrimaryButtonStyle())
.disabled(round == nil)
}
.padding(HermesTheme.contentPadding)
}
HStack(spacing: 12) {
HermesMetricPill(label: localization.string(for: "feed.lock_label"), value: "01:42")
HermesMetricPill(label: localization.string(for: "feed.odds_label"), value: "1.85 / 2.05")
.onAppear {
analytics.track("feed_viewed", attributes: ["screen_name": "feed"])
analytics.track("screen_viewed", attributes: ["screen_name": "feed"])
}
Button {
analytics.track("next_round_requested", attributes: ["screen_name": "feed"])
analytics.track("cta_pressed", attributes: ["screen_name": "feed", "action": "watch_preview"])
} label: {
Text(localization.string(for: "feed.cta"))
}
.buttonStyle(HermesPrimaryButtonStyle())
}
.hermesCard(elevated: true)
.onAppear {
analytics.track("feed_viewed", attributes: ["screen_name": "feed"])
analytics.track("screen_viewed", attributes: ["screen_name": "feed"])
}
@ViewBuilder
private func heroCard(round: HermesRound?) -> some View {
ZStack(alignment: .bottomLeading) {
RoundedRectangle(cornerRadius: HermesTheme.cornerRadius, style: .continuous)
.fill(
LinearGradient(
colors: [HermesTheme.surfaceElevated, HermesTheme.background],
startPoint: .topLeading,
endPoint: .bottomTrailing
)
)
.frame(height: 220)
VStack(alignment: .leading, spacing: 8) {
Text(round.map { localizedEventTitle($0) } ?? localization.string(for: "feed.hero_title"))
.font(.title2.weight(.bold))
.foregroundStyle(HermesTheme.textPrimary)
Text(round.map { localization.string(for: "feed.hero_subtitle") } ?? localization.string(for: "feed.hero_subtitle"))
.font(.callout)
.foregroundStyle(HermesTheme.textSecondary)
.frame(maxWidth: 260, alignment: .leading)
}
.padding(HermesTheme.contentPadding)
}
}
@ViewBuilder
private func feedLoadingState(title: String, subtitle: String) -> some View {
VStack(alignment: .leading, spacing: 8) {
Text(title)
.font(.title2.weight(.bold))
.foregroundStyle(HermesTheme.textPrimary)
Text(subtitle)
.font(.callout)
.foregroundStyle(HermesTheme.textSecondary)
}
.padding(HermesTheme.contentPadding)
.frame(maxWidth: .infinity, minHeight: 220, alignment: .center)
.background(
RoundedRectangle(cornerRadius: HermesTheme.cornerRadius, style: .continuous)
.fill(
LinearGradient(
colors: [HermesTheme.surfaceElevated, HermesTheme.background],
startPoint: .topLeading,
endPoint: .bottomTrailing
)
)
)
}
@ViewBuilder
private func feedErrorState(message: String, retryText: String, onRetry: @escaping () -> Void) -> some View {
VStack(alignment: .leading, spacing: 12) {
Text(message)
.font(.callout)
.foregroundStyle(HermesTheme.warning)
Button {
onRetry()
} label: {
Text(retryText)
}
.buttonStyle(HermesSecondaryButtonStyle())
}
}
@ViewBuilder
private func feedBanner(message: String) -> some View {
Text(message)
.font(.callout)
.foregroundStyle(HermesTheme.warning)
.padding(.horizontal, 12)
.padding(.vertical, 10)
.frame(maxWidth: .infinity, alignment: .leading)
.background(HermesTheme.warning.opacity(0.12))
.clipShape(RoundedRectangle(cornerRadius: HermesTheme.insetRadius, style: .continuous))
}
private func localizedEventTitle(_ round: HermesRound) -> String {
localization.localeCode == "sv" ? round.event.titleSv : round.event.titleEn
}
private static func countdownText(for remaining: TimeInterval) -> String {
let totalSeconds = max(Int(remaining.rounded(.down)), 0)
let minutes = totalSeconds / 60
let seconds = totalSeconds % 60
return String(format: "%02d:%02d", minutes, seconds)
}
private static func formatOdds(_ round: HermesRound) -> String {
let oddsByOutcomeId = Dictionary(uniqueKeysWithValues: round.oddsVersion.odds.map { ($0.outcomeId, $0) })
return round.market.outcomes
.sorted(by: { $0.sortOrder < $1.sortOrder })
.compactMap { oddsByOutcomeId[$0.id]?.decimalOdds }
.map { String(format: "%.2f", $0) }
.joined(separator: " / ")
}
}
@@ -4,6 +4,8 @@ struct OnboardingView: View {
@EnvironmentObject private var localization: LocalizationStore
@EnvironmentObject private var analytics: HermesAnalyticsClient
let onStartSession: () -> Void
var body: some View {
VStack(alignment: .leading, spacing: HermesTheme.sectionSpacing) {
HermesSectionHeader(
@@ -27,6 +29,7 @@ struct OnboardingView: View {
Button {
analytics.track("consent_accepted", attributes: ["screen_name": "onboarding"])
analytics.track("cta_pressed", attributes: ["screen_name": "onboarding", "action": "start_session"])
onStartSession()
} label: {
Text(localization.string(for: "onboarding.start_session"))
}
@@ -13,8 +13,6 @@ struct ResultView: View {
let nextRoundTitle: String
let onNextRound: () -> Void
@EnvironmentObject private var analytics: HermesAnalyticsClient
var body: some View {
VStack(alignment: .leading, spacing: HermesTheme.sectionSpacing) {
HermesSectionHeader(title: title, subtitle: subtitle)
@@ -33,16 +31,11 @@ struct ResultView: View {
}
Button {
analytics.track("next_round_requested", attributes: ["screen_name": "result"])
onNextRound()
} label: {
Text(nextRoundTitle)
}
.buttonStyle(HermesPrimaryButtonStyle())
}
.onAppear {
analytics.track("screen_viewed", attributes: ["screen_name": "result"])
analytics.track("result_viewed", attributes: ["screen_name": "result", "selection": selectionValue, "outcome": outcomeValue])
}
}
}
@@ -9,8 +9,6 @@ struct RevealView: View {
let continueTitle: String
let onContinue: () -> Void
@EnvironmentObject private var analytics: HermesAnalyticsClient
var body: some View {
VStack(alignment: .leading, spacing: HermesTheme.sectionSpacing) {
HermesSectionHeader(title: title, subtitle: subtitle)
@@ -24,16 +22,11 @@ struct RevealView: View {
}
Button {
analytics.track("reveal_completed", attributes: ["screen_name": "reveal", "selection": selectionValue])
onContinue()
} label: {
Text(continueTitle)
}
.buttonStyle(HermesPrimaryButtonStyle())
}
.onAppear {
analytics.track("screen_viewed", attributes: ["screen_name": "reveal"])
analytics.track("reveal_started", attributes: ["screen_name": "reveal", "selection": selectionValue])
}
}
}
+326 -135
View File
@@ -3,15 +3,17 @@ import SwiftUI
struct RoundView: View {
@EnvironmentObject private var localization: LocalizationStore
@EnvironmentObject private var analytics: HermesAnalyticsClient
@EnvironmentObject private var repository: HermesRepository
@EnvironmentObject private var playerCoordinator: PlayerCoordinator
let onRetry: () -> Void
@StateObject private var playerCoordinator = PlayerCoordinator()
@State private var phase: Phase = .preview
@State private var selectedOutcomeID: String? = nil
@State private var lockAt: Date = Date().addingTimeInterval(47)
@State private var lockAt: Date = .now
@State private var transitionTask: Task<Void, Never>?
private let previewDuration: TimeInterval = 47
private let winningOutcomeID = "home"
@State private var actionMessage: String?
@State private var isSubmitting = false
private enum Phase {
case preview
@@ -20,40 +22,19 @@ struct RoundView: View {
case result
}
private var selectionOptions: [SelectionOption] {
[
SelectionOption(
id: "home",
title: localization.string(for: "round.home"),
subtitle: localization.string(for: "round.selection_prompt"),
odds: "1.85"
),
SelectionOption(
id: "away",
title: localization.string(for: "round.away"),
subtitle: localization.string(for: "round.selection_prompt"),
odds: "2.05"
),
]
}
private var selectedOutcome: SelectionOption? {
guard let selectedOutcomeID else {
return nil
}
return selectionOptions.first { $0.id == selectedOutcomeID }
}
private var winningOutcome: SelectionOption {
selectionOptions.first { $0.id == winningOutcomeID } ?? selectionOptions[0]
}
var body: some View {
TimelineView(.periodic(from: Date(), by: 1)) { context in
let remaining = max(lockAt.timeIntervalSince(context.date), 0)
let timerLocked = remaining <= 0
TimelineView(.periodic(from: Date(), by: 1)) { _ in
let round = repository.currentRound
let now = repository.serverNow()
let hasRound = round != nil
let remaining = max(lockAt.timeIntervalSince(now), 0)
let timerLocked = round != nil && remaining <= 0
let countdownText = Self.countdownText(for: remaining)
let bannerMessage = actionMessage ?? hermesUserFacingErrorMessage(
localization: localization,
localeCode: localization.localeCode,
error: repository.errorCause
)
VStack(alignment: .leading, spacing: HermesTheme.sectionSpacing) {
HermesSectionHeader(
@@ -61,15 +42,87 @@ struct RoundView: View {
subtitle: localization.string(for: "round.subtitle")
)
videoSection(countdownText: countdownText, remaining: remaining, isTimerLocked: timerLocked)
if !hasRound {
if let bannerMessage {
roundErrorState(
message: bannerMessage,
retryText: localization.string(for: "common.retry"),
onRetry: onRetry
)
} else {
roundLoadingState(
title: localization.string(for: "common.loading"),
subtitle: localization.string(for: "round.subtitle")
)
}
} else {
if let bannerMessage {
roundBanner(message: bannerMessage)
}
phaseContent(isTimerLocked: timerLocked)
videoSection(
round: round,
countdownText: countdownText,
remaining: remaining,
isTimerLocked: timerLocked
)
if let round {
switch phase {
case .preview, .locked:
SelectionView(
statusText: phase == .preview && !timerLocked ? localization.string(for: "round.selection_prompt") : localization.string(for: "round.locked_label"),
options: selectionOptions(for: round),
selectedOptionID: selectedOutcomeID,
isLocked: phase != .preview || timerLocked || isSubmitting,
confirmTitle: localization.string(for: "round.primary_cta"),
onSelect: handleSelection,
onConfirm: confirmSelection
)
case .reveal:
RevealView(
title: localization.string(for: "reveal.title"),
subtitle: localization.string(for: "reveal.subtitle"),
statusText: localization.string(for: "reveal.status"),
selectionLabel: localization.string(for: "result.selection_label"),
selectionValue: selectedOutcomeTitle(for: round),
continueTitle: localization.string(for: "reveal.cta"),
onContinue: showResult
)
case .result:
ResultView(
title: localization.string(for: "result.title"),
subtitle: localization.string(for: "result.subtitle"),
selectionLabel: localization.string(for: "result.selection_label"),
selectionValue: selectedOutcomeTitle(for: round),
outcomeLabel: localization.string(for: "result.outcome_label"),
outcomeValue: winningOutcomeTitle(for: round),
didWin: selectedOutcomeID == round.settlement.winningOutcomeId.uuidString,
winLabel: localization.string(for: "result.win"),
loseLabel: localization.string(for: "result.lose"),
nextRoundTitle: localization.string(for: "result.next_round"),
onNextRound: nextRound
)
}
}
}
}
.onAppear {
if let round {
startPreview(round)
}
}
.onChange(of: round?.event.id) { _, newValue in
guard newValue != nil, let round else {
return
}
startPreview(round)
}
}
.hermesCard(elevated: true)
.onAppear {
startPreview()
}
.onDisappear {
transitionTask?.cancel()
playerCoordinator.pause()
@@ -77,65 +130,36 @@ struct RoundView: View {
}
@ViewBuilder
private func phaseContent(isTimerLocked: Bool) -> some View {
switch phase {
case .preview, .locked:
SelectionView(
statusText: isTimerLocked || phase != .preview ? localization.string(for: "round.locked_label") : localization.string(for: "round.selection_prompt"),
options: selectionOptions,
selectedOptionID: selectedOutcomeID,
isLocked: isTimerLocked || phase != .preview,
confirmTitle: localization.string(for: "round.primary_cta"),
onSelect: handleSelection,
onConfirm: confirmSelection
)
case .reveal:
RevealView(
title: localization.string(for: "reveal.title"),
subtitle: localization.string(for: "reveal.subtitle"),
statusText: localization.string(for: "reveal.status"),
selectionLabel: localization.string(for: "result.selection_label"),
selectionValue: selectedOutcome?.title ?? localization.string(for: "round.selection_prompt"),
continueTitle: localization.string(for: "reveal.cta"),
onContinue: showResult
)
case .result:
ResultView(
title: localization.string(for: "result.title"),
subtitle: localization.string(for: "result.subtitle"),
selectionLabel: localization.string(for: "result.selection_label"),
selectionValue: selectedOutcome?.title ?? localization.string(for: "round.selection_prompt"),
outcomeLabel: localization.string(for: "result.outcome_label"),
outcomeValue: winningOutcome.title,
didWin: selectedOutcomeID == winningOutcomeID,
winLabel: localization.string(for: "result.win"),
loseLabel: localization.string(for: "result.lose"),
nextRoundTitle: localization.string(for: "result.next_round"),
onNextRound: resetRound
)
}
}
@ViewBuilder
private func videoSection(countdownText: String, remaining: TimeInterval, isTimerLocked: Bool) -> some View {
private func videoSection(round: HermesRound?, countdownText: String, remaining: TimeInterval, isTimerLocked: Bool) -> some View {
ZStack(alignment: .topTrailing) {
StudyVideoPlayerView(coordinator: playerCoordinator)
RoundedRectangle(cornerRadius: HermesTheme.cornerRadius, style: .continuous)
.fill(HermesTheme.surfaceElevated)
.frame(height: 220)
VStack(alignment: .trailing, spacing: 10) {
HermesCountdownBadge(
label: localization.string(for: "round.countdown_label"),
value: countdownText,
warning: !isTimerLocked && remaining <= 10
)
HermesMetricPill(
label: localization.string(for: "round.odds_label"),
value: "1.85 / 2.05"
)
if let round {
HermesVideoPlayerView(coordinator: playerCoordinator)
} else {
Text(localization.string(for: "round.video_placeholder"))
.font(.headline.weight(.semibold))
.foregroundStyle(HermesTheme.textSecondary)
}
if let round {
VStack(alignment: .trailing, spacing: 10) {
HermesCountdownBadge(
label: localization.string(for: "round.countdown_label"),
value: countdownText,
warning: !isTimerLocked && remaining <= 10
)
HermesMetricPill(
label: localization.string(for: "round.odds_label"),
value: formatOdds(round)
)
.frame(maxWidth: 160)
}
.padding(12)
}
.padding(12)
Text(phaseLabel(isTimerLocked: isTimerLocked))
.font(.caption.weight(.bold))
@@ -147,35 +171,25 @@ struct RoundView: View {
.padding(12)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomLeading)
}
.clipShape(RoundedRectangle(cornerRadius: HermesTheme.cornerRadius, style: .continuous))
}
private func phaseLabel(isTimerLocked: Bool) -> String {
switch phase {
case .preview:
return isTimerLocked ? localization.string(for: "round.locked_label") : localization.string(for: "round.preview_label")
case .locked:
return localization.string(for: "round.locked_label")
case .reveal:
return localization.string(for: "reveal.title")
case .result:
return localization.string(for: "result.title")
}
}
private func startPreview() {
private func startPreview(_ round: HermesRound) {
transitionTask?.cancel()
phase = .preview
lockAt = Date().addingTimeInterval(previewDuration)
selectedOutcomeID = nil
playerCoordinator.restart()
actionMessage = nil
isSubmitting = false
lockAt = round.event.lockAt
playerCoordinator.prepareForPreview(url: round.media.hlsMasterUrl, startTimeMs: round.media.previewStartMs)
analytics.track("round_loaded", attributes: ["screen_name": "round"])
analytics.track("preview_started", attributes: ["screen_name": "round"])
analytics.track("round_loaded", attributes: roundAnalyticsAttributes(round))
analytics.track("preview_started", attributes: roundAnalyticsAttributes(round))
analytics.track("screen_viewed", attributes: ["screen_name": "round"])
}
private func handleSelection(_ option: SelectionOption) {
guard phase == .preview else {
guard phase == .preview, !isSubmitting else {
return
}
@@ -185,39 +199,204 @@ struct RoundView: View {
}
private func confirmSelection() {
guard phase == .preview, let selectedOutcomeID else {
guard phase == .preview, !isSubmitting, let round = repository.currentRound else {
return
}
analytics.track("selection_submitted", attributes: ["screen_name": "round", "outcome_id": selectedOutcomeID])
analytics.track("selection_accepted", attributes: ["screen_name": "round", "outcome_id": selectedOutcomeID])
analytics.track("market_locked", attributes: ["screen_name": "round", "lock_reason": "manual_selection"])
guard let selectedOutcomeID, let session = repository.currentSession else {
actionMessage = localization.string(for: "errors.session_expired")
return
}
phase = .locked
playerCoordinator.pause()
guard let outcomeID = UUID(uuidString: selectedOutcomeID) ?? round.market.outcomes.first?.id else {
actionMessage = localization.string(for: "errors.generic")
return
}
transitionTask?.cancel()
transitionTask = Task { @MainActor in
try? await Task.sleep(nanoseconds: 750_000_000)
guard !Task.isCancelled else {
return
if repository.serverNow() >= round.event.lockAt {
return
}
isSubmitting = true
actionMessage = nil
Task { @MainActor in
do {
let request = HermesBetIntentRequest(
sessionId: session.sessionId,
eventId: round.event.id,
marketId: round.market.id,
outcomeId: outcomeID,
idempotencyKey: UUID().uuidString,
clientSentAt: Date()
)
let response = try await repository.submitBetIntent(request)
guard response.accepted else {
actionMessage = localization.string(for: "errors.generic")
phase = .preview
isSubmitting = false
return
}
analytics.track("selection_submitted", attributes: baseSelectionAttributes(selectedOutcomeID))
analytics.track("selection_accepted", attributes: baseSelectionAttributes(selectedOutcomeID))
analytics.track("market_locked", attributes: baseSelectionAttributes(selectedOutcomeID).merging(["lock_reason": "manual_selection"]) { _, new in new })
phase = .locked
playerCoordinator.pause()
transitionTask?.cancel()
transitionTask = Task { @MainActor in
try? await Task.sleep(nanoseconds: 750_000_000)
guard !Task.isCancelled, phase == .locked else {
return
}
phase = .reveal
analytics.track("reveal_started", attributes: roundAnalyticsAttributes(round).merging(baseSelectionAttributes(selectedOutcomeID)) { _, new in new })
}
} catch {
actionMessage = hermesUserFacingErrorMessage(localization: localization, localeCode: localization.localeCode, error: error) ?? localization.string(for: "errors.generic")
phase = .preview
}
phase = .reveal
isSubmitting = false
}
}
private func showResult() {
analytics.track("reveal_completed", attributes: ["screen_name": "round"])
guard let round = repository.currentRound, let selectedOutcomeID else {
return
}
analytics.track("reveal_completed", attributes: roundAnalyticsAttributes(round).merging(baseSelectionAttributes(selectedOutcomeID)) { _, new in new })
phase = .result
analytics.track(
"result_viewed",
attributes: roundAnalyticsAttributes(round)
.merging(baseSelectionAttributes(selectedOutcomeID)) { _, new in new }
.merging(["outcome": winningOutcomeTitle(for: round)]) { _, new in new }
)
}
private func resetRound() {
private func nextRound() {
analytics.track("next_round_requested", attributes: ["screen_name": "result"])
transitionTask?.cancel()
selectedOutcomeID = nil
phase = .preview
lockAt = Date().addingTimeInterval(previewDuration)
playerCoordinator.restart()
actionMessage = nil
Task { @MainActor in
do {
_ = try await repository.refreshRoundFromNetwork()
} catch {
actionMessage = hermesUserFacingErrorMessage(localization: localization, localeCode: localization.localeCode, error: error) ?? localization.string(for: "errors.generic")
}
}
}
private func roundLoadingState(title: String, subtitle: String) -> some View {
VStack(alignment: .leading, spacing: 8) {
Text(title)
.font(.headline.weight(.semibold))
.foregroundStyle(HermesTheme.textPrimary)
Text(subtitle)
.font(.callout)
.foregroundStyle(HermesTheme.textSecondary)
}
}
private func roundErrorState(message: String, retryText: String, onRetry: @escaping () -> Void) -> some View {
VStack(alignment: .leading, spacing: 12) {
Text(message)
.font(.callout)
.foregroundStyle(HermesTheme.warning)
Button {
onRetry()
} label: {
Text(retryText)
}
.buttonStyle(HermesSecondaryButtonStyle())
}
}
private func roundBanner(message: String) -> some View {
Text(message)
.font(.callout)
.foregroundStyle(HermesTheme.warning)
.padding(.horizontal, 12)
.padding(.vertical, 10)
.frame(maxWidth: .infinity, alignment: .leading)
.background(HermesTheme.warning.opacity(0.12))
.clipShape(RoundedRectangle(cornerRadius: HermesTheme.insetRadius, style: .continuous))
}
private func selectionOptions(for round: HermesRound) -> [SelectionOption] {
let oddsByOutcomeId = Dictionary(uniqueKeysWithValues: round.oddsVersion.odds.map { ($0.outcomeId, $0) })
return round.market.outcomes
.sorted(by: { $0.sortOrder < $1.sortOrder })
.map { outcome in
SelectionOption(
id: outcome.id.uuidString,
title: outcomeTitle(outcome),
subtitle: localization.string(for: "round.selection_prompt"),
odds: oddsByOutcomeId[outcome.id].map { String(format: "%.2f", $0.decimalOdds) } ?? "--"
)
}
}
private func outcomeTitle(_ outcome: HermesOutcome) -> String {
switch outcome.labelKey {
case "round.home":
return localization.string(for: "round.home")
case "round.away":
return localization.string(for: "round.away")
default:
return outcome.outcomeCode.capitalized
}
}
private func selectedOutcomeTitle(for round: HermesRound) -> String {
guard let selectedOutcomeID,
let outcome = round.market.outcomes.first(where: { $0.id.uuidString == selectedOutcomeID }) else {
return localization.string(for: "round.selection_prompt")
}
return outcomeTitle(outcome)
}
private func winningOutcomeTitle(for round: HermesRound) -> String {
guard let outcome = round.market.outcomes.first(where: { $0.id == round.settlement.winningOutcomeId }) else {
return localization.string(for: "round.selection_prompt")
}
return outcomeTitle(outcome)
}
private func formatOdds(_ round: HermesRound) -> String {
let oddsByOutcomeId = Dictionary(uniqueKeysWithValues: round.oddsVersion.odds.map { ($0.outcomeId, $0) })
return round.market.outcomes
.sorted(by: { $0.sortOrder < $1.sortOrder })
.compactMap { oddsByOutcomeId[$0.id]?.decimalOdds }
.map { String(format: "%.2f", $0) }
.joined(separator: " / ")
}
private func phaseLabel(isTimerLocked: Bool) -> String {
switch phase {
case .preview:
if isTimerLocked {
return localization.string(for: "round.locked_label")
}
return localization.string(for: "round.preview_label")
case .locked:
return localization.string(for: "round.locked_label")
case .reveal:
return localization.string(for: "reveal.title")
case .result:
return localization.string(for: "result.title")
}
}
private static func countdownText(for remaining: TimeInterval) -> String {
@@ -226,4 +405,16 @@ struct RoundView: View {
let seconds = totalSeconds % 60
return String(format: "%02d:%02d", minutes, seconds)
}
private func baseSelectionAttributes(_ outcomeId: String) -> [String: String] {
["screen_name": "round", "outcome_id": outcomeId]
}
private func roundAnalyticsAttributes(_ round: HermesRound) -> [String: String] {
[
"screen_name": "round",
"event_id": round.event.id.uuidString,
"market_id": round.market.id.uuidString,
]
}
}
@@ -1,7 +1,147 @@
import SwiftUI
struct SessionView: View {
@EnvironmentObject private var analytics: HermesAnalyticsClient
@EnvironmentObject private var localization: LocalizationStore
@EnvironmentObject private var repository: HermesRepository
let onRetry: () -> Void
var body: some View {
Text("Session scaffold")
let session = repository.currentSession
let bannerMessage = hermesUserFacingErrorMessage(
localization: localization,
localeCode: localization.localeCode,
error: repository.errorCause
)
let statusText: String
if session != nil {
statusText = localization.string(for: "session.status_ready")
} else if bannerMessage != nil {
statusText = localization.string(for: "session.status_error")
} else {
statusText = localization.string(for: "session.status_loading")
}
return HermesCard {
VStack(alignment: .leading, spacing: HermesTheme.sectionSpacing) {
HermesSectionHeader(
title: localization.string(for: "session.title"),
subtitle: localization.string(for: "session.subtitle")
)
if session == nil {
if let bannerMessage {
sessionErrorState(
message: bannerMessage,
retryText: localization.string(for: "common.retry"),
onRetry: onRetry
)
} else {
sessionLoadingState(
title: statusText,
subtitle: localization.string(for: "session.note")
)
}
} else {
sessionStatusBadge(text: statusText, warning: bannerMessage != nil)
if let bannerMessage {
sessionBanner(message: bannerMessage)
}
sessionRow(label: localization.string(for: "session.id_label"), value: session?.sessionId.uuidString ?? "--")
sessionRow(label: localization.string(for: "session.user_id_label"), value: session?.userId.uuidString ?? "--")
sessionRow(
label: localization.string(for: "session.locale_label"),
value: localization.localeName(for: session?.localeCode ?? localization.localeCode)
)
sessionRow(label: localization.string(for: "session.started_label"), value: session.map { Self.compactDateFormatter.string(from: $0.startedAt) } ?? "--")
sessionRow(label: localization.string(for: "session.variant_label"), value: session?.experimentVariant ?? "--")
sessionRow(label: localization.string(for: "session.app_version_label"), value: session?.appVersion ?? "--")
sessionRow(label: localization.string(for: "session.device_model_label"), value: session?.deviceModel ?? "--")
sessionRow(label: localization.string(for: "session.os_version_label"), value: session?.osVersion ?? "--")
Text(localization.string(for: "session.note"))
.font(.callout)
.foregroundStyle(HermesTheme.textSecondary)
}
}
}
.onAppear {
analytics.track("screen_viewed", attributes: ["screen_name": "session"])
}
}
@ViewBuilder
private func sessionLoadingState(title: String, subtitle: String) -> some View {
VStack(alignment: .leading, spacing: 8) {
Text(title)
.font(.headline.weight(.semibold))
.foregroundStyle(HermesTheme.textPrimary)
Text(subtitle)
.font(.callout)
.foregroundStyle(HermesTheme.textSecondary)
}
}
@ViewBuilder
private func sessionErrorState(message: String, retryText: String, onRetry: @escaping () -> Void) -> some View {
VStack(alignment: .leading, spacing: 12) {
Text(message)
.font(.callout)
.foregroundStyle(HermesTheme.warning)
Button {
onRetry()
} label: {
Text(retryText)
}
.buttonStyle(HermesSecondaryButtonStyle())
}
}
@ViewBuilder
private func sessionStatusBadge(text: String, warning: Bool) -> some View {
Text(text)
.font(.caption.weight(.semibold))
.foregroundStyle(warning ? HermesTheme.warning : HermesTheme.accent)
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background((warning ? HermesTheme.warning : HermesTheme.accent).opacity(0.16))
.clipShape(Capsule())
}
@ViewBuilder
private func sessionBanner(message: String) -> some View {
Text(message)
.font(.callout)
.foregroundStyle(HermesTheme.warning)
.padding(.horizontal, 12)
.padding(.vertical, 10)
.frame(maxWidth: .infinity, alignment: .leading)
.background(HermesTheme.warning.opacity(0.12))
.clipShape(RoundedRectangle(cornerRadius: HermesTheme.insetRadius, style: .continuous))
}
@ViewBuilder
private func sessionRow(label: String, value: String) -> some View {
HStack {
Text(label)
.font(.callout)
.foregroundStyle(HermesTheme.textSecondary)
Spacer(minLength: 12)
Text(value)
.font(.callout.weight(.semibold))
.foregroundStyle(HermesTheme.textPrimary)
}
}
private static let compactDateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = .current
formatter.dateFormat = "yyyy-MM-dd HH:mm"
return formatter
}()
}