2022-03-17 09:42:59 +00:00
|
|
|
//
|
|
|
|
// ComposeView.swift
|
|
|
|
// SimpleX
|
|
|
|
//
|
|
|
|
// Created by Evgeny on 13/03/2022.
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
2022-03-25 22:26:05 +04:00
|
|
|
// TODO
|
|
|
|
//enum ComposeState {
|
|
|
|
// case plain
|
|
|
|
// case quoted(quotedItem: ChatItem)
|
|
|
|
// case editing(editingItem: ChatItem)
|
|
|
|
//}
|
|
|
|
|
2022-03-17 09:42:59 +00:00
|
|
|
struct ComposeView: View {
|
2022-03-25 22:26:05 +04:00
|
|
|
@Binding var message: String
|
2022-03-17 09:42:59 +00:00
|
|
|
@Binding var quotedItem: ChatItem?
|
2022-03-25 22:26:05 +04:00
|
|
|
@Binding var editingItem: ChatItem?
|
2022-04-08 18:17:10 +01:00
|
|
|
@Binding var linkPreview: LinkPreview?
|
|
|
|
|
2022-03-17 09:42:59 +00:00
|
|
|
var sendMessage: (String) -> Void
|
2022-03-25 22:26:05 +04:00
|
|
|
var resetMessage: () -> Void
|
2022-03-17 09:42:59 +00:00
|
|
|
var inProgress: Bool = false
|
|
|
|
@FocusState.Binding var keyboardVisible: Bool
|
2022-03-25 22:26:05 +04:00
|
|
|
@State var editing: Bool = false
|
2022-04-08 18:17:10 +01:00
|
|
|
@State var linkUrl: URL? = nil
|
|
|
|
@State var prevLinkUrl: URL? = nil
|
|
|
|
@State var pendingLinkUrl: URL? = nil
|
|
|
|
@State var cancelledLinks: Set<String> = []
|
|
|
|
|
|
|
|
|
|
|
|
private func isValidLink(link: String) -> Bool {
|
|
|
|
return !(link.starts(with: "https://simplex.chat") || link.starts(with: "http://simplex.chat"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func cancelPreview() {
|
|
|
|
if let uri = linkPreview?.uri.absoluteString {
|
|
|
|
cancelledLinks.insert(uri)
|
|
|
|
}
|
|
|
|
linkPreview = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseMessage(_ msg: String) -> URL? {
|
|
|
|
do {
|
|
|
|
if let parsedMsg = try apiParseMarkdown(text: msg),
|
|
|
|
let link = parsedMsg.first(where: {
|
|
|
|
$0.format == .uri && !cancelledLinks.contains($0.text)
|
|
|
|
}),
|
|
|
|
isValidLink(link: link.text) {
|
|
|
|
return URL(string: link.text)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logger.error("apiParseMarkdown error: \(error.localizedDescription)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-03-17 09:42:59 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack(spacing: 0) {
|
2022-04-08 18:17:10 +01:00
|
|
|
if let metadata = linkPreview {
|
|
|
|
ComposeLinkView(linkPreview: metadata, cancelPreview: cancelPreview)
|
|
|
|
}
|
2022-03-25 22:26:05 +04:00
|
|
|
if (quotedItem != nil) {
|
|
|
|
ContextItemView(contextItem: $quotedItem, editing: $editing)
|
|
|
|
} else if (editingItem != nil) {
|
|
|
|
ContextItemView(contextItem: $editingItem, editing: $editing, resetMessage: resetMessage)
|
|
|
|
}
|
2022-03-17 09:42:59 +00:00
|
|
|
SendMessageView(
|
2022-04-08 18:17:10 +01:00
|
|
|
sendMessage: { text in
|
|
|
|
sendMessage(text)
|
|
|
|
resetLinkPreview()
|
|
|
|
},
|
2022-03-17 09:42:59 +00:00
|
|
|
inProgress: inProgress,
|
2022-03-25 22:26:05 +04:00
|
|
|
message: $message,
|
|
|
|
keyboardVisible: $keyboardVisible,
|
|
|
|
editing: $editing
|
2022-03-17 09:42:59 +00:00
|
|
|
)
|
|
|
|
.background(.background)
|
|
|
|
}
|
2022-04-08 18:17:10 +01:00
|
|
|
.onChange(of: message) { _ in
|
|
|
|
if message.count > 0 {
|
|
|
|
prevLinkUrl = linkUrl
|
|
|
|
linkUrl = parseMessage(message)
|
|
|
|
if let url = linkUrl {
|
|
|
|
if prevLinkUrl == linkUrl {
|
|
|
|
loadLinkPreview(url)
|
|
|
|
} else {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
|
|
|
|
loadLinkPreview(url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
linkPreview = nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resetLinkPreview()
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 22:26:05 +04:00
|
|
|
.onChange(of: editingItem == nil) { _ in
|
|
|
|
editing = (editingItem != nil)
|
|
|
|
}
|
2022-03-17 09:42:59 +00:00
|
|
|
}
|
2022-04-08 18:17:10 +01:00
|
|
|
|
|
|
|
func loadLinkPreview(_ url: URL) {
|
|
|
|
if url != linkPreview?.uri && url != pendingLinkUrl {
|
|
|
|
pendingLinkUrl = url
|
|
|
|
getLinkPreview(url: url) { lp in
|
|
|
|
if pendingLinkUrl == url {
|
|
|
|
linkPreview = lp
|
|
|
|
pendingLinkUrl = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetLinkPreview() {
|
|
|
|
linkUrl = nil
|
|
|
|
prevLinkUrl = nil
|
|
|
|
pendingLinkUrl = nil
|
|
|
|
cancelledLinks = []
|
|
|
|
}
|
2022-03-17 09:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ComposeView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-03-25 22:26:05 +04:00
|
|
|
@State var message: String = ""
|
2022-03-17 09:42:59 +00:00
|
|
|
@FocusState var keyboardVisible: Bool
|
2022-03-25 22:26:05 +04:00
|
|
|
@State var item: ChatItem? = ChatItem.getSample(1, .directSnd, .now, "hello")
|
|
|
|
@State var nilItem: ChatItem? = nil
|
2022-04-08 18:17:10 +01:00
|
|
|
@State var linkPreview: LinkPreview? = nil
|
2022-03-17 09:42:59 +00:00
|
|
|
|
2022-03-25 22:26:05 +04:00
|
|
|
return Group {
|
|
|
|
ComposeView(
|
|
|
|
message: $message,
|
|
|
|
quotedItem: $item,
|
|
|
|
editingItem: $nilItem,
|
2022-04-08 18:17:10 +01:00
|
|
|
linkPreview: $linkPreview,
|
2022-03-25 22:26:05 +04:00
|
|
|
sendMessage: { print ($0) },
|
|
|
|
resetMessage: {},
|
|
|
|
keyboardVisible: $keyboardVisible
|
|
|
|
)
|
|
|
|
ComposeView(
|
|
|
|
message: $message,
|
|
|
|
quotedItem: $nilItem,
|
|
|
|
editingItem: $item,
|
2022-04-08 18:17:10 +01:00
|
|
|
linkPreview: $linkPreview,
|
2022-03-25 22:26:05 +04:00
|
|
|
sendMessage: { print ($0) },
|
|
|
|
resetMessage: {},
|
|
|
|
keyboardVisible: $keyboardVisible
|
|
|
|
)
|
|
|
|
}
|
2022-03-17 09:42:59 +00:00
|
|
|
}
|
|
|
|
}
|