|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +/// Variants of the ellipse loader: ellipse colors, animation, and center content are defined per variant. |
| 4 | +enum EllipseLoaderVariant { |
| 5 | + case sync |
| 6 | + case quickpay |
| 7 | + case transfer |
| 8 | + case hardware |
| 9 | + |
| 10 | + var accentColor: String { |
| 11 | + switch self { |
| 12 | + case .sync, .quickpay, .transfer: return "purple" |
| 13 | + case .hardware: return "blue" |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + var centerScale: CGFloat { |
| 18 | + switch self { |
| 19 | + case .sync, .transfer, .hardware: return 0.85 |
| 20 | + case .quickpay: return 1 |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + var ellipseAnimation: Animation { |
| 25 | + switch self { |
| 26 | + case .sync: |
| 27 | + return Animation.easeOut(duration: 1.5).repeatForever(autoreverses: true) |
| 28 | + case .quickpay: |
| 29 | + return Animation.easeOut(duration: 1.6).repeatForever(autoreverses: true) |
| 30 | + case .transfer: |
| 31 | + return Animation.easeInOut(duration: 3).repeatForever(autoreverses: true) |
| 32 | + case .hardware: |
| 33 | + return Animation.linear(duration: 1).repeatForever(autoreverses: true) |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// Center content for transfer variant: transfer figure with its own rotation animation. |
| 39 | +private struct AnimatedTransferFigure: View { |
| 40 | + @State private var rotation: Double = 0 |
| 41 | + |
| 42 | + var body: some View { |
| 43 | + Image("transfer-figure") |
| 44 | + .resizable() |
| 45 | + .aspectRatio(contentMode: .fit) |
| 46 | + .rotationEffect(.degrees(rotation)) |
| 47 | + .onAppear { |
| 48 | + withAnimation(.easeInOut(duration: 3).repeatForever(autoreverses: true)) { |
| 49 | + rotation = 90 |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Center content for quickpay variant: coin stack with subtle rotation. |
| 56 | +private struct AnimatedCoinStack: View { |
| 57 | + @State private var rotation: Double = 0 |
| 58 | + |
| 59 | + var body: some View { |
| 60 | + Image("coin-stack-4") |
| 61 | + .resizable() |
| 62 | + .aspectRatio(contentMode: .fit) |
| 63 | + .rotationEffect(.degrees(rotation)) |
| 64 | + .onAppear { |
| 65 | + withAnimation(.easeInOut(duration: 3).repeatForever(autoreverses: true)) { |
| 66 | + rotation = 20 |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/// Animated loading view with rotating ellipses and variant-specific center content. |
| 73 | +/// Sizes to the available space so it can shrink on small screens and leave room for text. |
| 74 | +struct EllipseLoader: View { |
| 75 | + let variant: EllipseLoaderVariant |
| 76 | + |
| 77 | + @State private var outerRotation: Double = 0 |
| 78 | + @State private var innerRotation: Double = 0 |
| 79 | + |
| 80 | + var body: some View { |
| 81 | + GeometryReader { geo in |
| 82 | + let container = min(geo.size.width, geo.size.height) |
| 83 | + let figure = container * variant.centerScale |
| 84 | + let inner = container * 0.7 |
| 85 | + |
| 86 | + ZStack(alignment: .center) { |
| 87 | + Image("ellipse-outer-\(variant.accentColor)") |
| 88 | + .resizable() |
| 89 | + .aspectRatio(contentMode: .fit) |
| 90 | + .frame(width: container, height: container) |
| 91 | + .rotationEffect(.degrees(outerRotation)) |
| 92 | + |
| 93 | + Image("ellipse-inner-\(variant.accentColor)") |
| 94 | + .resizable() |
| 95 | + .aspectRatio(contentMode: .fit) |
| 96 | + .frame(width: inner, height: inner) |
| 97 | + .rotationEffect(.degrees(innerRotation)) |
| 98 | + |
| 99 | + centerContent |
| 100 | + .frame(width: figure, height: figure) |
| 101 | + } |
| 102 | + .frame(width: container, height: container) |
| 103 | + .clipped() |
| 104 | + } |
| 105 | + .aspectRatio(1, contentMode: .fit) |
| 106 | + .frame(maxWidth: .infinity) |
| 107 | + .onAppear { |
| 108 | + withAnimation(variant.ellipseAnimation) { outerRotation = -180 } |
| 109 | + withAnimation(variant.ellipseAnimation) { innerRotation = 180 } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @ViewBuilder private var centerContent: some View { |
| 114 | + switch variant { |
| 115 | + case .sync: |
| 116 | + Image("lightning") |
| 117 | + .resizable() |
| 118 | + .aspectRatio(contentMode: .fit) |
| 119 | + case .quickpay: |
| 120 | + AnimatedCoinStack() |
| 121 | + case .transfer: |
| 122 | + AnimatedTransferFigure() |
| 123 | + case .hardware: |
| 124 | + // TODO: change to hardware figure |
| 125 | + Image("shield-figure") |
| 126 | + .resizable() |
| 127 | + .aspectRatio(contentMode: .fit) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments