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

140 lines
4.4 KiB
Swift
Raw Normal View History

2024-11-21 13:32:38 +00:00
import SwiftUI
struct AddContactOrChannelScreen: View {
@EnvironmentObject var wrapper: MonalXmppWrapper
@Environment(\.router) var router
enum Field {
case account
case contact
}
@FocusState private var focus: Field?
2024-11-22 13:12:09 +00:00
@State private var ownerAccount: Account?
2024-11-21 13:32:38 +00:00
@State private var contactJID: String = ""
var body: some View {
ZStack {
// Background color
Color.Material.Background.light
.ignoresSafeArea()
// Content
VStack(spacing: 0) {
// Header
SharedNavigationBar(
leftButton: .init(
image: Image(systemName: "xmark"),
action: {
router.dismissScreen()
}
),
centerText: .init(text: L10n.Contacts.Add.title),
rightButton: .init(
image: Image(systemName: "plus.viewfinder"),
action: {
print("Scan QR-code")
}
)
)
// Content
VStack(spacing: 16) {
// Explanation text
Text(L10n.Contacts.Add.explanation)
.font(.body3)
.foregroundColor(.Material.Shape.separator)
.multilineTextAlignment(.center)
.padding(.top, 16)
// Account selector
HStack(spacing: 0) {
Text("Use account:")
.font(.body2)
.foregroundColor(.Material.Text.main)
.frame(alignment: .leading)
Spacer()
}
2024-11-22 13:12:09 +00:00
UniversalInputCollection.DropDownMenu(
prompt: "Use account",
elements: wrapper.accounts,
selected: $ownerAccount,
focus: $focus,
fieldType: .account
)
2024-11-21 13:32:38 +00:00
// Contact text input
HStack(spacing: 0) {
Text("Contact JID:")
.font(.body2)
.foregroundColor(.Material.Text.main)
.frame(alignment: .leading)
Spacer()
}
2024-11-22 13:12:09 +00:00
UniversalInputCollection.TextField(
prompt: "Contact or channel JID",
text: $contactJID,
focus: $focus,
fieldType: .contact,
contentType: .emailAddress,
keyboardType: .emailAddress,
submitLabel: .done,
action: {
focus = .account
}
)
2024-11-21 13:32:38 +00:00
// Save button
Button {
Task {
await save()
}
} label: {
Text(L10n.Global.save)
}
.buttonStyle(PrimaryButtonStyle())
2024-11-22 13:12:09 +00:00
.disabled(!inputValid)
2024-11-21 13:32:38 +00:00
.padding(.top)
Spacer()
}
.padding(.horizontal, 32)
}
}
.onAppear {
2024-11-22 13:12:09 +00:00
if let exists = wrapper.accounts.first {
ownerAccount = exists
}
2024-11-21 13:32:38 +00:00
}
}
2024-11-22 13:12:09 +00:00
private var inputValid: Bool {
ownerAccount != nil && !contactJID.isEmpty && UniversalInputCollection.Validators.isEmail(contactJID)
}
2024-11-21 13:32:38 +00:00
private func save() async {
2024-11-22 13:12:09 +00:00
guard let ownerAccount = ownerAccount else { return }
2024-11-21 13:32:38 +00:00
router.showModal {
LoadingScreen()
}
defer {
router.dismissModal()
}
do {
2024-11-22 14:45:38 +00:00
try await wrapper.addContact(contactJid: contactJID, forAccountID: ownerAccount.id)
2024-11-21 13:32:38 +00:00
router.dismissScreen()
} catch {
router.showAlert(
.alert,
title: L10n.Global.Error.title,
subtitle: L10n.Contacts.Add.serverError
) {
Button(L10n.Global.ok, role: .cancel) {}
}
}
}
}