WidgetBobble.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import SwiftUI
  2. struct WidgetBobble: View {
  3. @State private var rotationDegrees: Double = 0.0
  4. @State private var angularGradient = AngularGradient(colors: [
  5. // 184, 87, 255
  6. // 159, 108, 250
  7. // 124, 139, 243
  8. // 87, 170, 236
  9. // 67, 187, 233
  10. Color(red: 0.7215686275, green: 0.3411764706, blue: 1),
  11. Color(red: 0.6235294118, green: 0.4235294118, blue: 0.9803921569),
  12. Color(red: 0.4862745098, green: 0.5450980392, blue: 0.9529411765),
  13. Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961),
  14. Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902),
  15. Color(red: 0.7215686275, green: 0.3411764706, blue: 1)
  16. ], center: .center, startAngle: .degrees(270), endAngle: .degrees(-90))
  17. @Environment(\.colorScheme) var colorScheme
  18. var body: some View {
  19. let triangleColor = Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
  20. TrendShape(gradient: angularGradient, color: triangleColor)
  21. .rotationEffect(.degrees(rotationDegrees))
  22. // .onChange(of: context.state.bg) { newDirection in
  23. // withAnimation {
  24. // switch newDirection {
  25. // case .doubleUp,
  26. // .singleUp,
  27. // .tripleUp:
  28. // rotationDegrees = -90
  29. //
  30. // case .fortyFiveUp:
  31. // rotationDegrees = -45
  32. //
  33. // case .flat:
  34. // rotationDegrees = 0
  35. //
  36. // case .fortyFiveDown:
  37. // rotationDegrees = 45
  38. //
  39. // case .doubleDown,
  40. // .singleDown,
  41. // .tripleDown:
  42. // rotationDegrees = 90
  43. //
  44. // case .none,
  45. // .notComputable,
  46. // .rateOutOfRange:
  47. // rotationDegrees = 0
  48. //
  49. // @unknown default:
  50. // rotationDegrees = 0
  51. // }
  52. // }
  53. // }
  54. }
  55. }
  56. struct Triangle: Shape {
  57. func path(in rect: CGRect) -> Path {
  58. var path = Path()
  59. let cornerRadius: CGFloat = 8
  60. path.move(to: CGPoint(x: rect.midX, y: rect.minY))
  61. path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY - cornerRadius))
  62. path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.maxY - cornerRadius), control: CGPoint(x: rect.midX, y: rect.maxY))
  63. path.closeSubpath()
  64. return path
  65. }
  66. }
  67. struct TrendShape: View {
  68. @Environment(\.colorScheme) var colorScheme
  69. let gradient: AngularGradient
  70. let color: Color
  71. var body: some View {
  72. HStack(alignment: .center) {
  73. ZStack {
  74. Group {
  75. CircleShape(gradient: gradient)
  76. TriangleShape(color: color)
  77. }.shadow(color: Color.black.opacity(colorScheme == .dark ? 0.75 : 0.33), radius: colorScheme == .dark ? 5 : 3)
  78. CircleShape(gradient: gradient)
  79. }
  80. }
  81. }
  82. }
  83. struct CircleShape: View {
  84. @Environment(\.colorScheme) var colorScheme
  85. let gradient: AngularGradient
  86. var body: some View {
  87. let colorBackground: Color = colorScheme == .dark ? Color(
  88. red: 0.05490196078,
  89. green: 0.05490196078,
  90. blue: 0.05490196078
  91. ) : .white
  92. Circle()
  93. .stroke(gradient, lineWidth: 6)
  94. .background(Circle().fill(colorBackground))
  95. .frame(width: 130, height: 130)
  96. }
  97. }
  98. struct TriangleShape: View {
  99. let color: Color
  100. var body: some View {
  101. Triangle()
  102. .fill(color)
  103. .frame(width: 35, height: 35)
  104. .rotationEffect(.degrees(90))
  105. .offset(x: 70)
  106. }
  107. }