conversations-classic-ios/ConversationsClassic/AppData/Store/ConversationStore.swift

181 lines
5.1 KiB
Swift
Raw Normal View History

2024-08-17 11:22:47 +00:00
import AVFoundation
2024-08-14 13:48:30 +00:00
import Combine
2024-08-13 08:40:27 +00:00
import Foundation
2024-08-14 13:48:30 +00:00
import GRDB
2024-08-17 11:22:47 +00:00
import Photos
2024-08-13 08:40:27 +00:00
@MainActor
final class ConversationStore: ObservableObject {
2024-08-15 15:15:49 +00:00
@Published private(set) var messages: [Message] = []
@Published var replyText = ""
2024-08-13 08:40:27 +00:00
2024-08-17 11:22:47 +00:00
private(set) var roster: Roster
2024-08-13 08:40:27 +00:00
private let client: Client
2024-08-14 13:48:30 +00:00
private let blockSize = Const.messagesPageSize
private let messagesMax = Const.messagesMaxSize
2024-08-15 11:37:21 +00:00
private var messagesCancellable: AnyCancellable?
2024-08-13 08:40:27 +00:00
init(roster: Roster, client: Client) {
self.client = client
self.roster = roster
2024-08-15 15:15:49 +00:00
subscribe()
2024-08-14 13:48:30 +00:00
}
}
extension ConversationStore {
2024-08-15 15:15:49 +00:00
func sendMessage(_ message: String) async {
// prepare message
let message = Message(
id: UUID().uuidString,
type: .chat,
date: Date(),
contentType: .text,
status: .pending,
from: roster.bareJid,
to: roster.contactBareJid,
body: message,
subject: nil,
thread: nil,
oobUrl: nil
)
2024-08-15 11:37:21 +00:00
2024-08-15 15:15:49 +00:00
// store as pending on db, and send
do {
try await message.save()
2024-08-17 08:06:28 +00:00
try await client.sendMessage(message)
try await message.setStatus(.sent)
} catch {
try? await message.setStatus(.error)
}
2024-08-14 13:48:30 +00:00
}
}
2024-08-17 16:15:05 +00:00
extension ConversationStore {
var attachmentsStore: AttachmentsStore {
AttachmentsStore()
}
func sendMedia(_ items: [GalleryItem]) async {
print("media!", items)
// guard !ids.isEmpty else { return }
// let items = galleryItems.filter { ids.contains($0.id) }
// for item in items {
// await client.uploadMedia(item.url)
// }
}
2024-08-17 16:25:35 +00:00
func sendCaptured(_ data: Data, _ type: GalleryMediaType) async {
2024-08-17 21:12:39 +00:00
// save locally and make message
let messageId = UUID().uuidString
do {
let (localName, msgType) = try await FileStore.shared.storeCaptured(messageId: messageId, data, type)
let message = Message(
id: UUID().uuidString,
type: .chat,
date: Date(),
contentType: .attachment(
Attachment(
type: msgType,
localName: localName,
thumbnailName: nil,
remotePath: nil
)
),
status: .pending,
from: roster.bareJid,
to: roster.contactBareJid,
body: nil,
subject: nil,
thread: nil,
oobUrl: nil
)
try await message.save()
} catch {
logIt(.error, "Can't save file for uploading: \(error)")
}
// upload and save
upload(message: message)
2024-08-17 16:56:04 +00:00
}
func sendDocuments(_ data: [Data], _ extensions: [String]) async {
2024-08-17 21:12:39 +00:00
// do {
// let newMessageId = UUID().uuidString
// let fileId = UUID().uuidString
// let localName = "\(newMessageId)_\(fileId).\(ext)"
// try await FileStore.shared.storeForUploading(data, localName)
//
// } catch {
// print("error", error)
// }
2024-08-17 16:56:04 +00:00
print("documents!", data, extensions)
//
//
//
}
func sendContact(_ jidStr: String) async {
2024-08-17 17:37:19 +00:00
await sendMessage("contact:\(jidStr)")
2024-08-17 16:56:04 +00:00
}
2024-08-17 17:33:54 +00:00
func sendLocation(_ lat: Double, _ lon: Double) async {
2024-08-17 17:37:19 +00:00
await sendMessage("geo:\(lat),\(lon)")
2024-08-17 17:33:54 +00:00
}
2024-08-17 21:12:39 +00:00
private func upload(message: Message) async {
let remotePath: String
do {
remotePath = try await client.uploadFile(localPath)
message.contentType = .attachment(
Attachment(
type: msgType,
localName: localName,
thumbnailName: nil,
remotePath: remotePath
)
)
try await message.save()
} catch {
message.status = .error
try? await message.save()
}
}
2024-08-17 16:56:04 +00:00
}
extension ConversationStore {
var contacts: [Roster] {
get async {
do {
let rosters = try await Database.shared.dbQueue.read { db in
try Roster
.filter(Column("locallyDeleted") == false)
.fetchAll(db)
}
return rosters
} catch {
return []
}
}
2024-08-17 16:25:35 +00:00
}
2024-08-17 16:15:05 +00:00
}
2024-08-15 11:37:21 +00:00
private extension ConversationStore {
2024-08-15 15:15:49 +00:00
func subscribe() {
messagesCancellable = ValueObservation.tracking(Message
.filter(
(Column("to") == roster.bareJid && Column("from") == roster.contactBareJid) ||
(Column("from") == roster.bareJid && Column("to") == roster.contactBareJid)
2024-08-15 11:37:21 +00:00
)
2024-08-15 15:15:49 +00:00
.order(Column("date").desc)
.fetchAll
)
.publisher(in: Database.shared.dbQueue, scheduling: .immediate)
.receive(on: DispatchQueue.main)
.sink { _ in
} receiveValue: { [weak self] messages in
self?.messages = messages
2024-08-15 11:37:21 +00:00
}
2024-08-13 08:40:27 +00:00
}
}