aboutsummaryrefslogtreecommitdiff
path: root/Hotline/macOS/TransfersView.swift
blob: 6b7a21bc75f2cec2fea6748187ed212451d654e3 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import SwiftUI

struct TransfersView: View {
  @Environment(\.appState) private var appState
  
  @State private var selectedTransfers = Set<TransferInfo>()
  
  var body: some View {
    VStack(spacing: 0) {
      if self.appState.transfers.isEmpty {
        self.emptyState
      } else {
        self.transfersList
      }
    }
    .frame(minWidth: 500, minHeight: 200)
    .navigationTitle("Transfers")
    .toolbar {
//      ToolbarItem(placement: .primaryAction) {
//        Button {
//          self.appState.sweepTransfers()
//          self.selectedTransfers = []
//        } label: {
//          Label("Remove Completed", systemImage: "checklist")
//        }
//        .disabled(self.appState.transfers.isEmpty)
//      }
      
      ToolbarItem(placement: .primaryAction) {
        Button {
          for transfer in self.selectedTransfers {
            self.appState.cancelTransfer(id: transfer.id)
          }
          self.selectedTransfers = []
        } label: {
          Label("Cancel Transfer", systemImage: "xmark")
        }
        .disabled(self.selectedTransfers.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(selection: self.$selectedTransfers) {
      ForEach(self.appState.transfers) { transfer in
        TransferRow(transfer: transfer)
          .id(transfer)
      }
    }
    .listStyle(.inset)
    .environment(\.defaultMinListRowHeight, 56)
    .contextMenu(forSelectionType: TransferInfo.self) { items in
      if let item = items.first {
        if item.completed,
           let fileURL = item.fileURL {
          Button("Remove Transfer") {
            self.appState.cancelTransfer(id: item.id)
          }
          
          Divider()
          
          Button("Show in Finder") {
            NSWorkspace.shared.activateFileViewerSelecting([fileURL])
          }

          Button("Open") {
            NSWorkspace.shared.open(fileURL)
          }

          Divider()

          Button("Move to Trash") {
            NSWorkspace.shared.recycle([fileURL])
          }
        }
        else if !item.done {
          Button("Cancel Transfer") {
            self.appState.cancelTransfer(id: item.id)
          }
        }
      }
    } primaryAction: { items in
      let fileURLs: [URL] = items.compactMap { $0.fileURL }
      if !fileURLs.isEmpty {
        NSWorkspace.shared.activateFileViewerSelecting(fileURLs)
      }
    }
  }
}

// MARK: - Transfer Row

struct TransferRow: View {
  @Environment(\.appState) private var appState
  
  @Bindable var transfer: TransferInfo
  
  private var statsView: some View {
    HStack(spacing: 8) {
      // Progress percentage
//      Text("\(Int(self.transfer.progress * 100))%")
      
      // Speed
      if let speed = self.transfer.speed {
        Text(self.formatSpeed(speed))
      }

      // Time remaining
      if let timeRemaining = self.transfer.timeRemaining {
        Text(self.formatTimeRemaining(timeRemaining))
      }
      
      // File size
      Text(self.formatSize(self.transfer.size))
    }
    .font(.subheadline)
    .foregroundStyle(.secondary)
    .monospacedDigit()
  }
  
  private var fileIconView: some View {
    FileIconView(filename: self.transfer.title, fileType: nil)
      .frame(width: 32, height: 32)
      .overlay(alignment: .bottomTrailing) {
        if self.transfer.cancelled || self.transfer.failed {
          Image(systemName: "exclamationmark.triangle.fill")
            .resizable()
            .symbolRenderingMode(.multicolor)
            .scaledToFit()
            .frame(width: 16, height: 16)
        }
        else if self.transfer.completed {
          Image(systemName: "checkmark.circle.fill")
            .resizable()
            .symbolRenderingMode(.palette)
            .foregroundStyle(.white, .fileComplete)
            .scaledToFit()
            .frame(width: 16, height: 16)
        }
      }
  }

  var body: some View {
    HStack(alignment: .center, spacing: 8) {
      self.fileIconView
      
      VStack(alignment: .leading, spacing: 4) {
        HStack(alignment: .firstTextBaseline, spacing: 4) {
          Text(self.transfer.title)
            .font(.headline)
            .lineLimit(1)
            .truncationMode(.tail)
          
          Spacer()
          
          if !self.transfer.done {
            self.statsView
          }
        }
        
        // Progress bar and status
        if self.transfer.cancelled {
          Text("Cancelled")
            .font(.subheadline)
            .foregroundStyle(.secondary)
        }
        else if self.transfer.failed {
          Text("Failed")
            .font(.subheadline)
            .foregroundStyle(.secondary)
        }
        else if self.transfer.completed {
          Text("Complete")
            .font(.subheadline)
            .foregroundStyle(.fileComplete)
        }
        else {
          ProgressView(value: self.transfer.progress, total: 1.0)
            .progressViewStyle(.linear)
            .controlSize(.large)
        }
      }
      
      if self.transfer.completed {
        Button {
          guard let fileURL = self.transfer.fileURL else {
            return
          }
          
          NSWorkspace.shared.activateFileViewerSelecting([fileURL])
        } label: {
          Image(systemName: "eye.circle.fill")
            .resizable()
            .scaledToFit()
            .frame(width: 24, height: 24)
            .foregroundStyle(.secondary)
        }
        .buttonBorderShape(.circle)
        .buttonStyle(.plain)
      }
    }
        
//        VStack(alignment: .leading, spacing: 2) {
          

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

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

  // 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)
}