622 lines · 27.7 KB · SwiftRaw
// CINEMAS' SHARED PIECES (cinemas-3.html): the poster and its honest states (part watched, on this phone, still being// prepared by his Mac, from a friend; CONTRACT-2 "States on a poster"), the press-in and the long-press lift, a row// of posters with its title, count, "yours" mark and All, the soft snap, the skeletons in the final sizes so nothing// jumps when the answer lands, the buttons and chips, and the refusal with Try again.#if os(iOS)import CottonKitimport SwiftUI/// The sizes a row draws at, from the screen's width: about three and a third posters across (108 on a 390 screen),/// so the next one peeks.struct CinemasSizes: Equatable { var width: CGFloat var poster: CGSize { let w = max(((width - CinemasInk.side - 3 * CinemasInk.gap) / 3.3).rounded(.down), 90) return CGSize(width: w, height: (w * 1.5).rounded()) } /// The Videos row's wide tile: 200 on a 390 screen, 16:9. var wide: CGSize { let w = max((width * 0.513).rounded(.down), 180) return CGSize(width: w, height: (w * 9 / 16).rounded()) } /// One of two buttons side by side across the screen. var halfButton: CGFloat { max(((width - 2 * CinemasInk.side - 10) / 2).rounded(.down), 0) } /// The featured block: 540 high on a 390 screen. var featured: CGSize { CGSize(width: width, height: (width * 1.385).rounded()) }}/// The words VoiceOver reads for a poster: its title, year or episode, and each state it carries./// A length in the line set's words: "1h 52m", or "52 min" under an hour.func cinemasLength(_ minutes: Int) -> String { minutes >= 60 ? ShellLines.line("cinemas", "home.length_hours", ["h": "\(minutes / 60)", "m": "\(minutes % 60)"]) : ShellLines.line("cinemas", "home.length_minutes", ["m": "\(minutes)"])}func cinemasLabel(_ item: CinemasItem) -> String { var parts = [item.year.map { CinemasWords.line("stream.poster_year", ["title": item.title, "year": $0]) } ?? CinemasWords.line("stream.poster", ["title": item.title])] if let caption = item.caption { parts.append(caption) } if let progress = item.progress { parts.append(CinemasWords.line("stream.poster_progress", ["percent": "\(Int(progress * 100))"])) } switch item.onPhone { case .kept: parts.append(CinemasWords.line("stream.poster_kept")) case .keeping: parts.append(CinemasWords.line("stream.poster_keeping")) case nil: break } if item.preparing { parts.append(CinemasWords.line("stream.poster_preparing")) } if let from = item.from { parts.append(CinemasWords.line("stream.poster_from", ["name": from])) } return parts.joined(separator: ", ")}/// A poster with its states, which presses in under a finger, opens its page on a tap, and lifts into the sheet on a/// long press (350 ms, a light tap of the phone, up to 1.04 with a shadow).struct CinemasPoster: View { let model: CinemasModel let item: CinemasItem /// The row it stands in, so the same title in two rows grows from the one that was touched. let row: String let size: CGSize let grow: Namespace.ID var wide = false @Environment(\.accessibilityReduceMotion) private var reduceMotion @State private var pressed = false @State private var lifted = false var body: some View { VStack(alignment: .leading, spacing: 5) { ZStack { CinemasPicture(key: wide ? (item.backdrop ?? item.poster) : item.poster, title: wide ? nil : item.title, seed: item.id, art: item.art) CinemasStates(item: item, wide: wide) } .frame(width: size.width, height: size.height) .clipShape(RoundedRectangle(cornerRadius: CinemasInk.corner, style: .continuous)) .matchedTransitionSource(id: "\(row)/\(item.key)", in: grow) { source in source.clipShape(RoundedRectangle(cornerRadius: CinemasInk.corner, style: .continuous)) } .shadow(color: .black.opacity(lifted ? 0.6 : 0), radius: lifted ? 18 : 0, y: lifted ? 10 : 0) if wide { Text(item.title).font(CinemasType.smallBold).foregroundStyle(CinemasInk.ink).lineLimit(1) .frame(width: size.width, alignment: .leading) } if let caption = item.caption { Text(caption).font(CinemasFonts.sans(11)).foregroundStyle(CinemasInk.faint).lineLimit(1) .frame(width: size.width, alignment: .leading) } } .scaleEffect(lifted ? CinemasMotion.liftScale : pressed ? (reduceMotion ? CinemasMotion.pressScaleReduced : CinemasMotion.pressScale) : 1) .animation(lifted ? CinemasMotion.lift : pressed ? CinemasMotion.pressDown : (reduceMotion ? nil : CinemasMotion.pressUp), value: pressed) .animation(CinemasMotion.lift, value: lifted) .contentShape(Rectangle()) .onTapGesture { model.open(item, from: row) } .onLongPressGesture(minimumDuration: CinemasMotion.liftAfter) { UIImpactFeedbackGenerator(style: .light).impactOccurred() lifted = true model.sheet = .item(item) } onPressingChanged: { now in pressed = now } .onChange(of: model.sheet == nil) { _, closed in if closed { lifted = false } } .accessibilityElement(children: .ignore) .accessibilityLabel(Text(cinemasLabel(item))) .accessibilityAddTraits(.isButton) .accessibilityAction { model.open(item, from: row) } .accessibilityAction(named: Text(CinemasWords.line("stream.group_sheet"))) { model.sheet = .item(item) } .accessibilityIdentifier("cinemas-poster-\(row)-\(item.id)") }}/// The states drawn on a poster, worked out by the core and never kept by the phone.struct CinemasStates: View { let item: CinemasItem var wide = false @Environment(\.accessibilityReduceMotion) private var reduceMotion @State private var breathing = false /// The marks are part of the picture, sized to it: they stop growing at the largest ordinary size, and the poster's /// spoken name says them in full. var body: some View { marks.dynamicTypeSize(...DynamicTypeSize.xxLarge) } private var marks: some View { GeometryReader { box in ZStack { if item.preparing { // Getting ready on his Mac: dimmed, breathing softly (0.55 ↔ 0.75 every 2.4 s; still under Reduce // Motion), a dashed amber tag and a dashed line at the foot. Color(red: 0.03, green: 0.024, blue: 0.02).opacity(breathing ? 0.55 : 0.75) .onAppear { guard !reduceMotion else { return } withAnimation(CinemasMotion.preparingPulse) { breathing = true } } VStack { HStack { Text(CinemasWords.line("stream.poster_preparing")) .font(CinemasFonts.sans(max(10, box.size.width * 0.09), 600, relativeTo: .caption2)) .foregroundStyle(CinemasInk.accent) .lineLimit(2) .minimumScaleFactor(0.6) .padding(.horizontal, 8) .padding(.vertical, 2) .background(Capsule().fill(Color.black.opacity(0.5))) .overlay(Capsule().strokeBorder(CinemasInk.accent, style: StrokeStyle(lineWidth: 1, dash: [3, 2]))) Spacer(minLength: 0) } Spacer(minLength: 0) Rectangle() .stroke(CinemasInk.accent, style: StrokeStyle(lineWidth: 3, dash: [6, 4])) .frame(height: 3) } .padding(6) } VStack(spacing: 0) { if let from = item.from { Text(CinemasWords.line("stream.poster_from", ["name": from])) .font(CinemasFonts.sans(10, 600, relativeTo: .caption2)) .foregroundStyle(CinemasInk.onAccent) .lineLimit(1) .frame(maxWidth: .infinity) .padding(.vertical, 3) .background(CinemasInk.accent.opacity(0.92)) } HStack(alignment: .top) { Spacer(minLength: 0) if let onPhone = item.onPhone { CinemasKeptMark(keeping: onPhone == .keeping) .padding(6) } } Spacer(minLength: 0) if let progress = item.progress, !item.preparing { // The part watched: an amber line along the foot over a dim track. ZStack(alignment: .leading) { Rectangle().fill(Color.white.opacity(0.22)) Rectangle().fill(CinemasInk.accent).frame(width: (box.size.width * min(max(progress, 0), 1)).drawableOrZero) } .frame(height: 3.5) } } } } }}/// On this phone: a small dark circle with an amber arrow; keeping, its ring is dashed and the arrow breathes.struct CinemasKeptMark: View { let keeping: Bool @Environment(\.accessibilityReduceMotion) private var reduceMotion var body: some View { Image(systemName: "arrow.down") .font(.system(size: 11, weight: .bold)) .foregroundStyle(CinemasInk.accent) .frame(width: 22, height: 22) .background(Circle().fill(Color(red: 0.04, green: 0.03, blue: 0.027).opacity(0.82))) .overlay(Circle().strokeBorder(CinemasInk.accent.opacity(keeping ? 0.9 : 0.35), style: StrokeStyle(lineWidth: 1, dash: keeping ? [2, 2] : []))) .symbolEffect(.pulse, isActive: keeping && !reduceMotion) .accessibilityHidden(true) }}/// A row of posters under its title: momentum, then a soft snap that lines a poster's leading edge up with the margin.struct CinemasPosterRow: View { let model: CinemasModel let row: CinemasHome.Row let sizes: CinemasSizes let grow: Namespace.ID @Environment(\.accessibilityReduceMotion) private var reduceMotion /// A web video draws wide, as a web page's video is, in whatever row it stands. private func wide(_ item: CinemasItem) -> Bool { row.id == "videos" || item.kind == .clip } var body: some View { VStack(alignment: .leading, spacing: 9) { CinemasRowHead(model: model, row: row) if let binned = row.binned, let restore = row.restore { CinemasBinnedStrip(model: model, title: row.title, binned: binned, restore: restore) } ScrollView(.horizontal) { LazyHStack(alignment: .top, spacing: CinemasInk.gap) { if row.items.isEmpty, row.group != nil { CinemasAddTile(model: model, size: sizes.poster) } ForEach(row.items) { item in CinemasPoster(model: model, item: item, row: row.id, size: wide(item) ? sizes.wide : sizes.poster, grow: grow, wide: wide(item)) .transition(.scale(scale: 0.8).combined(with: .opacity).animation(CinemasMotion.removeOut)) } } .scrollTargetLayout() .animation(reduceMotion ? CinemasMotion.reduced : CinemasMotion.closeGap, value: row.items.map(\.id)) } .scrollIndicators(.hidden) .scrollTargetBehavior(.viewAligned) .contentMargins(.horizontal, CinemasInk.side, for: .scrollContent) .scrollClipDisabled() } .accessibilityElement(children: .contain) .accessibilityIdentifier("cinemas-row-\(row.id)") }}/// A row's title with its count, "yours" on his own rows, and All on the right (not on Carry on). The title opens the/// whole row; his own rows also carry a menu: rename, move up or down on Home, delete./// Inside Hidden, a hidden category the Bin holds (HIDE FROM SCREEN core 3): the core's "In the Bin", and Restore.struct CinemasBinnedStrip: View { let model: CinemasModel let title: String let binned: String let restore: CinemasNamedAct @State private var busy = false var body: some View { ViewThatFits(in: .horizontal) { HStack(spacing: 10) { said; Spacer(minLength: 8); act } VStack(alignment: .leading, spacing: 8) { said; act } } .padding(.horizontal, CinemasInk.side) } private var said: some View { Label { Text(binned).font(CinemasType.meta).foregroundStyle(CinemasInk.quiet).fixedSize(horizontal: false, vertical: true) } icon: { Image(systemName: "trash").foregroundStyle(CinemasInk.faint) } .accessibilityElement(children: .combine) .accessibilityLabel(Text(verbatim: "\(title), \(binned)")) } private var act: some View { CinemasButton(label: restore.label, icon: "arrow.uturn.backward", style: .outline, height: 44) { guard !busy else { return } busy = true Task { await model.restoreNamed(restore) busy = false } } .fixedSize() .accessibilityIdentifier("cinemas-hidden-restore-\(restore.text("id") ?? title)") }}struct CinemasRowHead: View { let model: CinemasModel let row: CinemasHome.Row @Environment(\.dynamicTypeSize) private var typeSize var body: some View { // At the accessibility sizes the name stands whole on its own lines, its count and acts under it. if typeSize.isAccessibilitySize { VStack(alignment: .leading, spacing: 4) { Button { model.push(.row(id: row.id, title: row.title)) } label: { Text(row.title).font(CinemasType.rowTitle).foregroundStyle(CinemasInk.ink) .multilineTextAlignment(.leading).fixedSize(horizontal: false, vertical: true) } .buttonStyle(CinemasPress()) .accessibilityAddTraits(.isHeader) .contextMenu { if let group = row.group { hiding(group) } } .accessibilityIdentifier("cinemas-row-head-\(row.id)") .padding(.horizontal, CinemasInk.side) line(named: false) } } else { line(named: true) } } /// The row's line: its name (unless it stands above), count and "yours", its menu and All. private func line(named: Bool) -> some View { HStack(alignment: .firstTextBaseline, spacing: 6) { if named || row.id != "carry_on" || row.group != nil { Button { model.push(.row(id: row.id, title: row.title)) } label: { HStack(alignment: .firstTextBaseline, spacing: 6) { if named { Text(row.title).font(CinemasType.rowTitle).foregroundStyle(CinemasInk.ink).lineLimit(2).multilineTextAlignment(.leading) } if row.id != "carry_on" { Text(row.count.formatted()).font(CinemasType.rowCount).foregroundStyle(CinemasInk.faint) } if row.group != nil { CinemasYours() } } // A thumb's height to touch, the row's own height kept (the padding below gives it back). .frame(minHeight: 44) .contentShape(Rectangle()) } .buttonStyle(CinemasPress()) .padding(.vertical, -12) .accessibilityAddTraits(named ? .isHeader : []) // Held, a category's name offers to hide it (or, inside Hidden, to stop hiding it). .contextMenu { if let group = row.group { hiding(group) } } .accessibilityLabel(Text(verbatim: row.id == "carry_on" ? row.title : "\(row.title), \(row.count.formatted())")) .accessibilityIdentifier(named ? "cinemas-row-head-\(row.id)" : "cinemas-row-count-\(row.id)") } Spacer(minLength: 8) if let group = row.group { Menu { Button(CinemasWords.line("stream.group_rename"), systemImage: "pencil") { model.sheet = .rename(id: group, name: row.title) } Button(CinemasWords.line("stream.group_earlier"), systemImage: "arrow.up") { Task { model.act(await model.source.moveGroup(group, earlier: true)) } } Button(CinemasWords.line("stream.group_later"), systemImage: "arrow.down") { Task { model.act(await model.source.moveGroup(group, earlier: false)) } } hiding(group) Button(CinemasWords.line("stream.group_delete"), systemImage: "trash", role: .destructive) { Task { await model.deleteGroup(id: group, name: row.title) } } } label: { Image(systemName: "ellipsis") .font(.system(size: 15, weight: .semibold)) .foregroundStyle(CinemasInk.quiet) .frame(width: 36, height: 28) .contentShape(Rectangle()) } .accessibilityLabel(Text("\(row.title), \(CinemasWords.line("stream.more"))")) .accessibilityIdentifier("cinemas-row-menu-\(row.id)") } if row.id != "carry_on" { Button { model.push(.row(id: row.id, title: row.title)) } label: { Text(CinemasWords.line("stream.see_all")).font(CinemasType.meta).foregroundStyle(CinemasInk.quiet) .frame(minWidth: 44, minHeight: 44, alignment: .trailing) .contentShape(Rectangle()) } .buttonStyle(CinemasPress()) .accessibilityLabel(Text("\(CinemasWords.line("stream.see_all")), \(row.title)")) .accessibilityIdentifier("cinemas-row-all-\(row.id)") } } .padding(.horizontal, CinemasInk.side) } /// Hide this category (the Hide sheet first), or inside Hidden, Stop hiding at once with Undo. @ViewBuilder private func hiding(_ group: String) -> some View { if !model.hiddenReachable { EmptyView() } else if model.hiddenReads { Button(CinemasWords.line("stream.stop_hiding"), systemImage: "eye") { Task { await model.hide(group, hidden: false) } } .accessibilityIdentifier("cinemas-row-stop-hiding-\(group)") } else { Button(CinemasWords.line("stream.hide_category"), systemImage: "eye.slash") { model.sheet = .hide(id: group, name: row.title) } .accessibilityIdentifier("cinemas-row-hide-\(group)") } }}/// "yours": the mark on his own groups' rows.struct CinemasYours: View { var body: some View { Text(CinemasWords.line("stream.yours")) .font(CinemasFonts.sans(10, 700, relativeTo: .caption2)) .foregroundStyle(CinemasInk.accent) .padding(.horizontal, 6) .padding(.vertical, 1) .overlay(Capsule().strokeBorder(CinemasInk.accent.opacity(0.45), lineWidth: 1)) }}/// An empty group's only tile: Add films, which goes to Search, where a long press adds a title.struct CinemasAddTile: View { let model: CinemasModel let size: CGSize var body: some View { Button { model.tab = .search } label: { VStack(spacing: 8) { Image(systemName: "plus").font(.system(size: 22, weight: .semibold)) Text(CinemasWords.line("stream.add_films")).font(CinemasType.chip).multilineTextAlignment(.center) } .foregroundStyle(CinemasInk.quiet) .frame(width: size.width, height: size.height) .background(RoundedRectangle(cornerRadius: CinemasInk.corner).strokeBorder(CinemasInk.rule, style: StrokeStyle(lineWidth: 1.5, dash: [5, 4]))) } .buttonStyle(CinemasPress()) .accessibilityIdentifier("cinemas-add-tile") }}/// A skeleton block: a shape at its final size with a shimmer passing.struct CinemasBlock: View { var corner: CGFloat = CinemasInk.corner var body: some View { RoundedRectangle(cornerRadius: corner, style: .continuous).fill(CinemasInk.skeleton).cinemasShimmer() }}/// A skeleton row: a title bar and posters in the final sizes, shimmering.struct CinemasSkeletonRow: View { let sizes: CinemasSizes var wide = false var body: some View { VStack(alignment: .leading, spacing: 9) { CinemasBlock(corner: 4).frame(width: 140, height: 16).padding(.horizontal, CinemasInk.side) HStack(spacing: CinemasInk.gap) { ForEach(0..<4, id: \.self) { _ in let size = wide ? sizes.wide : sizes.poster CinemasBlock().frame(width: size.width, height: size.height) } } .padding(.leading, CinemasInk.side) .frame(maxWidth: .infinity, alignment: .leading) .clipped() } .accessibilityHidden(true) }}/// A screen's empty line, in the core's words.struct CinemasLine: View { let line: String var body: some View { Text(line) .font(CinemasType.list) .foregroundStyle(CinemasInk.quiet) .multilineTextAlignment(.center) .frame(maxWidth: 420) .frame(maxWidth: .infinity) .padding(.horizontal, 32) .padding(.vertical, 48) }}/// The core's refusal in its own words, and Try again.struct CinemasRefused: View { let line: String let retry: () -> Void var body: some View { VStack(spacing: 16) { Text(line) .font(CinemasType.list) .foregroundStyle(CinemasInk.quiet) .multilineTextAlignment(.center) .frame(maxWidth: 420) CinemasButton(label: CinemasWords.line("stream.try_again"), style: .outline, action: retry) .frame(maxWidth: 220) .accessibilityIdentifier("cinemas-try-again") } .frame(maxWidth: .infinity) .padding(.horizontal, 32) .padding(.vertical, 48) .accessibilityElement(children: .contain) .accessibilityIdentifier("cinemas-refused") }}/// The design's buttons: amber for the act that leads, an outline for the second, plain for the rest; 46 high, 10/// corners, pressing in.struct CinemasButton: View { enum Style { case primary, outline, plain } let label: String var icon: String? var style: Style = .primary var disabled = false var height: CGFloat = CinemasInk.buttonHeight let action: () -> Void @Environment(\.dynamicTypeSize) private var typeSize var body: some View { Button(action: action) { HStack(spacing: 8) { if let icon { Image(systemName: icon).font(.system(size: 15, weight: .bold)) } // One line at the ordinary sizes; at the accessibility sizes the words wrap rather than shrink away. Text(label).font(height < 40 ? CinemasFonts.sans(13, 700) : CinemasType.button) .lineLimit(typeSize.isAccessibilitySize ? 3 : 1) .minimumScaleFactor(typeSize.isAccessibilitySize ? 1 : 0.7) .multilineTextAlignment(.center) } .padding(.vertical, typeSize.isAccessibilitySize ? 8 : 0) .foregroundStyle(disabled ? CinemasInk.faint : style == .primary ? CinemasInk.onAccent : CinemasInk.ink) .frame(maxWidth: .infinity, minHeight: height) .padding(.horizontal, 10) .background { let shape = RoundedRectangle(cornerRadius: CinemasInk.buttonCorner, style: .continuous) if disabled { shape.fill(CinemasInk.disabled) } else { switch style { case .primary: shape.fill(CinemasInk.accent) case .outline: shape.fill(Color.white.opacity(0.08)).overlay(shape.strokeBorder(Color.white.opacity(0.35), lineWidth: 1.5)) case .plain: shape.fill(Color.white.opacity(0.12)) } } } .contentShape(Rectangle()) } .buttonStyle(CinemasPress()) .disabled(disabled) }}/// A chip: normal, on (ink filled), or accent (amber).struct CinemasChip: View { enum Style { case normal, on, accent } let label: String var icon: String? var style: Style = .normal let action: () -> Void @Environment(\.dynamicTypeSize) private var typeSize var body: some View { Button(action: action) { HStack(spacing: 5) { if let icon { Image(systemName: icon).font(.system(size: 11, weight: .bold)) } Text(label).font(CinemasType.chip).lineLimit(typeSize.isAccessibilitySize ? 3 : 1) } .foregroundStyle(style == .on ? CinemasInk.ground : style == .accent ? CinemasInk.accent : CinemasInk.ink) .padding(.horizontal, 12) .frame(minHeight: 32) .background { switch style { case .normal: Capsule().fill(Color.white.opacity(0.07)).overlay(Capsule().strokeBorder(Color.white.opacity(0.14), lineWidth: 1)) case .on: Capsule().fill(CinemasInk.ink) case .accent: Capsule().fill(CinemasInk.accent.opacity(0.14)).overlay(Capsule().strokeBorder(CinemasInk.accent.opacity(0.4), lineWidth: 1)) } } .contentShape(Capsule()) } .buttonStyle(CinemasPress()) .accessibilityAddTraits(style == .on ? [.isButton, .isSelected] : .isButton) }}/// A round button over pictures: 38 across, black at 55% with a blur, in a 44 target.struct CinemasRoundButton: View { let icon: String let label: String let action: () -> Void var body: some View { Button(action: action) { Image(systemName: icon) .font(.system(size: 16, weight: .semibold)) .foregroundStyle(CinemasInk.ink) .frame(width: 38, height: 38) .background(Circle().fill(.ultraThinMaterial).overlay(Circle().fill(Color.black.opacity(0.55)))) .frame(width: 44, height: 44) .contentShape(Circle()) } .buttonStyle(CinemasPress()) .accessibilityLabel(Text(label)) }}/// A section's label: upper case, spaced, faint.struct CinemasLabel: View { let text: String var body: some View { Text(text.uppercased()).font(CinemasType.label).tracking(0.96).foregroundStyle(CinemasInk.faint).accessibilityAddTraits(.isHeader) }}extension CinemasModel { func deleteGroup(id: String, name: String) async { switch await source.deleteGroup(id) { case .read(let change): changed() let source = self.source var undo: (@Sendable () async -> Void)? if let change { undo = { @Sendable [weak self] in _ = await source.undo(change) await self?.changed() } } say(CinemasWords.line("stream.group_deleted", ["group": name]), undo: undo) case .refused(let line): UINotificationFeedbackGenerator().notificationOccurred(.warning) say(line) } }}#endif