82 lines
2.7 KiB
Swift
82 lines
2.7 KiB
Swift
|
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 {
|
||
|
ForEach(clientsStore.actualChats) { chat in
|
||
|
ChatsRow(chat: chat)
|
||
|
}
|
||
|
}
|
||
|
.listStyle(.plain)
|
||
|
.background(Color.Material.Background.light)
|
||
|
} else {
|
||
|
Spacer()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private struct ChatsRow: View {
|
||
|
@Environment(\.router) var router
|
||
|
@EnvironmentObject var clientsStore: ClientsStore
|
||
|
|
||
|
var chat: Chat
|
||
|
|
||
|
var body: some View {
|
||
|
SharedListRow(iconType: .charCircle(chat.participant), text: chat.participant)
|
||
|
.onTapGesture {
|
||
|
Task {
|
||
|
router.showModal {
|
||
|
LoadingScreen()
|
||
|
}
|
||
|
defer {
|
||
|
router.dismissModal()
|
||
|
}
|
||
|
|
||
|
do {
|
||
|
try? await clientsStore.addRosterForNewChatIfNeeded(chat)
|
||
|
let (messages, attachments) = try await clientsStore.conversationStores(for: chat)
|
||
|
router.showScreen(.push) { _ in
|
||
|
ConversationScreen(messagesStore: messages, attachments: attachments)
|
||
|
.navigationBarHidden(true)
|
||
|
}
|
||
|
} catch {
|
||
|
router.showAlert(
|
||
|
.alert,
|
||
|
title: L10n.Global.Error.title,
|
||
|
subtitle: L10n.Conversation.startError
|
||
|
) {
|
||
|
Button(L10n.Global.ok, role: .cancel) {}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|