Skip to content

Commit

Permalink
feat(App): update cardSize to dynamically adapts
Browse files Browse the repository at this point in the history
* Card Size is not dynamic and if a pile exceed a certain amount of
  height, it will reduce the size of the card. Allowing for iPad in
  landscape mode to still be able to drag and drop cards
  • Loading branch information
renaudjenny committed Jul 7, 2023
1 parent 946f1ce commit 82160ac
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
58 changes: 58 additions & 0 deletions SoliArt/App/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,64 @@ extension App.State {
)
}

static var maximumCardInAPile: Self {
var cards = [Card].standard52Deck

let useOnLastPileCards: [Card] = [
Card(.king, of: .hearts, isFacedUp: false),
Card(.queen, of: .spades, isFacedUp: false),
Card(.jack, of: .hearts, isFacedUp: false),
Card(.ten, of: .spades, isFacedUp: false),
Card(.nine, of: .hearts, isFacedUp: false),
Card(.eight, of: .spades, isFacedUp: false),
Card(.seven, of: .hearts, isFacedUp: false),
Card(.six, of: .spades, isFacedUp: false),
Card(.five, of: .hearts, isFacedUp: false),
Card(.four, of: .spades, isFacedUp: false),
Card(.three, of: .hearts, isFacedUp: false),
Card(.two, of: .spades, isFacedUp: false),
Card(.ace, of: .hearts, isFacedUp: false),
]

cards.removeAll { useOnLastPileCards.contains($0) }

var game = Game.State()
game.piles = IdentifiedArrayOf(uniqueElements: game.piles.map {
var pile = $0
pile.cards = IdentifiedArrayOf(uniqueElements: cards[..<$0.id])
cards = Array(cards[$0.id...])

if var last = pile.cards.last {
last.isFacedUp = true
pile.cards.updateOrAppend(last)
}

return pile
})

game.deck.upwards = []
game.deck.downwards = IdentifiedArrayOf(uniqueElements: cards)

game.foundations = IdentifiedArrayOf(
uniqueElements: Suit.orderedCases.map { Foundation(suit: $0, cards: []) }
)

game.isGameOver = false

game.piles[id: 7]?.cards.append(contentsOf: useOnLastPileCards.map {
var card = $0
card.isFacedUp = true
return card
})

return App.State(game: game)
}

private static func swapCards(_ cards: inout [Card], a: Card, b: Card) {
guard let aIndex = cards.firstIndex(of: a), let bIndex = cards.firstIndex(of: b) else { return }
cards.swapAt(aIndex, bIndex)
}

@Namespace private static var namespace: Namespace.ID
}

Expand Down
10 changes: 10 additions & 0 deletions SoliArt/App/Drag/CardPosition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,13 @@ enum Frame: Equatable, Hashable, Identifiable {
}
}
}

#if DEBUG
import ComposableArchitecture

struct CardPosition_Preview: PreviewProvider {
static var previews: some View {
AppView(store: Store(initialState: .maximumCardInAPile, reducer: App()))
}
}
#endif
8 changes: 7 additions & 1 deletion SoliArt/App/Drag/DragCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ struct Drag: ReducerProtocol {

extension Drag.State {
var cardSize: CGSize {
let height = min(windowSize.height * 16/100, windowSize.width * 15/100)
var height = min(windowSize.height * 16/100, windowSize.width * 15/100)
guard let maxCardsInPiles = piles.map(\.cards.count).max(), maxCardsInPiles > 0 else {
return CGSize(width: height * 5/7, height: height)
}
while height * 1/2 * Double(maxCardsInPiles) > windowSize.height {
height -= 4
}
return CGSize(width: height * 5/7, height: height)
}
}
Expand Down

0 comments on commit 82160ac

Please sign in to comment.