diff --git a/apps/ios/Shared/Views/ChatList/ChatListView.swift b/apps/ios/Shared/Views/ChatList/ChatListView.swift index f30756a345..3291c9eb8a 100644 --- a/apps/ios/Shared/Views/ChatList/ChatListView.swift +++ b/apps/ios/Shared/Views/ChatList/ChatListView.swift @@ -277,6 +277,8 @@ private struct ChatListSearchBar: View { @Binding var searchMode: Bool @Binding var searchText: String @State private var showScanCodeSheet = false + @State private var alert: PlanAndConnectAlert? + @State private var sheet: PlanAndConnectActionSheet? var body: some View { VStack(spacing: 12) { @@ -329,6 +331,33 @@ private struct ChatListSearchBar: View { NewChatView(selection: .connect, showQRCodeScanner: true) .environment(\EnvironmentValues.refresh as! WritableKeyPath, nil) // fixes .refreshable in ChatListView affecting nested view } + .onChange(of: searchText) { t in + let link = t.trimmingCharacters(in: .whitespaces) + if strIsSimplexLink(link) { + hideKeyboard() + searchText = "" + withAnimation { + searchMode = false + } + connect(link) + } + } + .alert(item: $alert) { a in + planAndConnectAlert(a, dismiss: true, onCancel: { searchText = "" }) + } + .actionSheet(item: $sheet) { s in + planAndConnectActionSheet(s, dismiss: true, onCancel: { searchText = "" }) + } + } + + private func connect(_ link: String) { + planAndConnect( + link, + showAlert: { alert = $0 }, + showActionSheet: { sheet = $0 }, + dismiss: false, + incognito: nil + ) } } diff --git a/apps/ios/Shared/Views/NewChat/NewChatView.swift b/apps/ios/Shared/Views/NewChat/NewChatView.swift index 7533bce8c4..0e61ff94de 100644 --- a/apps/ios/Shared/Views/NewChat/NewChatView.swift +++ b/apps/ios/Shared/Views/NewChat/NewChatView.swift @@ -226,11 +226,15 @@ private struct ConnectView: View { viewBody() .alert(item: $alert) { a in switch(a) { - case let .planAndConnectAlert(alert): return planAndConnectAlert(alert, dismiss: true, onCancel: { pastedLink = "" }) - case let .connectSomeAlert(.someAlert(alert, _)): return alert + case let .planAndConnectAlert(alert): + return planAndConnectAlert(alert, dismiss: true, onCancel: { pastedLink = "" }) + case let .connectSomeAlert(.someAlert(alert, _)): + return alert } } - .actionSheet(item: $sheet) { s in planAndConnectActionSheet(s, dismiss: true, onCancel: { pastedLink = "" }) } + .actionSheet(item: $sheet) { s in + planAndConnectActionSheet(s, dismiss: true, onCancel: { pastedLink = "" }) + } } @ViewBuilder private func viewBody() -> some View { @@ -246,9 +250,9 @@ private struct ConnectView: View { Button { if let str = UIPasteboard.general.string { let link = str.trimmingCharacters(in: .whitespaces) - if checkParsedLink(link) { + if strIsSimplexLink(link) { pastedLink = link - connect(pastedLink) // TODO clear link on cancel + connect(pastedLink) } else { alert = .connectSomeAlert(alert: .someAlert( alert: mkAlert(title: "Invalid link", message: "The text you pasted is not a SimpleX link."), @@ -272,16 +276,6 @@ private struct ConnectView: View { } } - private func checkParsedLink(_ link: String) -> Bool { - if let parsedMd = parseSimpleXMarkdown(link), - parsedMd.count == 1, - case .simplexLink = parsedMd[0].format { - return true - } else { - return false - } - } - private func scanCodeView() -> some View { Section("Or scan QR code") { if showQRCodeScanner { @@ -326,7 +320,7 @@ private struct ConnectView: View { switch resp { case let .success(r): let link = r.string - if checkParsedLink(link) { + if strIsSimplexLink(link) { connect(link) } else { alert = .connectSomeAlert(alert: .someAlert( @@ -354,6 +348,13 @@ private struct ConnectView: View { } } +private func linkTextView(_ link: String) -> some View { + Text(link) + .lineLimit(1) + .font(.caption) + .truncationMode(.middle) +} + struct InfoSheetButton: View { @ViewBuilder let content: Content @State private var showInfoSheet = false @@ -373,11 +374,14 @@ struct InfoSheetButton: View { } } -private func linkTextView(_ link: String) -> some View { - Text(link) - .lineLimit(1) - .font(.caption) - .truncationMode(.middle) +func strIsSimplexLink(_ str: String) -> Bool { + if let parsedMd = parseSimpleXMarkdown(str), + parsedMd.count == 1, + case .simplexLink = parsedMd[0].format { + return true + } else { + return false + } } // TODO move IncognitoToggle here