1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import AVFoundation
import FoundationModels
import PhotosUI
import SwiftUI
import UIKit
import UniformTypeIdentifiers
extension ComposeView {
var visibleMentionAccounts: [Post.Author] {
mentionResults.filter { model.isAccountSummaryVisible($0) }
}
func mentionDropdown(editorFrame: CGRect, viewport: CGSize) -> some View {
let rowCount = max(1, visibleMentionAccounts.count)
let contentHeight =
loadedMention == activeMention && mentionError == nil
? CGFloat(rowCount) * mentionRowHeight : mentionRowHeight * (mentionError == nil ? 1 : 2)
return ComposerSuggestionDropdown(
caret: caretRect.offsetBy(dx: editorFrame.minX, dy: editorFrame.minY),
viewport: viewport, contentHeight: contentHeight
) {
mentionSuggestions
}
}
@ViewBuilder
var mentionSuggestions: some View {
if let mention = activeMention {
VStack(alignment: .leading, spacing: 0) {
if loadedMention != mention {
ProgressView("Looking up accounts")
.frame(maxWidth: .infinity, minHeight: mentionRowHeight)
} else if let mentionError {
VStack(alignment: .leading, spacing: 8) {
Text(mentionError).appFont(.footnote).foregroundStyle(.secondary)
Button("Retry") { mentionRetry += 1 }
}
.padding(12)
} else if visibleMentionAccounts.isEmpty {
Text("No accounts found.")
.appFont(.subheadline)
.foregroundStyle(.secondary)
.frame(maxWidth: .infinity, minHeight: mentionRowHeight)
} else {
ForEach(visibleMentionAccounts, id: \.id) { author in
Button {
guard activeMention == mention,
ComposerMention.active(in: text, selection: textSelection) == mention,
let replacement = mention.inserting(handle: author.handle, in: text)
else { return }
text = replacement.text
textSelection = replacement.selection
activeMention = nil
textIsFocused = true
} label: {
HStack(spacing: 10) {
AuthorAvatar(author: author, size: 32)
VStack(alignment: .leading, spacing: 2) {
Text(displayPreferences.name(author)).foregroundStyle(.primary)
if !displayPreferences.handlesOnly {
Text("@\(author.handle)").appFont(.subheadline).foregroundStyle(.secondary)
}
}
.lineLimit(1)
Spacer(minLength: 0)
}
.padding(.horizontal, 12)
.frame(height: mentionRowHeight)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityLabel(
displayPreferences.handlesOnly
? "Mention @\(author.handle)" : "Mention \(author.name), @\(author.handle)"
)
.accessibilityIdentifier("mentionSuggestion-\(author.id)")
}
}
}
}
}
func loadMentionSuggestions() async {
mentionResults = []
loadedMention = nil
mentionError = nil
guard let mention = activeMention, textIsFocused, !model.isPublishing else { return }
do {
try await Task.sleep(for: .milliseconds(300))
let accounts = try await model.mentionSuggestions(query: mention.query)
guard !Task.isCancelled, activeMention == mention else { return }
mentionResults = accounts
loadedMention = mention
} catch {
guard !Task.isCancelled, activeMention == mention else { return }
if NetworkFailure.isCancellation(error) {
activeMention = nil
return
}
mentionError =
NetworkFailure.isConnectionFailure(error)
? NetworkFailure.message(for: error) : "Account suggestions are unavailable. Try again."
loadedMention = mention
}
}
}