RoundedCard.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // RoundedCard.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 2/9/21.
  6. //
  7. import SwiftUI
  8. fileprivate let inset: CGFloat = 16
  9. struct RoundedCardTitle: View {
  10. var title: String
  11. init(_ title: String) {
  12. self.title = title
  13. }
  14. var body: some View {
  15. Text(title)
  16. .font(.headline)
  17. .foregroundColor(.primary)
  18. }
  19. }
  20. struct RoundedCardFooter: View {
  21. var text: String
  22. init(_ text: String) {
  23. self.text = text
  24. }
  25. var body: some View {
  26. Text(text)
  27. .font(.caption)
  28. .fixedSize(horizontal: false, vertical: true)
  29. .foregroundColor(.secondary)
  30. }
  31. }
  32. public struct RoundedCardValueRow: View {
  33. var label: String
  34. var value: String
  35. var highlightValue: Bool
  36. var disclosure: Bool
  37. public init(label: String, value: String, highlightValue: Bool = false, disclosure: Bool = false) {
  38. self.label = label
  39. self.value = value
  40. self.highlightValue = highlightValue
  41. self.disclosure = disclosure
  42. }
  43. public var body: some View {
  44. HStack {
  45. Text(label)
  46. .fixedSize(horizontal: false, vertical: true)
  47. .foregroundColor(.primary)
  48. Spacer()
  49. Text(value)
  50. .fixedSize(horizontal: true, vertical: true)
  51. .foregroundColor(highlightValue ? .accentColor : .secondary)
  52. if disclosure {
  53. Image(systemName: "chevron.right")
  54. .imageScale(.small)
  55. .font(.headline)
  56. .foregroundColor(.secondary)
  57. .opacity(0.5)
  58. }
  59. }
  60. }
  61. }
  62. struct RoundedCard<Content: View>: View {
  63. var content: () -> Content?
  64. var alignment: HorizontalAlignment
  65. var title: String?
  66. var footer: String?
  67. @Environment(\.horizontalSizeClass) var horizontalSizeClass
  68. init(title: String? = nil, footer: String? = nil, alignment: HorizontalAlignment = .leading, @ViewBuilder content: @escaping () -> Content? = { nil }) {
  69. self.content = content
  70. self.alignment = alignment
  71. self.title = title
  72. self.footer = footer
  73. }
  74. var body: some View {
  75. VStack(spacing: 10) {
  76. if let title = title {
  77. RoundedCardTitle(title)
  78. .frame(maxWidth: .infinity, alignment: Alignment(horizontal: .leading, vertical: .center))
  79. .padding(.leading, titleInset)
  80. }
  81. if content() != nil {
  82. if isCompact {
  83. VStack(spacing: 0) {
  84. borderLine
  85. VStack(alignment: alignment, content: content)
  86. .frame(maxWidth: .infinity, alignment: Alignment(horizontal: alignment, vertical: .center))
  87. .padding(inset)
  88. .background(Color(.secondarySystemGroupedBackground))
  89. borderLine
  90. }
  91. } else {
  92. VStack(alignment: alignment, content: content)
  93. .frame(maxWidth: .infinity, alignment: Alignment(horizontal: alignment, vertical: .center))
  94. .padding(.horizontal, inset)
  95. .padding(.vertical, 10)
  96. .background(Color(.secondarySystemGroupedBackground))
  97. .clipShape(RoundedRectangle(cornerRadius: cornerRadius, style: .continuous))
  98. }
  99. }
  100. if let footer = footer {
  101. RoundedCardFooter(footer)
  102. .frame(maxWidth: .infinity, alignment: Alignment(horizontal: alignment, vertical: .center))
  103. .padding(.horizontal, inset)
  104. }
  105. }
  106. }
  107. var borderLine: some View {
  108. Rectangle().fill(Color(.quaternaryLabel))
  109. .frame(height: 0.5)
  110. }
  111. private var isCompact: Bool {
  112. return self.horizontalSizeClass == .compact
  113. }
  114. private var titleInset: CGFloat {
  115. return isCompact ? inset : 0
  116. }
  117. private var padding: CGFloat {
  118. return isCompact ? 0 : inset
  119. }
  120. private var cornerRadius: CGFloat {
  121. return isCompact ? 0 : 8
  122. }
  123. }
  124. struct RoundedCardScrollView<Content: View>: View {
  125. var content: () -> Content
  126. var title: String?
  127. @Environment(\.horizontalSizeClass) var horizontalSizeClass
  128. init(title: String? = nil, @ViewBuilder content: @escaping () -> Content) {
  129. self.title = title
  130. self.content = content
  131. }
  132. var body: some View {
  133. ScrollView {
  134. if let title = title {
  135. HStack {
  136. Text(title)
  137. .font(Font.largeTitle.weight(.bold))
  138. .padding(.top)
  139. Spacer()
  140. }
  141. .padding([.leading, .trailing])
  142. }
  143. VStack(alignment: .leading, spacing: 25, content: content)
  144. .padding(padding)
  145. }
  146. .background(Color(.systemGroupedBackground).edgesIgnoringSafeArea(.all))
  147. }
  148. private var padding: CGFloat {
  149. return self.horizontalSizeClass == .regular ? inset : 0
  150. }
  151. }