This commit is contained in:
fmodf 2024-08-19 09:34:46 +02:00
parent 178a92469d
commit 7a4ef6f3ac
6 changed files with 47 additions and 27 deletions

View file

@ -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)
}
}

View file

@ -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) {

View file

@ -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
}
}

View file

@ -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 {

View file

@ -29,8 +29,7 @@ struct MainTabScreen: View {
ContactsScreen()
case .settings:
Color.green
// SettingsScreen()
SettingsScreen()
}
// Tab bar

View file

@ -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)
}
}
}