568 lines · 23.4 KB · SwiftRaw
// A character (wireframes 5), Places and a place (wireframes 14, 15): the// pages behind a face on the home and behind the Places pill. Each is the// core's answer drawn plain: the face and the name, how much of the script// is theirs, their pictures, notes written in place (kept a moment after the// last key and on leaving), all their lines, the scenes they are in, and the// story days they are in with a picture row for each. ⋯ renames everywhere// or merges with another. Same words and order as the Android twin// (`apps/android/.../ink/PeopleAndDrafts.kt`, `Places.kt`).#if os(iOS)import CottonKitimport SwiftUI/// A page under the home: ‹ at the top left, then its content scrolling.struct InkPageFrame<Content: View>: View { var title: String = "" /// ⋯ at the top right, when the page has more to do. var more: (() -> Void)? = nil @ViewBuilder let content: () -> Content @Environment(\.dismiss) private var dismiss var body: some View { VStack(alignment: .leading, spacing: 0) { HStack { InkGlyph(symbol: "chevron.left", label: InkWords.line("writer.back")) { dismiss() } Spacer() if let more { InkGlyph(symbol: "ellipsis", label: InkWords.line("home.more"), action: more) } } .padding(.horizontal, 8) .padding(.top, 6) ScrollView { VStack(alignment: .leading, spacing: 20) { // A page whose name stands by its face draws no title line. if !title.isEmpty { Text(title) .font(InkType.title(34, relativeTo: .largeTitle)) .foregroundStyle(Ink.ink) .fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.isHeader) } content() } .padding(.horizontal, Ink.gutter) .padding(.top, 8) .padding(.bottom, 32) .frame(maxWidth: .infinity, alignment: .leading) } } .inkGround() .toolbar(.hidden, for: .navigationBar) }}/// A row on a page: a title over a quiet line, a dotted rule under it.struct InkLinkRow: View { let title: String let line: String? /// A short quiet count on the right, `8 lines`. var detail: String? = nil var last = false let open: () -> Void var body: some View { VStack(spacing: 0) { Button(action: open) { HStack(alignment: .firstTextBaseline, spacing: 8) { VStack(alignment: .leading, spacing: 4) { Text(title) .font(InkType.sceneTitle) .foregroundStyle(Ink.ink) .fixedSize(horizontal: false, vertical: true) if let line, !line.isEmpty { Text(line) .font(InkType.count) .foregroundStyle(Ink.quiet) .lineLimit(2) } } .frame(maxWidth: .infinity, alignment: .leading) .layoutPriority(1) if let detail, !detail.isEmpty { Text(detail) .font(InkType.sceneLength) .foregroundStyle(Ink.quiet) .multilineTextAlignment(.trailing) } } .padding(.vertical, 13) .contentShape(Rectangle()) } .buttonStyle(InkPress()) .accessibilityElement(children: .combine) if !last { InkRule() } } }}/// Notes on a character or a place, written in place and kept 0.7 s after/// the last change (and on leaving), only when they changed.struct InkNotes: View { let hint: String let start: String let keep: (String) async -> Void @State private var text = "" @State private var kept = "" @State private var loaded = false @FocusState private var focused: Bool var body: some View { TextField(hint, text: $text, axis: .vertical) .font(InkType.line) .foregroundStyle(Ink.ink) .tint(Ink.blue) .lineLimit(3...) .focused($focused) .accessibilityLabel(hint) .padding(.bottom, 8) .overlay(alignment: .bottom) { InkRule() } .onAppear { guard !loaded else { return } text = start kept = start loaded = true } .task(id: text) { guard loaded, text != kept else { return } try? await Task.sleep(for: .milliseconds(700)) guard !Task.isCancelled else { return } await save() } .onDisappear { Task { await save() } } } private func save() async { let now = text.trimmingCharacters(in: .whitespacesAndNewlines) guard loaded, now != kept.trimmingCharacters(in: .whitespacesAndNewlines) else { return } kept = text await keep(now) }}/// Wireframes' name sheet: a title, an empty field, Done once something is written.struct InkNameSheet: View { let title: String let hint: String let done: (String) -> Void @State private var text = "" @FocusState private var focused: Bool @Environment(\.closeCard) private var closeCard /// Closes the panel this stands in. private func dismiss() { closeCard?() } var body: some View { let named = text.trimmingCharacters(in: .whitespacesAndNewlines) VStack(alignment: .leading, spacing: 18) { InkSheetTop(title: title, close: InkWords.line("home.close")) { dismiss() } InkField(hint: hint, text: $text, focused: $focused, font: InkType.title(22, relativeTo: .title2)) { finish(named) } HStack { Spacer() InkButton(words: InkWords.line("ink.done")) { finish(named) } .disabled(named.isEmpty) .opacity(named.isEmpty ? 0.4 : 1) } Spacer(minLength: 0) } .padding(.horizontal, Ink.gutter) .padding(.top, 22) .onAppear { focused = true } } private func finish(_ named: String) { guard !named.isEmpty else { return } dismiss() done(named) }}/// A page's section caption, `Their scenes`, in the core's own case.struct InkCaption: View { let words: String var body: some View { Text(words) .font(InkType.caption) .foregroundStyle(Ink.quiet) .fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.isHeader) }}/// The scenes a character or a place is in, as the home lists them: "n ·/// title", then a quiet line (the scene's own, or the page's for it), and a/// count on the right.struct InkSceneLinks: View { let story: InkStory let caption: String let ids: [String] /// The quiet line under a scene, by id; the scene's own line when nil. var line: ((String) -> String?)? = nil /// The count on the right, by id. var detail: (String) -> String? = { _ in nil } var body: some View { let rows = ids.compactMap { id in story.home?.scenes.first { $0.id == id } } if !rows.isEmpty { VStack(alignment: .leading, spacing: 4) { InkCaption(words: caption) ForEach(Array(rows.enumerated()), id: \.element.id) { at, row in InkLinkRow(title: [row.number.map(String.init), row.title].compactMap { $0 }.joined(separator: " · "), line: line.map { $0(row.id) } ?? (row.line ?? row.question), detail: detail(row.id), last: at == rows.count - 1) { story.path.append(.scene(row.id, fresh: false)) } } } } }}/// How much a character says, and where: "12 lines · in 3 scenes".enum InkCounting { static func lines(_ n: Int) -> String { switch n { case 0: InkWords.line("character.speaks.zero") case 1: InkWords.line("character.speaks.one") default: InkWords.line("character.speaks.other", ["n": String(n)]) } } static func inScenes(_ n: Int) -> String { InkWords.line(n == 1 ? "character.in_scenes.one" : "character.in_scenes.other", ["n": String(n)]) } static func scenes(_ n: Int) -> String { InkWords.line(n == 1 ? "length.scenes.one" : "length.scenes.other", ["n": String(n)]) }}// MARK: - A characterstruct InkCharacterScreen: View { let story: InkStory let id: String @State private var person: StoryPerson? @State private var desk: InkPictureDesk @State private var menu = false @State private var renaming = false @State private var merging = false init(story: InkStory, id: String) { self.story = story self.id = id _desk = State(initialValue: InkPictureDesk(story: story)) } var body: some View { InkPageFrame(more: person == nil ? nil : { menu = true }) { if let person { HStack(alignment: .center, spacing: 16) { InkPictureFace(initial: person.initial ?? String(person.name.prefix(1)), picture: person.picture, size: 70) VStack(alignment: .leading, spacing: 4) { Text(person.name) .font(InkType.title(34, relativeTo: .largeTitle)) .foregroundStyle(Ink.ink) .fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.isHeader) Text(InkWords.line("ink.character_line", ["lines": InkCounting.lines(person.lines), "scenes": InkCounting.inScenes(person.scenes)])) .font(InkType.count) .foregroundStyle(Ink.quiet) .fixedSize(horizontal: false, vertical: true) } } .accessibilityElement(children: .combine) InkPictureRow(desk: desk, owner: InkPictureOwner(character: id), pictures: (person.pictures ?? []).filter { $0.day == nil }, name: person.name) InkNotes(hint: InkWords.line("ink.who_hint"), start: person.about ?? "") { about in await story.attempt { try await story.shelf.writeAbout(table: StoryTable.character, id: id, about: about) } } if person.lines > 0 { InkViewRow(words: InkWords.line("ink.all_lines"), detail: nil) { story.path.append(.lines(id)) } } InkSceneLinks(story: story, caption: InkWords.line("ink.their_scenes"), ids: person.sceneIds) { scene in person.linesIn?[scene] } if let days = person.days, !days.isEmpty { VStack(alignment: .leading, spacing: 12) { InkCaption(words: InkWords.line("ink.by_day")) ForEach(days) { day in let pictures = (person.pictures ?? []).filter { $0.day == day.day } VStack(alignment: .leading, spacing: 8) { Text(day.label) .font(InkType.sceneTitle) .foregroundStyle(Ink.ink) .accessibilityAddTraits(.isHeader) if pictures.isEmpty { Text(InkWords.line("ink.day_hint")) .font(InkType.count) .foregroundStyle(Ink.quiet) .fixedSize(horizontal: false, vertical: true) } InkPictureRow(desk: desk, owner: InkPictureOwner(character: id, day: day.day), pictures: pictures, name: person.name) } } } } } } .inkPictures(desk) .lookPanel(isPresented: $menu, style: Ink.panel) { InkActsSheet(title: person?.name ?? "", acts: [ InkAct(words: InkWords.line("ink.rename")) { menu = false; renaming = true }, InkAct(words: InkWords.line("ink.merge_character")) { menu = false; merging = true }, ]) } .lookPanel(isPresented: $renaming, style: Ink.panel) { InkNameSheet(title: InkWords.line("ink.rename"), hint: InkWords.line("ink.rename_hint")) { name in Task { await story.attempt { try await story.shelf.rename(table: StoryTable.character, id: id, name: name) } await load() await story.reloadHome() } } } .lookPanel(isPresented: $merging, style: Ink.panel, tall: true) { InkActsSheet(title: InkWords.line("ink.merge_title"), acts: others.map { other in InkAct(words: other.name) { merge(into: other.id) } }, line: InkWords.line("ink.merge_character_line", ["name": person?.name ?? ""])) } .task { desk.changed = { await load() } await load() } } /// The rest of the cast, to merge this one into. private var others: [StoryScenesAnswer.Person] { (story.home?.cast ?? []).filter { $0.id != id } } private func merge(into other: String) { merging = false Task { await story.attempt { try await story.shelf.merge(table: StoryTable.character, id: id, into: other) // This one is gone: the page becomes the one it was merged into. if !story.path.isEmpty { story.path.removeLast() } story.path.append(.character(other)) } await story.reloadHome() } } private func load() async { await story.attempt { person = try await story.shelf.person(id) } }}// MARK: - All their lines/// Everything one character says, in order: "n · scene", the direction/// quiet, then the words. A tap opens that scene.struct InkLinesScreen: View { let story: InkStory let id: String @State private var said: StoryPersonLines? var body: some View { InkPageFrame(title: said?.name ?? "") { if let said { VStack(spacing: 0) { ForEach(Array(said.lines.enumerated()), id: \.element.id) { at, line in Button { story.path.append(.scene(line.scene, fresh: false)) } label: { VStack(alignment: .leading, spacing: 4) { Text(String(line.number) + " · " + line.title) .font(InkType.sceneLength) .foregroundStyle(Ink.quiet) .fixedSize(horizontal: false, vertical: true) if let aside = line.aside, !aside.isEmpty { Text("(" + aside + ")") .font(InkType.direction) .foregroundStyle(Ink.quiet) .fixedSize(horizontal: false, vertical: true) } Text(line.text) .font(InkType.line) .foregroundStyle(Ink.ink) .fixedSize(horizontal: false, vertical: true) } .frame(maxWidth: .infinity, alignment: .leading) .padding(.vertical, 12) .contentShape(Rectangle()) } .buttonStyle(InkPress()) .accessibilityElement(children: .combine) if at < said.lines.count - 1 { InkRule() } } } } } .task { await story.attempt { said = try await story.shelf.lines(of: id) } } }}// MARK: - Places/// A place's face in the list: its picture in a pen circle, else a thin "+".struct InkPlaceFace: View { let place: StoryPlace var size: CGFloat = 44 var body: some View { if let picture = place.picture, !picture.isEmpty { InkPictureFace(initial: String(place.name.prefix(1)), picture: picture, size: size) } else { Image(systemName: "plus") .font(.system(size: size * 0.36, weight: .light)) .foregroundStyle(Ink.quiet) .frame(width: size, height: size) .overlay(Circle().strokeBorder(Ink.rule, lineWidth: 1)) } }}struct InkPlacesScreen: View { let story: InkStory let script: String let title: String @State private var places: [StoryPlace] = [] var body: some View { InkPageFrame(title: title) { VStack(spacing: 0) { ForEach(Array(places.enumerated()), id: \.element.id) { at, place in Button { story.path.append(.place(place.id)) } label: { HStack(alignment: .center, spacing: 14) { InkPlaceFace(place: place) VStack(alignment: .leading, spacing: 4) { Text(place.name) .font(InkType.sceneTitle) .foregroundStyle(Ink.ink) .fixedSize(horizontal: false, vertical: true) Text(place.line ?? InkCounting.scenes(place.scenes)) .font(InkType.count) .foregroundStyle(Ink.quiet) .fixedSize(horizontal: false, vertical: true) } .frame(maxWidth: .infinity, alignment: .leading) } .padding(.vertical, 12) .contentShape(Rectangle()) } .buttonStyle(InkPress()) .accessibilityElement(children: .combine) .accessibilityAddTraits(.isButton) if at < places.count - 1 { InkRule() } } } } .task(id: story.home) { await story.attempt { places = try await story.shelf.places(story: script) } } }}struct InkPlaceScreen: View { let story: InkStory let id: String @State private var place: StoryPlace? @State private var desk: InkPictureDesk @State private var menu = false @State private var renaming = false @State private var merging = false /// The script's other places, to merge into. @State private var others: [StoryPlace] = [] init(story: InkStory, id: String) { self.story = story self.id = id _desk = State(initialValue: InkPictureDesk(story: story)) } var body: some View { InkPageFrame(more: place == nil ? nil : { menu = true }) { if let place { VStack(alignment: .leading, spacing: 4) { Text(place.name) .font(InkType.title(34, relativeTo: .largeTitle)) .foregroundStyle(Ink.ink) .fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.isHeader) Text(place.line ?? InkCounting.scenes(place.scenes)) .font(InkType.count) .foregroundStyle(Ink.quiet) .fixedSize(horizontal: false, vertical: true) } .accessibilityElement(children: .combine) InkPictureRow(desk: desk, owner: InkPictureOwner(place: id), pictures: (place.pictures ?? []).filter { $0.day == nil }, name: place.name) InkNotes(hint: InkWords.line("ink.place_hint"), start: place.about ?? "") { about in await story.attempt { try await story.shelf.writeAbout(table: StoryTable.place, id: id, about: about) } } if let times = place.times, !times.isEmpty { VStack(alignment: .leading, spacing: 10) { InkCaption(words: InkWords.line("ink.when_here")) InkFlow(spacing: 8) { ForEach(times, id: \.time) { time in InkChip(on: false) { Text(time.time + " · " + String(time.scenes)) } .accessibilityElement(children: .ignore) .accessibilityLabel(time.time + ", " + InkCounting.scenes(time.scenes)) } } } } InkSceneLinks(story: story, caption: InkWords.line("ink.scenes_here"), ids: place.sceneIds, line: { scene in place.people?[scene] ?? "" }) } } .inkPictures(desk) .lookPanel(isPresented: $menu, style: Ink.panel) { InkActsSheet(title: place?.name ?? "", acts: [ InkAct(words: InkWords.line("ink.rename")) { menu = false; renaming = true }, InkAct(words: InkWords.line("ink.merge_place")) { menu = false Task { if let story = story.story { others = ((try? await self.story.shelf.places(story: story)) ?? []).filter { $0.id != id } } merging = true } }, ]) } .lookPanel(isPresented: $renaming, style: Ink.panel) { InkNameSheet(title: InkWords.line("ink.rename"), hint: InkWords.line("heading.place_hint")) { name in Task { await story.attempt { try await story.shelf.rename(table: StoryTable.place, id: id, name: name) } await load() await story.reloadHome() } } } .lookPanel(isPresented: $merging, style: Ink.panel, tall: true) { InkActsSheet(title: InkWords.line("ink.merge_title"), acts: others.map { other in InkAct(words: other.name, detail: other.line) { merge(into: other.id) } }, line: InkWords.line("ink.merge_place_line", ["name": place?.name ?? ""])) } .task { desk.changed = { await load() } await load() } } private func merge(into other: String) { merging = false Task { await story.attempt { try await story.shelf.merge(table: StoryTable.place, id: id, into: other) if !story.path.isEmpty { story.path.removeLast() } story.path.append(.place(other)) } await story.reloadHome() } } private func load() async { await story.attempt { place = try await story.shelf.place(id) } }}#endif