95 lines
2.8 KiB
Swift
95 lines
2.8 KiB
Swift
import AVFoundation
|
|
import Combine
|
|
import Foundation
|
|
import GRDB
|
|
import Photos
|
|
|
|
@MainActor
|
|
final class ConversationStore: ObservableObject {
|
|
@Published private(set) var messages: [Message] = []
|
|
@Published var replyText = ""
|
|
@Published var cameraAccessGranted = false
|
|
@Published var galleryAccessGranted = false
|
|
|
|
private(set) var roster: Roster
|
|
private let client: Client
|
|
private let blockSize = Const.messagesPageSize
|
|
private let messagesMax = Const.messagesMaxSize
|
|
|
|
private var messagesCancellable: AnyCancellable?
|
|
|
|
init(roster: Roster, client: Client) {
|
|
self.client = client
|
|
self.roster = roster
|
|
subscribe()
|
|
}
|
|
}
|
|
|
|
extension ConversationStore {
|
|
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
|
|
)
|
|
|
|
// store as pending on db, and send
|
|
do {
|
|
try await message.save()
|
|
try await client.sendMessage(message)
|
|
try await message.setStatus(.sent)
|
|
} catch {
|
|
try? await message.setStatus(.error)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ConversationStore {
|
|
func checkCameraAuthorization() async {
|
|
let status = AVCaptureDevice.authorizationStatus(for: .video)
|
|
var isAuthorized = status == .authorized
|
|
if status == .notDetermined {
|
|
isAuthorized = await AVCaptureDevice.requestAccess(for: .video)
|
|
}
|
|
cameraAccessGranted = isAuthorized
|
|
}
|
|
|
|
func checkGalleryAuthorization() async {
|
|
let status = PHPhotoLibrary.authorizationStatus()
|
|
var isAuthorized = status == .authorized
|
|
if status == .notDetermined {
|
|
let req = await PHPhotoLibrary.requestAuthorization(for: .readWrite)
|
|
isAuthorized = (req == .authorized) || (req == .limited)
|
|
}
|
|
galleryAccessGranted = isAuthorized
|
|
}
|
|
}
|
|
|
|
private extension ConversationStore {
|
|
func subscribe() {
|
|
messagesCancellable = ValueObservation.tracking(Message
|
|
.filter(
|
|
(Column("to") == roster.bareJid && Column("from") == roster.contactBareJid) ||
|
|
(Column("from") == roster.bareJid && Column("to") == roster.contactBareJid)
|
|
)
|
|
.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
|
|
}
|
|
}
|
|
}
|