Add mentions when composing thread

This commit is contained in:
Justin Mazzocchi 2021-02-16 23:07:53 -08:00
parent 8a7bf4ddb6
commit 63a66ed06b
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -163,6 +163,19 @@ public extension NewStatusViewModel {
newViewModel.contentWarning = after.contentWarning newViewModel.contentWarning = after.contentWarning
newViewModel.displayContentWarning = after.displayContentWarning newViewModel.displayContentWarning = after.displayContentWarning
let mentions = Self.mentionsRegularExpression.matches(
in: after.text,
range: NSRange(location: 0, length: after.text.count))
.compactMap { result -> String? in
guard let range = Range(result.range, in: after.text) else { return nil }
return String(after.text[range])
}
if !mentions.isEmpty {
newViewModel.text = mentions.joined(separator: " ").appending(" ")
}
if index >= compositionViewModels.count - 1 { if index >= compositionViewModels.count - 1 {
compositionViewModels.append(newViewModel) compositionViewModels.append(newViewModel)
} else { } else {
@ -186,6 +199,9 @@ public extension NewStatusViewModel {
} }
private extension NewStatusViewModel { private extension NewStatusViewModel {
// swiftlint:disable:next force_try
static let mentionsRegularExpression = try! NSRegularExpression(pattern: #"@\w+"#)
func handle(event: CompositionViewModel.Event) { func handle(event: CompositionViewModel.Event) {
switch event { switch event {
case let .editAttachment(attachmentViewModel, compositionViewModel): case let .editAttachment(attachmentViewModel, compositionViewModel):
@ -194,6 +210,7 @@ private extension NewStatusViewModel {
publisher.assignErrorsToAlertItem(to: \.alertItem, on: self).sink { _ in }.store(in: &cancellables) publisher.assignErrorsToAlertItem(to: \.alertItem, on: self).sink { _ in }.store(in: &cancellables)
} }
} }
func post(viewModel: CompositionViewModel, inReplyToId: Status.Id?) { func post(viewModel: CompositionViewModel, inReplyToId: Status.Id?) {
postingState = .posting postingState = .posting
identityContext.service.post(statusComponents: viewModel.components( identityContext.service.post(statusComponents: viewModel.components(