wip
This commit is contained in:
parent
13ace49560
commit
60e14a5406
|
@ -107,14 +107,10 @@ struct ConversationScreen: View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// .environmentObject(messagesStore)
|
.safeAreaInset(edge: .bottom, spacing: 0) {
|
||||||
// .environmentObject(attachments)
|
ConversationTextInput(autoScroll: $autoScroll)
|
||||||
// .safeAreaInset(edge: .bottom, spacing: 0) {
|
.environmentObject(chatWrapper)
|
||||||
// ConversationTextInput(autoScroll: $autoScroll)
|
}
|
||||||
// .environmentObject(messagesStore)
|
|
||||||
// .environmentObject(attachments)
|
|
||||||
// .environmentObject(settings)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func centerText() -> String {
|
private func centerText() -> String {
|
||||||
|
|
|
@ -3,8 +3,7 @@ import UIKit
|
||||||
|
|
||||||
struct ConversationTextInput: View {
|
struct ConversationTextInput: View {
|
||||||
@Environment(\.router) var router
|
@Environment(\.router) var router
|
||||||
// @EnvironmentObject var messages: MessagesStore
|
@EnvironmentObject var chatWrapper: MonalChatWrapper
|
||||||
// @EnvironmentObject var attachments: AttachmentsStore
|
|
||||||
|
|
||||||
@State private var messageStr = ""
|
@State private var messageStr = ""
|
||||||
@FocusState private var isFocused: Bool
|
@FocusState private var isFocused: Bool
|
||||||
|
@ -16,35 +15,35 @@ struct ConversationTextInput: View {
|
||||||
.foregroundColor(.Material.Shape.separator)
|
.foregroundColor(.Material.Shape.separator)
|
||||||
.frame(height: 0.5)
|
.frame(height: 0.5)
|
||||||
.padding(.bottom, 8)
|
.padding(.bottom, 8)
|
||||||
// if !messages.replyText.isEmpty {
|
if !chatWrapper.replyText.isEmpty {
|
||||||
// VStack(spacing: 0) {
|
VStack(spacing: 0) {
|
||||||
// HStack(alignment: .top) {
|
HStack(alignment: .top) {
|
||||||
// Text(messages.replyText)
|
Text(chatWrapper.replyText)
|
||||||
// .font(.body3)
|
.font(.body3)
|
||||||
// .foregroundColor(Color.Material.Text.main)
|
.foregroundColor(Color.Material.Text.main)
|
||||||
// .multilineTextAlignment(.leading)
|
.multilineTextAlignment(.leading)
|
||||||
// .lineLimit(3)
|
.lineLimit(3)
|
||||||
// .padding(8)
|
.padding(8)
|
||||||
// Spacer()
|
Spacer()
|
||||||
// Image(systemName: "xmark")
|
Image(systemName: "xmark")
|
||||||
// .font(.title2)
|
.font(.title2)
|
||||||
// .foregroundColor(.Material.Elements.active)
|
.foregroundColor(.Material.Elements.active)
|
||||||
// .padding(.leading, 8)
|
.padding(.leading, 8)
|
||||||
// .tappablePadding(.symmetric(8)) {
|
.tappablePadding(.symmetric(8)) {
|
||||||
// messages.replyText = ""
|
chatWrapper.replyText = ""
|
||||||
// }
|
}
|
||||||
// .padding(8)
|
.padding(8)
|
||||||
// }
|
}
|
||||||
// .frame(maxWidth: .infinity)
|
.frame(maxWidth: .infinity)
|
||||||
// .background(RoundedRectangle(cornerRadius: 4)
|
.background(RoundedRectangle(cornerRadius: 4)
|
||||||
// .foregroundColor(.Material.Background.light)
|
.foregroundColor(.Material.Background.light)
|
||||||
// .shadow(radius: 0.5)
|
.shadow(radius: 0.5)
|
||||||
// )
|
)
|
||||||
// .padding(.bottom, 8)
|
.padding(.bottom, 8)
|
||||||
// .padding(.horizontal, 8)
|
.padding(.horizontal, 8)
|
||||||
// }
|
}
|
||||||
// .padding(.horizontal, 8)
|
.padding(.horizontal, 8)
|
||||||
// }
|
}
|
||||||
HStack {
|
HStack {
|
||||||
Image(systemName: "paperclip")
|
Image(systemName: "paperclip")
|
||||||
.font(.title2)
|
.font(.title2)
|
||||||
|
@ -52,7 +51,8 @@ struct ConversationTextInput: View {
|
||||||
.padding(.leading, 8)
|
.padding(.leading, 8)
|
||||||
.tappablePadding(.symmetric(8)) {
|
.tappablePadding(.symmetric(8)) {
|
||||||
router.showScreen(.fullScreenCover) { _ in
|
router.showScreen(.fullScreenCover) { _ in
|
||||||
AttachmentPickerScreen()
|
Text("not yet implemented")
|
||||||
|
// AttachmentPickerScreen()
|
||||||
// .environmentObject(messages)
|
// .environmentObject(messages)
|
||||||
// .environmentObject(attachments)
|
// .environmentObject(attachments)
|
||||||
}
|
}
|
||||||
|
@ -74,31 +74,31 @@ struct ConversationTextInput: View {
|
||||||
.padding(.trailing, 8)
|
.padding(.trailing, 8)
|
||||||
.tappablePadding(.symmetric(8)) {
|
.tappablePadding(.symmetric(8)) {
|
||||||
if !messageStr.isEmpty {
|
if !messageStr.isEmpty {
|
||||||
// messages.sendMessage(composedMessage)
|
chatWrapper.sendText(composedMessage)
|
||||||
// messageStr = ""
|
messageStr = ""
|
||||||
// autoScroll = true
|
autoScroll = true
|
||||||
// if !messages.replyText.isEmpty {
|
if !chatWrapper.replyText.isEmpty {
|
||||||
// messages.replyText = ""
|
chatWrapper.replyText = ""
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.padding(.bottom, 8)
|
.padding(.bottom, 8)
|
||||||
.background(Color.Material.Background.dark)
|
.background(Color.Material.Background.dark)
|
||||||
// .onChange(of: messages.replyText) { new in
|
.onChange(of: chatWrapper.replyText) { new in
|
||||||
// if !new.isEmpty {
|
if !new.isEmpty {
|
||||||
// isFocused = true
|
isFocused = true
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var composedMessage: String {
|
private var composedMessage: String {
|
||||||
var result = ""
|
var result = ""
|
||||||
// if !messages.replyText.isEmpty {
|
if !chatWrapper.replyText.isEmpty {
|
||||||
// result += messages.replyText.makeReply + "\n\n"
|
result += chatWrapper.replyText.makeReply + "\n\n"
|
||||||
// }
|
}
|
||||||
// result += messageStr
|
result += messageStr
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,13 +60,16 @@ extension MonalXmppWrapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
func chat(with: Contact) -> MonalChatWrapper {
|
func chat(with: Contact) -> MonalChatWrapper {
|
||||||
let chatModel = MonalChatWrapper(contact: with, db: db, xmpp: xmpp)
|
// swiftlint:disable:next force_unwrapping
|
||||||
|
let account = accounts.first { $0.id == with.ownerId }!
|
||||||
|
let chatModel = MonalChatWrapper(account: account, contact: with, db: db, xmpp: xmpp)
|
||||||
return chatModel
|
return chatModel
|
||||||
}
|
}
|
||||||
|
|
||||||
func chat(with: Chat) -> MonalChatWrapper? {
|
func chat(with: Chat) -> MonalChatWrapper? {
|
||||||
|
guard let account = accounts.first(where: { $0.id == with.accountId }) else { return nil }
|
||||||
guard let contact = contacts.first(where: { $0.ownerId == with.accountId && $0.contactJid == with.participantJid }) else { return nil }
|
guard let contact = contacts.first(where: { $0.ownerId == with.accountId && $0.contactJid == with.participantJid }) else { return nil }
|
||||||
let chatModel = MonalChatWrapper(contact: contact, db: db, xmpp: xmpp)
|
let chatModel = MonalChatWrapper(account: account, contact: contact, db: db, xmpp: xmpp)
|
||||||
return chatModel
|
return chatModel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -177,36 +180,70 @@ private extension MonalXmppWrapper {
|
||||||
// MARK: - Chat object
|
// MARK: - Chat object
|
||||||
final class MonalChatWrapper: ObservableObject {
|
final class MonalChatWrapper: ObservableObject {
|
||||||
@Published private(set) var messages: [Message] = []
|
@Published private(set) var messages: [Message] = []
|
||||||
|
@Published var replyText: String = ""
|
||||||
|
|
||||||
let contact: Contact
|
let contact: Contact
|
||||||
|
|
||||||
|
private let monalContact: MLContact
|
||||||
|
private let account: Account
|
||||||
private let xmpp: MLXMPPManager
|
private let xmpp: MLXMPPManager
|
||||||
private let db: DataLayer
|
private let db: DataLayer
|
||||||
private var notificationObservers: [AnyObject] = []
|
private var notificationObservers: [AnyObject] = []
|
||||||
|
|
||||||
init(contact: Contact, db: DataLayer, xmpp: MLXMPPManager) {
|
init(account: Account, contact: Contact, db: DataLayer, xmpp: MLXMPPManager) {
|
||||||
self.contact = contact
|
self.contact = contact
|
||||||
|
self.account = account
|
||||||
self.db = db
|
self.db = db
|
||||||
self.xmpp = xmpp
|
self.xmpp = xmpp
|
||||||
|
|
||||||
|
// swiftlint:disable:next force_unwrapping
|
||||||
|
monalContact = db.contactList().first { $0.accountID.intValue == contact.ownerId && $0.contactJid == contact.contactJid }!
|
||||||
|
|
||||||
subscribe()
|
subscribe()
|
||||||
|
NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
notificationObservers.forEach { NotificationCenter.default.removeObserver($0) }
|
notificationObservers.forEach { NotificationCenter.default.removeObserver($0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
private func subscribe() {
|
func sendText(_ text: String) {
|
||||||
|
let msgId = db.addMessageHistory(
|
||||||
|
to: contact.contactJid,
|
||||||
|
forAccount: monalContact.accountID,
|
||||||
|
withMessage: text,
|
||||||
|
actuallyFrom: account.jid,
|
||||||
|
withId: UUID().uuidString,
|
||||||
|
encrypted: monalContact.isEncrypted,
|
||||||
|
messageType: kMessageTypeText,
|
||||||
|
mimeType: nil,
|
||||||
|
size: nil
|
||||||
|
)
|
||||||
|
xmpp.sendMessage(text, to: monalContact, isEncrypted: monalContact.isEncrypted, isUpload: false, messageId: "\(msgId)")
|
||||||
|
NotificationCenter.default.post(name: Notification.Name(kMonalSentMessageNotice), object: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private extension MonalChatWrapper {
|
||||||
|
func subscribe() {
|
||||||
let newMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalNewMessageNotice), object: nil, queue: .main) { [weak self] _ in
|
let newMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalNewMessageNotice), object: nil, queue: .main) { [weak self] _ in
|
||||||
self?.refreshMessages()
|
self?.refreshMessages()
|
||||||
}
|
}
|
||||||
notificationObservers.append(newMsg)
|
notificationObservers.append(newMsg)
|
||||||
|
|
||||||
|
let sentMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalSentMessageNotice), object: nil, queue: .main) { [weak self] _ in
|
||||||
|
self?.refreshMessages()
|
||||||
|
}
|
||||||
|
notificationObservers.append(sentMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func refreshMessages() {
|
func refreshMessages() {
|
||||||
messages = db.messages(forContact: contact.contactJid, forAccount: NSNumber(value: contact.ownerId))
|
let messages = db.messages(forContact: contact.contactJid, forAccount: NSNumber(value: contact.ownerId))
|
||||||
.compactMap {
|
.compactMap { obj -> Message? in
|
||||||
guard let message = $0 as? MLMessage else { return nil }
|
guard let message = obj as? MLMessage else { return nil }
|
||||||
return Message(message)
|
return Message(message)
|
||||||
}
|
}
|
||||||
|
.sorted { $0.timestamp > $1.timestamp }
|
||||||
|
self.messages = messages
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue