aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Library/Views/SpinningGlobeView.swift
blob: bccb969dc9e33c35edde8e021c0b00851766b97d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import SwiftUI

/// An animated globe icon that cycles through different world regions
///
/// Displays a spinning globe effect by cycling through SF Symbol images showing
/// different parts of the world. Useful for indicating network activity or global content.
///
/// Example:
/// ```swift
/// SpinningGlobeView()
///   .frame(width: 16, height: 16)
///
/// SpinningGlobeView(frameDelay: 0.5)
///   .frame(width: 24, height: 24)
/// ```
struct SpinningGlobeView: View {
  /// Delay between frames in seconds (default: 0.3)
  let frameDelay: TimeInterval

  /// SF Symbol names for each frame of the globe animation
  private let globeFrames = [
    "globe.americas.fill",
    "globe.europe.africa.fill",
    "globe.central.south.asia.fill",
    "globe.asia.australia.fill"
  ]

  @State private var currentFrameIndex = 0
  @State private var animationTask: Task<Void, Never>?

  init(frameDelay: TimeInterval = 0.25) {
    self.frameDelay = frameDelay
  }

  var body: some View {
    Image(systemName: globeFrames[currentFrameIndex])
      .onAppear {
        startAnimation()
      }
      .onDisappear {
        stopAnimation()
      }
  }

  private func startAnimation() {
    animationTask = Task {
      while !Task.isCancelled {
        try? await Task.sleep(nanoseconds: UInt64(frameDelay * 1_000_000_000))

        guard !Task.isCancelled else { break }

        currentFrameIndex = (currentFrameIndex + 1) % globeFrames.count
      }
    }
  }

  private func stopAnimation() {
    animationTask?.cancel()
    animationTask = nil
  }
}

#Preview {
  VStack(spacing: 20) {
    HStack(spacing: 20) {
      SpinningGlobeView()
        .frame(width: 12, height: 12)

      SpinningGlobeView()
        .frame(width: 16, height: 16)

      SpinningGlobeView()
        .frame(width: 24, height: 24)
    }

    Text("Different frame delays:")

    HStack(spacing: 20) {
      SpinningGlobeView(frameDelay: 0.1)
        .frame(width: 16, height: 16)

      SpinningGlobeView(frameDelay: 0.3)
        .frame(width: 16, height: 16)

      SpinningGlobeView(frameDelay: 0.6)
        .frame(width: 16, height: 16)
    }
  }
  .padding()
}