2022-05-09 09:52:09 +01:00
|
|
|
//
|
|
|
|
// CreateProfile.swift
|
|
|
|
// SimpleX (iOS)
|
|
|
|
//
|
|
|
|
// Created by Evgeny on 07/05/2022.
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2022-05-31 07:55:13 +01:00
|
|
|
import SimpleXChat
|
2022-05-09 09:52:09 +01:00
|
|
|
|
|
|
|
struct CreateProfile: View {
|
|
|
|
@EnvironmentObject var m: ChatModel
|
2023-01-20 12:38:38 +00:00
|
|
|
@Environment(\.dismiss) var dismiss
|
2022-05-09 09:52:09 +01:00
|
|
|
@State private var displayName: String = ""
|
|
|
|
@State private var fullName: String = ""
|
|
|
|
@FocusState private var focusDisplayName
|
|
|
|
@FocusState private var focusFullName
|
|
|
|
|
|
|
|
var body: some View {
|
2022-05-09 17:40:39 +01:00
|
|
|
VStack(alignment: .leading) {
|
|
|
|
Text("Create your profile")
|
|
|
|
.font(.largeTitle)
|
|
|
|
.padding(.bottom, 4)
|
|
|
|
Text("Your profile, contacts and delivered messages are stored on your device.")
|
|
|
|
.padding(.bottom, 4)
|
|
|
|
Text("The profile is only shared with your contacts.")
|
|
|
|
.padding(.bottom)
|
|
|
|
ZStack(alignment: .topLeading) {
|
|
|
|
if !validDisplayName(displayName) {
|
|
|
|
Image(systemName: "exclamationmark.circle")
|
|
|
|
.foregroundColor(.red)
|
|
|
|
.padding(.top, 4)
|
2022-05-09 09:52:09 +01:00
|
|
|
}
|
2022-05-09 17:40:39 +01:00
|
|
|
textField("Display name", text: $displayName)
|
|
|
|
.focused($focusDisplayName)
|
|
|
|
.submitLabel(.next)
|
2022-05-09 09:52:09 +01:00
|
|
|
.onSubmit {
|
2022-05-09 17:40:39 +01:00
|
|
|
if canCreateProfile() { focusFullName = true }
|
|
|
|
else { focusDisplayName = true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
textField("Full name (optional)", text: $fullName)
|
|
|
|
.focused($focusFullName)
|
|
|
|
.submitLabel(.go)
|
|
|
|
.onSubmit {
|
|
|
|
if canCreateProfile() { createProfile() }
|
|
|
|
else { focusFullName = true }
|
|
|
|
}
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
HStack {
|
2023-01-23 13:20:58 +00:00
|
|
|
if m.users.isEmpty {
|
|
|
|
Button {
|
|
|
|
hideKeyboard()
|
|
|
|
withAnimation {
|
|
|
|
m.onboardingStage = .step1_SimpleXInfo
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
Image(systemName: "lessthan")
|
|
|
|
Text("About SimpleX")
|
|
|
|
}
|
2022-05-09 09:52:09 +01:00
|
|
|
}
|
2022-05-09 17:40:39 +01:00
|
|
|
}
|
2022-05-09 09:52:09 +01:00
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
Button {
|
2022-05-09 17:40:39 +01:00
|
|
|
createProfile()
|
2022-05-09 09:52:09 +01:00
|
|
|
} label: {
|
2022-05-09 17:40:39 +01:00
|
|
|
Text("Create")
|
|
|
|
Image(systemName: "greaterthan")
|
2022-05-09 09:52:09 +01:00
|
|
|
}
|
2022-05-09 17:40:39 +01:00
|
|
|
.disabled(!canCreateProfile())
|
2022-05-09 09:52:09 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-09 17:40:39 +01:00
|
|
|
}
|
|
|
|
.onAppear() {
|
|
|
|
focusDisplayName = true
|
2022-12-26 14:08:01 +00:00
|
|
|
setLastVersionDefault()
|
2022-05-09 09:52:09 +01:00
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
|
|
|
|
func textField(_ placeholder: LocalizedStringKey, text: Binding<String>) -> some View {
|
|
|
|
TextField(placeholder, text: text)
|
|
|
|
.textInputAutocapitalization(.never)
|
|
|
|
.disableAutocorrection(true)
|
|
|
|
.padding(.leading, 28)
|
|
|
|
.padding(.bottom)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createProfile() {
|
|
|
|
hideKeyboard()
|
|
|
|
let profile = Profile(
|
|
|
|
displayName: displayName,
|
|
|
|
fullName: fullName
|
|
|
|
)
|
|
|
|
do {
|
|
|
|
m.currentUser = try apiCreateActiveUser(profile)
|
2023-01-20 12:38:38 +00:00
|
|
|
if m.users.isEmpty {
|
|
|
|
try startChat()
|
2023-01-19 16:22:56 +00:00
|
|
|
withAnimation { m.onboardingStage = .step3_SetNotificationsMode }
|
|
|
|
} else {
|
2023-01-20 12:38:38 +00:00
|
|
|
dismiss()
|
|
|
|
m.users = try listUsers()
|
|
|
|
try getUserChatData()
|
2023-01-19 16:22:56 +00:00
|
|
|
}
|
2022-05-09 09:52:09 +01:00
|
|
|
} catch {
|
2022-06-24 13:52:20 +01:00
|
|
|
fatalError("Failed to create user or start chat: \(responseError(error))")
|
2022-05-09 09:52:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func canCreateProfile() -> Bool {
|
|
|
|
displayName != "" && validDisplayName(displayName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 15:07:28 +01:00
|
|
|
func validDisplayName(_ name: String) -> Bool {
|
2022-07-28 11:49:36 +01:00
|
|
|
name.firstIndex(of: " ") == nil && name.first != "@" && name.first != "#"
|
2022-05-12 15:07:28 +01:00
|
|
|
}
|
|
|
|
|
2022-05-09 09:52:09 +01:00
|
|
|
struct CreateProfile_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
CreateProfile()
|
|
|
|
}
|
|
|
|
}
|