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
|
2022-05-31 07:55:13 +01:00
|
|
|
import SimpleXChat
|
2022-01-31 21:28:07 +00:00
|
|
|
|
2022-05-09 09:52:09 +01:00
|
|
|
enum NewChatAction: Identifiable {
|
2022-09-21 16:28:01 +01:00
|
|
|
case createLink(link: String)
|
2022-09-22 08:36:39 +01:00
|
|
|
case connectViaLink
|
2022-07-14 16:40:32 +04:00
|
|
|
case createGroup
|
2022-05-09 09:52:09 +01:00
|
|
|
|
2022-09-21 16:28:01 +01:00
|
|
|
var id: String {
|
|
|
|
|
switch self {
|
|
|
|
|
case let .createLink(link): return "createLink \(link)"
|
2022-09-22 08:36:39 +01:00
|
|
|
case .connectViaLink: return "connectViaLink"
|
2022-09-21 16:28:01 +01:00
|
|
|
case .createGroup: return "createGroup"
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-09 09:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-31 21:28:07 +00:00
|
|
|
struct NewChatButton: View {
|
2022-09-21 15:11:52 +01:00
|
|
|
@Binding var showAddChat: Bool
|
2022-05-09 09:52:09 +01:00
|
|
|
@State private var actionSheet: NewChatAction?
|
2022-01-31 21:28:07 +00:00
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
Button { showAddChat = true } label: {
|
2022-09-21 15:11:52 +01:00
|
|
|
Image(systemName: "square.and.pencil")
|
2022-07-26 10:55:58 +04:00
|
|
|
.resizable()
|
|
|
|
|
.scaledToFit()
|
2022-07-27 11:16:07 +04:00
|
|
|
.frame(width: 24, height: 24)
|
2022-01-31 21:28:07 +00:00
|
|
|
}
|
2022-09-22 08:36:39 +01:00
|
|
|
.confirmationDialog("Start a new chat", isPresented: $showAddChat, titleVisibility: .visible) {
|
|
|
|
|
Button("Share one-time invitation link") { addContactAction() }
|
|
|
|
|
Button("Connect via link / QR code") { actionSheet = .connectViaLink }
|
2022-07-28 11:49:36 +01:00
|
|
|
Button("Create secret group") { actionSheet = .createGroup }
|
2022-05-09 09:52:09 +01:00
|
|
|
}
|
|
|
|
|
.sheet(item: $actionSheet) { sheet in
|
|
|
|
|
switch sheet {
|
2022-09-22 08:36:39 +01:00
|
|
|
case let .createLink(link):
|
|
|
|
|
CreateLinkView(selection: .oneTime, connReqInvitation: link)
|
|
|
|
|
case .connectViaLink: ConnectViaLinkView()
|
2022-07-30 18:46:10 +01:00
|
|
|
case .createGroup: AddGroupView()
|
2022-05-09 09:52:09 +01:00
|
|
|
}
|
2022-01-31 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addContactAction() {
|
2022-09-21 16:28:01 +01:00
|
|
|
Task {
|
|
|
|
|
if let connReq = await apiAddContact() {
|
|
|
|
|
actionSheet = .createLink(link: connReq)
|
|
|
|
|
}
|
2022-01-31 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-04-25 07:54:07 +01:00
|
|
|
}
|
2022-01-31 21:28:07 +00:00
|
|
|
|
2022-04-25 07:54:07 +01:00
|
|
|
enum ConnReqType: Equatable {
|
|
|
|
|
case contact
|
|
|
|
|
case invitation
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-30 18:46:10 +01:00
|
|
|
func connectViaLink(_ connectionLink: String, _ dismiss: DismissAction? = nil) {
|
2022-04-25 07:54:07 +01:00
|
|
|
Task {
|
2022-09-21 17:18:48 +04:00
|
|
|
if let connReqType = await apiConnect(connReq: connectionLink) {
|
2022-04-25 07:54:07 +01:00
|
|
|
DispatchQueue.main.async {
|
2022-07-30 18:46:10 +01:00
|
|
|
dismiss?()
|
2023-05-16 15:03:41 +04:00
|
|
|
AlertManager.shared.showAlert(connReqSentAlert(connReqType))
|
2022-01-31 21:28:07 +00:00
|
|
|
}
|
2022-09-21 17:18:48 +04:00
|
|
|
} else {
|
2022-04-25 07:54:07 +01:00
|
|
|
DispatchQueue.main.async {
|
2022-07-30 18:46:10 +01:00
|
|
|
dismiss?()
|
2022-04-25 07:54:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-31 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 19:28:11 +03:00
|
|
|
struct CReqClientData: Decodable {
|
2022-11-05 17:48:57 +04:00
|
|
|
var type: String
|
|
|
|
|
var groupLinkId: String?
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 19:28:11 +03:00
|
|
|
func parseLinkQueryData(_ connectionLink: String) -> CReqClientData? {
|
2022-11-05 17:48:57 +04:00
|
|
|
if let hashIndex = connectionLink.firstIndex(of: "#"),
|
|
|
|
|
let urlQuery = URL(string: String(connectionLink[connectionLink.index(after: hashIndex)...])),
|
|
|
|
|
let components = URLComponents(url: urlQuery, resolvingAgainstBaseURL: false),
|
|
|
|
|
let data = components.queryItems?.first(where: { $0.name == "data" })?.value,
|
|
|
|
|
let d = data.data(using: .utf8),
|
2022-11-07 19:28:11 +03:00
|
|
|
let crData = try? getJSONDecoder().decode(CReqClientData.self, from: d) {
|
2022-11-05 17:48:57 +04:00
|
|
|
return crData
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 19:28:11 +03:00
|
|
|
func checkCRDataGroup(_ crData: CReqClientData) -> Bool {
|
2022-11-05 17:48:57 +04:00
|
|
|
return crData.type == "group" && crData.groupLinkId != nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func groupLinkAlert(_ connectionLink: String) -> Alert {
|
|
|
|
|
return Alert(
|
|
|
|
|
title: Text("Connect via group link?"),
|
|
|
|
|
message: Text("You will join a group this link refers to and connect to its group members."),
|
|
|
|
|
primaryButton: .default(Text("Connect")) {
|
|
|
|
|
connectViaLink(connectionLink)
|
|
|
|
|
},
|
|
|
|
|
secondaryButton: .cancel()
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 15:03:41 +04:00
|
|
|
func connReqSentAlert(_ type: ConnReqType) -> Alert {
|
|
|
|
|
return mkAlert(
|
2022-02-14 11:53:44 +00:00
|
|
|
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 {
|
2022-09-21 15:11:52 +01:00
|
|
|
NewChatButton(showAddChat: Binding.constant(false))
|
2022-01-31 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
}
|