2023-03-21 18:15:48 +03:00
|
|
|
//
|
|
|
|
// GroupWelcomeView.swift
|
|
|
|
// SimpleX (iOS)
|
|
|
|
//
|
|
|
|
// Created by Avently on 21/03/2022.
|
|
|
|
// Copyright © 2023 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import SimpleXChat
|
|
|
|
|
|
|
|
struct GroupWelcomeView: View {
|
|
|
|
@Environment(\.dismiss) var dismiss: DismissAction
|
|
|
|
@Binding var groupInfo: GroupInfo
|
2024-01-25 16:08:10 +04:00
|
|
|
@State var groupProfile: GroupProfile
|
|
|
|
@State var welcomeText: String
|
2023-04-28 20:34:24 +04:00
|
|
|
@State private var editMode = true
|
2023-03-21 18:15:48 +03:00
|
|
|
@FocusState private var keyboardVisible: Bool
|
|
|
|
@State private var showSaveDialog = false
|
|
|
|
|
|
|
|
var body: some View {
|
2023-04-28 20:34:24 +04:00
|
|
|
VStack {
|
|
|
|
if groupInfo.canEdit {
|
|
|
|
editorView()
|
|
|
|
.modifier(BackButton {
|
2024-01-25 16:08:10 +04:00
|
|
|
if welcomeTextUnchanged() {
|
2023-04-28 20:34:24 +04:00
|
|
|
dismiss()
|
|
|
|
} else {
|
|
|
|
showSaveDialog = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.confirmationDialog("Save welcome message?", isPresented: $showSaveDialog) {
|
|
|
|
Button("Save and update group profile") {
|
|
|
|
save()
|
|
|
|
}
|
|
|
|
Button("Exit without saving") { dismiss() }
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
List {
|
|
|
|
Section {
|
|
|
|
textPreview()
|
|
|
|
copyButton()
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 18:15:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.onAppear {
|
2024-01-25 16:08:10 +04:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
|
|
keyboardVisible = true
|
|
|
|
}
|
2023-03-21 18:15:48 +03:00
|
|
|
}
|
2023-04-28 20:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private func textPreview() -> some View {
|
2023-12-30 18:57:10 +00:00
|
|
|
messageText(welcomeText, parseSimpleXMarkdown(welcomeText), nil, showSecrets: false)
|
2023-05-23 14:31:43 +04:00
|
|
|
.frame(minHeight: 140, alignment: .topLeading)
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
2023-04-28 20:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private func editorView() -> some View {
|
|
|
|
List {
|
|
|
|
Section {
|
|
|
|
if editMode {
|
|
|
|
ZStack {
|
|
|
|
Group {
|
|
|
|
if welcomeText.isEmpty {
|
|
|
|
TextEditor(text: Binding.constant(NSLocalizedString("Enter welcome message…", comment: "placeholder")))
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.disabled(true)
|
|
|
|
}
|
|
|
|
TextEditor(text: $welcomeText)
|
|
|
|
.focused($keyboardVisible)
|
|
|
|
}
|
|
|
|
.padding(.horizontal, -5)
|
|
|
|
.padding(.top, -8)
|
2023-05-05 15:49:10 +04:00
|
|
|
.frame(height: 140, alignment: .topLeading)
|
2023-04-28 20:34:24 +04:00
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
textPreview()
|
|
|
|
}
|
|
|
|
|
|
|
|
Button {
|
|
|
|
editMode = !editMode
|
|
|
|
keyboardVisible = editMode
|
|
|
|
} label: {
|
|
|
|
if editMode {
|
|
|
|
Label ("Preview", systemImage: "character")
|
|
|
|
} else {
|
|
|
|
Label ("Edit", systemImage: "pencil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.disabled(welcomeText.isEmpty)
|
|
|
|
copyButton()
|
2023-03-21 18:15:48 +03:00
|
|
|
}
|
2023-04-28 20:34:24 +04:00
|
|
|
|
|
|
|
Section {
|
|
|
|
saveButton()
|
2023-03-21 18:15:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 20:34:24 +04:00
|
|
|
private func copyButton() -> some View {
|
|
|
|
Button {
|
|
|
|
UIPasteboard.general.string = welcomeText
|
|
|
|
} label: {
|
|
|
|
Label ("Copy", systemImage: "doc.on.doc")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func saveButton() -> some View {
|
2023-03-21 18:15:48 +03:00
|
|
|
Button("Save and update group profile") {
|
|
|
|
save()
|
|
|
|
}
|
2024-01-25 16:08:10 +04:00
|
|
|
.disabled(welcomeTextUnchanged())
|
|
|
|
}
|
|
|
|
|
|
|
|
private func welcomeTextUnchanged() -> Bool {
|
|
|
|
welcomeText == groupInfo.groupProfile.description || (welcomeText == "" && groupInfo.groupProfile.description == nil)
|
2023-03-21 18:15:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private func save() {
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
var welcome: String? = welcomeText.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
if welcome?.count == 0 {
|
|
|
|
welcome = nil
|
|
|
|
}
|
2024-01-25 16:08:10 +04:00
|
|
|
groupProfile.description = welcome
|
|
|
|
let gInfo = try await apiUpdateGroup(groupInfo.groupId, groupProfile)
|
|
|
|
await MainActor.run {
|
|
|
|
groupInfo = gInfo
|
|
|
|
ChatModel.shared.updateGroup(gInfo)
|
|
|
|
dismiss()
|
|
|
|
}
|
2023-03-21 18:15:48 +03:00
|
|
|
} catch let error {
|
|
|
|
logger.error("apiUpdateGroup error: \(responseError(error))")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GroupWelcomeView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2024-01-25 16:08:10 +04:00
|
|
|
GroupProfileView(groupInfo: Binding.constant(GroupInfo.sampleData), groupProfile: GroupProfile.sampleData)
|
2023-03-21 18:15:48 +03:00
|
|
|
}
|
|
|
|
}
|