SimpleX-Chat/apps/ios/Shared/Views/Chat/ChatInfoView.swift

216 lines
6.7 KiB
Swift
Raw Normal View History

//
// ChatInfoView.swift
// SimpleX
//
// Created by Evgeny Poberezkin on 05/02/2022.
// Copyright © 2022 SimpleX Chat. All rights reserved.
//
import SwiftUI
import SimpleXChat
2022-07-27 13:40:26 +04:00
func infoRow(_ title: LocalizedStringKey, _ value: String) -> some View {
HStack {
Text(title)
Spacer()
Text(value)
.foregroundStyle(.secondary)
}
}
func localizedInfoRow(_ title: LocalizedStringKey, _ value: LocalizedStringKey) -> some View {
HStack {
Text(title)
Spacer()
Text(value)
.foregroundStyle(.secondary)
}
}
@ViewBuilder func smpServers(_ title: LocalizedStringKey, _ servers: [String]?) -> some View {
if let servers = servers,
servers.count > 0 {
infoRow(title, serverHost(servers[0]))
}
}
private func serverHost(_ s: String) -> String {
if let i = s.range(of: "@")?.lowerBound {
return String(s[i...].dropFirst())
} else {
return s
}
}
struct ChatInfoView: View {
@EnvironmentObject var chatModel: ChatModel
@ObservedObject var chat: Chat
@Binding var showSheet: Bool
2022-07-27 13:40:26 +04:00
@State private var alert: ChatInfoViewAlert? = nil
@State private var connectionStats: ConnectionStats?
enum ChatInfoViewAlert: Identifiable {
case deleteContactAlert
case clearChatAlert
2022-07-27 13:40:26 +04:00
case networkStatusAlert
var id: ChatInfoViewAlert { get { self } }
}
var body: some View {
2022-07-27 13:40:26 +04:00
NavigationView {
List {
contactInfoHeader()
.listRowBackground(Color.clear)
2022-07-27 13:40:26 +04:00
if let connStats = connectionStats {
Section("Servers") {
networkStatusRow()
.onTapGesture {
alert = .networkStatusAlert
}
smpServers("Receiving via", connStats.rcvServers)
smpServers("Sending via", connStats.sndServers)
}
}
2022-07-27 13:40:26 +04:00
Section {
clearChatButton()
deleteContactButton()
}
Section(header: Text("For console")) {
infoRow("Local name", chat.chatInfo.localDisplayName)
infoRow("Database ID", "\(chat.chatInfo.apiId)")
}
2022-07-14 16:40:32 +04:00
}
2022-07-27 13:40:26 +04:00
.navigationBarHidden(true)
2022-07-14 16:40:32 +04:00
}
2022-07-27 13:40:26 +04:00
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
2022-07-14 16:40:32 +04:00
.alert(item: $alert) { alertItem in
switch(alertItem) {
case .deleteContactAlert: return deleteContactAlert()
2022-07-14 16:40:32 +04:00
case .clearChatAlert: return clearChatAlert()
2022-07-27 13:40:26 +04:00
case .networkStatusAlert: return networkStatusAlert()
}
}
.task {
do {
let stats = try await apiContactInfo(contactId: chat.chatInfo.apiId)
await MainActor.run { connectionStats = stats }
} catch let error {
logger.error("apiContactInfo error: \(responseError(error))")
}
}
2022-07-27 13:40:26 +04:00
}
func contactInfoHeader() -> some View {
VStack {
let cInfo = chat.chatInfo
ChatInfoImage(chat: chat, color: Color(uiColor: .tertiarySystemFill))
.frame(width: 192, height: 192)
.padding(.top, 12)
.padding()
Text(cInfo.displayName)
.font(.largeTitle)
.lineLimit(1)
.padding(.bottom, 2)
if cInfo.fullName != "" && cInfo.fullName != cInfo.displayName {
Text(cInfo.fullName)
.font(.title2)
.lineLimit(2)
}
}
.frame(maxWidth: .infinity, alignment: .center)
}
func networkStatusRow() -> some View {
HStack {
Text("Network status")
Image(systemName: "info.circle")
.foregroundColor(.accentColor)
.font(.system(size: 14))
Spacer()
Text(chat.serverInfo.networkStatus.statusString)
.foregroundColor(.secondary)
serverImage()
}
}
func serverImage() -> some View {
let status = chat.serverInfo.networkStatus
return Image(systemName: status.imageName)
.foregroundColor(status == .connected ? .green : .secondary)
2022-07-27 13:40:26 +04:00
.font(.system(size: 12))
}
func deleteContactButton() -> some View {
Button(role: .destructive) {
alert = .deleteContactAlert
} label: {
Label("Delete contact", systemImage: "trash")
.foregroundColor(Color.red)
}
}
func clearChatButton() -> some View {
Button() {
alert = .clearChatAlert
} label: {
Label("Clear conversation", systemImage: "gobackward")
.foregroundColor(Color.orange)
}
}
private func deleteContactAlert() -> Alert {
Alert(
title: Text("Delete contact?"),
message: Text("Contact and all messages will be deleted - this cannot be undone!"),
primaryButton: .destructive(Text("Delete")) {
Task {
do {
try await apiDeleteChat(type: chat.chatInfo.chatType, id: chat.chatInfo.apiId)
await MainActor.run {
chatModel.removeChat(chat.chatInfo.id)
showSheet = false
}
} catch let error {
logger.error("deleteContactAlert apiDeleteChat error: \(error.localizedDescription)")
}
}
},
secondaryButton: .cancel()
)
}
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)
await MainActor.run {
showSheet = false
}
}
},
secondaryButton: .cancel()
)
}
2022-07-27 13:40:26 +04:00
private func networkStatusAlert() -> Alert {
Alert(
title: Text("Network status"),
message: Text(chat.serverInfo.networkStatus.statusExplanation)
)
}
}
struct ChatInfoView_Previews: PreviewProvider {
static var previews: some View {
@State var showSheet = true
return ChatInfoView(chat: Chat(chatInfo: ChatInfo.sampleData.direct, chatItems: []), showSheet: $showSheet)
}
}