Files
RedLintu16andClaude Opus 5.5 64ec524ba0 CrossSound for Apple platforms
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-14 13:28:34 -07:00

300 lines
9.6 KiB
Swift
Executable File

// UpdateView — the Update screen (Flutter: the services_screen.dart "Update"
// row calling runUpdateCheck(context)).
//
// The Dart version was a chain of SnackBars and modal dialogs: "Checking for
// updates…", then either "You're on the latest version." or an
// "Update available — v1.1.0" alert with a Download & install button and a
// progress dialog. None of that shape survives here, for two reasons:
// - a pushed SCREEN can show current-vs-latest, notes and the action at once,
// where the dialog chain hid the current version entirely (the Dart UI
// never displayed it anywhere);
// - the progress dialog measured an APK download that led to an Android
// package installer. Neither platform here can install from a file (see
// UpdateChecker's header), so there is nothing to show progress FOR. The
// action is a hand-off, and UpdateAction.explanation states exactly what
// tapping it does — this screen must never imply an in-app install.
//
// The check runs on appear (the Dart row needed a tap because the check WAS the
// row) and can be re-run; failures are reported with the reason, since the
// live Android manifest currently answers 403 and a silent "up to date" would
// be a lie.
import CrossSoundKit
import Inject
import RedWhiteUI
import SwiftUI
@MainActor
public struct UpdateView: View {
// The active RedWhiteUI palette (set once by AppRoot's .rwTheme()).
@Environment(\.rw) private var p
// Both platforms route https:// and itms-services:// through this.
@Environment(\.openURL) private var openURL
private enum Phase {
case checking
case done(UpdateOutcome)
}
@State private var phase: Phase = .checking
public init() {}
@ObserveInjection var inject
public var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
header(p)
installedSection(p)
latestSection(p)
footer(p)
}
.padding(.bottom, RwSizes.section)
}
.background(p.bg)
.task { await check() }
.enableInjection()
}
// MARK: - Header
/// The family's page header (28pt title + AccentBar) — pushed screens in this
/// app carry their titles in-content (ServicesView's pattern).
private func header(_ p: RwPalette) -> some View {
VStack(alignment: .leading, spacing: 6) {
Text("Update")
.rw(.headlineLarge)
AccentBar(width: 40)
}
.padding(.horizontal, RwSizes.pagePadding)
.padding(.top, RwSizes.pagePadding)
.padding(.bottom, RwSizes.section)
}
// MARK: - Installed
private func installedSection(_ p: RwPalette) -> some View {
section("Installed", palette: p) {
HStack(spacing: RwSizes.gap) {
Image(systemName: "app.badge.checkmark")
.font(.system(size: 17))
.foregroundStyle(p.red)
.frame(width: 28)
VStack(alignment: .leading, spacing: 2) {
Text("CrossSound \(UpdateChecker.currentVersion)")
.rw(.bodyLarge)
// The build number is support information only — UpdateChecker.isNewer
// cannot see it, so saying so here stops "but my build is higher".
Text("Build \(UpdateChecker.currentBuild) · \(Self.platformName)")
.rw(.labelMedium)
}
Spacer(minLength: RwSizes.gap)
}
.padding(.horizontal, RwSizes.pagePadding)
.padding(.vertical, 10)
}
}
// MARK: - Latest
@ViewBuilder
private func latestSection(_ p: RwPalette) -> some View {
section("Latest release", palette: p) {
switch phase {
case .checking:
HStack(spacing: RwSizes.gap) {
ProgressView()
// .color: ProgressView only honors the Color overload of .tint.
.tint(p.red.color)
.controlSize(.small)
Text("Checking for updates…")
.rw(.bodyLarge)
Spacer(minLength: 0)
}
.padding(.horizontal, RwSizes.pagePadding)
.padding(.vertical, 14)
case .done(.upToDate(let current)):
statusRow(
icon: "checkmark.seal",
tint: p.success,
title: "You're on the latest version",
detail: "\(current) is the newest build published for \(Self.platformName).",
palette: p,
trailing: { EmptyView() })
hairline(p)
recheckRow(p)
case .done(.failed(let message)):
statusRow(
icon: "exclamationmark.triangle",
tint: p.red,
title: "Couldn't check for updates",
detail: message,
palette: p,
trailing: { EmptyView() })
hairline(p)
recheckRow(p)
case .done(.available(let info)):
available(info, palette: p)
}
}
}
@ViewBuilder
private func available(_ info: UpdateInfo, palette p: RwPalette) -> some View {
statusRow(
icon: "arrow.down.circle",
tint: p.red,
title: "Version \(info.version) is available",
detail: "You have \(UpdateChecker.currentVersion).",
palette: p,
trailing: { OutlinedBadge("NEW") })
if let notes = info.notes {
hairline(p)
VStack(alignment: .leading, spacing: 4) {
Text("What's new")
.rw(.labelSmall)
Text(notes)
.rw(.bodySmall)
.fixedSize(horizontal: false, vertical: true)
}
.padding(.horizontal, RwSizes.pagePadding)
.padding(.vertical, 12)
}
hairline(p)
VStack(alignment: .leading, spacing: RwSizes.gap) {
if let action = info.action {
// The house button (page-colored, red hairline, inverted on press).
Button {
CSLog.shared.log("update", "opening \(action.url.scheme ?? "?") hand-off")
openURL(action.url)
} label: {
Label(action.label, systemImage: "arrow.up.forward.app")
}
.buttonStyle(.bare)
Text(action.explanation)
.rw(.labelMedium)
.fixedSize(horizontal: false, vertical: true)
} else {
// A manifest that names a version but no route this platform can use.
// Saying nothing would look like a broken button; this says what to do.
Text(Self.noRouteCopy)
.rw(.labelMedium)
.fixedSize(horizontal: false, vertical: true)
}
Button("Check again") { Task { await check() } }
.buttonStyle(.bareText)
}
.padding(.horizontal, RwSizes.pagePadding)
.padding(.vertical, 12)
}
/// Why an available version can have no button, per platform.
private static var noRouteCopy: String {
#if os(macOS)
return "The manifest didn't include a download link for macOS, so there's nothing "
+ "to open here yet."
#else
return "iOS can't install an app from inside another app, and this release didn't "
+ "list an over-the-air install page or a TestFlight link. Grab the new build "
+ "from wherever you installed this one."
#endif
}
// MARK: - Rows
/// Icon + title + detail, with whatever the caller wants pinned trailing.
/// Explicitly generic (not an opaque parameter with a default) so the three
/// plain call sites can hand it an EmptyView builder.
private func statusRow<Trailing: View>(
icon: String, tint: RwColor, title: String, detail: String, palette p: RwPalette,
@ViewBuilder trailing: () -> Trailing
) -> some View {
HStack(spacing: RwSizes.gap) {
Image(systemName: icon)
.font(.system(size: 17))
.foregroundStyle(tint)
.frame(width: 28)
VStack(alignment: .leading, spacing: 2) {
Text(title)
.rw(.bodyLarge)
.fixedSize(horizontal: false, vertical: true)
Text(detail)
.rw(.labelMedium)
.fixedSize(horizontal: false, vertical: true)
}
Spacer(minLength: RwSizes.gap)
trailing()
}
.padding(.horizontal, RwSizes.pagePadding)
.padding(.vertical, 10)
}
private func recheckRow(_ p: RwPalette) -> some View {
HStack {
Button("Check again") { Task { await check() } }
.buttonStyle(.bareText)
Spacer(minLength: 0)
}
.padding(.horizontal, RwSizes.pagePadding - 12) // .rwText carries 12pt insets
.padding(.vertical, 4)
}
// MARK: - Footer
/// Which URL was actually checked. A 403/404 manifest is the failure this
/// feature really hits, and naming the URL is what makes that diagnosable
/// instead of "updates are broken".
private func footer(_ p: RwPalette) -> some View {
Text("Checked \(UpdateChecker.manifestURLString)")
.font(.system(size: 11, design: .monospaced))
.foregroundStyle(p.textSubtle)
.textSelection(.enabled)
.fixedSize(horizontal: false, vertical: true)
.padding(.horizontal, RwSizes.pagePadding)
}
// MARK: - Actions
private func check() async {
phase = .checking
let outcome = await UpdateChecker.check()
phase = .done(outcome)
}
private static var platformName: String {
#if os(macOS)
"macOS"
#else
"iOS"
#endif
}
// MARK: - Section chrome (private copies of ServicesView's grammar)
private func section(
_ title: String, palette p: RwPalette, @ViewBuilder rows: () -> some View
) -> some View {
VStack(alignment: .leading, spacing: RwSizes.gap) {
RwSectionHeader(title)
// Zero insets: the rows own their padding.
RwCard(padding: EdgeInsets()) {
VStack(alignment: .leading, spacing: 0) { rows() }
}
}
.padding(.horizontal, RwSizes.pagePadding)
.padding(.bottom, RwSizes.section)
}
/// Hair separators are the list's job, not the row's (LikesView convention).
private func hairline(_ p: RwPalette) -> some View {
Rectangle()
.fill(p.hair)
.frame(height: 1)
.padding(.leading, RwSizes.pagePadding)
}
}