2022-07-14 16:40:32 +04:00
|
|
|
|
//
|
|
|
|
|
|
// AddGroupView.swift
|
|
|
|
|
|
// SimpleX (iOS)
|
|
|
|
|
|
//
|
|
|
|
|
|
// Created by JRoberts on 13.07.2022.
|
|
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
import SimpleXChat
|
|
|
|
|
|
|
|
|
|
|
|
struct AddGroupView: View {
|
|
|
|
|
|
@Binding var openedSheet: NewChatAction?
|
|
|
|
|
|
@EnvironmentObject var m: ChatModel
|
2022-07-28 11:49:36 +01:00
|
|
|
|
@State private var profile = GroupProfile(displayName: "", fullName: "")
|
2022-07-14 16:40:32 +04:00
|
|
|
|
@FocusState private var focusDisplayName
|
|
|
|
|
|
@FocusState private var focusFullName
|
2022-07-28 11:49:36 +01:00
|
|
|
|
@State private var showChooseSource = false
|
|
|
|
|
|
@State private var showImagePicker = false
|
|
|
|
|
|
@State private var showTakePhoto = false
|
|
|
|
|
|
@State private var chosenImage: UIImage? = nil
|
2022-07-14 16:40:32 +04:00
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
|
VStack(alignment: .leading) {
|
2022-07-28 11:49:36 +01:00
|
|
|
|
Text("Create secret group")
|
2022-07-14 16:40:32 +04:00
|
|
|
|
.font(.largeTitle)
|
2022-07-28 11:49:36 +01:00
|
|
|
|
.padding(.vertical, 4)
|
|
|
|
|
|
Text("The group is fully decentralized – it is visible only to the members.")
|
2022-07-14 16:40:32 +04:00
|
|
|
|
.padding(.bottom, 4)
|
2022-07-26 10:55:58 +04:00
|
|
|
|
.padding(.bottom)
|
2022-07-28 11:49:36 +01:00
|
|
|
|
|
|
|
|
|
|
ZStack(alignment: .center) {
|
|
|
|
|
|
ZStack(alignment: .topTrailing) {
|
|
|
|
|
|
profileImageView(profile.image)
|
|
|
|
|
|
if profile.image != nil {
|
|
|
|
|
|
Button {
|
|
|
|
|
|
profile.image = nil
|
|
|
|
|
|
} label: {
|
|
|
|
|
|
Image(systemName: "multiply")
|
|
|
|
|
|
.resizable()
|
|
|
|
|
|
.aspectRatio(contentMode: .fit)
|
|
|
|
|
|
.frame(width: 12)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
editImageButton { showChooseSource = true }
|
|
|
|
|
|
}
|
|
|
|
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
|
|
|
|
.padding(.bottom, 4)
|
|
|
|
|
|
|
2022-07-14 16:40:32 +04:00
|
|
|
|
ZStack(alignment: .topLeading) {
|
2022-07-28 11:49:36 +01:00
|
|
|
|
if !validDisplayName(profile.displayName) {
|
2022-07-14 16:40:32 +04:00
|
|
|
|
Image(systemName: "exclamationmark.circle")
|
|
|
|
|
|
.foregroundColor(.red)
|
|
|
|
|
|
.padding(.top, 4)
|
|
|
|
|
|
}
|
2022-07-28 11:49:36 +01:00
|
|
|
|
textField("Group display name", text: $profile.displayName)
|
2022-07-14 16:40:32 +04:00
|
|
|
|
.focused($focusDisplayName)
|
|
|
|
|
|
.submitLabel(.next)
|
|
|
|
|
|
.onSubmit {
|
|
|
|
|
|
if canCreateProfile() { focusFullName = true }
|
|
|
|
|
|
else { focusDisplayName = true }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-07-28 11:49:36 +01:00
|
|
|
|
textField("Group full name (optional)", text: $profile.fullName)
|
2022-07-14 16:40:32 +04:00
|
|
|
|
.focused($focusFullName)
|
|
|
|
|
|
.submitLabel(.go)
|
|
|
|
|
|
.onSubmit {
|
|
|
|
|
|
if canCreateProfile() { createGroup() }
|
|
|
|
|
|
else { focusFullName = true }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
|
|
|
|
Button {
|
|
|
|
|
|
createGroup()
|
|
|
|
|
|
} label: {
|
|
|
|
|
|
Text("Create")
|
|
|
|
|
|
Image(systemName: "greaterthan")
|
|
|
|
|
|
}
|
|
|
|
|
|
.disabled(!canCreateProfile())
|
|
|
|
|
|
.frame(maxWidth: .infinity, alignment: .trailing)
|
|
|
|
|
|
}
|
|
|
|
|
|
.onAppear() {
|
2022-07-28 11:49:36 +01:00
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
|
|
|
|
focusDisplayName = true
|
|
|
|
|
|
}
|
2022-07-14 16:40:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
.padding()
|
2022-07-28 11:49:36 +01:00
|
|
|
|
.confirmationDialog("Group image", isPresented: $showChooseSource, titleVisibility: .visible) {
|
|
|
|
|
|
Button("Take picture") {
|
|
|
|
|
|
showTakePhoto = true
|
|
|
|
|
|
}
|
|
|
|
|
|
Button("Choose from library") {
|
|
|
|
|
|
showImagePicker = true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
.fullScreenCover(isPresented: $showTakePhoto) {
|
|
|
|
|
|
ZStack {
|
|
|
|
|
|
Color.black.edgesIgnoringSafeArea(.all)
|
|
|
|
|
|
CameraImagePicker(image: $chosenImage)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
.sheet(isPresented: $showImagePicker) {
|
|
|
|
|
|
LibraryImagePicker(image: $chosenImage) {
|
|
|
|
|
|
didSelectItem in showImagePicker = false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
.onChange(of: chosenImage) { image in
|
|
|
|
|
|
if let image = image {
|
|
|
|
|
|
profile.image = resizeImageToStrSize(cropToSquare(image), maxDataSize: 12500)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
profile.image = nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
.contentShape(Rectangle())
|
|
|
|
|
|
.onTapGesture { hideKeyboard() }
|
2022-07-14 16:40:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func textField(_ placeholder: LocalizedStringKey, text: Binding<String>) -> some View {
|
|
|
|
|
|
TextField(placeholder, text: text)
|
|
|
|
|
|
.textInputAutocapitalization(.never)
|
|
|
|
|
|
.disableAutocorrection(true)
|
|
|
|
|
|
.padding(.leading, 28)
|
|
|
|
|
|
.padding(.bottom)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func createGroup() {
|
|
|
|
|
|
hideKeyboard()
|
|
|
|
|
|
do {
|
2022-07-28 11:49:36 +01:00
|
|
|
|
let groupInfo = try apiNewGroup(profile)
|
2022-07-14 16:40:32 +04:00
|
|
|
|
m.addChat(Chat(chatInfo: .group(groupInfo: groupInfo), chatItems: []))
|
|
|
|
|
|
openedSheet = nil
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
|
m.chatId = groupInfo.id
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch {
|
2022-07-27 11:16:07 +04:00
|
|
|
|
openedSheet = nil
|
|
|
|
|
|
AlertManager.shared.showAlert(
|
|
|
|
|
|
Alert(
|
2022-07-28 11:49:36 +01:00
|
|
|
|
title: Text("Error creating group"),
|
2022-07-27 11:16:07 +04:00
|
|
|
|
message: Text(responseError(error))
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2022-07-14 16:40:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func hideKeyboard() {
|
|
|
|
|
|
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func canCreateProfile() -> Bool {
|
2022-07-28 11:49:36 +01:00
|
|
|
|
profile.displayName != "" && validDisplayName(profile.displayName)
|
2022-07-14 16:40:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct AddGroupView_Previews: PreviewProvider {
|
|
|
|
|
|
static var previews: some View {
|
|
|
|
|
|
@State var openedSheet: NewChatAction? = nil
|
|
|
|
|
|
return AddGroupView(openedSheet: $openedSheet)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|