// SEARCH, LATELY AND ASSISTANTS (data.html; Android's `Search.kt`): one field over everything, grouped by table, with
// the line that says exactly what was searched; every write by day, by who; what each assistant read and wrote.
#if os(iOS)
import CottonKit
import SwiftUI
/// SEARCH (data.html "Search across everything"; `data.search`): the core's chips (Everything, Rows, Table names,
/// Column names), what was found grouped by table, and the last line saying what was searched. Nothing found says so in
/// the core's words and offers its next try.
struct DataSearchScreen: View {
let model: DataModel
@State private var q = ""
@State private var within = "everything"
@State private var read: DataRead = .reading
@State private var reads = ReadInTurn()
@State private var searched: String?
var body: some View {
DataPage(tag: "data-search") {
Spacer().frame(height: 8)
DataTitle(text: read.answer?.title ?? DataWords.line("search.title"))
DataSearchBox(text: $q, placeholder: read.answer?.placeholder ?? DataWords.line("search.placeholder"), tag: "data-search-field")
if let a = read.answer {
if let chips = a.chips, !chips.isEmpty {
DataChips {
ForEach(chips) { chip in
DataChip(label: chip.title, on: chip.id == within, tag: "data-search-chip-\(chip.id)") { within = chip.id }
}
}
}
if let found = a.foundLine { DataNote(text: found).padding(.top, -2) }
let rows = within == "everything" || within == "rows"
let tables = within == "everything" || within == "tables"
let columns = within == "everything" || within == "columns"
if rows {
ForEach(Array(a.groups.enumerated()), id: \.offset) { _, g in
HStack(spacing: 8) {
if let title = g.title, title != g.table {
Text(verbatim: title).font(DataType.sans(16, .semibold)).foregroundStyle(Stone.ink).fixedSize(horizontal: false, vertical: true)
}
DataTableTag(name: g.table)
Spacer(minLength: 0)
Text(verbatim: String(g.rows.count)).font(DataType.line).foregroundStyle(Stone.quiet)
}
.padding(.top, 18).padding(.bottom, 4)
.accessibilityElement(children: .combine)
.accessibilityAddTraits(.isHeader)
DataRule()
ForEach(Array(g.rows.enumerated()), id: \.offset) { at, r in
DataListRow(title: r.title, line: r.line, chevron: true, last: at == g.rows.count - 1, tag: "data-search-row-\(r.id)") {
model.open(.row(table: r.table ?? g.table, id: r.id, trail: []))
}
}
}
}
if tables, !a.tables.isEmpty {
DataHeading(text: DataWords.line("search.tables"), end: String(a.tables.count))
ForEach(Array(a.tables.enumerated()), id: \.offset) { at, t in
DataListRow(title: t.table, line: t.line, monoTitle: true, chevron: true, last: at == a.tables.count - 1,
tag: "data-search-table-\(t.table)") { model.open(.rows(t.table)) }
}
}
if columns, !a.columns.isEmpty {
DataHeading(text: DataWords.line("search.columns"), end: String(a.columns.count))
ForEach(Array(a.columns.enumerated()), id: \.offset) { at, c in
DataListRow(title: c.column, monoTitle: true, chevron: true, last: at == a.columns.count - 1, label: "\(c.column), \(c.table)",
tap: { model.open(.columns(c.table)) }) {
DataTableTag(name: c.table)
}
}
}
if let nothing = a.nothing {
Text(verbatim: nothing.title).font(DataType.sans(21, .bold, .title3)).foregroundStyle(Stone.ink)
.fixedSize(horizontal: false, vertical: true).padding(.top, 28).padding(.bottom, 8)
DataRule()
if let names = nothing.names {
DataPlainRow(left: names, chevron: true, tag: "data-search-names") { within = "tables" }
}
}
if let searched = a.searchedLine { DataNote(text: searched).padding(.top, 2) }
} else if let words = read.refusal {
DataNote(text: words)
}
}
.task(id: "\(q)/\(within)/\(model.revision)") {
if !q.isEmpty { try? await Task.sleep(for: .milliseconds(220)) }
guard !Task.isCancelled else { return }
let asked = q.trimmingCharacters(in: .whitespaces)
if "\(asked)/\(within)" != searched { searched = "\(asked)/\(within)"; await find(asked) } else { reads.ask { await find(asked) } }
}
}
private func find(_ asked: String) async {
let next = await DataRead.of { try await model.calls.search(asked, in: within) }
if next.answer != nil || read.answer == nil { read = next }
}
}
/// LATELY (data.html "What changed lately"; `data.lately`): every write, by day, each a sentence of who did what to
/// which table, the core's chips (All, You, Apps, Assistants), Show more for older days (`before`), and the foot on how
/// long changes are kept. Offline, the core's own line says why it shows nothing.
struct DataLatelyScreen: View {
let model: DataModel
@State private var who = "all"
@State private var answer: DataLately?
@State private var reads = ReadInTurn()
@State private var shownWho: String?
@State private var days: [DataLately.Day] = []
@State private var refused: String?
@State private var loading = false
var body: some View {
DataPage(tag: "data-lately") {
Spacer().frame(height: 8)
DataTitle(text: answer?.title ?? DataWords.line("lately.title"))
DataLead(text: answer?.lead ?? DataWords.line("lately.lead"))
if let chips = answer?.chips, !chips.isEmpty {
DataChips {
ForEach(chips) { chip in
DataChip(label: chip.title, on: chip.id == who, tag: "data-lately-chip-\(chip.id)") { who = chip.id }
}
}
}
if let answer, !(refused != nil && days.isEmpty) {
// In this phone's first sync, writes still coming are said as coming, above whatever has come.
if let first = answer.firstSync { DataFirstSyncStrip(first: first) }
ForEach(Array(days.enumerated()), id: \.offset) { _, day in
DataHeading(text: day.title, end: day.countLine)
ForEach(Array(day.items.enumerated()), id: \.offset) { at, item in
DataListRow(title: item.line, end: item.when, monoEnd: false, last: at == day.items.count - 1, tag: "data-lately-item")
}
}
if answer.more {
Button { Task { await page(answer.before) } } label: { Text(verbatim: DataWords.line("rows.more")) }
.buttonStyle(DataButtonStyle())
.disabled(loading)
.padding(.top, 14)
.accessibilityIdentifier("data-lately-more")
}
if let refused { DataNote(text: refused) }
DataNote(text: answer.foot).padding(.top, 2)
} else if let refused {
DataNotice(mark: .cloud, title: refused, tag: "data-lately-offline")
}
}
.task(id: "\(who)/\(model.revision)") {
if who != shownWho { shownWho = who; await page(nil) } else { reads.ask { await page(nil) } }
}
}
private func page(_ before: JSONValue?) async {
loading = true
defer { loading = false }
do {
let a = try await model.calls.lately(who: who, before: before)
refused = nil
if before == nil { days = [] }
// A day that runs across two pages is one day.
for d in a.days {
if let last = days.last, last.title == d.title { days[days.count - 1].items += d.items } else { days.append(d) }
}
answer = a
} catch {
refused = DataModel.words(error)
}
}
}
/// ASSISTANTS (data.html "What each assistant has read"; `data.assistants`): one band per assistant, its reads and
/// writes newest first with the table, the count and the tool; the tables no window opens; the gaps in the log.
struct DataAssistantsScreen: View {
let model: DataModel
@State private var read: DataRead = .reading
@State private var reads = ReadInTurn()
var body: some View {
DataPage(tag: "data-assistants") {
DataBar(back: model.backWord) { model.back() }
DataTitle(text: read.answer?.title ?? DataWords.line("assistants.title"))
if let a = read.answer {
DataLead(text: a.lead)
if let none = a.none { DataNote(text: none) }
ForEach(Array(a.assistants.enumerated()), id: \.offset) { _, band in
VStack(alignment: .leading, spacing: 0) {
DataRule(colour: Stone.ink, thick: true).padding(.top, 18)
HStack(spacing: 6) {
Text(verbatim: band.name).font(DataType.sans(18, .bold)).foregroundStyle(Stone.ink)
.fixedSize(horizontal: false, vertical: true)
.frame(maxWidth: .infinity, alignment: .leading)
if let open = band.openLine {
Circle().fill(Stone.accent).frame(width: 7, height: 7)
Text(verbatim: open).font(DataType.sans(14, .semibold, .subheadline)).foregroundStyle(Stone.accent)
}
}
.padding(.top, 12)
.accessibilityElement(children: .combine)
.accessibilityAddTraits(.isHeader)
if let today = band.todayLine { Text(verbatim: today).font(DataType.sans(15)).foregroundStyle(Stone.quiet).padding(.top, 2) }
DataRule().padding(.top, 6)
ForEach(Array(band.items.enumerated()), id: \.offset) { at, item in
DataListRow(title: item.table, line: item.line, end: item.when, monoTitle: true, monoEnd: false,
last: at == band.items.count - 1 && band.moreLine == nil)
}
if let more = band.moreLine { DataListRow(title: more, last: true) }
}
}
DataSmall(text: a.neverTitle)
Text(verbatim: a.neverLine).font(DataType.body).foregroundStyle(Stone.ink).fixedSize(horizontal: false, vertical: true)
DataNote(text: a.gapLine)
} else if let words = read.refusal {
DataLead(text: words)
} else {
DataWaiting()
}
}
.task(id: model.revision) {
reads.ask {
let next = await DataRead.of { try await model.calls.assistants() }
if next.answer != nil || read.answer == nil { read = next }
}
}
}
}
#endif