mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2025-06-28 20:29:53 +00:00
* ios: share extension (#4414) * ios: add share extension target * ios: Add UI * ios: send file from share-sheet * image utils * ShareError * error handling; ui-cleanup * progress bar; completion for direct chat * cleanup * cleanup * ios: unify filter and sort between forward and share sheets * ios: match share sheet styling with the main app * ios: fix text input stroke width * ios: align compose views * more of the same... * ShareAPI * remove combine * minor * Better error descriptions --------- Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com> * ios: enable file sending workers in share extension (#4474) * ios: align compose background, row height and fallback images for share-sheet (#4467) Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com> * ios: coordinate database access between share extension, the app and notifications extension (#4472) * ios: database management proposal * Add SEState * Global event loop * minor * reset state * use apiCreateItem for local chats * simplify waiting for suspension * loading bar * Dismiss share sheet with error --------- Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com> * send image message (#4481) Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com> * ios: improve share extension completion handling (#4486) * improve completion handling * minor * show only spinner for group send * rework event loop, errorAlert * group chat timeout loading bar * state machine WIP * event loop actor * alert * errors text * default * file error --------- Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com> * ios: add remaining share types; process attachment in background on launch (#4510) * add remaining share types; process attachment in background on launch * cleanup diff * revert `makeVideoQualityLower` * reduce diff * reduce diff * iOS15 support * process events when sharing link and text * cleanup * remove video file on failure * cleanup CompletionHandler --------- Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com> * ios: share extension - additional alerts and media previews (#4521) * add remaining share types; process attachment in background on launch * cleanup diff * revert `makeVideoQualityLower` * reduce diff * reduce diff * iOS15 support * process events when sharing link and text * cleanup * remove video file on failure * cleanup CompletionHandler * media previews * network timeout alert * revert framework compiler optimisation flag * suspend chat after sheet dismiss * activate chat * update * fix search * sendMessageColor, file preview, chat deselect, simplify error action * cleanup * interupt database closing when sheet is reopened quickly * cleanup redundant alert check * restore package * refactor previews, remove link preview * show link preview when becomes available * comment * dont fail on invalid image * suspend --------- Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com> * ios: descriptive database errors (#4527) * ios: set share extension as inactive when suspending chat --------- Co-authored-by: Arturs Krumins <auth@levitatingpineapple.com>
121 lines
4 KiB
Swift
121 lines
4 KiB
Swift
//
|
|
// SharedFileSubscriber.swift
|
|
// SimpleXChat
|
|
//
|
|
// Created by Evgeny on 09/12/2023.
|
|
// Copyright © 2023 SimpleX Chat. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public typealias AppSubscriber = SharedFileSubscriber<ProcessMessage<AppProcessMessage>>
|
|
|
|
public typealias NSESubscriber = SharedFileSubscriber<ProcessMessage<NSEProcessMessage>>
|
|
|
|
public typealias SESubscriber = SharedFileSubscriber<ProcessMessage<SEProcessMessage>>
|
|
|
|
public class SharedFileSubscriber<Message: Codable>: NSObject, NSFilePresenter {
|
|
var fileURL: URL
|
|
public var presentedItemURL: URL?
|
|
public var presentedItemOperationQueue: OperationQueue = .main
|
|
var subscriber: (Message) -> Void
|
|
|
|
init(fileURL: URL, onMessage: @escaping (Message) -> Void) {
|
|
self.fileURL = fileURL
|
|
presentedItemURL = fileURL
|
|
subscriber = onMessage
|
|
super.init()
|
|
NSFileCoordinator.addFilePresenter(self)
|
|
}
|
|
|
|
public func presentedItemDidChange() {
|
|
do {
|
|
let data = try Data(contentsOf: fileURL)
|
|
let msg = try jsonDecoder.decode(Message.self, from: data)
|
|
subscriber(msg)
|
|
} catch let error {
|
|
logger.error("presentedItemDidChange error: \(error)")
|
|
}
|
|
}
|
|
|
|
static func notify(url: URL, message: Message) {
|
|
let fc = NSFileCoordinator(filePresenter: nil)
|
|
fc.coordinate(writingItemAt: url, options: [], error: nil) { newURL in
|
|
do {
|
|
let data = try jsonEncoder.encode(message)
|
|
try data.write(to: newURL, options: [.atomic])
|
|
} catch {
|
|
logger.error("notifyViaSharedFile error: \(error)")
|
|
}
|
|
}
|
|
}
|
|
|
|
deinit {
|
|
NSFileCoordinator.removeFilePresenter(self)
|
|
}
|
|
}
|
|
|
|
let appMessagesSharedFile = getGroupContainerDirectory().appendingPathComponent("chat.simplex.app.messages", isDirectory: false)
|
|
|
|
let nseMessagesSharedFile = getGroupContainerDirectory().appendingPathComponent("chat.simplex.app.SimpleX-NSE.messages", isDirectory: false)
|
|
|
|
let seMessagesSharedFile = getGroupContainerDirectory().appendingPathComponent("chat.simplex.app.SimpleX-SE.messages", isDirectory: false)
|
|
|
|
public struct ProcessMessage<Message: Codable>: Codable {
|
|
var createdAt: Date = Date.now
|
|
var message: Message
|
|
}
|
|
|
|
public enum AppProcessMessage: Codable {
|
|
case state(state: AppState)
|
|
}
|
|
|
|
public enum NSEProcessMessage: Codable {
|
|
case state(state: NSEState)
|
|
}
|
|
|
|
public enum SEProcessMessage: Codable {
|
|
case state(state: SEState)
|
|
}
|
|
|
|
public func sendAppProcessMessage(_ message: AppProcessMessage) {
|
|
SharedFileSubscriber.notify(url: appMessagesSharedFile, message: ProcessMessage(message: message))
|
|
}
|
|
|
|
public func sendNSEProcessMessage(_ message: NSEProcessMessage) {
|
|
SharedFileSubscriber.notify(url: nseMessagesSharedFile, message: ProcessMessage(message: message))
|
|
}
|
|
|
|
public func sendSEProcessMessage(_ message: SEProcessMessage) {
|
|
SharedFileSubscriber.notify(url: seMessagesSharedFile, message: ProcessMessage(message: message))
|
|
}
|
|
|
|
public func appMessageSubscriber(onMessage: @escaping (AppProcessMessage) -> Void) -> AppSubscriber {
|
|
SharedFileSubscriber(fileURL: appMessagesSharedFile) { (msg: ProcessMessage<AppProcessMessage>) in
|
|
onMessage(msg.message)
|
|
}
|
|
}
|
|
|
|
public func nseMessageSubscriber(onMessage: @escaping (NSEProcessMessage) -> Void) -> NSESubscriber {
|
|
SharedFileSubscriber(fileURL: nseMessagesSharedFile) { (msg: ProcessMessage<NSEProcessMessage>) in
|
|
onMessage(msg.message)
|
|
}
|
|
}
|
|
|
|
public func seMessageSubscriber(onMessage: @escaping (SEProcessMessage) -> Void) -> SESubscriber {
|
|
SharedFileSubscriber(fileURL: seMessagesSharedFile) { (msg: ProcessMessage<SEProcessMessage>) in
|
|
onMessage(msg.message)
|
|
}
|
|
}
|
|
|
|
public func sendAppState(_ state: AppState) {
|
|
sendAppProcessMessage(.state(state: state))
|
|
}
|
|
|
|
public func sendNSEState(_ state: NSEState) {
|
|
sendNSEProcessMessage(.state(state: state))
|
|
}
|
|
|
|
public func sendSEState(_ state: SEState) {
|
|
sendSEProcessMessage(.state(state: state))
|
|
}
|