2022-02-05 20:10:47 +00:00
|
|
|
//
|
|
|
|
// ChatInfoView.swift
|
|
|
|
// SimpleX
|
|
|
|
//
|
|
|
|
// Created by Evgeny Poberezkin on 05/02/2022.
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2022-05-31 07:55:13 +01:00
|
|
|
import SimpleXChat
|
2022-02-05 20:10:47 +00:00
|
|
|
|
|
|
|
struct ChatInfoView: View {
|
2022-02-07 10:36:11 +00:00
|
|
|
@EnvironmentObject var chatModel: ChatModel
|
2022-02-12 15:59:43 +00:00
|
|
|
@ObservedObject var alertManager = AlertManager.shared
|
2022-02-05 20:10:47 +00:00
|
|
|
@ObservedObject var chat: Chat
|
2022-07-26 12:33:10 +04:00
|
|
|
@Binding var showSheet: Bool
|
2022-05-19 16:56:34 +04:00
|
|
|
@State var alert: ChatInfoViewAlert? = nil
|
2022-02-05 20:10:47 +00:00
|
|
|
|
2022-05-19 16:56:34 +04:00
|
|
|
enum ChatInfoViewAlert: Identifiable {
|
|
|
|
case deleteContactAlert
|
|
|
|
case clearChatAlert
|
|
|
|
|
|
|
|
var id: ChatInfoViewAlert { get { self } }
|
|
|
|
}
|
|
|
|
|
2022-02-05 20:10:47 +00:00
|
|
|
var body: some View {
|
2022-07-26 12:33:10 +04:00
|
|
|
VStack {
|
2022-02-05 20:10:47 +00:00
|
|
|
ChatInfoImage(chat: chat)
|
|
|
|
.frame(width: 192, height: 192)
|
|
|
|
.padding(.top, 48)
|
|
|
|
.padding()
|
|
|
|
Text(chat.chatInfo.localDisplayName).font(.largeTitle)
|
|
|
|
.padding(.bottom, 2)
|
|
|
|
Text(chat.chatInfo.fullName).font(.title)
|
|
|
|
.padding(.bottom)
|
|
|
|
|
2022-07-14 16:40:32 +04:00
|
|
|
HStack {
|
|
|
|
serverImage()
|
|
|
|
Text(chat.serverInfo.networkStatus.statusString)
|
|
|
|
.foregroundColor(.primary)
|
|
|
|
}
|
|
|
|
Text(chat.serverInfo.networkStatus.statusExplanation)
|
|
|
|
.font(.subheadline)
|
|
|
|
.multilineTextAlignment(.center)
|
|
|
|
.padding(.horizontal, 64)
|
|
|
|
.padding(.vertical, 8)
|
2022-02-07 10:36:11 +00:00
|
|
|
|
2022-07-14 16:40:32 +04:00
|
|
|
Spacer()
|
|
|
|
Button() {
|
|
|
|
alert = .clearChatAlert
|
|
|
|
} label: {
|
|
|
|
Label("Clear conversation", systemImage: "gobackward")
|
2022-02-05 20:10:47 +00:00
|
|
|
}
|
2022-07-14 16:40:32 +04:00
|
|
|
.tint(Color.orange)
|
|
|
|
Button(role: .destructive) {
|
|
|
|
alert = .deleteContactAlert
|
|
|
|
} label: {
|
|
|
|
Label("Delete contact", systemImage: "trash")
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
.alert(item: $alert) { alertItem in
|
|
|
|
switch(alertItem) {
|
2022-07-26 12:33:10 +04:00
|
|
|
case .deleteContactAlert: return deleteContactAlert()
|
2022-07-14 16:40:32 +04:00
|
|
|
case .clearChatAlert: return clearChatAlert()
|
2022-05-19 16:56:34 +04:00
|
|
|
}
|
2022-02-05 20:10:47 +00:00
|
|
|
}
|
2022-07-14 16:40:32 +04:00
|
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
|
2022-02-05 20:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func serverImage() -> some View {
|
|
|
|
let status = chat.serverInfo.networkStatus
|
|
|
|
return Image(systemName: status.imageName)
|
|
|
|
.foregroundColor(status == .connected ? .green : .secondary)
|
|
|
|
}
|
2022-02-07 10:36:11 +00:00
|
|
|
|
2022-07-26 12:33:10 +04:00
|
|
|
private func deleteContactAlert() -> Alert {
|
2022-02-07 10:36:11 +00:00
|
|
|
Alert(
|
|
|
|
title: Text("Delete contact?"),
|
2022-04-16 09:37:01 +01:00
|
|
|
message: Text("Contact and all messages will be deleted - this cannot be undone!"),
|
2022-02-07 10:36:11 +00:00
|
|
|
primaryButton: .destructive(Text("Delete")) {
|
2022-02-24 17:16:41 +00:00
|
|
|
Task {
|
|
|
|
do {
|
2022-07-26 12:33:10 +04:00
|
|
|
try await apiDeleteChat(type: chat.chatInfo.chatType, id: chat.chatInfo.apiId)
|
|
|
|
await MainActor.run {
|
|
|
|
chatModel.removeChat(chat.chatInfo.id)
|
|
|
|
showSheet = false
|
2022-02-24 17:16:41 +00:00
|
|
|
}
|
|
|
|
} catch let error {
|
2022-07-26 12:33:10 +04:00
|
|
|
logger.error("deleteContactAlert apiDeleteChat error: \(error.localizedDescription)")
|
2022-02-24 17:16:41 +00:00
|
|
|
}
|
2022-02-07 10:36:11 +00:00
|
|
|
}
|
2022-02-12 15:59:43 +00:00
|
|
|
},
|
|
|
|
secondaryButton: .cancel()
|
2022-02-07 10:36:11 +00:00
|
|
|
)
|
|
|
|
}
|
2022-05-19 16:56:34 +04:00
|
|
|
|
|
|
|
private func clearChatAlert() -> Alert {
|
|
|
|
Alert(
|
|
|
|
title: Text("Clear conversation?"),
|
|
|
|
message: Text("All messages will be deleted - this cannot be undone! The messages will be deleted ONLY for you."),
|
|
|
|
primaryButton: .destructive(Text("Clear")) {
|
|
|
|
Task {
|
|
|
|
await clearChat(chat)
|
2022-07-26 12:33:10 +04:00
|
|
|
await MainActor.run {
|
|
|
|
showSheet = false
|
2022-05-19 16:56:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
secondaryButton: .cancel()
|
|
|
|
)
|
|
|
|
}
|
2022-02-05 20:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ChatInfoView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-07-26 12:33:10 +04:00
|
|
|
@State var showSheet = true
|
|
|
|
return ChatInfoView(chat: Chat(chatInfo: ChatInfo.sampleData.direct, chatItems: []), showSheet: $showSheet)
|
2022-02-05 20:10:47 +00:00
|
|
|
}
|
|
|
|
}
|