From 7a4ef6f3ac89e363bedfed6403431235ebe9d57c Mon Sep 17 00:00:00 2001 From: fmodf Date: Mon, 19 Aug 2024 09:34:46 +0200 Subject: [PATCH] wip --- .../AppData/Model/Message.swift | 4 +-- .../AppData/Store/AttachmentsStore.swift | 19 +++++++------- ConversationsClassic/Helpers/Const.swift | 25 +++++++++++-------- .../ConversationMessageContainer.swift | 7 +++--- .../View/Main/MainTabScreen.swift | 3 +-- .../View/Main/Settings/SettingsScreen.swift | 16 ++++++++++++ 6 files changed, 47 insertions(+), 27 deletions(-) create mode 100644 ConversationsClassic/View/Main/Settings/SettingsScreen.swift diff --git a/ConversationsClassic/AppData/Model/Message.swift b/ConversationsClassic/AppData/Model/Message.swift index 5836b48..b393a99 100644 --- a/ConversationsClassic/AppData/Model/Message.swift +++ b/ConversationsClassic/AppData/Model/Message.swift @@ -23,12 +23,12 @@ struct Attachment: Codable & Equatable, DatabaseValueConvertible { var localPath: URL? { guard let attachmentLocalName = localName else { return nil } - return Const.fileFolder.appendingPathComponent(attachmentLocalName) + return FolderWrapper.shared.fileFolder.appendingPathComponent(attachmentLocalName) } var thumbnailPath: URL? { guard let attachmentThumbnailName = thumbnailName else { return nil } - return Const.fileFolder.appendingPathComponent(attachmentThumbnailName) + return FolderWrapper.shared.fileFolder.appendingPathComponent(attachmentThumbnailName) } } diff --git a/ConversationsClassic/AppData/Store/AttachmentsStore.swift b/ConversationsClassic/AppData/Store/AttachmentsStore.swift index 3004821..c9f1f11 100644 --- a/ConversationsClassic/AppData/Store/AttachmentsStore.swift +++ b/ConversationsClassic/AppData/Store/AttachmentsStore.swift @@ -69,7 +69,7 @@ extension AttachmentsStore { guard let photo = try? await PHImageManager.default().getPhoto(for: asset) else { return } guard let data = photo.jpegData(compressionQuality: 1.0) else { return } let localName = "\(message.id)_\(UUID().uuidString).jpg" - let localUrl = Const.fileFolder.appendingPathComponent(localName) + let localUrl = FolderWrapper.shared.fileFolder.appendingPathComponent(localName) try? data.write(to: localUrl) message.contentType = .attachment( Attachment( @@ -88,7 +88,7 @@ extension AttachmentsStore { let assetURL = video as! AVURLAsset let url = assetURL.url let localName = "\(message.id)_\(UUID().uuidString).mov" - let localUrl = Const.fileFolder.appendingPathComponent(localName) + let localUrl = FolderWrapper.shared.fileFolder.appendingPathComponent(localName) try? FileManager.default.copyItem(at: url, to: localUrl) message.contentType = .attachment( Attachment( @@ -130,7 +130,7 @@ extension AttachmentsStore { } // save - let localUrl = Const.fileFolder.appendingPathComponent(localName) + let localUrl = FolderWrapper.shared.fileFolder.appendingPathComponent(localName) try data.write(to: localUrl) return (localName, msgType) }.value @@ -164,7 +164,7 @@ extension AttachmentsStore { let newMessageId = UUID().uuidString let fileId = UUID().uuidString let localName = "\(newMessageId)_\(fileId).\(extensions[index])" - let localUrl = Const.fileFolder.appendingPathComponent(localName) + let localUrl = FolderWrapper.shared.fileFolder.appendingPathComponent(localName) do { try data.write(to: localUrl) } catch { @@ -215,19 +215,20 @@ private extension AttachmentsStore { .filter { $0.contentType.isAttachment } for message in forProcessing { if case .attachment(let attachment) = message.contentType { - if attachment.localPath != nil, attachment.remotePath == nil { + let localPath = attachment.localPath + if localPath != nil, attachment.remotePath == nil { // Uploading self?.processing.insert(message.id) Task { await self?.uploadAttachment(message) } - } else if attachment.localPath == nil, attachment.remotePath != nil { + } else if localPath == nil, attachment.remotePath != nil { // Downloading self?.processing.insert(message.id) Task { await self?.downloadAttachment(message) } - } else if attachment.localPath != nil, attachment.remotePath != nil, attachment.thumbnailName == nil, attachment.type == .image { + } else if localPath != nil, attachment.remotePath != nil, attachment.thumbnailName == nil, attachment.type == .image { // Generate thumbnail self?.processing.insert(message.id) Task { @@ -282,7 +283,7 @@ extension AttachmentsStore { } do { let localName = "\(message.id)_\(UUID().uuidString).\(remoteUrl.lastPathComponent)" - let localUrl = Const.fileFolder.appendingPathComponent(localName) + let localUrl = FolderWrapper.shared.fileFolder.appendingPathComponent(localName) // Download the file let (tempUrl, _) = try await URLSession.shared.download(from: remoteUrl) @@ -315,7 +316,7 @@ extension AttachmentsStore { return } let thumbnailFileName = "thumb_\(localName)" - let thumbnailUrl = Const.fileFolder.appendingPathComponent(thumbnailFileName) + let thumbnailUrl = FolderWrapper.shared.fileFolder.appendingPathComponent(thumbnailFileName) // if !FileManager.default.fileExists(atPath: thumbnailUrl.path) { diff --git a/ConversationsClassic/Helpers/Const.swift b/ConversationsClassic/Helpers/Const.swift index 6659ade..4c2d9f9 100644 --- a/ConversationsClassic/Helpers/Const.swift +++ b/ConversationsClassic/Helpers/Const.swift @@ -14,17 +14,6 @@ enum Const { Bundle.main.bundleIdentifier ?? "Conversations Classic iOS" } - // Folder for storing files - static var fileFolder: URL { - // swiftlint:disable:next force_unwrapping - let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! - let subdirectoryURL = documentsURL.appendingPathComponent("Downloads") - if !FileManager.default.fileExists(atPath: subdirectoryURL.path) { - try? FileManager.default.createDirectory(at: subdirectoryURL, withIntermediateDirectories: true, attributes: nil) - } - return subdirectoryURL - } - // Trusted servers enum TrustedServers: String { case narayana = "narayana.im" @@ -46,3 +35,17 @@ enum Const { // MAM request page size static let mamRequestPageSize = 50 } + +final class FolderWrapper { + static let shared = FolderWrapper() + let fileFolder: URL + private init() { + // swiftlint:disable:next force_unwrapping + let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! + let subdirectoryURL = documentsURL.appendingPathComponent("Downloads") + if !FileManager.default.fileExists(atPath: subdirectoryURL.path) { + try? FileManager.default.createDirectory(at: subdirectoryURL, withIntermediateDirectories: true, attributes: nil) + } + fileFolder = subdirectoryURL + } +} diff --git a/ConversationsClassic/View/Main/Conversation/ConversationMessageContainer.swift b/ConversationsClassic/View/Main/Conversation/ConversationMessageContainer.swift index b106fa9..7d5c22f 100644 --- a/ConversationsClassic/View/Main/Conversation/ConversationMessageContainer.swift +++ b/ConversationsClassic/View/Main/Conversation/ConversationMessageContainer.swift @@ -110,14 +110,15 @@ private struct AttachmentView: View { } else { switch attachment.type { case .image: - if let thumbnail = thumbnail() { - thumbnail + AsyncImage(url: attachment.thumbnailPath) { image in + image .resizable() .aspectRatio(contentMode: .fit) .frame(width: Const.attachmentPreviewSize, height: Const.attachmentPreviewSize) - } else { + } placeholder: { placeholder } + .frame(width: Const.attachmentPreviewSize, height: Const.attachmentPreviewSize) case .video: if let file = attachment.localPath { diff --git a/ConversationsClassic/View/Main/MainTabScreen.swift b/ConversationsClassic/View/Main/MainTabScreen.swift index fc09424..e34311f 100644 --- a/ConversationsClassic/View/Main/MainTabScreen.swift +++ b/ConversationsClassic/View/Main/MainTabScreen.swift @@ -29,8 +29,7 @@ struct MainTabScreen: View { ContactsScreen() case .settings: - Color.green - // SettingsScreen() + SettingsScreen() } // Tab bar diff --git a/ConversationsClassic/View/Main/Settings/SettingsScreen.swift b/ConversationsClassic/View/Main/Settings/SettingsScreen.swift new file mode 100644 index 0000000..99c77da --- /dev/null +++ b/ConversationsClassic/View/Main/Settings/SettingsScreen.swift @@ -0,0 +1,16 @@ +import SwiftUI + +struct SettingsScreen: View { + var body: some View { + ZStack { + // bg + Color.Material.Background.light + .ignoresSafeArea() + + // content + Text("Soon!") + .font(.head1l) + .foregroundColor(.Material.Elements.active) + } + } +}