CardStack.swift 997 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // CardStack.swift
  3. // LoopKitUI
  4. //
  5. // Created by Michael Pangburn on 4/16/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. public struct CardStack: View {
  10. var cards: [Card?]
  11. var spacing: CGFloat?
  12. public init(cards: [Card?], spacing: CGFloat? = nil) {
  13. self.cards = cards
  14. self.spacing = spacing
  15. }
  16. public var body: some View {
  17. VStack(spacing: spacing) {
  18. ForEach(self.cards.indices, id: \.self) { index in
  19. self.cards[index]
  20. }
  21. }
  22. .padding(.bottom)
  23. }
  24. }
  25. extension CardStack {
  26. init(reducing stacks: [CardStack]) {
  27. self.cards = stacks.flatMap { $0.cards }
  28. self.spacing = nil
  29. }
  30. }
  31. extension CardStack {
  32. private init(_ other: Self, spacing: CGFloat? = nil) {
  33. self.cards = other.cards
  34. self.spacing = spacing ?? other.spacing
  35. }
  36. func spacing(_ spacing: CGFloat?) -> Self { Self(self, spacing: spacing) }
  37. }