another.im-ios/AnotherIM/View/Main/ChatList/ChatsListScreen.swift

104 lines
3.6 KiB
Swift
Raw Normal View History

import SwiftUI
struct ChatsListScreen: View {
@Environment(\.router) var router
@EnvironmentObject var clientsStore: ClientsStore
var body: some View {
ZStack {
// Background color
Color.Material.Background.light
.ignoresSafeArea()
// Content
VStack(spacing: 0) {
// Header
SharedNavigationBar(
centerText: .init(text: L10n.ChatsList.title),
rightButton: .init(
image: Image(systemName: "square.and.pencil"),
action: {
router.showScreen(.fullScreenCover) { _ in
ChatsCreateScreenMain()
}
}
)
)
// Chats list
if !clientsStore.actualChats.isEmpty {
List {
2024-10-21 08:23:13 +00:00
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)
.background(Color.Material.Background.light)
} else {
Spacer()
}
}
}
}
2024-10-21 08:23:13 +00:00
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 {
@Environment(\.router) var router
@EnvironmentObject var clientsStore: ClientsStore
var chat: Chat
var body: some View {
2024-09-19 16:50:48 +00:00
SharedListRow(iconType: .charCircle(chat.participant), text: chat.participant, controlType: .none)
.onTapGesture {
Task {
router.showModal {
LoadingScreen()
}
defer {
router.dismissModal()
}
do {
try? await clientsStore.addRosterForNewChatIfNeeded(chat)
2024-09-20 15:32:10 +00:00
let (messages, attachments, settings) = try await clientsStore.conversationStores(for: chat)
router.showScreen(.push) { _ in
2024-09-20 15:32:10 +00:00
ConversationScreen(messagesStore: messages, attachments: attachments, settings: settings)
.navigationBarHidden(true)
}
} catch {
router.showAlert(
.alert,
title: L10n.Global.Error.title,
subtitle: L10n.Conversation.startError
) {
Button(L10n.Global.ok, role: .cancel) {}
}
}
}
}
}
}