Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
183 lines
6.6 KiB
Swift
Executable File
183 lines
6.6 KiB
Swift
Executable File
// MyTracksView — the user's own uploads (port of the Flutter
|
|
// my_tracks_screen.dart).
|
|
//
|
|
// Data flow matches the Dart screen exactly: one getMyTracks(limit: 100)
|
|
// fetch on appear, pull-to-refresh refetches the same page, and the WHOLE
|
|
// loaded list becomes the play context — TrackRow(track:in:) hands it to
|
|
// QueueController.playTrack, so next/prev/autoplay walk the uploads just like
|
|
// TrackTile(queue: _tracks) did in Flutter.
|
|
//
|
|
// No pagination on purpose: getMyTracks (like the Dart api) returns a single
|
|
// page with no next_href, and the Flutter screen never paged either. No Play
|
|
// All — the Dart screen has none.
|
|
//
|
|
// Selection Mode IS here (Components/SelectionMode.swift) even though the Dart
|
|
// screen had none — my_tracks_screen.dart passed no `selectable` flag (and was
|
|
// in fact dead code, referenced by nothing). The holder is this screen's
|
|
// @State, so it dies with the route; the bar rides a bottom safe-area inset so
|
|
// it stacks ABOVE the shell's MiniPlayerBar instead of replacing it.
|
|
|
|
import CrossSoundKit
|
|
import Inject
|
|
import RedWhiteUI
|
|
import SwiftUI
|
|
|
|
@MainActor
|
|
public struct MyTracksView: View {
|
|
// The active RedWhiteUI palette (set once by AppRoot's .rwTheme()).
|
|
@Environment(\.rw) private var p
|
|
@Environment(AppServices.self) private var services
|
|
|
|
private enum Phase {
|
|
case loading
|
|
case failed(String)
|
|
case loaded([SCTrack])
|
|
}
|
|
|
|
@State private var phase: Phase = .loading
|
|
@State private var next: String?
|
|
@State private var loadingMore = false
|
|
/// One-shot guard for the initial load; reset on cancellation so a torn
|
|
/// down .task retries on the next appearance (the Dart screen was
|
|
/// keep-alive, so it never reloaded on re-entry either).
|
|
@State private var hasLoaded = false
|
|
/// Multi-select. NOT cleared in onDisappear: an iOS tab switch disappears
|
|
/// the whole stack, and selection is meant to survive that — only popping
|
|
/// the route (which drops this @State) ends it.
|
|
@State private var selection = TrackSelection()
|
|
|
|
public init() {}
|
|
|
|
@ObserveInjection var inject
|
|
|
|
public var body: some View {
|
|
Group {
|
|
switch phase {
|
|
case .loading:
|
|
LoadingView()
|
|
case .failed(let message):
|
|
ErrorStateView(message: message) { Task { await load() } }
|
|
case .loaded(let tracks) where tracks.isEmpty:
|
|
EmptyStateView(
|
|
icon: "music.note",
|
|
title: "You haven't uploaded any tracks",
|
|
message: "Tracks you upload to SoundCloud show up here.")
|
|
case .loaded(let tracks):
|
|
list(tracks, palette: p)
|
|
}
|
|
}
|
|
.background(p.bg)
|
|
// A nested inset: AppShell already insets the transport at the stack
|
|
// level, so this lands ABOVE MiniPlayerBar rather than replacing it.
|
|
.safeAreaInset(edge: .bottom) { selectionBar }
|
|
.task {
|
|
// One-shot: .task re-runs on every reappear (pop back from a push,
|
|
// tab re-entry), and reloading would drop the list and scroll
|
|
// position. Pull-to-refresh is the deliberate reload path.
|
|
guard !hasLoaded else { return }
|
|
hasLoaded = true
|
|
await load()
|
|
}
|
|
.enableInjection()
|
|
}
|
|
|
|
@ViewBuilder private var selectionBar: some View {
|
|
if selection.isActive {
|
|
SelectionActionBar(selection: selection, tracks: loadedTracks)
|
|
}
|
|
}
|
|
|
|
/// What the ticks resolve against, and what "Select all" covers.
|
|
private var loadedTracks: [SCTrack] {
|
|
if case .loaded(let tracks) = phase { return tracks }
|
|
return []
|
|
}
|
|
|
|
// MARK: - Loading
|
|
|
|
private func load() async {
|
|
phase = .loading
|
|
await fetch()
|
|
}
|
|
|
|
/// Shared fetch for first load and pull-to-refresh. Refresh deliberately
|
|
/// does NOT flip the phase back to .loading: .refreshable shows its own
|
|
/// indicator, and yanking the list out from under the drag would end the
|
|
/// gesture early (Flutter's RefreshIndicator kept the list visible too).
|
|
private func fetch() async {
|
|
do {
|
|
let page = try await services.api.getMyTracksPage(limit: 100)
|
|
CSLog.shared.log("mytracks", "loaded \(page.tracks.count) own tracks")
|
|
next = page.nextHref
|
|
phase = .loaded(page.tracks)
|
|
} catch is CancellationError {
|
|
// The .task was torn down mid-load (view left before the fetch
|
|
// landed): rearm the one-shot guard so the next appearance retries
|
|
// instead of sitting on a stale spinner.
|
|
hasLoaded = false
|
|
} catch {
|
|
CSLog.shared.log("mytracks", "load failed: \(error.localizedDescription)")
|
|
phase = .failed(error.localizedDescription)
|
|
}
|
|
}
|
|
|
|
private func loadMore() async {
|
|
guard !loadingMore, let cursor = next, case .loaded(let existing) = phase else { return }
|
|
loadingMore = true
|
|
defer { loadingMore = false }
|
|
do {
|
|
let page = try await services.api.getMyTracksPage(limit: 100, nextHref: cursor)
|
|
next = page.nextHref
|
|
let seen = Set(existing.map(\.id))
|
|
phase = .loaded(existing + page.tracks.filter { !seen.contains($0.id) })
|
|
} catch {
|
|
CSLog.shared.log("mytracks", "load-more failed: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
|
|
// MARK: - List
|
|
|
|
private func list(_ tracks: [SCTrack], palette p: RwPalette) -> some View {
|
|
ScrollView {
|
|
LazyVStack(alignment: .leading, spacing: 0) {
|
|
// The family's page header (28pt title + AccentBar), same as LikesView
|
|
// — now with the Selection Mode entry pinned to its trailing edge,
|
|
// where LikesView keeps its own header controls.
|
|
HStack(alignment: .top) {
|
|
VStack(alignment: .leading, spacing: 6) {
|
|
// headlineLarge is the library's page-title role (the old hand-set
|
|
// 28/heavy).
|
|
Text("My Tracks")
|
|
.rw(.headlineLarge)
|
|
AccentBar(width: 40)
|
|
}
|
|
Spacer(minLength: RwSizes.gap)
|
|
// Hidden while selecting — the bar's ✕ is the way out.
|
|
if !selection.isActive {
|
|
SelectionEnterButton(selection: selection)
|
|
}
|
|
}
|
|
.padding(.horizontal, RwSizes.pagePadding)
|
|
.padding(.top, RwSizes.pagePadding)
|
|
.padding(.bottom, RwSizes.gap)
|
|
|
|
ForEach(tracks) { track in
|
|
// Queue context = the full uploads list (the behavior contract:
|
|
// a row is never a dead-end single-track queue).
|
|
TrackRow(track: track, in: tracks)
|
|
Rectangle()
|
|
.fill(p.hair)
|
|
.frame(height: 1)
|
|
.padding(.leading, RwSizes.pagePadding)
|
|
}
|
|
InfiniteScrollFooter(hasMore: next != nil, isLoading: loadingMore) {
|
|
await loadMore()
|
|
}
|
|
}
|
|
}
|
|
// Rows read the holder from here — no per-row parameters.
|
|
.trackSelection(selection)
|
|
.pullToRefresh { await fetch() }
|
|
}
|
|
}
|