diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index edd9412..dd87076 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -211,6 +211,13 @@ private extension ClientsStore { .catch { _ in Just([]) } .sink { [weak self] rosters in self?.actualRosters = rosters + .sorted { + if $0.bareJid != $1.bareJid { + return $0.bareJid < $1.bareJid + } else { + return $0.contactBareJid < $1.contactBareJid + } + } } } diff --git a/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift b/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift index 1a354bf..e2e0ff6 100644 --- a/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift +++ b/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift @@ -26,20 +26,46 @@ struct ContactsScreen: View { ) // Contacts list - if !clientsStore.actualRosters.isEmpty { - List { - ForEach(clientsStore.actualRosters) { roster in - ContactsScreenRow( - roster: roster - ) - } + contactsList + } + } + } + + @ViewBuilder private var contactsList: some View { + if !clientsStore.actualRosters.isEmpty { + List { + ForEach(elements.indices, id: \.self) { index in + let element = elements[index] + if let roster = element as? Roster { + ContactsScreenRow( + roster: roster + ) + } else if let bareJid = element as? String { + SharedSectionTitle(text: bareJid) } - .listStyle(.plain) - .background(Color.Material.Background.light) - } else { - Spacer() } } + .listStyle(.plain) + .background(Color.Material.Background.light) + } else { + Spacer() + } + } + + private var elements: [Any] { + if clientsStore.clients.filter({ $0.credentials.isActive }).count == 1 { + return clientsStore.actualRosters + } else { + var result: [Any] = [] + for roster in clientsStore.actualRosters { + if result.isEmpty { + result.append(roster.bareJid) + } else if let last = result.last as? Roster, last.bareJid != roster.bareJid { + result.append(roster.bareJid) + } + result.append(roster) + } + return result } } }