113 lines
3.6 KiB
Swift
113 lines
3.6 KiB
Swift
import SwiftUI
|
|
|
|
func known(_ value: String) -> String? {
|
|
let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
return trimmed.isEmpty || trimmed == "?" ? nil : trimmed
|
|
}
|
|
|
|
func knownElo(_ value: String) -> String? {
|
|
guard let text = known(value), let rating = Int(text), rating > 0 else { return nil }
|
|
return text
|
|
}
|
|
|
|
func pgnYear(_ date: String) -> String? {
|
|
let year = String(date.prefix(4))
|
|
return year.count == 4 && Int(year) != nil ? year : nil
|
|
}
|
|
|
|
func joinedKnown(_ values: [String?]) -> String {
|
|
values.compactMap { $0.flatMap(known) }.joined(separator: " · ")
|
|
}
|
|
|
|
func sideResult(_ result: String, white: Bool) -> String? {
|
|
switch result {
|
|
case "1-0": return white ? "1" : "0"
|
|
case "0-1": return white ? "0" : "1"
|
|
case "1/2-1/2": return "½"
|
|
default: return nil
|
|
}
|
|
}
|
|
|
|
func gameResultAccessibility(_ result: String) -> String {
|
|
switch result {
|
|
case "1-0": return "1-0, \(String(localized: "white_won"))"
|
|
case "0-1": return "0-1, \(String(localized: "black_won"))"
|
|
case "1/2-1/2": return "½-½, \(String(localized: "draw"))"
|
|
default: return "\(result.isEmpty ? "*" : result), \(String(localized: "unfinished"))"
|
|
}
|
|
}
|
|
|
|
func gameRowAccessibility(row: GameRow, number: Int, lastOpened: Bool) -> String {
|
|
var parts = [String(format: String(localized: "game_number"), number)]
|
|
for (side, name, rating) in [(L10n.white, row.white, row.whiteElo), (L10n.black, row.black, row.blackElo)] {
|
|
var player = "\(side): \(known(name) ?? "?")"
|
|
if let elo = knownElo(rating) { player += ", Elo \(elo)" }
|
|
parts.append(player)
|
|
}
|
|
parts.append(gameResultAccessibility(row.result))
|
|
let context = joinedKnown([row.event, pgnYear(row.date), row.eco])
|
|
if !context.isEmpty { parts.append(context) }
|
|
if lastOpened { parts.append(String(localized: "last_opened")) }
|
|
return parts.joined(separator: ". ")
|
|
}
|
|
|
|
extension GameData {
|
|
func tag(_ name: String) -> String? {
|
|
tags.first { $0.name == name }?.value
|
|
}
|
|
}
|
|
|
|
struct SideDisc: View {
|
|
let white: Bool
|
|
var size: CGFloat = 10
|
|
|
|
var body: some View {
|
|
Circle()
|
|
.fill(white ? Color.white : Color(rgb: 0x0E0F10))
|
|
.overlay(Circle().stroke(Theme.onSurfaceVariant, lineWidth: 1))
|
|
.frame(width: size, height: size)
|
|
.accessibilityHidden(true)
|
|
}
|
|
}
|
|
|
|
struct ResultTile: View {
|
|
let result: String?
|
|
|
|
var body: some View {
|
|
Group {
|
|
if let result {
|
|
Text(result)
|
|
.font(.subheadline.monospacedDigit())
|
|
.fontWeight(result == "1" ? .bold : .regular)
|
|
.foregroundStyle(result == "0" ? Theme.onSurfaceVariant : Theme.onSurface)
|
|
.frame(minWidth: 24, minHeight: 20)
|
|
.background(result == "0" ? Color.clear : (result == "½" ? Theme.containerHigh : Theme.primaryContainer))
|
|
.clipShape(RoundedRectangle(cornerRadius: 6))
|
|
} else {
|
|
Color.clear.frame(width: 24, height: 20)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct ErrorMessage: View {
|
|
let title: String
|
|
let detail: String
|
|
var retry: (() -> Void)?
|
|
|
|
var body: some View {
|
|
VStack(spacing: 12) {
|
|
Text(title).font(.title3).multilineTextAlignment(.center)
|
|
if !detail.isEmpty {
|
|
Text(detail)
|
|
.foregroundStyle(Theme.onSurfaceVariant)
|
|
.multilineTextAlignment(.center)
|
|
}
|
|
if let retry {
|
|
Button(L10n.retry, action: retry)
|
|
}
|
|
}
|
|
.padding(32)
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
}
|