Files
2026-10-11 16:01:50 -07:00

163 lines
6.9 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import RedWhiteUI
import SwiftUI
import UIKit
/// The account's games, by title. Tapping one downloads it; the row shows how
/// far it is, and a bar at the bottom follows the running download.
struct LibraryView: View {
@Environment(LibraryModel.self) private var library
@Environment(SignInModel.self) private var signIn
@Environment(\.rw) private var p
@State private var query = ""
@State private var downloadsOnly = false
@State private var confirmingSignOut = false
@State private var confirmingDelete = false
@State private var deleteTarget: LibraryModel.Game?
private var shown: [LibraryModel.Game] {
let text = query.trimmingCharacters(in: .whitespaces)
return library.games.filter { game in
(!downloadsOnly || library.downloads[game.id] != nil || library.finished[game.id] != nil)
&& (text.isEmpty || game.name.localizedCaseInsensitiveContains(text))
}
}
private var subtitle: String? {
let count = library.games.isEmpty ? nil : "\(library.games.count) games"
let parts = [signIn.accountName, count].compactMap { $0 }
return parts.isEmpty ? nil : parts.joined(separator: " · ")
}
var body: some View {
VStack(spacing: 0) {
RwAppBar("Library", subtitle: subtitle, automaticallyImplyLeading: false, actions: [
RwAction(systemImage: "arrow.clockwise", label: "Refresh",
action: library.isRefreshing ? nil : { Task { await library.refresh() } }),
RwAction(systemImage: "rectangle.portrait.and.arrow.right", label: "Sign out",
action: { confirmingSignOut = true }),
])
if let problem = library.problem {
Button { library.problem = nil } label: { RwAlert(problem, tone: .red) }
.buttonStyle(.plain)
.padding(.horizontal, RwSizes.pagePadding)
.padding(.top, RwSizes.gap)
.accessibilityHint("Dismisses the message")
}
if library.games.isEmpty {
emptyState
} else {
filterBar
RwDivider()
list
}
if let current = library.current {
DownloadBar(game: current.game, download: current.download, waiting: library.waitingCount) {
library.pause(current.game.id)
}
}
}
.rwDialog(
isPresented: $confirmingSignOut,
title: "Sign out of Steam?",
message: "Downloads in progress are paused. Games already on this device stay.",
actions: [.cancel(), .destructive("Sign out") { signIn.signOut() }]
)
.rwDialog(
isPresented: $confirmingDelete,
title: "Delete \(deleteTarget?.name ?? "this download")?",
message: "Its files are removed from this device. You can download it again later.",
actions: [
.cancel(),
.destructive("Delete") { if let game = deleteTarget { library.remove(game.id) } },
]
)
}
private var filterBar: some View {
HStack(spacing: RwSizes.gap) {
TextField("", text: $query)
.rwPlaceholder("Search games", when: query.isEmpty)
.autocorrectionDisabled()
.textInputAutocapitalization(.never)
.submitLabel(.search)
Button("Downloads") { downloadsOnly.toggle() }
.buttonStyle(.rw(selected: downloadsOnly))
.accessibilityAddTraits(downloadsOnly ? .isSelected : [])
}
.padding(.horizontal, RwSizes.pagePadding)
.padding(.vertical, RwSizes.gap)
}
private var list: some View {
ScrollView {
LazyVStack(spacing: 0) {
ForEach(shown) { game in
GameRow(
game: game,
download: library.downloads[game.id],
finished: library.finished[game.id],
menu: menu(for: game)
) { tap(game) }
RwDivider()
}
if shown.isEmpty {
Text(downloadsOnly && query.isEmpty ? "Nothing downloaded yet. Tap a game to download it." : "No games match.")
.rw(.bodyMedium, color: p.textMuted.color)
.padding(RwSizes.pagePadding)
}
Text("Downloads are saved in the Files app, under On My \(UIDevice.current.userInterfaceIdiom == .pad ? "iPad" : "iPhone") › Steam Downloader › steamapps.")
.rw(.bodySmall)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(RwSizes.pagePadding)
}
}
.scrollDismissesKeyboard(.immediately)
.rwRefreshable { [library] in await library.refresh() }
}
private var emptyState: some View {
VStack(spacing: RwSizes.gap) {
if library.isRefreshing || (!library.hasList && library.problem == nil) {
ProgressView().controlSize(.large)
Text("Loading your library…").rw(.titleMedium)
Text("A big library can take a minute the first time.").rw(.bodySmall)
} else {
Text(library.problem == nil ? "No games found" : "Your library couldn't be loaded").rw(.titleMedium)
Button("Try again") { Task { await library.refresh() } }
}
}
.multilineTextAlignment(.center)
.padding(RwSizes.pagePadding)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
// MARK: Actions
/// A tap starts the download; on a running one it pauses, on a stopped one it continues.
private func tap(_ game: LibraryModel.Game) {
guard library.finished[game.id] == nil else { return }
switch library.downloads[game.id]?.status {
case .running?, .waiting?: library.pause(game.id)
case .paused?, .stopped?, nil: library.download(game.id)
}
}
private func menu(for game: LibraryModel.Game) -> [RwMenuItem] {
let delete = RwMenuItem("Delete download", systemImage: "trash", destructive: true) {
deleteTarget = game
confirmingDelete = true
}
if library.finished[game.id] != nil { return [delete] }
switch library.downloads[game.id]?.status {
case .running?, .waiting?:
return [RwMenuItem("Pause", systemImage: "pause") { library.pause(game.id) }, .separator, delete]
case .paused?:
return [RwMenuItem("Resume", systemImage: "play") { library.download(game.id) }, .separator, delete]
case .stopped?:
return [RwMenuItem("Try again", systemImage: "arrow.clockwise") { library.download(game.id) }, .separator, delete]
case nil:
return []
}
}
}