TagCloudView.swift 4.0 KB

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