159 lines
6.0 KiB
Swift
159 lines
6.0 KiB
Swift
import RedWhiteUI
|
|
import SwiftUI
|
|
|
|
/// What a download says about itself, shared by its row and the bottom bar.
|
|
enum DownloadText {
|
|
/// "1.4 GB / 48.2 GB · 12.3 MB/s", "Paused · 1.4 GB / 48.2 GB", or why it stopped.
|
|
static func status(_ download: LibraryModel.Download) -> String {
|
|
let amount = download.totalBytes > 0
|
|
? ByteFormat.progress(done: download.doneBytes, total: download.totalBytes) : nil
|
|
switch download.status {
|
|
case .waiting: return joined("Waiting", amount)
|
|
case .paused: return joined("Paused", amount)
|
|
case .stopped(let reason): return reason
|
|
case .running:
|
|
switch download.stage {
|
|
case .preparing: return joined("Preparing…", amount)
|
|
case .finishing: return "Finishing…"
|
|
case .downloading: return joined(amount ?? "Starting…", ByteFormat.speed(download.bytesPerSecond))
|
|
}
|
|
}
|
|
}
|
|
|
|
private static func joined(_ first: String, _ second: String?) -> String {
|
|
second.map { "\(first) · \($0)" } ?? first
|
|
}
|
|
}
|
|
|
|
/// One game: its title, and under it the download size, the progress or the
|
|
/// finished mark. The row is the button; the ⋯ menu holds the rest.
|
|
struct GameRow: View {
|
|
let game: LibraryModel.Game
|
|
let download: LibraryModel.Download?
|
|
let finished: LibraryModel.Finished?
|
|
let menu: [RwMenuItem]
|
|
let onTap: () -> Void
|
|
@Environment(\.rw) private var p
|
|
|
|
var body: some View {
|
|
HStack(spacing: 0) {
|
|
RwPressable(
|
|
padding: EdgeInsets(top: 14, leading: RwSizes.pagePadding, bottom: 14, trailing: 12),
|
|
semanticLabel: [game.name, summary].compactMap { $0 }.joined(separator: ", "),
|
|
action: finished == nil ? onTap : nil
|
|
) { fg, active in
|
|
label(fg: fg, active: active)
|
|
}
|
|
if !menu.isEmpty {
|
|
RwMenu(menu) {
|
|
Image(systemName: "ellipsis").font(.system(size: 16, weight: .semibold))
|
|
}
|
|
.accessibilityLabel("More for \(game.name)")
|
|
.padding(.trailing, 8)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func label(fg: Color, active: Bool) -> some View {
|
|
let detail = (active ? p.onRed : p.textMuted).color
|
|
return HStack(spacing: 12) {
|
|
VStack(alignment: .leading, spacing: 6) {
|
|
Text(game.name)
|
|
.rw(.titleMedium, color: (active ? p.onRed : p.text).color)
|
|
.lineLimit(2)
|
|
.multilineTextAlignment(.leading)
|
|
if finished != nil {
|
|
HStack(spacing: 8) {
|
|
RwPill("Downloaded", tone: .success, systemImage: "checkmark")
|
|
if let summary { Text(summary).rw(.labelMedium, color: detail) }
|
|
}
|
|
} else if let download, case .stopped = download.status {
|
|
Text(DownloadText.status(download))
|
|
.rw(.bodySmall, color: (active ? p.onRed : p.redHover).color)
|
|
.multilineTextAlignment(.leading)
|
|
} else if let summary {
|
|
Text(summary).rw(.labelMedium, color: detail)
|
|
}
|
|
bar
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
if let icon {
|
|
Image(systemName: icon)
|
|
.font(.system(size: 17, weight: .semibold))
|
|
.foregroundStyle(fg)
|
|
.frame(width: 24)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The line under the title, as text.
|
|
private var summary: String? {
|
|
if let finished { return finished.bytes > 0 ? ByteFormat.size(finished.bytes) : nil }
|
|
if let download { return DownloadText.status(download) }
|
|
return game.downloadBytes > 0 ? ByteFormat.size(game.downloadBytes) : nil
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var bar: some View {
|
|
if let download, finished == nil {
|
|
if let fraction = download.fraction {
|
|
ProgressView(value: fraction).progressViewStyle(.rwBar)
|
|
} else if download.status == .running {
|
|
ProgressView().progressViewStyle(.rwBar)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// What a tap does next.
|
|
private var icon: String? {
|
|
guard finished == nil else { return nil }
|
|
switch download?.status {
|
|
case .running?, .waiting?: return "pause.fill"
|
|
case .paused?: return "play.fill"
|
|
case .stopped?: return "arrow.clockwise"
|
|
case nil: return "arrow.down.to.line"
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The running download, pinned under the list so it stays in view while the
|
|
/// list scrolls.
|
|
struct DownloadBar: View {
|
|
let game: LibraryModel.Game
|
|
let download: LibraryModel.Download
|
|
/// How many more downloads wait behind this one.
|
|
let waiting: Int
|
|
let onPause: () -> Void
|
|
@Environment(\.rw) private var p
|
|
|
|
var body: some View {
|
|
VStack(spacing: 0) {
|
|
RwDivider()
|
|
HStack(spacing: 12) {
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
Text(game.name).rw(.titleSmall).lineLimit(1)
|
|
Text(DownloadText.status(download) + (waiting > 0 ? " · \(waiting) waiting" : ""))
|
|
.rw(.labelMedium)
|
|
.lineLimit(1)
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
Button(action: onPause) {
|
|
Image(systemName: "pause.fill").font(.system(size: 16, weight: .semibold))
|
|
}
|
|
.buttonStyle(.rwIcon())
|
|
.accessibilityLabel("Pause download")
|
|
}
|
|
.padding(.leading, RwSizes.pagePadding)
|
|
.padding(.trailing, 8)
|
|
.padding(.vertical, 8)
|
|
if let fraction = download.fraction {
|
|
ProgressView(value: fraction).progressViewStyle(.rwBar)
|
|
} else {
|
|
ProgressView().progressViewStyle(.rwBar)
|
|
}
|
|
}
|
|
.background(p.bg.color)
|
|
.accessibilityElement(children: .contain)
|
|
}
|
|
}
|