TagCloudView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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("Bolus"):
  56. return .green
  57. case textTag where textTag.contains("TDD:"),
  58. textTag where textTag.contains("tdd_factor"),
  59. textTag where textTag.contains("Sigmoid function"),
  60. textTag where textTag.contains("Logarithmic formula"),
  61. textTag where textTag.contains("AF:"),
  62. textTag where textTag.contains("Autosens/Dynamic Limit:"),
  63. textTag where textTag.contains("Dynamic ISF/CR"),
  64. textTag where textTag.contains("Basal ratio"):
  65. return .zt
  66. case textTag where textTag.contains("Middleware:"):
  67. return .red
  68. case textTag where textTag.contains("SMB Ratio"):
  69. return .orange
  70. default:
  71. return .insulin
  72. }
  73. }
  74. return ZStack { Text(textTag)
  75. .padding(.vertical, 2)
  76. .padding(.horizontal, 4)
  77. .font(.subheadline)
  78. .background(colorOfTag.opacity(0.8))
  79. .foregroundColor(Color.white)
  80. .cornerRadius(2) }
  81. }
  82. private func viewHeightReader(_ binding: Binding<CGFloat>) -> some View {
  83. GeometryReader { geometry -> Color in
  84. let rect = geometry.frame(in: .local)
  85. DispatchQueue.main.async {
  86. binding.wrappedValue = rect.size.height
  87. }
  88. return .clear
  89. }
  90. }
  91. }
  92. struct TestTagCloudView: View {
  93. var body: some View {
  94. VStack {
  95. Text("Header").font(.largeTitle)
  96. TagCloudView(tags: ["Ninetendo", "XBox", "PlayStation", "PlayStation 2", "PlayStation 3", "PlayStation 4"])
  97. Text("Some other text")
  98. Divider()
  99. Text("Some other cloud")
  100. TagCloudView(tags: ["Apple", "Google", "Amazon", "Microsoft", "Oracle", "Facebook"])
  101. }
  102. }
  103. }