aboutsummaryrefslogtreecommitdiff
path: root/Hotline/macOS/TransfersView.swift
blob: 9932c0af58c003aa96b0abe156de3dc707ba3fd1 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import SwiftUI

struct TransfersView: View {
  @Environment(\.appState) private var appState

  var body: some View {
    VStack(spacing: 0) {
      if appState.transfers.isEmpty {
        emptyState
      } else {
        transfersList
      }
    }
    .frame(minWidth: 500, minHeight: 200)
    .navigationTitle("Transfers")
    .toolbar {
      ToolbarItem(placement: .primaryAction) {
        Button {
          appState.cancelAllTransfers()
        } label: {
          Label("Cancel All", systemImage: "xmark.circle")
        }
        .disabled(appState.transfers.isEmpty)
      }
    }
  }

  // MARK: - Empty State

  private var emptyState: some View {
    ContentUnavailableView {
      Label("No Transfers", systemImage: "arrow.up.arrow.down")
    } description: {
      Text("Your Hotline file transfers will appear here")
    }
  }

  // MARK: - Transfers List

  private var transfersList: some View {
    List {
      ForEach(appState.transfers) { transfer in
        TransferRow(transfer: transfer)
      }
    }
    .listStyle(.inset(alternatesRowBackgrounds: true))
  }
}

// MARK: - Transfer Row

struct TransferRow: View {
  @Bindable var transfer: TransferInfo

  var body: some View {
    VStack(alignment: .leading, spacing: 8) {
      // File name and server
      HStack {
        VStack(alignment: .leading, spacing: 2) {
          Text(transfer.title)
            .font(.system(.body, design: .default, weight: .medium))

          if let serverName = transfer.serverName {
            Text(serverName)
              .font(.caption)
              .foregroundStyle(.secondary)
          }
        }

        Spacer()

        // Cancel button
        Button {
          AppState.shared.cancelTransfer(id: transfer.id)
        } label: {
          Image(systemName: "xmark.circle.fill")
            .foregroundStyle(.secondary)
        }
        .buttonStyle(.plain)
        .help("Cancel download")
      }

      // Progress bar and status
      VStack(alignment: .leading, spacing: 4) {
        if transfer.failed {
          Label("Failed", systemImage: "exclamationmark.triangle.fill")
            .font(.caption)
            .foregroundStyle(.red)
        } else if transfer.completed {
          Label("Complete", systemImage: "checkmark.circle.fill")
            .font(.caption)
            .foregroundStyle(.green)
        } else {
          // Progress bar
          ProgressView(value: transfer.progress, total: 1.0)
            .progressViewStyle(.linear)

          // Progress info
          HStack(spacing: 8) {
            // Progress percentage
            Text("\(Int(transfer.progress * 100))%")
              .font(.caption)
              .foregroundStyle(.secondary)
              .monospacedDigit()

            // File size
            Text(formatSize(transfer.size))
              .font(.caption)
              .foregroundStyle(.secondary)

            // Speed
            if let speed = transfer.speed {
              Text(formatSpeed(speed))
                .font(.caption)
                .foregroundStyle(.secondary)
                .monospacedDigit()
            }

            // Time remaining
            if let timeRemaining = transfer.timeRemaining {
              Text(formatTimeRemaining(timeRemaining))
                .font(.caption)
                .foregroundStyle(.secondary)
                .monospacedDigit()
            }
          }
        }
      }
    }
    .padding(.vertical, 4)
  }

  // MARK: - Formatting

  private func formatSize(_ bytes: UInt) -> String {
    let formatter = ByteCountFormatter()
    formatter.countStyle = .file
    formatter.allowedUnits = [.useKB, .useMB, .useGB]
    return formatter.string(fromByteCount: Int64(bytes))
  }

  private func formatSpeed(_ bytesPerSecond: Double) -> String {
    let formatter = ByteCountFormatter()
    formatter.countStyle = .file
    formatter.allowedUnits = [.useKB, .useMB, .useGB]
    return "\(formatter.string(fromByteCount: Int64(bytesPerSecond)))/s"
  }

  private func formatTimeRemaining(_ seconds: TimeInterval) -> String {
    if seconds < 60 {
      return "\(Int(seconds))s"
    } else if seconds < 3600 {
      let minutes = Int(seconds / 60)
      let secs = Int(seconds.truncatingRemainder(dividingBy: 60))
      return "\(minutes)m \(secs)s"
    } else {
      let hours = Int(seconds / 3600)
      let minutes = Int((seconds.truncatingRemainder(dividingBy: 3600)) / 60)
      return "\(hours)h \(minutes)m"
    }
  }
}

// MARK: - Preview

#Preview {
  TransfersView()
    .environment(AppState.shared)
}