TagCloudView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import Combine
  2. import Foundation
  3. import SwiftUI
  4. import Swinject
  5. struct TagCloudView: View {
  6. var tags: [String]
  7. @State private var totalHeight
  8. // = CGFloat.zero // << variant for ScrollView/List
  9. = CGFloat.infinity // << variant for VStack
  10. var body: some View {
  11. VStack {
  12. GeometryReader { geometry in
  13. self.generateContent(in: geometry)
  14. }
  15. }
  16. // .frame(height: totalHeight)// << variant for ScrollView/List
  17. .frame(maxHeight: totalHeight) // << variant for VStack
  18. }
  19. private func generateContent(in g: GeometryProxy) -> some View {
  20. var width = CGFloat.zero
  21. var height = CGFloat.zero
  22. return ZStack(alignment: .topLeading) {
  23. ForEach(self.tags, id: \.self) { tag in
  24. self.item(for: tag)
  25. .padding([.horizontal, .vertical], 2)
  26. .alignmentGuide(.leading, computeValue: { d in
  27. if abs(width - d.width) > g.size.width
  28. {
  29. width = 0
  30. height -= d.height
  31. }
  32. let result = width
  33. if tag == self.tags.last! {
  34. width = 0 // last item
  35. } else {
  36. width -= d.width
  37. }
  38. return result
  39. })
  40. .alignmentGuide(.top, computeValue: { _ in
  41. let result = height
  42. if tag == self.tags.last! {
  43. height = 0 // last item
  44. }
  45. return result
  46. })
  47. }
  48. }.background(viewHeightReader($totalHeight))
  49. }
  50. private func item(for textTag: String) -> some View {
  51. var colorOfTag: Color {
  52. switch textTag {
  53. case textTag where textTag.contains("SMB Delivery Ratio:"):
  54. return .uam
  55. case textTag where textTag.contains("Weighted avg:"),
  56. textTag where textTag.contains("Total data avg:"):
  57. return .gray
  58. case textTag where textTag.contains("Parabolic Fit"):
  59. return .loopRed
  60. case textTag where textTag.contains("TDD:"),
  61. textTag where textTag.contains("Original formula"),
  62. textTag where textTag.contains("Logarithmic formula"),
  63. textTag where textTag.contains("AF:"),
  64. textTag where textTag.contains("Autosens/Dynamic Limit:"),
  65. textTag where textTag.contains("Dynamic ISF/CR"),
  66. textTag where textTag.contains("Basal ratio"):
  67. return .zt
  68. default:
  69. return .insulin
  70. }
  71. }
  72. return ZStack { Text(textTag)
  73. .padding(.vertical, 2)
  74. .padding(.horizontal, 4)
  75. .font(.subheadline)
  76. .background(colorOfTag.opacity(0.8))
  77. .foregroundColor(Color.white)
  78. .cornerRadius(2) }
  79. }
  80. private func viewHeightReader(_ binding: Binding<CGFloat>) -> some View {
  81. GeometryReader { geometry -> Color in
  82. let rect = geometry.frame(in: .local)
  83. DispatchQueue.main.async {
  84. binding.wrappedValue = rect.size.height
  85. }
  86. return .clear
  87. }
  88. }
  89. }
  90. struct TestTagCloudView: View {
  91. var body: some View {
  92. VStack {
  93. Text("Header").font(.largeTitle)
  94. TagCloudView(tags: ["Ninetendo", "XBox", "PlayStation", "PlayStation 2", "PlayStation 3", "PlayStation 4"])
  95. Text("Some other text")
  96. Divider()
  97. Text("Some other cloud")
  98. TagCloudView(tags: ["Apple", "Google", "Amazon", "Microsoft", "Oracle", "Facebook"])
  99. }
  100. }
  101. }