Tusker/Packages/TTTKit/Sources/TTTKit/UI/SubBoardView.swift

50 lines
1.2 KiB
Swift

//
// SubBoardView.swift
//
//
// Created by Shadowfacts on 12/21/22.
//
import SwiftUI
@available(iOS 16.0, *)
struct SubBoardView: View {
let board: TicTacToeBoard
let cellTapped: ((_ column: Int, _ row: Int) -> Void)?
@State private var cellSize: CGFloat = 0
init(board: TicTacToeBoard, cellTapped: ((Int, Int) -> Void)? = nil) {
self.board = board
self.cellTapped = cellTapped
}
var body: some View {
BoardView(board: board, cellSize: $cellSize, spacing: 10) { column, row in
applyTapHandler(column, row, MarkView(mark: board[column, row])
.contentShape(Rectangle()))
}
}
@ViewBuilder
private func applyTapHandler(_ column: Int, _ row: Int, _ view: some View) -> some View {
if let cellTapped {
view.onTapGesture {
cellTapped(column, row)
}
} else {
view
}
}
}
@available(iOS 16.0, *)
struct SubBoardView_Previews: PreviewProvider {
static var previews: some View {
SubBoardView(board: TicTacToeBoard(marks: [
[ .x, .o, .x],
[nil, .x, .o],
[ .x, nil, nil],
]))
}
}