session-ios/Session/Utilities/ContactUtilities.swift

24 lines
906 B
Swift
Raw Normal View History

2020-09-28 03:43:15 +02:00
enum ContactUtilities {
static func getAllContacts() -> [String] {
2021-05-05 07:27:31 +02:00
// Collect all contacts
2020-09-28 03:43:15 +02:00
var result: [String] = []
Storage.read { transaction in
TSContactThread.enumerateCollectionObjects(with: transaction) { object, _ in
2021-05-05 01:53:18 +02:00
guard let thread = object as? TSContactThread, thread.shouldBeVisible else { return }
result.append(thread.contactSessionID())
2020-09-28 03:43:15 +02:00
}
}
func getDisplayName(for publicKey: String) -> String {
2021-02-26 05:56:41 +01:00
return Storage.shared.getContact(with: publicKey)?.displayName(for: .regular) ?? publicKey
2020-09-28 03:43:15 +02:00
}
2021-05-05 07:27:31 +02:00
// Remove the current user
if let index = result.firstIndex(of: getUserHexEncodedPublicKey()) {
result.remove(at: index)
}
2021-05-05 07:27:31 +02:00
// Sort alphabetically
2020-09-28 03:43:15 +02:00
return result.sorted { getDisplayName(for: $0) < getDisplayName(for: $1) }
}
}