mirror of https://github.com/lukechilds/damus.git
Browse Source
Co-authored-by: William Casarin <jb55@jb55.com> Changelog-Added: User tagging and autocompletion in posts Closes: #347 Fixes: #411, #63translations_damus-localizations-en-us-xcloc-localized-contents-en-us-xliff--master_pl_PL
committed by
William Casarin
5 changed files with 133 additions and 4 deletions
@ -0,0 +1,87 @@ |
|||
// |
|||
// UserAutocompletion.swift |
|||
// damus |
|||
// |
|||
// Created by William Casarin on 2023-01-28. |
|||
// |
|||
|
|||
import SwiftUI |
|||
|
|||
struct SearchedUser: Identifiable { |
|||
let petname: String? |
|||
let profile: Profile? |
|||
let pubkey: String |
|||
|
|||
var id: String { |
|||
return pubkey |
|||
} |
|||
} |
|||
|
|||
struct UserSearch: View { |
|||
let damus_state: DamusState |
|||
let search: String |
|||
@Binding var post: String |
|||
|
|||
var users: [SearchedUser] { |
|||
guard let contacts = damus_state.contacts.event else { |
|||
return [] |
|||
} |
|||
|
|||
return search_users(profiles: damus_state.profiles, tags: contacts.tags, search: search) |
|||
} |
|||
|
|||
var body: some View { |
|||
ScrollView { |
|||
LazyVStack { |
|||
ForEach(users) { user in |
|||
UserView(damus_state: damus_state, pubkey: user.pubkey) |
|||
.onTapGesture { |
|||
guard let pk = bech32_pubkey(user.pubkey) else { |
|||
return |
|||
} |
|||
post = post.replacingOccurrences(of: "@"+search, with: "@"+pk) |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
struct UserSearch_Previews: PreviewProvider { |
|||
static let search: String = "jb55" |
|||
@State static var post: String = "some @jb55" |
|||
|
|||
static var previews: some View { |
|||
UserSearch(damus_state: test_damus_state(), search: search, post: $post) |
|||
} |
|||
} |
|||
|
|||
|
|||
func search_users(profiles: Profiles, tags: [[String]], search: String) -> [SearchedUser] { |
|||
var seen_user = Set<String>() |
|||
return tags.reduce(into: Array<SearchedUser>()) { arr, tag in |
|||
guard tag.count >= 2 && tag[0] == "p" else { |
|||
return |
|||
} |
|||
|
|||
let pubkey = tag[1] |
|||
guard !seen_user.contains(pubkey) else { |
|||
return |
|||
} |
|||
seen_user.insert(pubkey) |
|||
|
|||
var petname: String? = nil |
|||
if tag.count >= 4 { |
|||
petname = tag[3] |
|||
} |
|||
|
|||
let profile = profiles.lookup(id: pubkey) |
|||
|
|||
guard ((petname?.hasPrefix(search) ?? false) || (profile?.name?.hasPrefix(search) ?? false)) else { |
|||
return |
|||
} |
|||
|
|||
let searched_user = SearchedUser(petname: petname, profile: profile, pubkey: pubkey) |
|||
arr.append(searched_user) |
|||
} |
|||
} |
Loading…
Reference in new issue