From eeea33c7cbb8c2f6a69a6ea18434cf42a45c9b86 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 7 Feb 2022 10:36:11 +0000 Subject: [PATCH] fix loading chat, contact connection status info (#277) --- apps/ios/Shared/ContentView.swift | 10 +++ apps/ios/Shared/Model/ChatModel.swift | 16 ++++- apps/ios/Shared/Model/SimpleXAPI.swift | 1 - apps/ios/Shared/SimpleXApp.swift | 10 --- apps/ios/Shared/Views/Chat/ChatInfoView.swift | 61 +++++++++++++++++-- apps/ios/Shared/Views/Chat/ChatView.swift | 2 +- 6 files changed, 80 insertions(+), 20 deletions(-) diff --git a/apps/ios/Shared/ContentView.swift b/apps/ios/Shared/ContentView.swift index 0671516534..a66c3e503a 100644 --- a/apps/ios/Shared/ContentView.swift +++ b/apps/ios/Shared/ContentView.swift @@ -20,6 +20,16 @@ struct ContentView: View { } catch { fatalError("Failed to start or load chats: \(error)") } + + DispatchQueue.global().async { + while(true) { + do { + try processReceivedMsg(chatModel, chatRecvMsg()) + } catch { + print("error receiving message: ", error) + } + } + } } } else { WelcomeView() diff --git a/apps/ios/Shared/Model/ChatModel.swift b/apps/ios/Shared/Model/ChatModel.swift index d8709e3b50..fe539577d2 100644 --- a/apps/ios/Shared/Model/ChatModel.swift +++ b/apps/ios/Shared/Model/ChatModel.swift @@ -237,9 +237,19 @@ final class Chat: ObservableObject, Identifiable { var statusString: String { get { switch self { - case .connected: return "Connected to contact's server" - case let .error(err): return "Connecting to contact's server… (error: \(err))" - default: return "Connecting to contact's server…" + case .connected: return "Server connected" + case let .error(err): return "Connecting server… (error: \(err))" + default: return "Connecting server…" + } + } + } + + var statusExplanation: String { + get { + switch self { + case .connected: return "You are connected to the server you use to receve messages from this contact." + case let .error(err): return "Trying to connect to the server you use to receve messages from this contact (error: \(err))." + default: return "Trying to connect to the server you use to receve messages from this contact." } } } diff --git a/apps/ios/Shared/Model/SimpleXAPI.swift b/apps/ios/Shared/Model/SimpleXAPI.swift index 4d6aa8e62e..46e2a434f6 100644 --- a/apps/ios/Shared/Model/SimpleXAPI.swift +++ b/apps/ios/Shared/Model/SimpleXAPI.swift @@ -187,7 +187,6 @@ enum TerminalItem: Identifiable { func chatSendCmd(_ cmd: ChatCommand) throws -> ChatResponse { var c = cmd.cmdString.cString(using: .utf8)! - print("command", cmd.cmdString) // TODO some mechanism to update model without passing it - maybe Publisher / Subscriber? // DispatchQueue.main.async { // termId += 1 diff --git a/apps/ios/Shared/SimpleXApp.swift b/apps/ios/Shared/SimpleXApp.swift index 301f8522d3..35f3cad9b7 100644 --- a/apps/ios/Shared/SimpleXApp.swift +++ b/apps/ios/Shared/SimpleXApp.swift @@ -25,16 +25,6 @@ struct SimpleXApp: App { print(url) } .onAppear() { - DispatchQueue.global().async { - while(true) { - do { - try processReceivedMsg(chatModel, chatRecvMsg()) - } catch { - print("error receiving message: ", error) - } - } - } - do { chatModel.currentUser = try apiGetActiveUser() } catch { diff --git a/apps/ios/Shared/Views/Chat/ChatInfoView.swift b/apps/ios/Shared/Views/Chat/ChatInfoView.swift index e1cee27f3a..2932478eeb 100644 --- a/apps/ios/Shared/Views/Chat/ChatInfoView.swift +++ b/apps/ios/Shared/Views/Chat/ChatInfoView.swift @@ -9,7 +9,12 @@ import SwiftUI struct ChatInfoView: View { + @EnvironmentObject var chatModel: ChatModel @ObservedObject var chat: Chat + @Binding var showChatInfo: Bool + @State private var showDeleteContactAlert = false + @State private var alertContact: Contact? + @State private var showNetworkStatusInfo = false var body: some View { VStack{ @@ -22,10 +27,36 @@ struct ChatInfoView: View { Text(chat.chatInfo.fullName).font(.title) .padding(.bottom) - if case .direct = chat.chatInfo { - HStack { - serverImage() - Text(chat.serverInfo.networkStatus.statusString) + if case let .direct(contact) = chat.chatInfo { + VStack { + HStack { + Button { + showNetworkStatusInfo.toggle() + } label: { + serverImage() + Text(chat.serverInfo.networkStatus.statusString) + .foregroundColor(.primary) + } + } + if showNetworkStatusInfo { + Text(chat.serverInfo.networkStatus.statusExplanation) + .font(.subheadline) + .multilineTextAlignment(.center) + .padding(.horizontal, 64) + .padding(.vertical, 8) + } + + Spacer() + Button(role: .destructive) { + alertContact = contact + showDeleteContactAlert = true + } label: { + Label("Delete contact", systemImage: "trash") + } + .padding() + .alert(isPresented: $showDeleteContactAlert) { + deleteContactAlert(alertContact!) + } } } } @@ -37,12 +68,32 @@ struct ChatInfoView: View { return Image(systemName: status.imageName) .foregroundColor(status == .connected ? .green : .secondary) } + + private func deleteContactAlert(_ contact: Contact) -> Alert { + Alert( + title: Text("Delete contact?"), + message: Text("Contact and all messages will be deleted"), + primaryButton: .destructive(Text("Delete")) { + do { + try apiDeleteChat(type: .direct, id: contact.apiId) + chatModel.removeChat(contact.id) + showChatInfo = false + } catch let error { + print("Error: \(error)") + } + alertContact = nil + }, secondaryButton: .cancel() { + alertContact = nil + } + ) + } } struct ChatInfoView_Previews: PreviewProvider { var chatInfo = sampleDirectChatInfo static var previews: some View { - ChatInfoView(chat: Chat(chatInfo: sampleDirectChatInfo, chatItems: [])) + @State var showChatInfo = true + return ChatInfoView(chat: Chat(chatInfo: sampleDirectChatInfo, chatItems: []), showChatInfo: $showChatInfo) } } diff --git a/apps/ios/Shared/Views/Chat/ChatView.swift b/apps/ios/Shared/Views/Chat/ChatView.swift index 0d24aa43be..0bfe69d115 100644 --- a/apps/ios/Shared/Views/Chat/ChatView.swift +++ b/apps/ios/Shared/Views/Chat/ChatView.swift @@ -64,7 +64,7 @@ struct ChatView: View { .foregroundColor(.primary) } .sheet(isPresented: $showChatInfo) { - ChatInfoView(chat: chat) + ChatInfoView(chat: chat, showChatInfo: $showChatInfo) } } }