Files
ipodderx-app/ios/Sources/LibraryStore.swift
Ray Slakinski 14170fe5aa Send the filter names ipx actually understands (#6)
The Pinned tab was not filtering. It sent filter=pinned, and Filter::parse
in db.rs knows unread, downloaded, flagged and in_progress and falls
through to All for anything else -- so the tab returned every item and
looked like it had worked. The column is still named flagged, for what it
was before the interface called it pinned, and the page had this right all
along.

Currently Listening was worse in kind. ipx has filter=in_progress for
exactly it: started past the first few seconds, short of the 90% the UI
calls finished, measured against the length this person's player reported
where there is one. The client asked for everything and trimmed the fifty
rows it happened to receive, so the view showed whichever started episodes
were near the top of the library, left out the rest, and counted wrong.

Both came of writing the filter names from the interface's words instead of
reading what the server parses.

The tests now assert what each filter means rather than how many rows it
returns -- every unread row unread, every downloaded row with a file, every
pinned row pinned, every in-progress row started -- because the failure
here was a full page of entirely plausible rows, which no count would have
caught. Pinned also has to match fewer than everything, which is the shape
the bug took.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-20 09:56:28 -04:00

185 lines
7.1 KiB
Swift

import Foundation
import SwiftUI
/// What the lists are showing, and how it changes.
///
/// The sorting, filtering and searching are the server's work: `GET /api/entries` takes them all
/// and pages fifty at a time, so this holds a page and asks for the next rather than keeping ten
/// thousand items in memory and sorting them here.
@MainActor
final class LibraryStore: ObservableObject {
/// Where the list is pointed. The places come before any feed, as they do in the page.
enum Place: Hashable {
case all
case listening
case feed(String)
var feedId: String? { if case .feed(let id) = self { return id } else { return nil } }
}
@Published private(set) var feeds: [IPX.Feed] = []
@Published private(set) var entries: [IPX.Entry] = []
@Published private(set) var total = 0
@Published private(set) var loading = false
@Published private(set) var failure: String?
/// Light or dark as the account has it, nil for auto. ipx keeps the theme on the user so it
/// follows the person rather than the browser; the app should follow the same choice rather
/// than the phone's, or it disagrees with the page it sits in front of.
@Published private(set) var appearance: ColorScheme?
/// Whether the admin page is worth offering. The server sends it to admins only, so a link
/// for anyone else leads to a refusal.
@Published private(set) var isAdmin = false
@Published private(set) var signOutURL: String?
@Published var place: Place = .all { didSet { if place != oldValue { reload() } } }
@Published var filter: IPX.Filter = .all { didSet { if filter != oldValue { reload() } } }
@Published var sort: IPX.Sort = .published { didSet { reload() } }
@Published var direction: IPX.Direction = .desc { didSet { reload() } }
@Published var search = "" { didSet { searchChanged() } }
@Published var selected: IPX.Entry.ID?
private let api: API
private var searchTask: Task<Void, Never>?
private var loadTask: Task<Void, Never>?
init(api: API) { self.api = api }
var canLoadMore: Bool { entries.count < total }
var selectedEntry: IPX.Entry? { entries.first { $0.id == selected } }
/// The feed a row belongs to, for its name and art. All Subscriptions mixes them, so a row
/// cannot assume the selected feed is its own.
func feed(_ id: String) -> IPX.Feed? { feeds.first { $0.id == id } }
var title: String {
switch place {
case .all: return "All Subscriptions"
case .listening: return "Currently Listening"
case .feed(let id): return feed(id)?.name ?? id
}
}
// MARK: - loading
func refreshFeeds() async {
appearance = .dark
if let me = try? await api.me() {
isAdmin = me.admin
signOutURL = me.signOut
switch me.mode {
case "light": appearance = .light
case "auto": appearance = nil // follow the system, as the page does for Auto
// Dark when nobody has chosen, because that is ipx's own default -- theme.ts
// starts at modern/dark rather than at the system. Following the phone here would
// put a light list in front of a dark page.
default: appearance = .dark
}
}
do {
feeds = try await api.feeds()
failure = nil
} catch {
failure = (error as? LocalizedError)?.errorDescription ?? "\(error)"
}
}
func reload() {
loadTask?.cancel()
loadTask = Task { await load(offset: 0) }
}
func loadMore() {
guard !loading, canLoadMore else { return }
loadTask = Task { await load(offset: entries.count) }
}
private func load(offset: Int) async {
loading = true
defer { loading = false }
do {
// Currently Listening is not a feed; it is the started-but-unfinished filter, which
// the page reaches through the same route.
// Currently Listening is the server's in_progress filter, not a pass over the page
// we happened to receive: trimming fifty rows here showed whichever started episodes
// were near the top of the library and quietly left out the rest.
let page = try await api.entries(
feed: place.feedId,
filter: place == .listening ? .inProgress : filter,
search: search,
sort: sort,
direction: direction,
offset: offset)
guard !Task.isCancelled else { return }
entries = offset == 0 ? page.entries : entries + page.entries
total = page.total
failure = nil
} catch is CancellationError {
} catch {
failure = (error as? LocalizedError)?.errorDescription ?? "\(error)"
}
}
/// Typing should not fire a request per keystroke, nor wait so long it feels broken.
private func searchChanged() {
searchTask?.cancel()
searchTask = Task {
try? await Task.sleep(nanoseconds: 300_000_000)
guard !Task.isCancelled else { return }
reload()
}
}
// MARK: - writing
/// Read and pinned are set here first and sent after. The page does the same, for the same
/// reason: a list asked for before the write lands answers with the old state, which put the
/// dot back on an item just read.
func setRead(_ entry: IPX.Entry, _ read: Bool) {
replace(entry.id) { $0.with(read: read) }
Task {
do { try await api.setRead(read, feed: entry.feedId, guid: entry.guid) }
catch { replace(entry.id) { $0.with(read: !read) } }
}
}
func setPinned(_ entry: IPX.Entry, _ pinned: Bool) {
replace(entry.id) { $0.with(flagged: pinned) }
Task {
do { try await api.setPinned(pinned, feed: entry.feedId, guid: entry.guid) }
catch { replace(entry.id) { $0.with(flagged: !pinned) } }
}
}
func readAll() {
let ids = entries.map(\.id)
for id in ids { replace(id) { $0.with(read: true) } }
Task {
try? await api.readAll(feed: place.feedId)
await refreshFeeds()
}
}
func scan() {
Task {
try? await api.fetch(feed: place.feedId)
}
}
private func replace(_ id: IPX.Entry.ID, _ change: (IPX.Entry) -> IPX.Entry) {
guard let at = entries.firstIndex(where: { $0.id == id }) else { return }
entries[at] = change(entries[at])
}
}
extension IPX.Entry {
/// A copy with one flag moved. The type is decoded from the server and has no setters, which
/// is deliberate: the only things that change locally are the two this person owns.
func with(read: Bool? = nil, flagged: Bool? = nil) -> IPX.Entry {
IPX.Entry(guid: guid, feedId: feedId, title: title, link: link, published: published,
description: description, read: read ?? self.read,
flagged: flagged ?? self.flagged, image: image, duration: duration,
episode: episode, season: season, position: position, enclosures: enclosures)
}
}