diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index 7fb1f80..17c22b8 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -239,6 +239,13 @@ private extension ClientsStore { .catch { _ in Just([]) } .sink { [weak self] chats in self?.actualChats = chats + .sorted { + if $0.account != $1.account { + return $0.account < $1.account + } else { + return $0.participant < $1.participant + } + } } } } diff --git a/ConversationsClassic/View/Main/ChatList/ChatsListScreen.swift b/ConversationsClassic/View/Main/ChatList/ChatsListScreen.swift index 3679d5d..4984ec3 100644 --- a/ConversationsClassic/View/Main/ChatList/ChatsListScreen.swift +++ b/ConversationsClassic/View/Main/ChatList/ChatsListScreen.swift @@ -28,8 +28,13 @@ struct ChatsListScreen: View { // Chats list if !clientsStore.actualChats.isEmpty { List { - ForEach(clientsStore.actualChats) { chat in - ChatsRow(chat: chat) + ForEach(elements.indices, id: \.self) { index in + let element = elements[index] + if let chat = element as? Chat { + ChatsRow(chat: chat) + } else if let account = element as? String { + SharedSectionTitle(text: account) + } } } .listStyle(.plain) @@ -40,6 +45,23 @@ struct ChatsListScreen: View { } } } + + private var elements: [Any] { + if clientsStore.clients.filter({ $0.credentials.isActive }).count == 1 { + return clientsStore.actualChats + } else { + var result: [Any] = [] + for chat in clientsStore.actualChats { + if result.isEmpty { + result.append(chat.account) + } else if let last = result.last as? Chat, last.account != chat.account { + result.append(chat.account) + } + result.append(chat) + } + return result + } + } } private struct ChatsRow: View {