From 36d030b6960115aa7cac086638cf4278d00d3605 Mon Sep 17 00:00:00 2001 From: fmodf Date: Tue, 16 Jul 2024 13:48:50 +0200 Subject: [PATCH] wip --- .../AppCore/Actions/FileActions.swift | 7 ++++--- .../AppCore/Actions/SharingActions.swift | 2 +- .../AppCore/Files/FileProcessing.swift | 20 +++++++++++++++++++ .../AppCore/Middlewares/FileMiddleware.swift | 12 ++++++++++- .../Middlewares/SharingMiddleware.swift | 17 +++++++++++----- .../AppCore/Reducers/SharingReducer.swift | 14 ++++++------- .../AppCore/State/SharingState.swift | 8 ++++---- 7 files changed, 59 insertions(+), 21 deletions(-) diff --git a/ConversationsClassic/AppCore/Actions/FileActions.swift b/ConversationsClassic/AppCore/Actions/FileActions.swift index a8dc48e..46187c6 100644 --- a/ConversationsClassic/AppCore/Actions/FileActions.swift +++ b/ConversationsClassic/AppCore/Actions/FileActions.swift @@ -8,9 +8,10 @@ enum FileAction: Stateable { case createAttachmentThumbnail(messageId: String, localName: String) case attachmentThumbnailCreated(messageId: String, thumbnailName: String) - case copyGalleryItemsForUploading(items: [SharingGalleryItem]) - case galleryItemsCopiedForUploading(newMessageIds: [String], localNames: [String]) - case fetchItemsFromGallery case itemsFromGalleryFetched(items: [SharingGalleryItem]) + + case copyGalleryItemsForUploading(items: [SharingGalleryItem]) + case copyCameraCapturedForUploading(media: Data, type: SharingCameraMediaType) + case itemsCopiedForUploading(newMessageIds: [String], localNames: [String]) } diff --git a/ConversationsClassic/AppCore/Actions/SharingActions.swift b/ConversationsClassic/AppCore/Actions/SharingActions.swift index 522060f..b174af2 100644 --- a/ConversationsClassic/AppCore/Actions/SharingActions.swift +++ b/ConversationsClassic/AppCore/Actions/SharingActions.swift @@ -16,5 +16,5 @@ enum SharingAction: Stateable { case galleryItemsUpdated(items: [SharingGalleryItem]) case cameraCaptured(media: Data, type: SharingCameraMediaType) - case flushCameraCaptured + // case flushCameraCaptured } diff --git a/ConversationsClassic/AppCore/Files/FileProcessing.swift b/ConversationsClassic/AppCore/Files/FileProcessing.swift index a624d4d..226ac60 100644 --- a/ConversationsClassic/AppCore/Files/FileProcessing.swift +++ b/ConversationsClassic/AppCore/Files/FileProcessing.swift @@ -136,6 +136,26 @@ final class FileProcessing { } } } + + // This function also creates new id for file from camera capturing + func copyCameraCapturedForUploading(media: Data, type: SharingCameraMediaType) -> (String, String)? { + let newMessageId = UUID().uuidString + let fileId = UUID().uuidString + let localName: String + switch type { + case .photo: + localName = "\(newMessageId)_\(fileId).jpg" + case .video: + localName = "\(newMessageId)_\(fileId).mov" + } + let localUrl = FileProcessing.fileFolder.appendingPathComponent(localName) + do { + try media.write(to: localUrl) + return (newMessageId, localName) + } catch { + return nil + } + } } private extension FileProcessing { diff --git a/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift index 237002d..3261c9b 100644 --- a/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift @@ -104,7 +104,17 @@ final class FileMiddleware { case .fileAction(.copyGalleryItemsForUploading(let items)): return Future { promise in let ids = FileProcessing.shared.copyGalleryItemsForUploading(items: items) - promise(.success(.fileAction(.galleryItemsCopiedForUploading(newMessageIds: ids.map { $0.0 }, localNames: ids.map { $0.1 })))) + promise(.success(.fileAction(.itemsCopiedForUploading(newMessageIds: ids.map { $0.0 }, localNames: ids.map { $0.1 })))) + } + .eraseToAnyPublisher() + + case .fileAction(.copyCameraCapturedForUploading(let media, let type)): + return Future { promise in + if let (id, localName) = FileProcessing.shared.copyCameraCapturedForUploading(media: media, type: type) { + promise(.success(.fileAction(.itemsCopiedForUploading(newMessageIds: [id], localNames: [localName])))) + } else { + promise(.success(.empty)) + } } .eraseToAnyPublisher() diff --git a/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift index d31606d..5caab81 100644 --- a/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift @@ -64,11 +64,7 @@ final class SharingMiddleware { } .eraseToAnyPublisher() - case .sharingAction(.cameraCaptured(let media, let type)): - print("Camera captured: \(media.count)") - return Empty().eraseToAnyPublisher() - - case .fileAction(.galleryItemsCopiedForUploading(let newMessageIds, let localNames)): + case .fileAction(.itemsCopiedForUploading(let newMessageIds, let localNames)): if let chat = state.conversationsState.currentChat { return Just(.conversationAction(.sendMediaMessages( from: chat.account, @@ -81,6 +77,17 @@ final class SharingMiddleware { return Empty().eraseToAnyPublisher() } + case .sharingAction(.cameraCaptured(let media, let type)): + return Future { promise in + if let (id, localName) = FileProcessing.shared.copyCameraCapturedForUploading(media: media, type: type) { + promise(.success(.fileAction(.itemsCopiedForUploading(newMessageIds: [id], localNames: [localName]))) + ) + } else { + promise(.success(.empty)) + } + } + .eraseToAnyPublisher() + case .sharingAction(.shareLocation(let lat, let lon)): if let chat = state.conversationsState.currentChat { let msg = "geo:\(lat),\(lon)" diff --git a/ConversationsClassic/AppCore/Reducers/SharingReducer.swift b/ConversationsClassic/AppCore/Reducers/SharingReducer.swift index baddc95..4cfc2a2 100644 --- a/ConversationsClassic/AppCore/Reducers/SharingReducer.swift +++ b/ConversationsClassic/AppCore/Reducers/SharingReducer.swift @@ -12,13 +12,13 @@ extension SharingState { case .setGalleryAccess(let granted): state.isGalleryAccessGranted = granted - case .cameraCaptured(let media, let type): - state.cameraCapturedMedia = media - state.cameraCapturedMediaType = type - - case .flushCameraCaptured: - state.cameraCapturedMedia = Data() - state.cameraCapturedMediaType = .photo + // case .cameraCaptured(let media, let type): + // state.cameraCapturedMedia = media + // state.cameraCapturedMediaType = type + // + // case .flushCameraCaptured: + // state.cameraCapturedMedia = Data() + // state.cameraCapturedMediaType = .photo case .galleryItemsUpdated(let items): state.galleryItems = items diff --git a/ConversationsClassic/AppCore/State/SharingState.swift b/ConversationsClassic/AppCore/State/SharingState.swift index 323d83a..0357e15 100644 --- a/ConversationsClassic/AppCore/State/SharingState.swift +++ b/ConversationsClassic/AppCore/State/SharingState.swift @@ -17,8 +17,8 @@ struct SharingState: Stateable { var isCameraAccessGranted: Bool var isGalleryAccessGranted: Bool - var cameraCapturedMedia: Data - var cameraCapturedMediaType: SharingCameraMediaType + // var cameraCapturedMedia: Data + // var cameraCapturedMediaType: SharingCameraMediaType var galleryItems: [SharingGalleryItem] } @@ -30,8 +30,8 @@ extension SharingState { isCameraAccessGranted = false isGalleryAccessGranted = false - cameraCapturedMedia = Data() - cameraCapturedMediaType = .photo + // cameraCapturedMedia = Data() + // cameraCapturedMediaType = .photo galleryItems = [] }