Files
mobile/ios/LichessWidgets/Daily Puzzle Widget/Views/DailyPuzzleWidgetView.swift
T
r3econ fbbd822cdb Link to daily puzzle screen from puzzle widget (#2984)
* Link to puzzle screen in the app from puzzle widget

* Extract constants

* Fix build problem in widget

* Fetch first daily to avoid extra request; add tests

---------

Co-authored-by: Vincent Velociter <vincent.velociter@gmail.com>
2026-04-14 22:58:15 +02:00

97 lines
3.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import ChessgroundAssets
import SwiftUI
import WidgetKit
struct DailyPuzzleWidgetView: View {
let entry: DailyPuzzleEntry
var body: some View {
Group {
if let error = entry.error {
errorView(error)
} else {
contentView
}
}
.widgetURL(entry.puzzleURL)
}
@ViewBuilder
private var contentView: some View {
VStack(spacing: 0) {
HStack(spacing: DailyPuzzleWidgetLayout.headerSpacing) {
Image("LichessLogo")
.resizable()
.frame(
width: DailyPuzzleWidgetLayout.logoSize,
height: DailyPuzzleWidgetLayout.logoSize
)
Text("Daily Puzzle")
.font(.system(size: DailyPuzzleWidgetLayout.titleFontSize, weight: .semibold))
.foregroundStyle(.primary)
.lineLimit(1)
Spacer()
Text(entry.date, format: entry.date.widgetDateFormat)
.font(.system(size: DailyPuzzleWidgetLayout.metaFontSize))
.foregroundStyle(.secondary)
}
.padding(.bottom, DailyPuzzleWidgetLayout.headerBottomPadding)
boardView
.clipShape(ContainerRelativeShape())
.overlay(
ContainerRelativeShape()
.stroke(.tertiary, lineWidth: DailyPuzzleWidgetLayout.boardBorderWidth)
)
}
.padding(.horizontal, DailyPuzzleWidgetLayout.horizontalPadding)
.padding(.vertical, DailyPuzzleWidgetLayout.verticalPadding)
}
@ViewBuilder
private var boardView: some View {
if let fen = entry.fen {
ChessBoardView(
fen: fen,
lastMove: entry.lastMove,
flipped: !entry.isWhiteToMove,
boardStyle: entry.boardStyle
)
} else {
Color.secondary.opacity(0.2)
}
}
// MARK: - Error state
@ViewBuilder
private func errorView(_ message: String) -> some View {
VStack(spacing: DailyPuzzleWidgetLayout.errorStackSpacing) {
Image(systemName: "exclamationmark.circle")
.font(.system(size: DailyPuzzleWidgetLayout.errorIconSize))
.foregroundStyle(.secondary)
Text(message)
.font(.caption)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
.padding(.horizontal, DailyPuzzleWidgetLayout.errorMessageHorizontalPadding)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
#Preview("Puzzle Brown", as: .systemLarge) {
DailyPuzzleWidget()
} timeline: {
DailyPuzzleEntry(
date: .now,
puzzleId: "abcd1",
fen: "1n3rk1/4ppbp/rq1p2p1/3P4/2p1P3/2N2P1n/PPN3PP/R1BQ1R1K b - - 1 1",
lastMove: "g1h1",
boardStyle: .from(themeName: "brown"),
error: nil
)
}