|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +struct AppStatus: View { |
| 4 | + @EnvironmentObject private var app: AppViewModel |
| 5 | + @EnvironmentObject private var network: NetworkMonitor |
| 6 | + @EnvironmentObject private var wallet: WalletViewModel |
| 7 | + |
| 8 | + let showText: Bool |
| 9 | + let showReady: Bool |
| 10 | + let showColor: Bool |
| 11 | + let testID: String |
| 12 | + let onPress: (() -> Void)? |
| 13 | + |
| 14 | + @State private var rotationAngle: Double = 0 |
| 15 | + @State private var opacity: Double = 1 |
| 16 | + |
| 17 | + init( |
| 18 | + showText: Bool = false, |
| 19 | + showReady: Bool = false, |
| 20 | + showColor: Bool = true, |
| 21 | + testID: String, |
| 22 | + onPress: (() -> Void)? = nil |
| 23 | + ) { |
| 24 | + self.showText = showText |
| 25 | + self.showReady = showReady |
| 26 | + self.showColor = showColor |
| 27 | + self.testID = testID |
| 28 | + self.onPress = onPress |
| 29 | + } |
| 30 | + |
| 31 | + var body: some View { |
| 32 | + Button(action: { |
| 33 | + onPress?() |
| 34 | + }) { |
| 35 | + HStack(spacing: 8) { |
| 36 | + statusIcon |
| 37 | + |
| 38 | + if showText { |
| 39 | + BodyMSBText(t("wallet__drawer__status"), textColor: .black) |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + .frame(minWidth: 32, minHeight: 32) |
| 44 | + .accessibilityIdentifier(testID) |
| 45 | + .buttonStyle(PlainButtonStyle()) |
| 46 | + .onAppear { |
| 47 | + startAnimations() |
| 48 | + } |
| 49 | + .onChange(of: appStatus) { _ in |
| 50 | + startAnimations() |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // MARK: - Computed Properties |
| 55 | + |
| 56 | + private var appStatus: HealthStatus { |
| 57 | + // During initialization, return 'ready' instead of error |
| 58 | + if !app.appStatusInitialized { |
| 59 | + return .ready |
| 60 | + } |
| 61 | + |
| 62 | + return AppStatusHelper.combinedAppStatus(from: wallet, network: network) |
| 63 | + } |
| 64 | + |
| 65 | + private var statusColor: Color { |
| 66 | + if !showColor { |
| 67 | + return .black |
| 68 | + } |
| 69 | + |
| 70 | + return appStatus.iconColor |
| 71 | + } |
| 72 | + |
| 73 | + @ViewBuilder |
| 74 | + private var statusIcon: some View { |
| 75 | + switch appStatus { |
| 76 | + case .ready: |
| 77 | + if showReady { |
| 78 | + Image("power") |
| 79 | + .resizable() |
| 80 | + .foregroundColor(statusColor) |
| 81 | + .frame(width: 24, height: 24) |
| 82 | + } |
| 83 | + case .pending: |
| 84 | + Image("arrows-clockwise") |
| 85 | + .resizable() |
| 86 | + .foregroundColor(statusColor) |
| 87 | + .frame(width: 24, height: 24) |
| 88 | + .rotationEffect(.degrees(rotationAngle)) |
| 89 | + case .error: |
| 90 | + Image("warning") |
| 91 | + .resizable() |
| 92 | + .foregroundColor(statusColor) |
| 93 | + .frame(width: 24, height: 24) |
| 94 | + .opacity(opacity) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // MARK: - Animations |
| 99 | + |
| 100 | + private func startAnimations() { |
| 101 | + switch appStatus { |
| 102 | + case .ready: |
| 103 | + stopAnimations() |
| 104 | + case .pending: |
| 105 | + startRotationAnimation() |
| 106 | + case .error: |
| 107 | + startFadeAnimation() |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private func startRotationAnimation() { |
| 112 | + // Reset rotation to 0 first |
| 113 | + rotationAngle = 0 |
| 114 | + |
| 115 | + // First half turn with easing (0 to 180 degrees) |
| 116 | + withAnimation(.timingCurve(0.4, 0, 0.2, 1, duration: 0.8)) { |
| 117 | + rotationAngle = 180 |
| 118 | + } |
| 119 | + |
| 120 | + // Second half turn with different timing (180 to 360 degrees) |
| 121 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) { |
| 122 | + withAnimation(.timingCurve(0.4, 0, 0.2, 1, duration: 1.2)) { |
| 123 | + rotationAngle = 360 |
| 124 | + } |
| 125 | + |
| 126 | + // Reset and repeat the sequence |
| 127 | + DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) { |
| 128 | + rotationAngle = 0 |
| 129 | + startRotationAnimation() |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private func startFadeAnimation() { |
| 135 | + withAnimation( |
| 136 | + .easeInOut(duration: 0.6) |
| 137 | + .repeatForever(autoreverses: true) |
| 138 | + ) { |
| 139 | + opacity = opacity == 1 ? 0.3 : 1 |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private func stopAnimations() { |
| 144 | + withAnimation(.easeInOut(duration: 0.3)) { |
| 145 | + rotationAngle = 0 |
| 146 | + opacity = 1 |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments