// Where the one viewer is presented from: the root, once.//// An opener asks the presenter in its environment; it never presents a// full-screen view of its own (`OneViewerTests`). On the phone the viewer// grows out of the tile it was opened from and, swiped down, follows the// finger back to the tile of whatever it is showing then — the system's// zoom transition, which crossfades under Reduce Motion.importSwiftUI/// The viewer's state at the root: what is open, and which item it is on.@MainActor @ObservablefinalclassViewerPresenter {/// The set on screen; nil when the viewer is closed. Closed, however it/// closes (a swipe down sets it nil through the cover), the read still/// growing its set stops.var current: ViewerSet? { didSet { if current == nil { growing?.cancel(); growing = nil } } }/// The item the viewer stands on — the tile a dismissal returns to.var showing: String?/// The read growing the set on screen to the whole it is a window of. @ObservationIgnored privatevar growing: Task<Void, Never>?// Where `set` is a window of more (a deck's dealt cards), `rest` reads// the whole, and the viewer takes it standing where it is — unless it has// closed or opened another set meanwhile./// What plays a film's or an episode's record (`FilmPlayer.cinema`) or a/// podcast episode (`FilmPlayer.audio`): Apple's player, or the app's one/// audio player, set by the host; nil in a preview. @ObservationIgnored var streams: (@MainActor (ViewerItem) -> Void)?/// Opens `set` at once. A film opens in Apple's player alone, never in/// the viewer (`FilmPlayer`); the viewer walks the rest.funcopen(_ set: ViewerSet, rest: (@MainActor () async -> [ViewerItem])? = nil) { growing?.cancel() growing = nilguard !set.items.isEmpty else { return }let opened = set.items[set.start]ifFilmPlayer.of(opened) != .viewer { streams?(opened)return }let set = Self.viewed(set)guard !set.items.isEmpty else { return } showing = set.items[set.start].id current = setguardlet rest else { return } growing = Task { [weakself] inlet all = await rest().filter { FilmPlayer.of($0) == .viewer }guard !Task.isCancelled, letself, letopen = self.current, open.id == set.id, all.count > open.items.count else { return }let at = all.firstIndex { $0.id == self.showing } ?? open.startself.current = ViewerSet(id: open.id, items: all, start: at, pins: open.pins) } }/// `set` as the viewer walks it: the films left out, opened on the same/// item.staticfunc viewed(_ set: ViewerSet) -> ViewerSet {let opened = set.items[set.start].idlet items = set.items.filter { FilmPlayer.of($0) == .viewer }returnViewerSet(id: set.id, items: items, start: items.firstIndex { $0.id == opened } ?? 0, pins: set.pins) }func close() { current = nil }}privatestructViewerPresenterKey: EnvironmentKey {staticlet defaultValue: ViewerPresenter? = nil}privatestructViewerNamespaceKey: EnvironmentKey {staticlet defaultValue: Namespace.ID? = nil}extensionEnvironmentValues {/// The viewer this screen opens media in; nil in a preview or a test.var viewerPresenter: ViewerPresenter? { get { self[ViewerPresenterKey.self] } set { self[ViewerPresenterKey.self] = newValue } }/// The namespace a tile and the viewer match their transition in.var viewerNamespace: Namespace.ID? { get { self[ViewerNamespaceKey.self] } set { self[ViewerNamespaceKey.self] = newValue } }}/// Holds the presenter and presents the viewer over everything under it.privatestructViewerHost: ViewModifier { @State privatevar presenter = ViewerPresenter() @Namespace privatevar namespace @Environment(\.cottonCore) privatevar core @Environment(ToastCenter.self) privatevar toasts: ToastCenter?func body(content: Content) -> someView { content .onAppear { presenter.streams = streams } .environment(\.viewerPresenter, presenter) .environment(\.viewerNamespace, namespace) .fullScreenCover(item: $presenter.current) { set inMediaViewer(media: set, showing: $presenter.showing) { presenter.close() } .environment(\.viewerPresenter, presenter)// The cover is its own window: the cards opened over the// picture stand in it. .cardLayers() #if os(iOS)// The page under the viewer leaves the accessibility tree// while it is up: the zoom keeps the presenter in the// window, and its list stayed readable behind the picture// (walker 604bcce6). A sheet's anchor, the same rule. .background { SheetModalAnchor() } .navigationTransition(.zoom(sourceID: presenter.showing ?? set.id, in: namespace)) #endif } }}extensionViewerHost {/// A film's or an episode's record, dealt by the core's one player/// (`shell.player`) and played in Apple's player over everything/// (`FilmPlayback`); a podcast episode, in the app's one audio player/// (`PodcastPlayback`).privatevar streams: @MainActor (ViewerItem) -> Void { { [core, toasts] item in #if os(iOS)guardlet core else { return }let say: (String) -> Void = { said in toasts?.show(said, tone: .error) }ifFilmPlayer.of(item) == .audio {PodcastPlayback.shared.play(item, core: core, say: say) } elseiflet record = item.record {Task { awaitFilmPlayback.ask(table: record.table, id: record.id) } } #endif } }}extensionView {/// The root of a window: media opened anywhere under it opens here.func hostsViewer() -> someView { modifier(ViewerHost()) }/// A tile the viewer can grow out of and close back into, by the id of/// the item it shows.func viewerSource(_ id: String) -> someView { modifier(ViewerSource(id: id)) }}privatestructViewerSource: ViewModifier {let id: String @Environment(\.viewerNamespace) privatevar namespacefunc body(content: Content) -> someView { #if os(iOS)iflet namespace { content.matchedTransitionSource(id: id, in: namespace) } else { content } #else content #endif }}