SPB Git

Repository Public License

LobbyView.swift 11.7 KB 236 lines swift

1// ORIX Arcade2// File: App/LobbyView.swift3// Description: Arcade lobby — header, local dashboard, jump-back-in banner, the three game cards, recent sessions, welcome and recovery states.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI89struct LobbyView: View {10    let open: (GameID) -> Void11    @Environment(ArcadeServices.self) private var services12    @State private var appeared = false1314    var body: some View {15        let save = services.save16        ScrollView {17            VStack(alignment: .leading, spacing: OrixSpace.lg) {18                header19                if let notice = services.store.recoveryNotice { recoveryBanner(notice) }20                if !save.hasSeenWelcome && !LaunchOptions.skipIntro { welcomeCard }21                if let interrupted = save.interrupted { resumeBanner(interrupted) }22                dashboard(save)23                VStack(alignment: .leading, spacing: OrixSpace.sm) {24                    SectionHeader(title: "Games", trailing: "3 playable · offline")25                    ForEach(Array(GameCatalog.all.enumerated()), id: \.element.id) { index, descriptor in26                        GameCard(descriptor: descriptor, save: save) { open(descriptor.id) }27                            .opacity(appeared ? 1 : 0)28                            .offset(y: appeared ? 0 : 24)29                            .animation(OrixMotion.decorative(OrixMotion.bouncy.delay(0.06 * Double(index))), value: appeared)30                    }31                }32                recent(save)33            }34            .padding(.horizontal, OrixSpace.md)35            .padding(.top, OrixSpace.xs)36            .padding(.bottom, 110)37        }38        .scrollIndicators(.hidden)39        .onAppear { appeared = true }40    }4142    private var header: some View {43        HStack(spacing: OrixSpace.sm) {44            OrixLogoMark().frame(width: 40, height: 40)45            VStack(alignment: .leading, spacing: 2) {46                OrixWordmark(size: 19)47                Text(greeting).font(OrixFont.caption).foregroundStyle(OrixColor.textSecondary)48            }49            Spacer()50        }51        .padding(.top, OrixSpace.xs)52    }5354    private var greeting: String {55        let sessions = services.save.stats.totalSessions56        if sessions == 0 { return "Three games. No account. Works offline." }57        let hour = Calendar.current.component(.hour, from: Date())58        return hour < 12 ? "Good morning — ready for a quick run?" : hour < 18 ? "Good afternoon — beat a record today?" : "Good evening — one more run?"59    }6061    private func dashboard(_ save: SaveData) -> some View {62        HStack(spacing: OrixSpace.xs) {63            StatTile(value: save.stats.totalPlayTime.playTimeLabel, label: "Play time", systemImage: "clock.fill", tint: OrixColor.cyan)64            StatTile(value: "\(save.stats.totalSessions)", label: "Sessions", systemImage: "bolt.fill", tint: OrixColor.iris)65            StatTile(value: "\(save.achievements.count)/\(AchievementCatalog.all.count)", label: "Awards", systemImage: "trophy.fill", tint: OrixColor.gold)66        }67    }6869    private var welcomeCard: some View {70        VStack(alignment: .leading, spacing: OrixSpace.sm) {71            Text("Welcome to the arcade").font(OrixFont.title).foregroundStyle(OrixColor.textPrimary)72            Text("Pick any game and play right away. Everything stays on this iPhone: no sign-up, no ads, no tracking, no internet needed.")73                .font(OrixFont.callout).foregroundStyle(OrixColor.textSecondary)74            OrixButton(title: "Got it", systemImage: "checkmark", expand: false) {75                withAnimation(OrixMotion.snappy) { services.store.update { $0.hasSeenWelcome = true } }76            }77            .accessibilityIdentifier("welcomeDismiss")78        }79        .frame(maxWidth: .infinity, alignment: .leading)80        .glassPanel(tint: OrixColor.iris)81    }8283    private func recoveryBanner(_ notice: String) -> some View {84        HStack(alignment: .top, spacing: OrixSpace.sm) {85            Image(systemName: "exclamationmark.triangle.fill").foregroundStyle(OrixColor.gold)86            Text(notice).font(OrixFont.callout).foregroundStyle(OrixColor.textPrimary)87            Spacer(minLength: 0)88            Button("OK") { services.store.dismissRecoveryNotice() }89                .font(OrixFont.button).foregroundStyle(OrixColor.gold).frame(minWidth: 44, minHeight: 44)90        }91        .glassPanel(tint: OrixColor.gold)92    }9394    private func resumeBanner(_ interrupted: InterruptedSession) -> some View {95        let descriptor = GameCatalog.descriptor(interrupted.game)96        return Button {97            open(interrupted.game)98        } label: {99            HStack(spacing: OrixSpace.sm) {100                Image(systemName: "arrow.uturn.forward.circle.fill").font(.system(size: 30)).foregroundStyle(descriptor.theme.gradient)101                VStack(alignment: .leading, spacing: 2) {102                    Text("JUMP BACK IN").font(OrixFont.overline).tracking(1.4).foregroundStyle(descriptor.theme.primary)103                    Text(descriptor.title).font(OrixFont.headline).foregroundStyle(OrixColor.textPrimary)104                    Text(interrupted.note ?? "You left in the middle of a session.").font(OrixFont.caption).foregroundStyle(OrixColor.textSecondary)105                }106                Spacer(minLength: 0)107                Image(systemName: "chevron.right").font(.system(size: 14, weight: .heavy)).foregroundStyle(OrixColor.textTertiary)108            }109            .glassPanel(tint: descriptor.theme.primary)110        }111        .buttonStyle(OrixPressStyle())112        .accessibilityIdentifier("resumeBanner")113        .swipeActions { Button("Dismiss") { services.clearInterrupted() } }114    }115116    @ViewBuilder117    private func recent(_ save: SaveData) -> some View {118        VStack(alignment: .leading, spacing: OrixSpace.sm) {119            SectionHeader(title: "Recent sessions")120            if save.stats.recent.isEmpty {121                HStack(spacing: OrixSpace.sm) {122                    Image(systemName: "sparkles").font(.system(size: 22, weight: .bold)).foregroundStyle(OrixColor.iris)123                    Text("Your sessions will show up here. Play a game to set your first record.")124                        .font(OrixFont.callout).foregroundStyle(OrixColor.textSecondary)125                    Spacer(minLength: 0)126                }127                .glassPanel()128            } else {129                VStack(spacing: 0) {130                    let items = Array(save.stats.recent.prefix(5))131                    ForEach(Array(items.enumerated()), id: \.element.id) { index, record in132                        SessionRow(record: record)133                        if index < items.count - 1 { Divider().overlay(OrixColor.stroke) }134                    }135                }136                .glassPanel(padding: OrixSpace.sm)137            }138        }139    }140}141142struct SessionRow: View {143    let record: SessionRecord144145    var body: some View {146        let descriptor = GameCatalog.descriptor(record.game)147        HStack(spacing: OrixSpace.sm) {148            Circle().fill(descriptor.theme.gradient).frame(width: 10, height: 10)149            VStack(alignment: .leading, spacing: 1) {150                Text(descriptor.title).font(OrixFont.callout.weight(.bold)).foregroundStyle(OrixColor.textPrimary)151                Text(record.headline).font(OrixFont.caption).foregroundStyle(OrixColor.textSecondary).lineLimit(1)152            }153            Spacer(minLength: OrixSpace.xs)154            if record.isRecord { Image(systemName: "trophy.fill").font(.system(size: 12)).foregroundStyle(OrixColor.gold).accessibilityLabel("Record") }155            VStack(alignment: .trailing, spacing: 1) {156                Text(record.score.formatted()).font(OrixFont.callout.monospacedDigit().weight(.heavy)).foregroundStyle(OrixColor.textPrimary)157                Text(record.date, format: .relative(presentation: .numeric, unitsStyle: .abbreviated))158                    .font(OrixFont.caption).foregroundStyle(OrixColor.textTertiary)159            }160        }161        .padding(.vertical, 8)162        .accessibilityElement(children: .combine)163    }164}165166struct GameCard: View {167    let descriptor: GameDescriptor168    let save: SaveData169    let action: () -> Void170171    var body: some View {172        let best = descriptor.bestLabel(save)173        let progress = descriptor.progress(save)174        Button(action: action) {175            ZStack(alignment: .bottomLeading) {176                descriptor.preview()177                    .frame(maxWidth: .infinity)178                    .frame(height: 214)179                    .clipped()180                    .allowsHitTesting(false)181                LinearGradient(stops: [.init(color: .clear, location: 0.25), .init(color: OrixColor.void.opacity(0.94), location: 0.92)],182                               startPoint: .top, endPoint: .bottom)183                VStack(alignment: .leading, spacing: 6) {184                    HStack {185                        OrixChip(text: best ?? "NEW", systemImage: best == nil ? "sparkles" : "trophy.fill", tint: best == nil ? descriptor.theme.primary : OrixColor.gold)186                        Spacer()187                    }188                    Spacer()189                    HStack(alignment: .bottom, spacing: OrixSpace.sm) {190                        VStack(alignment: .leading, spacing: 3) {191                            Text(descriptor.title)192                                .font(.system(.title, design: .rounded, weight: .black))193                                .foregroundStyle(OrixColor.textPrimary)194                                .lineLimit(1).minimumScaleFactor(0.7)195                            Text(descriptor.tagline).font(OrixFont.caption).foregroundStyle(OrixColor.textSecondary).lineLimit(2)196                            if let progress {197                                HStack(spacing: OrixSpace.xs) {198                                    OrixProgressBar(fraction: progress.fraction, theme: descriptor.theme).frame(width: 84)199                                    Text(progress.label).font(OrixFont.overline).foregroundStyle(OrixColor.textTertiary).lineLimit(1)200                                }201                                .padding(.top, 2)202                            }203                        }204                        Spacer(minLength: 0)205                        HStack(spacing: 6) {206                            Image(systemName: "play.fill").font(.system(size: 13, weight: .black))207                            Text("Play").font(OrixFont.button)208                        }209                        .foregroundStyle(OrixColor.ink)210                        .padding(.horizontal, 18)211                        .frame(height: 46)212                        .background(descriptor.theme.gradient, in: Capsule())213                        .shadow(color: descriptor.theme.primary.opacity(0.5), radius: 12, y: 4)214                    }215                }216                .padding(OrixSpace.md)217            }218            .frame(height: 214)219            .background(descriptor.theme.deep)220            .clipShape(RoundedRectangle(cornerRadius: OrixRadius.xl, style: .continuous))221            .overlay(222                RoundedRectangle(cornerRadius: OrixRadius.xl, style: .continuous)223                    .strokeBorder(LinearGradient(colors: [descriptor.theme.primary.opacity(0.7), descriptor.theme.secondary.opacity(0.15)],224                                                 startPoint: .topLeading, endPoint: .bottomTrailing), lineWidth: 1.2)225            )226            .shadow(color: descriptor.theme.primary.opacity(0.18), radius: 22, y: 10)227        }228        .buttonStyle(OrixPressStyle())229        .accessibilityElement(children: .ignore)230        .accessibilityLabel("\(descriptor.title). \(descriptor.tagline). \(best ?? "Not played yet").")231        .accessibilityHint("Starts the game")232        .accessibilityAddTraits(.isButton)233        .accessibilityIdentifier("card-\(descriptor.id.rawValue)")234    }235}236
Files main
Menu

Theme