import SwiftUI struct DatabasesView: View { @EnvironmentObject private var model: AppModel var body: some View { ScrollView { VStack(alignment: .leading, spacing: 8) { Text(L10n.files) .font(.subheadline.weight(.medium)) .foregroundStyle(Theme.primary) .padding(.horizontal, 16) .padding(.top, 8) if let error = model.loadError { ErrorMessage(title: L10n.openFailed, detail: error) { Task { await model.loadSample() } } .frame(minHeight: 300) } else { Button { model.path.append(.games) } label: { HStack(spacing: 12) { Text("PGN") .font(.caption.weight(.medium)) .foregroundStyle(Theme.pgnBadgeText) .frame(width: 40, height: 40) .background(Theme.pgnBadge) .clipShape(RoundedRectangle(cornerRadius: 10)) VStack(alignment: .leading, spacing: 4) { Text(L10n.classics).foregroundStyle(Theme.onSurface) if model.isLoading { Text(L10n.reading).font(.subheadline).foregroundStyle(Theme.onSurfaceVariant) } else { Text("\(L10n.games(model.totalGames)) ยท PGN") .font(.subheadline) .foregroundStyle(Theme.onSurfaceVariant) } } Spacer() if model.isLoading { ProgressView() } else { Image(systemName: "chevron.right").foregroundStyle(Theme.onSurfaceVariant) } } .padding(12) .background(Theme.containerLow) .clipShape(RoundedRectangle(cornerRadius: 16)) } .buttonStyle(.plain) .disabled(model.database == nil) .padding(.horizontal, 12) } } } .background(Theme.background) .navigationTitle(L10n.appName) .task { await model.loadSample() } } } struct GamesView: View { @EnvironmentObject private var model: AppModel var body: some View { Group { if model.rows.isEmpty { Text(L10n.emptyDatabase) .foregroundStyle(Theme.onSurfaceVariant) .frame(maxWidth: .infinity, maxHeight: .infinity) } else { ScrollView { LazyVStack(spacing: 0) { ForEach(Array(model.rows.enumerated()), id: \.element.index) { place, row in Button { model.lastOpened = place model.path.append(.game(place: place)) } label: { GameListRow(row: row, number: Int(row.index) + 1, lastOpened: model.lastOpened == place) } .buttonStyle(.plain) .accessibilityElement(children: .ignore) .accessibilityLabel(gameRowAccessibility(row: row, number: Int(row.index) + 1, lastOpened: model.lastOpened == place)) if place + 1 < model.rows.count { Divider() .background(Theme.outlineVariant) .padding(.leading, 68) .padding(.trailing, 16) } } if model.canLoadMore || model.isLoadingPage || model.pageError != nil { VStack(spacing: 8) { if let error = model.pageError { Text(error).foregroundStyle(Theme.error) Button(L10n.retry) { Task { await model.loadNextPage() } } } else { ProgressView() Text(L10n.reading).foregroundStyle(Theme.onSurfaceVariant) } } .frame(maxWidth: .infinity, minHeight: 80) .task(id: model.rows.count) { await model.loadNextPage() } } } } } } .background(Theme.background) .navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .principal) { VStack(spacing: 0) { Text(L10n.classics).font(.headline) Text(L10n.games(model.totalGames)).font(.caption).foregroundStyle(Theme.onSurfaceVariant) } } } } } private struct GameListRow: View { let row: GameRow let number: Int let lastOpened: Bool var body: some View { HStack(alignment: .top, spacing: 12) { Text("\(number)") .font(.caption.monospacedDigit()) .foregroundStyle(Theme.onSurfaceVariant) .frame(width: 28, alignment: .trailing) .padding(.top, 4) VStack(spacing: 0) { ListPlayer(name: row.white, elo: row.whiteElo, result: sideResult(row.result, white: true), white: true) ListPlayer(name: row.black, elo: row.blackElo, result: sideResult(row.result, white: false), white: false) HStack(spacing: 8) { Text(joinedKnown([row.event, pgnYear(row.date)])) .font(.subheadline) .foregroundStyle(Theme.onSurfaceVariant) .lineLimit(1) .frame(maxWidth: .infinity, alignment: .leading) if let eco = known(row.eco) { Text(eco) .font(.caption2.monospacedDigit()) .foregroundStyle(Theme.onSurfaceVariant) .padding(.horizontal, 4) .frame(minWidth: 32, minHeight: 16) .overlay(RoundedRectangle(cornerRadius: 4).stroke(Theme.outline, lineWidth: 1)) } } .frame(height: 20) .padding(.leading, 16) } } .padding(.horizontal, 16) .padding(.vertical, 6) .frame(maxWidth: .infinity, minHeight: 80, alignment: .leading) .background(lastOpened ? Theme.container : Color.clear) .overlay(alignment: .leading) { if lastOpened { Theme.primary.frame(width: 3) } } .contentShape(Rectangle()) } } private struct ListPlayer: View { let name: String let elo: String let result: String? let white: Bool var body: some View { HStack(spacing: 8) { SideDisc(white: white, size: 8) Text(known(name) ?? "?") .foregroundStyle(known(name) == nil ? Theme.onSurfaceVariant : Theme.onSurface) .fontWeight(result == "1" ? .medium : .regular) .lineLimit(1) .frame(maxWidth: .infinity, alignment: .leading) if let elo = knownElo(elo) { Text(elo).font(.subheadline.monospacedDigit()).foregroundStyle(Theme.onSurfaceVariant) } ResultTile(result: result) } .frame(height: 24) } }