CardList.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // CardList.swift
  3. // LoopKitUI
  4. //
  5. // Created by Michael Pangburn on 4/10/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. public struct CardListSection: View {
  10. var title: Text
  11. var stack: CardStack
  12. public init(title: Text, @CardStackBuilder cards: () -> CardStack) {
  13. self.title = title
  14. self.stack = cards()
  15. }
  16. public var body: some View {
  17. VStack(spacing: 6) {
  18. HStack {
  19. title
  20. .font(Font(UIFont.preferredFont(forTextStyle: .title3)))
  21. .bold()
  22. Spacer()
  23. }
  24. .padding(.leading)
  25. stack
  26. }
  27. }
  28. }
  29. /// Displays a list of cards similar to a `List` with an inset grouped style,
  30. /// but without the baggage of `UITableViewCell` resizing, enabling cells to expand smoothly.
  31. struct CardList: View {
  32. enum Style {
  33. case simple(CardStack)
  34. case sectioned([CardListSection])
  35. }
  36. var title: Text
  37. var style: Style
  38. var body: some View {
  39. ScrollView {
  40. VStack(spacing: 4) {
  41. titleText
  42. .fixedSize(horizontal: false, vertical: true)
  43. cards
  44. }
  45. }
  46. .background(Color(.systemGroupedBackground))
  47. }
  48. private var titleText: some View {
  49. HStack {
  50. title
  51. .font(.largeTitle)
  52. .bold()
  53. Spacer()
  54. }
  55. .padding()
  56. .padding(.bottom, 4)
  57. .background(Color(.systemGroupedBackground))
  58. }
  59. private var cards: some View {
  60. switch style {
  61. case .simple(let stack):
  62. return AnyView(stack)
  63. case .sectioned(let sections):
  64. return AnyView(
  65. VStack(spacing: 16) {
  66. ForEach(Array(sections.enumerated()), id: \.offset) { index, section in
  67. section
  68. }
  69. }
  70. )
  71. }
  72. }
  73. }