mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.3 KiB
Swift
38 lines
1.3 KiB
Swift
3 years ago
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||
|
|
||
|
import GRDB
|
||
|
import SessionMessagingKit
|
||
|
|
||
|
|
||
|
public class NewConversationViewModel {
|
||
|
struct SectionData {
|
||
|
var sectionName: String
|
||
|
var contacts: [Profile]
|
||
|
}
|
||
|
|
||
|
let sectionData: [SectionData]
|
||
|
|
||
|
init() {
|
||
|
let contactProfiles: [Profile] = Profile.fetchAllContactProfiles(excludeCurrentUser: true)
|
||
|
|
||
|
var groupedContacts: [String: SectionData] = [:]
|
||
|
contactProfiles.forEach { profile in
|
||
|
let displayName = NSMutableString(string: profile.displayName())
|
||
|
CFStringTransform(displayName, nil, kCFStringTransformToLatin, false)
|
||
|
CFStringTransform(displayName, nil, kCFStringTransformStripDiacritics, false)
|
||
|
let section: String = displayName.substring(to: 1).capitalized.isSingleAlphabet ?
|
||
|
displayName.substring(to: 1).capitalized :
|
||
|
"#"
|
||
|
|
||
|
if groupedContacts[section] == nil {
|
||
|
groupedContacts[section] = SectionData(
|
||
|
sectionName: section,
|
||
|
contacts: [])
|
||
|
}
|
||
|
groupedContacts[section]?.contacts.append(profile)
|
||
|
}
|
||
|
|
||
|
sectionData = groupedContacts.values.sorted{ $0.sectionName < $1.sectionName }
|
||
|
}
|
||
|
}
|