// TIDY (assets/design/albums/albums.html "Tidy and Sets", "Bin"; DECISIONS "Tidy", "A Set", "Bin"): the Bin first,
// then what could be freed and the lists biggest first, each with its count and size; Sets with Keep on each row; a Set
// with its picks mounted and the rest faded; the other lists with Select and Bin all; the Bin with Bring back and the
// one Delete. Lists are read when he arrives or pulls, never under his finger; only the pass's lines move as it goes.
#if os(iOS)
import CottonKit
import SwiftUI
// MARK: - Tidy
struct AlbumsTidyScreen: View {
@Bindable var model: AlbumsModel
@State private var answer: AlbumsTidy?
var body: some View {
AlbumsPage(tag: "albums-tidy", pull: read) {
AlbumsTitle(text: answer?.title ?? AlbumsWords.line("tidy.title")).padding(.top, 8).padding(.bottom, 10)
AlbumsPassBand(lines: model.linesRead ? model.lines : answer?.progress)
if let answer {
if AlbumsBook.lookOnly {
AlbumsLookOnlyLine()
} else if let bin = answer.bin {
AlbumsBand(head: bin.line) {
Button(bin.act) { model.open(.bin(back: answer.title)) }
.buttonStyle(AlbumsPillStyle())
.accessibilityIdentifier("albums-tidy-bin")
}
}
if let free = answer.couldFree {
Text(verbatim: free).font(AlbumsType.bandHead).foregroundStyle(Olive.ink).padding(.top, 22).padding(.bottom, 4)
.accessibilityIdentifier("albums-could-free")
}
if let heading = answer.heading { AlbumsHeading(heading) }
ForEach(answer.lists) { list in
AlbumsListRow(cover: list.still == true ? nil : list.cover, title: list.name, line: list.count, end: list.size,
dim: list.still == true, tag: "albums-list-\(list.list)",
tap: list.still == true ? nil : { model.open(list.list == "sets" ? .sets : .list(key: list.list)) })
}
if let empty = answer.empty { AlbumsEmpty(title: empty.title, line: empty.line) }
}
}
.task(id: model.changed) { await read() }
.onChange(of: model.tab) { _, now in if now == .tidy { Task { await read() } } }
.onChange(of: model.stack) { _, now in if now.isEmpty, model.tab == .tidy { Task { await read() } } }
}
private func read() async {
do { answer = try await model.book.tidy() } catch { model.refused(error) }
}
}
// MARK: - Sets
struct AlbumsSetsScreen: View {
let model: AlbumsModel
@State private var answer: AlbumsSets?
@State private var rows: [AlbumsSets.Row] = []
@State private var loading = false
var body: some View {
VStack(alignment: .leading, spacing: 0) {
AlbumsBar(back: answer?.back ?? AlbumsWords.line("tidy.title"), tap: model.back)
AlbumsPage(tag: "albums-sets", pull: { await read(from: 0) }) {
AlbumsTitle(text: answer?.title ?? AlbumsWords.line("lists.sets"))
if let head = answer?.head {
Text(verbatim: head).font(AlbumsType.body).foregroundStyle(Olive.quiet).fixedSize(horizontal: false, vertical: true).padding(.top, 4)
}
Spacer().frame(height: 14)
AlbumsRule()
LazyVStack(spacing: 0) {
ForEach(rows) { row in
AlbumsSetRow(row: row, open: { model.open(.set(id: row.set)) }) {
Task {
do {
let done = try await model.book.keepSet(row.set, shown: row.strip, count: row.photos)
if done.changed != true { model.tell(done.after, undo: done.undo) }
await read(from: 0)
} catch { model.refused(error) }
}
}
.onAppear { if row == rows.last, let next = answer?.nextFrom { Task { await read(from: next) } } }
}
}
if let empty = answer?.empty { AlbumsEmpty(title: empty.title, line: empty.line) }
}
}
.task(id: model.changed) { await read(from: 0) }
}
private func read(from: Int) async {
guard !loading else { return }
loading = true
defer { loading = false }
do {
let page = try await model.book.sets(from: from)
answer = page
rows = from == 0 ? page.rows : rows + page.rows
} catch { model.refused(error) }
}
}
/// One Set as a row: its strip of shots, how many and when, the one question, Keep.
struct AlbumsSetRow: View {
let row: AlbumsSets.Row
let open: () -> Void
let keep: () -> Void
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Button(action: open) {
GeometryReader { geo in
let side = floor((geo.size.width - 6 * 2) / 7)
HStack(spacing: 2) {
ForEach(row.strip, id: \.self) { id in AlbumsTile(id: id, side: side) }
}
}
.frame(height: 48)
.clipShape(RoundedRectangle(cornerRadius: 4))
}
.buttonStyle(.plain)
// The strip opens the Set too; VoiceOver reaches it once, by the row's words below.
.accessibilityHidden(true)
HStack(alignment: .center, spacing: 12) {
Button(action: open) {
VStack(alignment: .leading, spacing: 2) {
(Text(verbatim: row.count).font(AlbumsType.sans(15, .bold, .subheadline)).foregroundColor(Olive.ink)
+ Text(verbatim: " ยท " + row.when).font(AlbumsType.line).foregroundColor(Olive.quiet))
Text(verbatim: row.ask).font(AlbumsType.line).foregroundStyle(Olive.ink)
}
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityElement(children: .combine)
.accessibilityIdentifier("albums-set-row-\(row.set)")
if !AlbumsBook.lookOnly {
Button(row.keep, action: keep)
.buttonStyle(AlbumsPillStyle())
.accessibilityLabel("\(row.keep), \(row.ask)")
.accessibilityIdentifier("albums-set-keep-\(row.set)")
}
}
}
.padding(.vertical, 12)
.overlay(alignment: .bottom) { AlbumsRule() }
}
}
// MARK: - A Set
struct AlbumsSetScreen: View {
let model: AlbumsModel
let id: String
@State private var answer: AlbumsSet?
@State private var keep: Set = []
@State private var done: AlbumsDone?
var body: some View {
VStack(alignment: .leading, spacing: 0) {
AlbumsBar(back: answer?.back ?? AlbumsWords.line("lists.sets"), tap: model.back) {
if let position = answer?.position { Text(verbatim: position).font(AlbumsType.body).foregroundStyle(Olive.quiet) }
}
if let answer {
if let done { after(answer, done) } else { choosing(answer) }
} else {
Spacer()
}
}
.accessibilityElement(children: .contain)
.accessibilityIdentifier("albums-set")
.task(id: id) { await read() }
}
private func fill(_ template: String) -> String {
let n = answer?.members.count ?? 0
return template.replacingOccurrences(of: "{keep}", with: keep.count.formatted()).replacingOccurrences(of: "{bin}", with: (n - keep.count).formatted())
}
@ViewBuilder private func choosing(_ answer: AlbumsSet) -> some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
AlbumsTitle(text: AlbumsBook.lookOnly
? AlbumsWords.line("look_only.would_keep", ["keep": keep.count.formatted(), "n": answer.members.count.formatted()])
: fill(answer.title))
if let when = answer.when { Text(verbatim: when).font(AlbumsType.body).foregroundStyle(Olive.quiet).padding(.top, 4) }
GeometryReader { geo in
let side = floor((geo.size.width - 4 * 2) / 5)
LazyVGrid(columns: Array(repeating: GridItem(.fixed(side), spacing: 2), count: 5), spacing: 2) {
ForEach(Array(answer.members.enumerated()), id: \.element.id) { at, member in
let kept = keep.contains(member.id)
AlbumsTile(id: member.id, side: side)
.overlay(alignment: .topLeading) { if kept { AlbumsCorner(side: side * 0.42) } }
.opacity(kept ? 1 : 0.38)
.contentShape(Rectangle())
.onTapGesture {
if AlbumsBook.lookOnly { model.open(.photo(id: member.id, back: answer.back)); return }
if kept { keep.remove(member.id) } else { keep.insert(member.id) }
}
.onLongPressGesture { model.open(.photo(id: member.id, back: fill(answer.title))) }
.accessibilityElement()
.accessibilityLabel(AlbumsWords.line("spoken.photo", ["n": (at + 1).formatted(), "total": answer.members.count.formatted()]))
.accessibilityValue(AlbumsBook.lookOnly ? (kept ? AlbumsWords.line("look_only.spoken_keeps") : "") : AlbumsWords.line(kept ? "spoken.keeps" : "spoken.to_bin"))
.accessibilityAddTraits(kept ? [.isButton, .isSelected] : .isButton)
.accessibilityAction(named: AlbumsWords.line("spoken.open")) { model.open(.photo(id: member.id, back: fill(answer.title))) }
.accessibilityIdentifier("albums-member-\(member.id)")
}
}
}
.frame(height: gridHeight(answer.members.count, columns: 5))
.padding(.top, 14)
Text(verbatim: answer.line).font(AlbumsType.line).foregroundStyle(Olive.quiet).fixedSize(horizontal: false, vertical: true).padding(.top, 14)
}
.padding(.horizontal, Olive.side)
}
.scrollIndicators(.hidden)
if AlbumsBook.lookOnly {
AlbumsLookOnlyLine().padding(.horizontal, Olive.side).padding(.bottom, 8)
} else {
VStack(spacing: 10) {
Button(fill(answer.button)) { Task { await keepNow(Array(keep)) } }
.buttonStyle(AlbumsMainButtonStyle()).disabled(keep.isEmpty)
.accessibilityIdentifier("albums-set-keep")
Button(answer.keepAll) { Task { await keepNow(answer.members.map(\.id)) } }
.buttonStyle(AlbumsOutlineButtonStyle(quiet: true))
.accessibilityIdentifier("albums-set-keep-all")
}
.padding(.horizontal, Olive.side).padding(.vertical, 12)
}
}
@ViewBuilder private func after(_ answer: AlbumsSet, _ done: AlbumsDone) -> some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
AlbumsTitle(text: done.title ?? "")
if let when = answer.when { Text(verbatim: when).font(AlbumsType.body).foregroundStyle(Olive.quiet).padding(.top, 4) }
AlbumsBand(head: done.after, lines: [done.afterLine].compactMap { $0 }) {
if let undo = done.undo {
Button {
Task {
await model.undo(undo)
self.done = nil
await read()
}
} label: {
Label(undo.label, systemImage: "arrow.uturn.backward")
}
.buttonStyle(AlbumsActStyle())
.accessibilityIdentifier("albums-set-undo")
}
}
.padding(.top, 14)
GeometryReader { geo in
let side = floor((geo.size.width - 2 * 2) / 3)
LazyVGrid(columns: Array(repeating: GridItem(.fixed(side), spacing: 2), count: 3), spacing: 2) {
ForEach(done.kept ?? [], id: \.self) { id in AlbumsTile(id: id, side: side) }
}
}
.frame(height: gridHeight(done.kept?.count ?? 0, columns: 3))
.padding(.top, 14)
if let stays = done.stays { Text(verbatim: stays).font(AlbumsType.line).foregroundStyle(Olive.quiet).padding(.top, 12) }
}
.padding(.horizontal, Olive.side)
}
.scrollIndicators(.hidden)
if let next = done.next {
Button(next.label) {
if let set = next.set { model.replace(.set(id: set)) } else { model.back() }
}
.buttonStyle(AlbumsMainButtonStyle())
.padding(.horizontal, Olive.side).padding(.vertical, 12)
.accessibilityIdentifier("albums-set-next")
}
}
private func gridHeight(_ count: Int, columns: Int) -> CGFloat {
let width = UIScreen.main.bounds.width - Olive.side * 2
let side = floor((width - CGFloat(columns - 1) * 2) / CGFloat(columns))
let rows = (count + columns - 1) / columns
return CGFloat(rows) * side + CGFloat(max(0, rows - 1)) * 2
}
private func read() async {
do {
let set = try await model.book.set(id)
if set.gone == true { model.back(); return }
answer = set
keep = Set(set.members.filter(\.keep).map(\.id))
} catch { model.refused(error) }
}
private func keepNow(_ ids: [String]) async {
let members = answer?.members.map(\.id) ?? []
do {
let answer = try await model.book.keepSet(id, keep: ids, shown: members, count: members.count)
if answer.changed == true { await read() } else { done = answer }
} catch { model.refused(error) }
}
}
/// A kept shot's photo corner: the print's paper folded over its top left.
struct AlbumsCorner: View {
let side: CGFloat
var body: some View {
Path { p in
p.move(to: .zero); p.addLine(to: CGPoint(x: side, y: 0)); p.addLine(to: CGPoint(x: 0, y: side)); p.closeSubpath()
}
.fill(Olive.print.opacity(0.92))
.frame(width: side, height: side)
.accessibilityHidden(true)
}
}
// MARK: - Another list
struct AlbumsListPage: View {
@Environment(\.dynamicTypeSize) private var size
let model: AlbumsModel
let key: String
@State private var answer: AlbumsList?
@State private var selecting = false
@State private var chosen: Set = []
var body: some View {
VStack(alignment: .leading, spacing: 0) {
AlbumsBar(back: answer?.back ?? AlbumsWords.line("tidy.title"), tap: model.back) {
if let answer, !answer.groups.isEmpty, !AlbumsBook.lookOnly {
Button(selecting ? answer.done : answer.select) { selecting.toggle(); chosen = [] }
.buttonStyle(AlbumsActStyle())
.accessibilityIdentifier("albums-list-select")
}
}
if let answer {
AlbumsPage(tag: "albums-list", pull: read) {
AlbumsTitle(text: answer.title)
if let head = answer.head { Text(verbatim: head).font(AlbumsType.body).foregroundStyle(Olive.quiet).padding(.top, 4) }
ForEach(answer.groups) { group in
if let heading = group.heading { AlbumsHeading(heading) } else { Spacer().frame(height: 14) }
if group.items.contains(where: { $0.original != nil }) {
ForEach(group.items) { item in pair(item) }
} else {
grid(group.items)
}
}
if let empty = answer.empty { AlbumsEmpty(title: empty.title, line: empty.line) }
}
foot(answer)
} else {
Spacer()
}
}
.accessibilityElement(children: .contain)
.accessibilityIdentifier("albums-list-\(key)")
.task(id: model.changed) { await read() }
}
@ViewBuilder private func grid(_ items: [AlbumsList.Item]) -> some View {
let width = UIScreen.main.bounds.width - Olive.side * 2
let side = floor((width - 2 * 2) / 3)
LazyVGrid(columns: Array(repeating: GridItem(.fixed(side), spacing: 2), count: 3), spacing: 2) {
ForEach(items) { item in tile(item, side: side) }
}
}
/// A WhatsApp copy beside the camera original it copies.
@ViewBuilder private func pair(_ item: AlbumsList.Item) -> some View {
HStack(spacing: 10) {
tile(item, side: 96)
if let original = item.original {
VStack(alignment: .leading, spacing: 4) {
AlbumsTile(id: original, side: 64).clipShape(RoundedRectangle(cornerRadius: 6))
Text(verbatim: AlbumsWords.line("list.original")).font(AlbumsType.small).foregroundStyle(Olive.quiet)
}
}
Spacer()
VStack(alignment: .trailing, spacing: 2) {
if let size = item.size { Text(verbatim: size).font(AlbumsType.figure).foregroundStyle(Olive.ink) }
if let when = item.when { Text(verbatim: when).font(AlbumsType.line).foregroundStyle(Olive.quiet) }
}
}
.padding(.vertical, 8)
.overlay(alignment: .bottom) { AlbumsRule() }
}
@ViewBuilder private func tile(_ item: AlbumsList.Item, side: CGFloat) -> some View {
let on = chosen.contains(item.id)
AlbumsTile(id: item.id, side: side, duration: item.duration)
.overlay(alignment: .topTrailing) {
if selecting {
Image(systemName: on ? "checkmark.circle.fill" : "circle")
.font(.system(size: 22)).foregroundStyle(on ? Olive.accent : .white)
.background(Circle().fill(on ? Olive.onAccent : .black.opacity(0.2)).padding(2))
.padding(6)
}
}
.overlay(alignment: .bottomLeading) {
if !selecting, item.duration == nil, let size = item.size {
Text(verbatim: size).font(AlbumsType.mono(11, .caption2, upTo: 13)).foregroundStyle(.white)
.padding(.horizontal, 4).padding(.vertical, 2).background(Color.black.opacity(0.35)).padding(4)
}
}
.contentShape(Rectangle())
.onTapGesture {
if selecting { if on { chosen.remove(item.id) } else { chosen.insert(item.id) } } else { model.open(.photo(id: item.id, back: answer?.title ?? "")) }
}
.accessibilityElement()
.accessibilityLabel([item.when, item.size, item.duration].compactMap { $0 }.joined(separator: ", "))
.accessibilityAddTraits(selecting && on ? [.isButton, .isSelected] : .isButton)
.accessibilityIdentifier("albums-item-\(item.id)")
}
@ViewBuilder private func foot(_ answer: AlbumsList) -> some View {
if AlbumsBook.lookOnly {
EmptyView()
} else if selecting {
// Side by side; stacked at the largest sizes.
(size.isAccessibilitySize ? AnyLayout(VStackLayout(spacing: 10)) : AnyLayout(HStackLayout(spacing: 10))) {
Button(answer.binSelected.replacingOccurrences(of: "{n}", with: chosen.count.formatted())) {
let ids = Array(chosen)
selecting = false; chosen = []
Task { await model.moveToBin(ids: ids, shown: shownIDs) }
}
.buttonStyle(AlbumsMainButtonStyle()).disabled(chosen.isEmpty)
.accessibilityIdentifier("albums-list-bin")
if let keep = answer.keepSelected {
Button(keep.replacingOccurrences(of: "{n}", with: chosen.count.formatted())) {
let ids = Array(chosen)
selecting = false; chosen = []
Task { await model.keep(ids: ids, shown: shownIDs) }
}
.buttonStyle(AlbumsOutlineButtonStyle()).disabled(chosen.isEmpty)
.accessibilityIdentifier("albums-list-keep")
}
}
.padding(.horizontal, Olive.side).padding(.vertical, 12)
} else if let all = answer.binAll {
Button(all.label) {
model.sheet = .ask(AlbumsAsk(question: all.confirm.question, line: all.confirm.line, yes: all.confirm.yes, no: all.confirm.no, tag: "bin-all") {
let shown = shownIDs
Task { await model.moveToBin(list: key, shown: shown) }
})
}
.buttonStyle(AlbumsOutlineButtonStyle())
.padding(.horizontal, Olive.side).padding(.vertical, 12)
.accessibilityIdentifier("albums-list-bin-all")
}
}
/// Every id this list showed: what an act on it carries.
private var shownIDs: [String] { answer?.groups.flatMap { $0.items.map(\.id) } ?? [] }
private func read() async {
do { answer = try await model.book.list(key) } catch { model.refused(error) }
}
}
// MARK: - The Bin
struct AlbumsBinScreen: View {
let model: AlbumsModel
let back: String
@State private var answer: AlbumsBin?
var body: some View {
VStack(alignment: .leading, spacing: 0) {
AlbumsBar(back: back, tap: model.back)
if let answer {
AlbumsPage(tag: "albums-bin-page", pull: read) {
AlbumsTitle(text: answer.title)
if let head = answer.head {
Text(verbatim: head).font(AlbumsType.body).foregroundStyle(Olive.quiet).fixedSize(horizontal: false, vertical: true).padding(.top, 4)
}
ForEach(answer.groups) { group in
AlbumsHeading(text: group.heading) {
Button(group.bringBack) { Task { await model.bringBack(group.ids, shown: group.ids) } }
.buttonStyle(AlbumsActStyle())
.accessibilityIdentifier("albums-bring-back")
}
strip(group.ids)
}
if let note = answer.note, answer.act != nil {
Text(verbatim: note).font(AlbumsType.line).foregroundStyle(Olive.quiet).fixedSize(horizontal: false, vertical: true).padding(.top, 18)
}
if let empty = answer.empty { AlbumsEmpty(title: empty.title, line: empty.line) }
}
if let act = answer.act {
Button(act.label) {
model.sheet = .ask(AlbumsAsk(question: act.question, line: act.line + "\n\n" + act.noWayBack, yes: act.yes, no: act.no, tag: "delete") {
let shown = answer.groups.flatMap(\.ids)
Task { await model.deleteBin(shown: shown) }
})
}
.buttonStyle(AlbumsMainButtonStyle())
.padding(.horizontal, Olive.side).padding(.vertical, 12)
.accessibilityIdentifier("albums-bin-delete")
}
} else {
Spacer()
}
}
.accessibilityElement(children: .contain)
.accessibilityIdentifier("albums-bin")
.task(id: model.changed) { await read() }
}
/// Up to two rows of five; the last says how many more.
@ViewBuilder private func strip(_ ids: [String]) -> some View {
let width = UIScreen.main.bounds.width - Olive.side * 2
let side = floor((width - 4 * 2) / 5)
let shown = Array(ids.prefix(10))
LazyVGrid(columns: Array(repeating: GridItem(.fixed(side), spacing: 2), count: 5), spacing: 2) {
ForEach(Array(shown.enumerated()), id: \.element) { at, id in
AlbumsTile(id: id, side: side)
.overlay {
if at == shown.count - 1, ids.count > shown.count {
ZStack {
Color.black.opacity(0.45)
Text(verbatim: "+\((ids.count - shown.count + 1).formatted())").font(AlbumsType.sans(15, .bold)).foregroundStyle(.white)
}
}
}
}
}
}
private func read() async {
do { answer = try await model.book.bin() } catch { model.refused(error) }
}
}
#endif