Skip to content

Commit

Permalink
Mentions refactoring
Browse files Browse the repository at this point in the history
Handle mention selection
  • Loading branch information
Oleg-Pecheneg committed Feb 26, 2024
1 parent de28fd9 commit 6d651b7
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,8 @@
C6FAED812B8C5B4C00CC1844 /* ChatMentionSuggestionRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FAED7F2B8C5B4C00CC1844 /* ChatMentionSuggestionRowView.swift */; };
C6FAED832B8C5C1200CC1844 /* ChatMentionSuggestionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FAED822B8C5C1200CC1844 /* ChatMentionSuggestionsView.swift */; };
C6FAED842B8C5C1200CC1844 /* ChatMentionSuggestionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FAED822B8C5C1200CC1844 /* ChatMentionSuggestionsView.swift */; };
C6FAED862B8C684700CC1844 /* MessageMentionString.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FAED852B8C684700CC1844 /* MessageMentionString.swift */; };
C6FAED872B8C684700CC1844 /* MessageMentionString.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FAED852B8C684700CC1844 /* MessageMentionString.swift */; };
C6FAFDE628119E0400734E0F /* TextButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FAFDE528119E0400734E0F /* TextButton.swift */; };
C6FE49D6285CBAA50058F9D1 /* CoreAppCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FE49D5285CBAA50058F9D1 /* CoreAppCoordinator.swift */; };
C6FE49DB285CBAB10058F9D1 /* CoreAppCoordinatorProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FE49DA285CBAB10058F9D1 /* CoreAppCoordinatorProtocol.swift */; };
Expand Down Expand Up @@ -3596,6 +3598,7 @@
C6F9FBE72A25CB5800102F81 /* PushMessageType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PushMessageType.swift; sourceTree = "<group>"; };
C6FAED7F2B8C5B4C00CC1844 /* ChatMentionSuggestionRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatMentionSuggestionRowView.swift; sourceTree = "<group>"; };
C6FAED822B8C5C1200CC1844 /* ChatMentionSuggestionsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatMentionSuggestionsView.swift; sourceTree = "<group>"; };
C6FAED852B8C684700CC1844 /* MessageMentionString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageMentionString.swift; sourceTree = "<group>"; };
C6FAFDE528119E0400734E0F /* TextButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextButton.swift; sourceTree = "<group>"; };
C6FE49D5285CBAA50058F9D1 /* CoreAppCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreAppCoordinator.swift; sourceTree = "<group>"; };
C6FE49DA285CBAB10058F9D1 /* CoreAppCoordinatorProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreAppCoordinatorProtocol.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5420,6 +5423,7 @@
C606863C2A30D3D300927396 /* MessagingNewsChannel.swift */,
C66107932A3180DA0076782F /* MessagingNewsChannelFeed.swift */,
C62BDA402B5E4104008E21AD /* MessagingBlockUserInChatType.swift */,
C6FAED852B8C684700CC1844 /* MessageMentionString.swift */,
C65272142A14937A001A084C /* Chats */,
C65272132A149370001A084C /* Messages */,
);
Expand Down Expand Up @@ -8268,6 +8272,7 @@
C6DDF2542B147F1A006D1F0B /* UDButtonImage.swift in Sources */,
C609820A2822D92300546392 /* ToastMessageService.swift in Sources */,
C6BA74742AD5013500628DC6 /* PullUpViewService+DomainProfile.swift in Sources */,
C6FAED862B8C684700CC1844 /* MessageMentionString.swift in Sources */,
C6ECBF8B28D2D20900E94309 /* WalletInfoBadgeView.swift in Sources */,
C6B91EB429BB718100389FF5 /* StripeService.swift in Sources */,
C613C22229E3BEEC0001A3AF /* NFCService.swift in Sources */,
Expand Down Expand Up @@ -9905,6 +9910,7 @@
C6D646DA2B1ED33400D724AC /* SocialDescription.swift in Sources */,
C61807E92B19A6290032E543 /* DomainPFPInfo.swift in Sources */,
C6C8F8D22B21833D00A9834D /* UBTSearchingView.swift in Sources */,
C6FAED872B8C684700CC1844 /* MessageMentionString.swift in Sources */,
C6D646722B1ED11B00D724AC /* DomainProfileUpdatingRecordsData.swift in Sources */,
C6D646FB2B1ED74E00D724AC /* GhostTertiaryWhiteButton.swift in Sources */,
C61808702B19BC100032E543 /* ForEach+Skeleton.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private extension ChatMentionSuggestionRowView {
width: 20,
height: 20)
.squareFrame(24)
.clipShape(Circle())
}

@ViewBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI
struct ChatMentionSuggestionsView: View {

let suggestingUsers: [MessagingChatUserDisplayInfo]
let selectionCallback: (MessagingChatUserDisplayInfo)->()

private let maximumNumberOfVisibleSuggestingUsers = 6

Expand All @@ -36,6 +37,7 @@ private extension ChatMentionSuggestionsView {
func selectableRowViewFor(user: MessagingChatUserDisplayInfo) -> some View {
Button {
UDVibration.buttonTap.vibrate()
selectionCallback(user)
} label: {
ChatMentionSuggestionRowView(user: user)
}
Expand All @@ -44,5 +46,6 @@ private extension ChatMentionSuggestionsView {
}

#Preview {
ChatMentionSuggestionsView(suggestingUsers: MockEntitiesFabric.Messaging.suggestingGroupChatMembersDisplayInfo())
ChatMentionSuggestionsView(suggestingUsers: MockEntitiesFabric.Messaging.suggestingGroupChatMembersDisplayInfo(),
selectionCallback: { _ in })
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct ChatView: View, ViewAnalyticsLogger {
@EnvironmentObject var navigationState: NavigationStateManager
@StateObject var viewModel: ChatViewModel
@FocusState var focused: Bool
@State private var suggestingUsers: [MessagingChatUserDisplayInfo] = MockEntitiesFabric.Messaging.suggestingGroupChatMembersDisplayInfo()
@State private var suggestingUsers: [MessagingChatUserDisplayInfo] = []

var analyticsName: Analytics.ViewName { .chatDialog }
var additionalAppearAnalyticParameters: Analytics.EventParameters { [:] }
Expand All @@ -39,7 +39,9 @@ struct ChatView: View, ViewAnalyticsLogger {
}
}
.onChange(of: viewModel.input) { newValue in
showMentionSuggestionsIfNeeded(in: newValue)
withAnimation {
showMentionSuggestionsIfNeeded(in: newValue)
}
}
.toolbar {
if !viewModel.navActions.isEmpty {
Expand Down Expand Up @@ -86,14 +88,24 @@ private extension ChatView {
if !listOfGroupParticipants.isEmpty {
let components = text.components(separatedBy: " ")
if let lastComponent = components.last,
lastComponent.first == "@" {
let mentionPart = String(lastComponent.dropFirst()).lowercased()
if mentionPart.isEmpty {
suggestingUsers = listOfGroupParticipants
} else {
// suggestingNames = listOfGroupParticipants.filter { $0.contains(mentionPart)}
}
print("Mention: \(mentionPart)")
let mention = MessageMentionString(string: lastComponent) {
showMentionSuggestions(using: listOfGroupParticipants,
mention: mention)
}
}
}

func showMentionSuggestions(using listOfGroupParticipants: [MessagingChatUserDisplayInfo],
mention: MessageMentionString) {
let mentionUsername = mention.mentionWithoutPrefix.lowercased()
if mentionUsername.isEmpty {
suggestingUsers = listOfGroupParticipants
} else {
suggestingUsers = listOfGroupParticipants.filter {
let nameForMention = $0.nameForMention
let isMentionFullyTyped = nameForMention == mentionUsername
let isUsernameContainMention = nameForMention?.contains(mentionUsername) == true
return isUsernameContainMention && !isMentionFullyTyped
}
}
}
Expand Down Expand Up @@ -212,7 +224,16 @@ private extension ChatView {

@ViewBuilder
func mentionSuggestionsView() -> some View {
ChatMentionSuggestionsView(suggestingUsers: suggestingUsers)
ChatMentionSuggestionsView(suggestingUsers: suggestingUsers,
selectionCallback: { user in
if let nameForMention = user.nameForMention,
let mention = MessageMentionString.makeMentionFrom(string: nameForMention) {
var userInput = viewModel.input.components(separatedBy: " ").dropLast()
userInput.append(mention.mentionWithPrefix)
viewModel.input = userInput.joined(separator: " ")
suggestingUsers.removeAll()
}
})
.padding(.init(horizontal: 20))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private extension TextMessageRowView {
}

func detectUsernamesIn(string: String, users: [String]) -> [NSTextCheckingResult] {
let pattern = "@([\\w.]+)"
let pattern = "\(MessageMentionString.messageMentionPrefix)([\\w.]+)"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let nsText = string as NSString

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// MessageMentionString.swift
// domains-manager-ios
//
// Created by Oleg Kuplin on 26.02.2024.
//

import Foundation

struct MessageMentionString {

static let messageMentionPrefix = "@"

let mentionWithPrefix: String
let mentionWithoutPrefix: String

init?(string: String) {
guard string.first == MessageMentionString.messageMentionPrefix.first else { return nil }

self.mentionWithPrefix = string
self.mentionWithoutPrefix = String(string.dropFirst())
}

static func makeMentionFrom(string: String) -> MessageMentionString? {
if let mention = MessageMentionString(string: string) { // Check if already mention first
return mention
}

let mentionString = messageMentionPrefix + string
return MessageMentionString(string: mentionString)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ struct MessagingChatUserDisplayInfo: Hashable, Codable {
func getETHWallet() -> String {
wallet.ethChecksumAddress()
}

var nameForMention: String? {
anyDomainName
}
}

0 comments on commit 6d651b7

Please sign in to comment.