another.im-ios/Monal/another.im/Views/Main/Contacts/ContactsScreen.swift

147 lines
4.1 KiB
Swift
Raw Normal View History

2024-11-21 13:32:38 +00:00
import SwiftUI
struct ContactsScreen: View {
2024-12-02 15:50:23 +00:00
@EnvironmentObject var wrapper: WrapperXMPP
2024-11-21 13:32:38 +00:00
@Environment(\.router) var router
var body: some View {
ZStack {
// Background color
Color.Material.Background.light
.ignoresSafeArea()
// Content
VStack(spacing: 0) {
// Header
SharedNavigationBar(
centerText: .init(text: L10n.Contacts.title),
rightButton: .init(
image: Image(systemName: "plus"),
action: {
router.showScreen(.fullScreenCover) { _ in
AddContactOrChannelScreen()
}
}
)
)
// Contacts list
2024-11-22 13:12:09 +00:00
if !wrapper.contacts.isEmpty {
List {
ForEach(wrapper.contacts) {
ContactsScreenRow(contact: $0)
}
}
.listStyle(.plain)
.background(Color.Material.Background.light)
} else {
Spacer()
}
2024-11-21 13:32:38 +00:00
}
}
}
}
private struct ContactsScreenRow: View {
2024-12-02 15:50:23 +00:00
@EnvironmentObject var wrapper: WrapperXMPP
2024-11-21 13:32:38 +00:00
@Environment(\.router) var router
2024-11-22 13:12:09 +00:00
var contact: Contact
2024-11-21 13:32:38 +00:00
var body: some View {
2024-11-22 13:12:09 +00:00
SharedListRow(
2024-11-29 22:36:19 +00:00
iconType: .charCircle(contact.name),
2024-11-22 13:12:09 +00:00
text: contact.contactJid,
controlType: .none
)
.onTapGesture {
startChat()
}
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button {
router.showAlert(.confirmationDialog, title: L10n.Contacts.deleteContact, subtitle: L10n.Contacts.Delete.message) {
deleteConfirmation
}
} label: {
Label("", systemImage: "trash")
}
.tint(Color.red)
}
.contextMenu {
Button(L10n.Contacts.sendMessage, systemImage: "message") {
startChat()
}
Divider()
Button(L10n.Contacts.editContact) {
print("Edit contact")
}
Button(L10n.Contacts.selectContact) {
print("Select contact")
}
Divider()
Button(L10n.Contacts.deleteContact, systemImage: "trash", role: .destructive) {
router.showAlert(.confirmationDialog, title: L10n.Contacts.deleteContact, subtitle: L10n.Contacts.Delete.message) {
deleteConfirmation
}
}
}
2024-11-21 13:32:38 +00:00
}
@ViewBuilder private var deleteConfirmation: some View {
Button(role: .destructive) {
Task {
await deleteCompletely()
}
} label: {
Text(L10n.Contacts.Delete.deleteCompletely)
}
Button(role: .cancel) {} label: {
Text(L10n.Global.cancel)
}
}
private func deleteCompletely() async {
router.showModal {
LoadingScreen()
}
defer {
router.dismissModal()
}
do {
2024-11-22 16:54:07 +00:00
try await wrapper.deleteContact(contact)
2024-11-21 13:32:38 +00:00
} catch {
router.showAlert(
.alert,
title: L10n.Global.Error.title,
subtitle: L10n.Contacts.Delete.error
) {
Button(L10n.Global.ok, role: .cancel) {}
}
}
}
private func startChat() {
2024-12-06 11:56:09 +00:00
guard let model = wrapper.chat(with: contact) else {
router.showAlert(
.alert,
title: L10n.Global.Error.title,
subtitle: L10n.Conversation.startError
) {
Button(L10n.Global.ok, role: .cancel) {}
}
return
}
2024-11-24 18:42:46 +00:00
router.showScreen(.push) { _ in
ConversationScreen()
.navigationBarHidden(true)
.environmentObject(model)
2024-11-21 13:32:38 +00:00
}
}
}