mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-06-09 08:33:00 +00:00
bf905eb545
* android, desktop: settings navigation reorganization Restructure the root Settings screen into two top-level sections and fold previously-scattered items into three new sub-screens. Root: - Appearance, Your privacy, Chat data - Help & support, Migrate to another device, Advanced settings Your privacy (renamed from Privacy & security): keeps Device, link previews / remove tracking, auto-accept images, blur media, contact requests from groups. Adds a "More privacy" sub-screen. More privacy (new): show last messages, message draft, encrypt local files, protect IP address (with original dynamic footer preserved), notification preview mode (moved from Notifications), and delivery receipts. Help & support (new): merges Help and Support SimpleX Chat sections into Help / About (with App version) / Contact / Support the project. Advanced settings (new): Network & servers, Notifications (Android), Audio & video calls; then Developer tools, Restart and Shutdown (Android), App update channel (desktop). Notifications is hidden on desktop because the screen is empty after Show preview moves to More privacy. Chat data is the new top-level menu label for the existing Database passphrase & export screen. * android, desktop: trim settings reorg diff - Remove 5 dead strings from base/strings.xml (privacy_and_security, database_passphrase_and_export, settings_section_title_chat_database, settings_section_title_support, settings_section_title_app) — no Kotlin references after the reorg. - Drop the now single-variant CurrentPage enum in NotificationsSettingsView; replace with a direct callback. - Read userDisplayName locally in HelpAndSupportView instead of threading it through SettingsLayout/SettingsView/Preview. * android, desktop: remove orphan locale strings Companion to442a368c9which removed 5 dead keys from base/strings.xml. The :common:adjustFormatting task enforces that every locale string has a corresponding base entry, so these orphans broke the build. Removed across 35 locale files (154 lines). Keys removed: - privacy_and_security - database_passphrase_and_export - settings_section_title_chat_database - settings_section_title_support - settings_section_title_app * Revert "android, desktop: remove orphan locale strings" This reverts commit0ad5fc9308. * android, desktop: restore base strings for removed keys Counterpart to revert of0ad5fc930: re-add the 5 base entries that442a368c9had deleted so the locale files (restored by the prior revert) are no longer orphaned. Translation keys must not be removed once introduced — the values can change but the keys stay. Keys restored to base with their master English values: - database_passphrase_and_export - privacy_and_security - settings_section_title_chat_database - settings_section_title_support - settings_section_title_app * android, desktop: keep share-button helpers in SettingsView ContributeItem, RateAppItem, StarOnGithubItem were moved from SettingsView.kt to HelpAndSupportView.kt as part of the reorg. Move them back: just drop the `private` modifier (one-word edit per function) so HelpAndSupportView can call them in place. Saves ~60 lines of diff churn vs the move + matches the file's existing pattern where helpers like AppVersionItem, ChatPreferencesItem, ChatLockItem, etc. are all public top-level @Composable. * android, desktop: inline HelpAndSupportView into SettingsView.kt HelpAndSupportView is the only call site of SettingsView.kt's share-button helpers; placing it as a top-level @Composable in SettingsView.kt keeps the help/about/contact/support flow next to the other settings entry points and removes the need for a new file. Three imports (BuildConfigCommon, SimpleXInfo, WhatsNewView) that the reorg was deleting from SettingsView.kt stay in place. Saves ~35 lines of diff and one new file. * android, desktop: inline AdvancedSettingsView into SettingsView.kt Same treatment as HelpAndSupportView in the previous commit: AdvancedSettingsView is only reached from SettingsLayout, so the function and its expect declaration live as top-level @Composable in SettingsView.kt instead of a new file. NetworkAndServersView import that the reorg was deleting from SettingsView.kt stays. The .android.kt / .desktop.kt actuals are unchanged and keep implementing the (now relocated) expect. Saves ~15 lines and a file. * ios: settings navigation reorganization Mirror the multiplatform reorg on iOS: Root SettingsView: collapse the 5 sections into 2 unlabeled groups — {Appearance, Your privacy, Chat data} and {Help & support, Migrate to another device, Advanced settings}. "Privacy & security" becomes "Your privacy"; the database row label becomes "Chat data". PrivacySettings: keeps Device, link previews / remove tracking, auto-accept images, blur media, contact requests from groups. Adds a "More privacy" link. MorePrivacy (new, inlined in PrivacySettings.swift): show last messages, message draft, encrypt local files, protect IP address (with original dynamic footer preserved), notification preview mode (moved from NotificationsView), delivery receipts. Own state and private helpers for the moved set* functions. HelpAndSupportView (new, inlined in SettingsView.swift): merges Help and Support sections into Help / About (with App version) / Contact / Support the project. AdvancedSettingsView (new, inlined in SettingsView.swift): Network & servers, Notifications, Audio & video calls, Developer tools. iOS has no Restart/Shutdown (Android-only) or App update channel (desktop). NotificationsView: "Show preview" navigation removed — it now lives in MorePrivacy. notificationsIcon() promoted to a free function so AdvancedSettingsView can render the notifications status badge. * android, desktop: keep platform file names as SettingsView.{android,desktop}.kt Revert the file renames from {Advanced}SettingsView.{android,desktop}.kt. Function rename SettingsSectionApp → AdvancedSettingsAppSection stays inside each file; only the file path returns to its original name. No behavior change; diff stat now shows two in-place modifications instead of renames. * ios: keep PrivacySettings/SettingsView state in place, use inline destinations Restructure the iOS reorg to avoid moving state, helpers, and the alert enum out of PrivacySettings — and to avoid moving notificationsIcon out of SettingsView. The Help & Support, Advanced Settings, and More Privacy "screens" become private computed properties on their parent struct, so all @AppStorage, @State, set* helpers, and the PrivacySettingsViewAlert enum stay UNCHANGED from master. NavigationLink destinations reference the computed properties directly. Net iOS diff vs master: 220+/154- (was 361+/259-) — saves ~245 lines. * simplex settings * android, desktop: mirror iOS settings reorganization - inline Advanced settings section into the main settings list (Network & servers, Notifications, Audio & video calls, App version); remove the separate Advanced settings page - reorder first section: Appearance, Your privacy, Help & support, Chat data, Migrate; merge About SimpleX Chat into the Help section - move the developer/maintenance section under App version (VersionInfoView); load core version inside the view so it always opens (Developer tools and Shutdown stay reachable even if the version request fails) - keep "Developer tools" label (not renamed to "Developer") - replace the Restart row with Cancel/Restart/Shutdown options in the Shutdown dialog - split DatabaseView: "Chat data" page (messages TTL, Database passphrase & export, Files & media) and a sub-page with passphrase/export/import/delete and the Run chat toggle; rename title to "Chat data" - align delivery receipts alert wording with the renamed "Your privacy" settings * android, desktop: simplify settings reorg internals - VersionInfoView: drop the section/card wrapping, keep the original plain version-text layout; load core version in-view so the screen always opens - DatabaseView: make the "Database passphrase & export" sub-page a self-contained DatabaseManagementView that owns its own state, mirroring the DatabaseView/DatabaseLayout pattern instead of threading params through a modal * android, desktop: show App version screen as a card screen VersionInfoView now hosts a settings section (Developer tools / updates), so open it with cardScreen = true like the other settings screens — otherwise the section renders without the card box around it. * android, desktop: show "Rate the app" only on mobile The action opens a Play Store link, which does nothing on desktop (the market:// scheme has no handler and the web fallback never fires). Gate it to Android, like the Contribute item. * android, desktop: move Shutdown to settings above app version Move the Shutdown action out of the app version screen into the main advanced settings section, just above the app version row. It stays Android only (desktop is closed via the window) through an AppShutdownItem expect/actual. * android, desktop: show app version info in its own card Wrap the version info block on the app version screen in a section card, matching iOS and the rest of the card-screen settings. * fix background --------- Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
543 lines
21 KiB
Swift
543 lines
21 KiB
Swift
//
|
|
// ShareModel.swift
|
|
// SimpleX SE
|
|
//
|
|
// Created by Levitating Pineapple on 09/07/2024.
|
|
// Copyright © 2024 SimpleX Chat. All rights reserved.
|
|
//
|
|
|
|
import UniformTypeIdentifiers
|
|
import AVFoundation
|
|
import SwiftUI
|
|
import SimpleXChat
|
|
|
|
/// Maximum size of hex encoded media previews
|
|
private let MAX_DATA_SIZE: Int64 = 14000
|
|
|
|
/// Maximum dimension (width or height) of an image, before passed for processing
|
|
private let MAX_DOWNSAMPLE_SIZE: Int64 = 2000
|
|
|
|
class ShareModel: ObservableObject {
|
|
@Published var sharedContent: SharedContent?
|
|
@Published var chats: [SEChatData] = []
|
|
@Published var profileImages: [ChatInfo.ID: UIImage] = [:]
|
|
@Published var search = ""
|
|
@Published var comment = ""
|
|
@Published var selected: SEChatData?
|
|
@Published var isLoaded = false
|
|
@Published var bottomBar: BottomBar = .loadingSpinner
|
|
@Published var errorAlert: ErrorAlert?
|
|
@Published var hasSimplexLink = false
|
|
@Published var alertRequiresPassword = false
|
|
var networkTimeout = CFAbsoluteTimeGetCurrent()
|
|
|
|
enum BottomBar {
|
|
case sendButton
|
|
case loadingSpinner
|
|
case loadingBar(progress: Double)
|
|
|
|
var isLoading: Bool {
|
|
switch self {
|
|
case .sendButton: false
|
|
case .loadingSpinner: true
|
|
case .loadingBar: true
|
|
}
|
|
}
|
|
}
|
|
|
|
var completion: () -> Void = {
|
|
fatalError("completion has not been set")
|
|
}
|
|
|
|
private var itemProvider: NSItemProvider?
|
|
|
|
var isSendDisbled: Bool { sharedContent == nil || selected == nil || isProhibited(selected) }
|
|
|
|
var isLinkPreview: Bool {
|
|
switch sharedContent {
|
|
case .url: true
|
|
default: false
|
|
}
|
|
}
|
|
|
|
func isProhibited(_ chat: SEChatData?) -> Bool {
|
|
if let chat, let sharedContent {
|
|
sharedContent.prohibited(in: chat, hasSimplexLink: hasSimplexLink)
|
|
} else { false }
|
|
}
|
|
|
|
var filteredChats: [SEChatData] {
|
|
search.isEmpty
|
|
? filterChatsToForwardTo(chats: chats)
|
|
: filterChatsToForwardTo(chats: chats)
|
|
.filter { foundChat($0, search.localizedLowercase) }
|
|
}
|
|
|
|
func setup(context: NSExtensionContext) {
|
|
if appLocalAuthEnabledGroupDefault.get() && !allowShareExtensionGroupDefault.get() {
|
|
errorAlert = ErrorAlert(title: "App is locked!", message: "You can allow sharing in Your privacy / SimpleX Lock settings.")
|
|
return
|
|
}
|
|
if let item = context.inputItems.first as? NSExtensionItem,
|
|
let itemProvider = item.attachments?.first {
|
|
self.itemProvider = itemProvider
|
|
self.completion = {
|
|
ShareModel.CompletionHandler.isEventLoopEnabled = false
|
|
context.completeRequest(returningItems: [item]) {
|
|
apiSuspendChat(expired: $0)
|
|
}
|
|
}
|
|
setup()
|
|
}
|
|
}
|
|
|
|
func setup(with dbKey: String? = nil) {
|
|
// Init Chat
|
|
Task {
|
|
if let e = initChat(with: dbKey) {
|
|
await MainActor.run { errorAlert = e }
|
|
} else {
|
|
// Load Chats
|
|
Task {
|
|
switch fetchChats() {
|
|
case let .success(chats):
|
|
// Decode base64 images on background thread
|
|
let profileImages = chats.reduce(into: Dictionary<ChatInfo.ID, UIImage>()) { dict, chatData in
|
|
if let profileImage = chatData.chatInfo.image,
|
|
let uiImage = imageFromBase64(profileImage) {
|
|
dict[chatData.id] = uiImage
|
|
}
|
|
}
|
|
await MainActor.run {
|
|
self.chats = chats
|
|
self.profileImages = profileImages
|
|
withAnimation { isLoaded = true }
|
|
}
|
|
case let .failure(error):
|
|
await MainActor.run { errorAlert = error }
|
|
}
|
|
}
|
|
// Process Attachment
|
|
Task {
|
|
switch await getSharedContent(self.itemProvider!) {
|
|
case let .success(chatItemContent):
|
|
await MainActor.run {
|
|
self.sharedContent = chatItemContent
|
|
self.bottomBar = .sendButton
|
|
if case let .text(string) = chatItemContent { comment = string }
|
|
}
|
|
case let .failure(errorAlert):
|
|
await MainActor.run { self.errorAlert = errorAlert }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func send() {
|
|
if let sharedContent, let selected {
|
|
Task {
|
|
await MainActor.run { self.bottomBar = .loadingSpinner }
|
|
do {
|
|
SEChatState.shared.set(.sendingMessage)
|
|
await waitForOtherProcessesToSuspend()
|
|
let chatItems = try apiSendMessages(
|
|
chatInfo: selected.chatInfo,
|
|
composedMessages: [ComposedMessage(fileSource: sharedContent.cryptoFile, msgContent: sharedContent.msgContent(comment: self.comment))]
|
|
)
|
|
if selected.chatInfo.chatType == .local {
|
|
completion()
|
|
} else {
|
|
// TODO batch send: share multiple items
|
|
if let ci = chatItems.first {
|
|
await MainActor.run { self.bottomBar = .loadingBar(progress: 0) }
|
|
if let e = await handleEvents(
|
|
isGroupChat: ci.chatInfo.chatType == .group,
|
|
isWithoutFile: sharedContent.cryptoFile == nil,
|
|
chatItemId: ci.chatItem.id
|
|
) {
|
|
await MainActor.run { errorAlert = e }
|
|
} else {
|
|
completion()
|
|
}
|
|
}
|
|
}
|
|
} catch {
|
|
if let e = error as? ErrorAlert {
|
|
await MainActor.run { errorAlert = e }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func initChat(with dbKey: String? = nil) -> ErrorAlert? {
|
|
do {
|
|
if hasChatCtrl() && dbKey == nil {
|
|
try apiActivateChat()
|
|
} else {
|
|
resetChatCtrl() // Clears retained migration result
|
|
registerGroupDefaults()
|
|
haskell_init_se()
|
|
let (_, result) = chatMigrateInit(dbKey, confirmMigrations: defaultMigrationConfirmation(), backgroundMode: false)
|
|
if let e = migrationError(result) { return e }
|
|
try apiSetAppFilePaths(
|
|
filesFolder: getAppFilesDirectory().path,
|
|
tempFolder: getTempFilesDirectory().path,
|
|
assetsFolder: getWallpaperDirectory().deletingLastPathComponent().path
|
|
)
|
|
let isRunning = try apiStartChat()
|
|
logger.log(level: .debug, "chat started, running: \(isRunning)")
|
|
}
|
|
try apiSetNetworkConfig(getNetCfg())
|
|
try apiSetEncryptLocalFiles(privacyEncryptLocalFilesGroupDefault.get())
|
|
} catch { return ErrorAlert(error) }
|
|
return nil
|
|
}
|
|
|
|
private func migrationError(_ r: DBMigrationResult) -> ErrorAlert? {
|
|
let useKeychain = storeDBPassphraseGroupDefault.get()
|
|
let storedDBKey = kcDatabasePassword.get()
|
|
if case .errorNotADatabase = r {
|
|
Task { await MainActor.run { self.alertRequiresPassword = true } }
|
|
}
|
|
return switch r {
|
|
case .errorNotADatabase:
|
|
if useKeychain && storedDBKey != nil && storedDBKey != "" {
|
|
ErrorAlert(
|
|
title: "Wrong database passphrase",
|
|
message: "Database passphrase is different from saved in the keychain."
|
|
)
|
|
} else {
|
|
ErrorAlert(
|
|
title: "Database encrypted!",
|
|
message: "Database passphrase is required to open chat."
|
|
)
|
|
}
|
|
case let .errorMigration(_, migrationError):
|
|
switch migrationError {
|
|
case .upgrade:
|
|
ErrorAlert(
|
|
title: "Database upgrade required",
|
|
message: "Open the app to upgrade the database."
|
|
)
|
|
case .downgrade:
|
|
ErrorAlert(
|
|
title: "Database downgrade required",
|
|
message: "Open the app to downgrade the database."
|
|
)
|
|
case let .migrationError(mtrError):
|
|
ErrorAlert(
|
|
title: "Incompatible database version",
|
|
message: mtrErrorDescription(mtrError)
|
|
)
|
|
}
|
|
case let .errorSQL(_, migrationSQLError):
|
|
ErrorAlert(
|
|
title: "Database error",
|
|
message: "Error: \(migrationSQLError)"
|
|
)
|
|
case .errorKeychain:
|
|
ErrorAlert(
|
|
title: "Keychain error",
|
|
message: "Cannot access keychain to save database password"
|
|
)
|
|
case .invalidConfirmation:
|
|
ErrorAlert("Invalid migration confirmation")
|
|
case let .unknown(json):
|
|
ErrorAlert(
|
|
title: "Database error",
|
|
message: "Unknown database error: \(json)"
|
|
)
|
|
case .ok: nil
|
|
}
|
|
}
|
|
|
|
private func fetchChats() -> Result<Array<SEChatData>, ErrorAlert> {
|
|
do {
|
|
guard let user = try apiGetActiveUser() else {
|
|
return .failure(
|
|
ErrorAlert(
|
|
title: "No active profile",
|
|
message: "Please create a profile in the SimpleX app"
|
|
)
|
|
)
|
|
}
|
|
return .success(try apiGetChats(userId: user.id))
|
|
} catch {
|
|
return .failure(ErrorAlert(error))
|
|
}
|
|
}
|
|
|
|
actor CompletionHandler {
|
|
static var isEventLoopEnabled = false
|
|
private var fileCompleted = false
|
|
private var messageCompleted = false
|
|
|
|
func completeFile() { fileCompleted = true }
|
|
|
|
func completeMessage() { messageCompleted = true }
|
|
|
|
var isRunning: Bool {
|
|
Self.isEventLoopEnabled && !(fileCompleted && messageCompleted)
|
|
}
|
|
}
|
|
|
|
/// Polls and processes chat events
|
|
/// Returns when message sending has completed optionally returning and error.
|
|
private func handleEvents(isGroupChat: Bool, isWithoutFile: Bool, chatItemId: ChatItem.ID) async -> ErrorAlert? {
|
|
func isMessage(for item: AChatItem?) -> Bool {
|
|
item.map { $0.chatItem.id == chatItemId } ?? false
|
|
}
|
|
|
|
CompletionHandler.isEventLoopEnabled = true
|
|
let ch = CompletionHandler()
|
|
if isWithoutFile { await ch.completeFile() }
|
|
networkTimeout = CFAbsoluteTimeGetCurrent()
|
|
while await ch.isRunning {
|
|
if CFAbsoluteTimeGetCurrent() - networkTimeout > 30 {
|
|
await MainActor.run {
|
|
self.errorAlert = ErrorAlert(title: "Slow network?", message: "Sending a message takes longer than expected.") {
|
|
Button("Wait", role: .cancel) { self.networkTimeout = CFAbsoluteTimeGetCurrent() }
|
|
Button("Cancel", role: .destructive) { self.completion() }
|
|
}
|
|
}
|
|
}
|
|
let r: APIResult<SEChatEvent>? = recvSimpleXMsg(messageTimeout: 1_000_000)
|
|
switch r {
|
|
case let .result(.sndFileProgressXFTP(_, ci, _, sentSize, totalSize)):
|
|
guard isMessage(for: ci) else { continue }
|
|
networkTimeout = CFAbsoluteTimeGetCurrent()
|
|
await MainActor.run {
|
|
withAnimation {
|
|
let progress = Double(sentSize) / Double(totalSize)
|
|
bottomBar = .loadingBar(progress: progress)
|
|
}
|
|
}
|
|
case let .result(.sndFileCompleteXFTP(_, ci, _)):
|
|
guard isMessage(for: ci) else { continue }
|
|
if isGroupChat {
|
|
await MainActor.run { bottomBar = .loadingSpinner }
|
|
}
|
|
await ch.completeFile()
|
|
if await !ch.isRunning { break }
|
|
case let .result(.chatItemsStatusesUpdated(_, chatItems)):
|
|
guard let ci = chatItems.last else { continue }
|
|
guard isMessage(for: ci) else { continue }
|
|
if let (title, message) = ci.chatItem.meta.itemStatus.statusInfo {
|
|
// `title` and `message` already localized and interpolated
|
|
return ErrorAlert(
|
|
title: "\(title)",
|
|
message: "\(message)"
|
|
)
|
|
} else if case let .sndSent(sndProgress) = ci.chatItem.meta.itemStatus {
|
|
switch sndProgress {
|
|
case .complete:
|
|
await ch.completeMessage()
|
|
case .partial:
|
|
if isGroupChat {
|
|
Task {
|
|
try? await Task.sleep(nanoseconds: 5 * NSEC_PER_SEC)
|
|
await ch.completeMessage()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
case let .result(.sndFileError(_, ci, _, errorMessage)):
|
|
guard isMessage(for: ci) else { continue }
|
|
if let ci { cleanupFile(ci) }
|
|
return ErrorAlert(title: "File error", message: "\(fileErrorInfo(ci) ?? errorMessage)")
|
|
case let .result(.sndFileWarning(_, ci, _, errorMessage)):
|
|
guard isMessage(for: ci) else { continue }
|
|
if let ci { cleanupFile(ci) }
|
|
return ErrorAlert(title: "File error", message: "\(fileErrorInfo(ci) ?? errorMessage)")
|
|
case let .error(chatError):
|
|
return ErrorAlert(chatError)
|
|
default: continue
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
private func fileErrorInfo(_ ci: AChatItem?) -> String? {
|
|
switch ci?.chatItem.file?.fileStatus {
|
|
case let .sndError(e): e.errorInfo
|
|
case let .sndWarning(e): e.errorInfo
|
|
default: nil
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Chat Item Content extracted from `NSItemProvider` without the comment
|
|
enum SharedContent {
|
|
case image(preview: String, cryptoFile: CryptoFile)
|
|
case movie(preview: String, duration: Int, cryptoFile: CryptoFile)
|
|
case url(preview: LinkPreview)
|
|
case text(string: String)
|
|
case data(cryptoFile: CryptoFile)
|
|
|
|
var cryptoFile: CryptoFile? {
|
|
switch self {
|
|
case let .image(_, cryptoFile): cryptoFile
|
|
case let .movie(_, _, cryptoFile): cryptoFile
|
|
case .url: nil
|
|
case .text: nil
|
|
case let .data(cryptoFile): cryptoFile
|
|
}
|
|
}
|
|
|
|
func msgContent(comment: String) -> MsgContent {
|
|
switch self {
|
|
case let .image(preview, _): .image(text: comment, image: preview)
|
|
case let .movie(preview, duration, _): .video(text: comment, image: preview, duration: duration)
|
|
case let .url(preview): .link(text: preview.uri + (comment == "" ? "" : "\n" + comment), preview: preview)
|
|
case .text: .text(comment)
|
|
case .data: .file(comment)
|
|
}
|
|
}
|
|
|
|
func prohibited(in chatData: SEChatData, hasSimplexLink: Bool) -> Bool {
|
|
chatData.prohibitedByPref(
|
|
hasSimplexLink: hasSimplexLink,
|
|
isMediaOrFileAttachment: cryptoFile != nil,
|
|
isVoice: false
|
|
)
|
|
}
|
|
}
|
|
|
|
fileprivate func getSharedContent(_ ip: NSItemProvider) async -> Result<SharedContent, ErrorAlert> {
|
|
if let type = firstMatching(of: [.image, .movie, .fileURL, .url, .text]) {
|
|
switch type {
|
|
// Prepare Image message
|
|
case .image:
|
|
// Animated
|
|
return if ip.hasItemConformingToTypeIdentifier(UTType.gif.identifier) {
|
|
if let url = try? await inPlaceUrl(type: type),
|
|
let data = try? Data(contentsOf: url),
|
|
let image = UIImage(data: data),
|
|
let cryptoFile = saveFile(data, generateNewFileName("IMG", "gif"), encrypted: privacyEncryptLocalFilesGroupDefault.get()),
|
|
let preview = await resizeImageToStrSize(image, maxDataSize: MAX_DATA_SIZE) {
|
|
.success(.image(preview: preview, cryptoFile: cryptoFile))
|
|
} else { .failure(ErrorAlert("Error preparing message")) }
|
|
|
|
// Static
|
|
} else {
|
|
if let image = await staticImage(),
|
|
let cryptoFile = saveImage(image),
|
|
let preview = await resizeImageToStrSize(image, maxDataSize: MAX_DATA_SIZE) {
|
|
.success(.image(preview: preview, cryptoFile: cryptoFile))
|
|
} else { .failure(ErrorAlert("Error preparing message")) }
|
|
}
|
|
|
|
// Prepare Movie message
|
|
case .movie:
|
|
if let url = try? await inPlaceUrl(type: type),
|
|
let trancodedUrl = await transcodeVideo(from: url),
|
|
let (image, duration) = AVAsset(url: trancodedUrl).generatePreview(),
|
|
let preview = await resizeImageToStrSize(image, maxDataSize: MAX_DATA_SIZE),
|
|
let cryptoFile = moveTempFileFromURL(trancodedUrl) {
|
|
try? FileManager.default.removeItem(at: trancodedUrl)
|
|
return .success(.movie(preview: preview, duration: duration, cryptoFile: cryptoFile))
|
|
} else { return .failure(ErrorAlert("Error preparing message")) }
|
|
|
|
// Prepare Data message
|
|
case .fileURL:
|
|
if let url = try? await inPlaceUrl(type: .data) {
|
|
if isFileTooLarge(for: url) {
|
|
let sizeString = ByteCountFormatter.string(
|
|
fromByteCount: Int64(getMaxFileSize(.xftp)),
|
|
countStyle: .binary
|
|
)
|
|
return .failure(
|
|
ErrorAlert(
|
|
title: "Large file!",
|
|
message: "Currently maximum supported file size is \(sizeString)."
|
|
)
|
|
)
|
|
}
|
|
if let file = saveFileFromURL(url) {
|
|
return .success(.data(cryptoFile: file))
|
|
}
|
|
}
|
|
return .failure(ErrorAlert("Error preparing file"))
|
|
|
|
// Prepare Link message
|
|
case .url:
|
|
if let url = try? await ip.loadItem(forTypeIdentifier: type.identifier) as? URL {
|
|
let content: SharedContent
|
|
if privacyLinkPreviewsGroupDefault.get() && !privacyLinkPreviewsShowAlertGroupDefault.get(), let linkPreview = await getLinkPreview(for: url) {
|
|
content = .url(preview: linkPreview)
|
|
} else {
|
|
content = .text(string: url.absoluteString)
|
|
}
|
|
return .success(content)
|
|
} else { return .failure(ErrorAlert("Error preparing message")) }
|
|
|
|
// Prepare Text message
|
|
case .text:
|
|
return if let text = try? await ip.loadItem(forTypeIdentifier: type.identifier) as? String {
|
|
.success(.text(string: text))
|
|
} else { .failure(ErrorAlert("Error preparing message")) }
|
|
default: return .failure(ErrorAlert("Unsupported format"))
|
|
}
|
|
} else {
|
|
return .failure(ErrorAlert("Unsupported format"))
|
|
}
|
|
|
|
|
|
func inPlaceUrl(type: UTType) async throws -> URL {
|
|
try await withCheckedThrowingContinuation { cont in
|
|
let _ = ip.loadInPlaceFileRepresentation(forTypeIdentifier: type.identifier) { url, bool, error in
|
|
if let url = url {
|
|
cont.resume(returning: url)
|
|
} else if let error = error {
|
|
cont.resume(throwing: error)
|
|
} else {
|
|
fatalError("Either `url` or `error` must be present")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func firstMatching(of types: Array<UTType>) -> UTType? {
|
|
for type in types {
|
|
if ip.hasItemConformingToTypeIdentifier(type.identifier) { return type }
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func staticImage() async -> UIImage? {
|
|
if let url = try? await inPlaceUrl(type: .image),
|
|
let downsampledImage = downsampleImage(at: url, to: MAX_DOWNSAMPLE_SIZE) {
|
|
downsampledImage
|
|
} else {
|
|
/// Fallback to loading image directly from `ItemProvider`
|
|
/// in case loading from disk is not possible. Required for sharing screenshots.
|
|
try? await ip.loadItem(forTypeIdentifier: UTType.image.identifier) as? UIImage
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
fileprivate func transcodeVideo(from input: URL) async -> URL? {
|
|
let outputUrl = URL(
|
|
fileURLWithPath: generateNewFileName(
|
|
getTempFilesDirectory().path + "/" + "video", "mp4",
|
|
fullPath: true
|
|
)
|
|
)
|
|
if await makeVideoQualityLower(input, outputUrl: outputUrl) {
|
|
return outputUrl
|
|
} else {
|
|
try? FileManager.default.removeItem(at: outputUrl)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
fileprivate func isFileTooLarge(for url: URL) -> Bool {
|
|
fileSize(url)
|
|
.map { $0 > getMaxFileSize(.xftp) }
|
|
?? false
|
|
}
|
|
|