2023-05-01 20:36:52 +04:00
|
|
|
//
|
|
|
|
// CreateSimpleXAddress.swift
|
|
|
|
// SimpleX (iOS)
|
|
|
|
//
|
|
|
|
// Created by spaced4ndy on 28.04.2023.
|
|
|
|
// Copyright © 2023 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import Contacts
|
|
|
|
import ContactsUI
|
|
|
|
import MessageUI
|
|
|
|
import SimpleXChat
|
|
|
|
|
|
|
|
struct CreateSimpleXAddress: View {
|
|
|
|
@EnvironmentObject var m: ChatModel
|
|
|
|
@State private var progressIndicator = false
|
|
|
|
@State private var showMailView = false
|
|
|
|
@State private var mailViewResult: Result<MFMailComposeResult, Error>? = nil
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
GeometryReader { g in
|
|
|
|
ScrollView {
|
|
|
|
ZStack {
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
Text("SimpleX Address")
|
|
|
|
.font(.largeTitle)
|
|
|
|
.bold()
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
if let userAddress = m.userAddress {
|
2025-04-14 21:25:32 +01:00
|
|
|
SimpleXCreatedLinkQRCode(link: userAddress.connLinkContact, short: Binding.constant(false))
|
2023-05-01 20:36:52 +04:00
|
|
|
.frame(maxHeight: g.size.width)
|
|
|
|
shareQRCodeButton(userAddress)
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
|
2023-05-04 13:13:53 +03:00
|
|
|
if MFMailComposeViewController.canSendMail() {
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
shareViaEmailButton(userAddress)
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
}
|
2023-05-01 20:36:52 +04:00
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
continueButton()
|
|
|
|
.padding(.bottom, 8)
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
} else {
|
|
|
|
createAddressButton()
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
skipButton()
|
|
|
|
.padding(.bottom, 56)
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.frame(minHeight: g.size.height)
|
|
|
|
|
|
|
|
if progressIndicator {
|
|
|
|
ProgressView().scaleEffect(2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.frame(maxHeight: .infinity)
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func createAddressButton() -> some View {
|
|
|
|
VStack(spacing: 8) {
|
|
|
|
Button {
|
|
|
|
progressIndicator = true
|
|
|
|
Task {
|
|
|
|
do {
|
2025-04-14 21:25:32 +01:00
|
|
|
let connLinkContact = try await apiCreateUserAddress(short: false)
|
2023-05-01 20:36:52 +04:00
|
|
|
DispatchQueue.main.async {
|
2025-04-14 21:25:32 +01:00
|
|
|
m.userAddress = UserContactLink(connLinkContact: connLinkContact)
|
2023-05-01 20:36:52 +04:00
|
|
|
}
|
|
|
|
await MainActor.run { progressIndicator = false }
|
|
|
|
} catch let error {
|
|
|
|
logger.error("CreateSimpleXAddress create address: \(responseError(error))")
|
|
|
|
await MainActor.run { progressIndicator = false }
|
|
|
|
let a = getErrorAlert(error, "Error creating address")
|
|
|
|
AlertManager.shared.showAlertMsg(
|
|
|
|
title: a.title,
|
|
|
|
message: a.message
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Text("Create SimpleX address").font(.title)
|
|
|
|
}
|
2023-12-27 14:54:44 +04:00
|
|
|
Text("You can make it visible to your SimpleX contacts via Settings.")
|
2023-06-20 10:15:28 +04:00
|
|
|
.multilineTextAlignment(.center)
|
|
|
|
.font(.footnote)
|
|
|
|
.padding(.horizontal, 32)
|
2023-05-01 20:36:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func skipButton() -> some View {
|
|
|
|
VStack(spacing: 8) {
|
|
|
|
Button {
|
|
|
|
withAnimation {
|
2023-05-05 12:56:48 +04:00
|
|
|
onboardingStageDefault.set(.step4_SetNotificationsMode)
|
2023-05-01 20:36:52 +04:00
|
|
|
m.onboardingStage = .step4_SetNotificationsMode
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
Text("Don't create address")
|
|
|
|
Image(systemName: "chevron.right")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Text("You can create it later").font(.footnote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func shareQRCodeButton(_ userAddress: UserContactLink) -> some View {
|
|
|
|
Button {
|
2025-04-14 21:25:32 +01:00
|
|
|
showShareSheet(items: [simplexChatLink(userAddress.connLinkContact.simplexChatUri(short: false))])
|
2023-05-01 20:36:52 +04:00
|
|
|
} label: {
|
|
|
|
Label("Share", systemImage: "square.and.arrow.up")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func shareViaEmailButton(_ userAddress: UserContactLink) -> some View {
|
|
|
|
Button {
|
|
|
|
showMailView = true
|
|
|
|
} label: {
|
|
|
|
Label("Invite friends", systemImage: "envelope")
|
|
|
|
.font(.title2)
|
|
|
|
}
|
|
|
|
.sheet(isPresented: $showMailView) {
|
|
|
|
SendAddressMailView(
|
|
|
|
showMailView: $showMailView,
|
|
|
|
mailViewResult: $mailViewResult,
|
|
|
|
userAddress: userAddress
|
|
|
|
)
|
|
|
|
.edgesIgnoringSafeArea(.bottom)
|
|
|
|
}
|
|
|
|
.onChange(of: mailViewResult == nil) { _ in
|
|
|
|
if let r = mailViewResult {
|
|
|
|
switch r {
|
|
|
|
case let .success(composeResult):
|
|
|
|
switch composeResult {
|
|
|
|
case .sent:
|
2023-05-05 12:56:48 +04:00
|
|
|
onboardingStageDefault.set(.step4_SetNotificationsMode)
|
2023-05-01 20:36:52 +04:00
|
|
|
m.onboardingStage = .step4_SetNotificationsMode
|
|
|
|
default: ()
|
|
|
|
}
|
|
|
|
case let .failure(error):
|
|
|
|
logger.error("CreateSimpleXAddress share via email: \(responseError(error))")
|
|
|
|
let a = getErrorAlert(error, "Error sending email")
|
|
|
|
AlertManager.shared.showAlertMsg(
|
|
|
|
title: a.title,
|
|
|
|
message: a.message
|
|
|
|
)
|
|
|
|
}
|
|
|
|
mailViewResult = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func continueButton() -> some View {
|
|
|
|
Button {
|
|
|
|
withAnimation {
|
2023-05-05 12:56:48 +04:00
|
|
|
onboardingStageDefault.set(.step4_SetNotificationsMode)
|
2023-05-01 20:36:52 +04:00
|
|
|
m.onboardingStage = .step4_SetNotificationsMode
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
Text("Continue")
|
|
|
|
Image(systemName: "greaterthan")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SendAddressMailView: View {
|
|
|
|
@Binding var showMailView: Bool
|
|
|
|
@Binding var mailViewResult: Result<MFMailComposeResult, Error>?
|
|
|
|
var userAddress: UserContactLink
|
|
|
|
|
|
|
|
var body: some View {
|
2023-05-04 18:40:47 +03:00
|
|
|
let messageBody = String(format: NSLocalizedString("""
|
2023-05-01 20:36:52 +04:00
|
|
|
<p>Hi!</p>
|
2023-05-04 18:40:47 +03:00
|
|
|
<p><a href="%@">Connect to me via SimpleX Chat</a></p>
|
2025-04-14 21:25:32 +01:00
|
|
|
""", comment: "email text"), simplexChatLink(userAddress.connLinkContact.simplexChatUri(short: false)))
|
2023-05-01 20:36:52 +04:00
|
|
|
MailView(
|
|
|
|
isShowing: self.$showMailView,
|
|
|
|
result: $mailViewResult,
|
2023-05-04 18:40:47 +03:00
|
|
|
subject: NSLocalizedString("Let's talk in SimpleX Chat", comment: "email subject"),
|
2023-05-01 20:36:52 +04:00
|
|
|
messageBody: messageBody
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CreateSimpleXAddress_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
CreateSimpleXAddress()
|
|
|
|
}
|
|
|
|
}
|