Watch: what changed on Recent cards and the thread screen, search in All threads, a switching complication with Allow and Deny in the Smart Stack, and question options on notifications
This commit is contained in:
1 parent
f8241e42bb
commit
bfcbb69550
29 files changed
+1100
-137
No files matched your search
@@ -1,11 +1,12 @@
|
||||
// All threads, opened from the last row of the feed: hosts, then a host's projects sorted by title,
|
||||
// then a project's threads with settled ones included. Hosts and projects come from
|
||||
// `GET /v1/projects`, threads from `GET /v1/projects/:hostId/:projectId/threads`. Each screen loads
|
||||
// when it appears; nothing polls. A thread opens the usual thread screen.
|
||||
// All threads, opened from the last row of the feed: a search field over the hosts, then a host's
|
||||
// projects sorted by title, then a project's threads with settled ones included. Hosts and projects
|
||||
// come from `GET /v1/projects`, threads from `GET /v1/projects/:hostId/:projectId/threads`, and
|
||||
// search results from `GET /v1/search`. Each screen loads when it appears; nothing polls. A thread or
|
||||
// a search hit opens the usual thread screen.
|
||||
|
||||
import SwiftUI
|
||||
|
||||
/// Every host, offline ones dimmed. Each opens its projects.
|
||||
/// A search field, then every host, offline ones dimmed. Each host opens its projects.
|
||||
struct HostsView: View {
|
||||
@Environment(AppModel.self) private var model
|
||||
/// Snapshots render the rows without their List, which ImageRenderer leaves blank.
|
||||
@@ -13,6 +14,9 @@ struct HostsView: View {
|
||||
|
||||
var body: some View {
|
||||
BrowseList(scrolls: scrolls, error: model.projectsError, loaded: model.projects != nil, reload: model.loadProjects) {
|
||||
SearchField()
|
||||
.feedRow()
|
||||
} rows: {
|
||||
if let hosts = model.projects?.hosts {
|
||||
ForEach(hosts) { host in
|
||||
BrowseRow(
|
||||
@@ -105,6 +109,144 @@ struct ProjectThreadsView: View {
|
||||
}
|
||||
}
|
||||
|
||||
/// Threads on every host matching `query`: each hit's host and project, its title, and the words
|
||||
/// around the match when it was inside a message. Says which hosts were left out.
|
||||
struct SearchView: View {
|
||||
@Environment(AppModel.self) private var model
|
||||
let query: String
|
||||
@State private var response: SearchResponse?
|
||||
@State private var error: String?
|
||||
private let scrolls: Bool
|
||||
|
||||
/// `response` starts the screen loaded, for snapshots.
|
||||
init(query: String, response: SearchResponse? = nil, scrolls: Bool = true) {
|
||||
self.query = query
|
||||
self.scrolls = scrolls
|
||||
_response = State(initialValue: response)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
BrowseList(scrolls: scrolls, error: error, loaded: response != nil, reload: load) {
|
||||
if let response {
|
||||
ForEach(response.hits) { hit in
|
||||
SearchHitCard(hit: hit, query: query)
|
||||
.feedRow()
|
||||
}
|
||||
if response.hits.isEmpty {
|
||||
Text("No threads match.")
|
||||
.font(.system(size: 13))
|
||||
.foregroundStyle(Theme.secondaryText)
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding(.top, 16)
|
||||
.feedRow()
|
||||
}
|
||||
if let note = SearchResponse.unsearchedNote(response.unsearched, offline: offlineHosts) {
|
||||
Text(note)
|
||||
.font(.system(size: 11))
|
||||
.foregroundStyle(Theme.secondaryText)
|
||||
.multilineTextAlignment(.center)
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding(.horizontal, 8)
|
||||
.feedRow()
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("\u{201C}\(query)\u{201D}")
|
||||
}
|
||||
|
||||
/// Labels of hosts the feed or All threads last saw offline.
|
||||
private var offlineHosts: Set<String> {
|
||||
let feed = model.feed?.hosts.filter { $0.status != .online }.map(\.label) ?? []
|
||||
let browsed = model.projects?.hosts.filter { $0.status != .online }.map(\.label) ?? []
|
||||
return Set(feed + browsed)
|
||||
}
|
||||
|
||||
private func load() async {
|
||||
do {
|
||||
response = try await model.api.search(query)
|
||||
error = nil
|
||||
} catch {
|
||||
self.error = model.message(for: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The field at the top of All threads. Tapping it opens dictation or the keyboard, as any watchOS
|
||||
/// text field does, and submitting opens the results.
|
||||
private struct SearchField: View {
|
||||
@Environment(AppModel.self) private var model
|
||||
|
||||
var body: some View {
|
||||
TextFieldLink(prompt: Text("Search threads")) {
|
||||
HStack(spacing: 7) {
|
||||
Image(systemName: "magnifyingglass")
|
||||
.foregroundStyle(Theme.secondaryText)
|
||||
Text("Search")
|
||||
.foregroundStyle(Theme.secondaryText)
|
||||
Spacer(minLength: 4)
|
||||
Image(systemName: "mic.fill")
|
||||
.foregroundStyle(Theme.work)
|
||||
}
|
||||
.font(.system(size: 14, weight: .semibold))
|
||||
.padding(.vertical, 9)
|
||||
.padding(.horizontal, 12)
|
||||
.background(Theme.control.opacity(0.7), in: RoundedRectangle(cornerRadius: 18, style: .continuous))
|
||||
.contentShape(Rectangle())
|
||||
} onSubmit: { text in
|
||||
let query = text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !query.isEmpty else { return }
|
||||
model.path.append(.search(query: query))
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.accessibilityLabel("Search threads")
|
||||
}
|
||||
}
|
||||
|
||||
/// A search hit: "host · project", the title, dimmed once finished or settled, and the snippet with
|
||||
/// the matched words picked out. Opens the thread.
|
||||
private struct SearchHitCard: View {
|
||||
@Environment(AppModel.self) private var model
|
||||
var hit: SearchHit
|
||||
var query: String
|
||||
|
||||
var body: some View {
|
||||
Button { model.path.append(hit.thread.route) } label: {
|
||||
Card(edge: Theme.card) {
|
||||
Text("\(hit.hostLabel) · \(hit.projectTitle)")
|
||||
.font(.system(size: 11, weight: .semibold))
|
||||
.foregroundStyle(Theme.secondaryText)
|
||||
.lineLimit(1)
|
||||
Text(hit.thread.title)
|
||||
.font(.system(size: 14, weight: .semibold))
|
||||
.foregroundStyle(hit.thread.isActive ? Color.white : Theme.secondaryText)
|
||||
.lineLimit(2)
|
||||
.padding(.top, 1)
|
||||
if let snippet = hit.snippet {
|
||||
Text(highlighted(snippet))
|
||||
.font(.system(size: 12))
|
||||
.foregroundStyle(Theme.secondaryText)
|
||||
.lineLimit(3)
|
||||
.padding(.top, 3)
|
||||
}
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
|
||||
/// The snippet with each match of the query bold and white.
|
||||
private func highlighted(_ snippet: String) -> AttributedString {
|
||||
var text = AttributedString(snippet)
|
||||
var searchFrom = text.startIndex
|
||||
while let range = text[searchFrom...].range(of: query, options: .caseInsensitive) {
|
||||
text[range].inlinePresentationIntent = .stronglyEmphasized
|
||||
text[range].foregroundColor = .white
|
||||
searchFrom = range.upperBound
|
||||
}
|
||||
return text
|
||||
}
|
||||
}
|
||||
|
||||
/// A thread on one line: status dot, title, and `trailing` on the right. Opens the thread.
|
||||
struct ThreadLine<Trailing: View>: View {
|
||||
@Environment(AppModel.self) private var model
|
||||
@@ -135,11 +277,13 @@ struct ThreadLine<Trailing: View>: View {
|
||||
}
|
||||
|
||||
/// The List each browse screen scrolls, with loading, an error and Try again. Loads on appear.
|
||||
private struct BrowseList<Rows: View>: View {
|
||||
/// `header` shows above the rows even while they load.
|
||||
private struct BrowseList<Header: View, Rows: View>: View {
|
||||
var scrolls: Bool
|
||||
var error: String?
|
||||
var loaded: Bool
|
||||
var reload: () async -> Void
|
||||
@ViewBuilder var header: Header
|
||||
@ViewBuilder var rows: Rows
|
||||
|
||||
var body: some View {
|
||||
@@ -155,6 +299,7 @@ private struct BrowseList<Rows: View>: View {
|
||||
}
|
||||
|
||||
@ViewBuilder private var content: some View {
|
||||
header
|
||||
if loaded {
|
||||
rows
|
||||
} else if error == nil {
|
||||
@@ -174,6 +319,12 @@ private struct BrowseList<Rows: View>: View {
|
||||
}
|
||||
}
|
||||
|
||||
extension BrowseList where Header == EmptyView {
|
||||
init(scrolls: Bool, error: String?, loaded: Bool, reload: @escaping () async -> Void, @ViewBuilder rows: () -> Rows) {
|
||||
self.init(scrolls: scrolls, error: error, loaded: loaded, reload: reload, header: { EmptyView() }, rows: rows)
|
||||
}
|
||||
}
|
||||
|
||||
/// A plain card with a title, an optional line under it, and a chevron.
|
||||
private struct BrowseRow: View {
|
||||
@Environment(AppModel.self) private var model
|
||||
@@ -228,6 +379,14 @@ private struct BrowseNote: View {
|
||||
.fontDesign(.rounded)
|
||||
}
|
||||
|
||||
#Preview("Search") {
|
||||
NavigationStack {
|
||||
SearchView(query: "containerfile")
|
||||
}
|
||||
.environment(AppModel.preview())
|
||||
.fontDesign(.rounded)
|
||||
}
|
||||
|
||||
#Preview("Threads") {
|
||||
NavigationStack {
|
||||
ProjectThreadsView(hostId: "mac-mini", projectId: "p-wc-mac")
|
||||
|
||||
Reference in new issue
Block a user