2022-01-31 21:28:07 +00:00
|
|
|
//
|
|
|
|
|
// NewChatButton.swift
|
|
|
|
|
// SimpleX
|
|
|
|
|
//
|
|
|
|
|
// Created by Evgeny Poberezkin on 31/01/2022.
|
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
struct NewChatButton: View {
|
|
|
|
|
@State private var showAddChat = false
|
|
|
|
|
@State private var addContact = false
|
|
|
|
|
@State private var connReqInvitation: String = ""
|
|
|
|
|
@State private var connectContact = false
|
|
|
|
|
@State private var createGroup = false
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
Button { showAddChat = true } label: {
|
2022-02-11 07:42:00 +00:00
|
|
|
Image(systemName: "person.crop.circle.badge.plus")
|
2022-01-31 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
.confirmationDialog("Start new chat", isPresented: $showAddChat, titleVisibility: .visible) {
|
|
|
|
|
Button("Add contact") { addContactAction() }
|
|
|
|
|
Button("Scan QR code") { connectContact = true }
|
|
|
|
|
Button("Create group") { createGroup = true }
|
|
|
|
|
.disabled(true)
|
|
|
|
|
}
|
|
|
|
|
.sheet(isPresented: $addContact, content: {
|
|
|
|
|
AddContactView(connReqInvitation: connReqInvitation)
|
|
|
|
|
})
|
|
|
|
|
.sheet(isPresented: $connectContact, content: {
|
|
|
|
|
connectContactSheet()
|
|
|
|
|
})
|
|
|
|
|
.sheet(isPresented: $createGroup, content: { CreateGroupView() })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addContactAction() {
|
2022-02-26 20:21:32 +00:00
|
|
|
do {
|
|
|
|
|
connReqInvitation = try apiAddContact()
|
|
|
|
|
addContact = true
|
|
|
|
|
} catch {
|
|
|
|
|
DispatchQueue.global().async {
|
|
|
|
|
connectionErrorAlert(error)
|
2022-02-14 11:53:44 +00:00
|
|
|
}
|
2022-02-26 20:21:32 +00:00
|
|
|
logger.error("NewChatButton.addContactAction apiAddContact error: \(error.localizedDescription)")
|
2022-01-31 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-02-26 20:21:32 +00:00
|
|
|
|
|
|
|
|
func addContactSheet() -> some View {
|
|
|
|
|
AddContactView(connReqInvitation: connReqInvitation)
|
|
|
|
|
}
|
2022-01-31 21:28:07 +00:00
|
|
|
|
|
|
|
|
func connectContactSheet() -> some View {
|
|
|
|
|
ConnectContactView(completed: { err in
|
|
|
|
|
connectContact = false
|
2022-02-14 11:53:44 +00:00
|
|
|
DispatchQueue.global().async {
|
2022-04-02 14:35:35 +01:00
|
|
|
switch (err) {
|
|
|
|
|
case let .success(ok):
|
|
|
|
|
if ok { connectionReqSentAlert(.invitation) }
|
|
|
|
|
case let .failure(error):
|
2022-02-14 11:53:44 +00:00
|
|
|
connectionErrorAlert(error)
|
|
|
|
|
}
|
2022-01-31 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 15:59:43 +00:00
|
|
|
func connectionErrorAlert(_ error: Error) {
|
2022-04-16 09:37:01 +01:00
|
|
|
AlertManager.shared.showAlertMsg(title: "Connection error", message: "Error: \(error.localizedDescription)")
|
2022-01-31 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 11:53:44 +00:00
|
|
|
enum ConnReqType: Equatable {
|
|
|
|
|
case contact
|
|
|
|
|
case invitation
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func connectionReqSentAlert(_ type: ConnReqType) {
|
|
|
|
|
AlertManager.shared.showAlertMsg(
|
|
|
|
|
title: "Connection request sent!",
|
2022-04-16 09:37:01 +01:00
|
|
|
message: type == .contact
|
|
|
|
|
? "You will be connected when your connection request is accepted, please wait or check later!"
|
|
|
|
|
: "You will be connected when your contact's device is online, please wait or check later!"
|
2022-02-14 11:53:44 +00:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-31 21:28:07 +00:00
|
|
|
struct NewChatButton_Previews: PreviewProvider {
|
|
|
|
|
static var previews: some View {
|
|
|
|
|
NewChatButton()
|
|
|
|
|
}
|
|
|
|
|
}
|