This commit is contained in:
fmodf 2024-07-16 13:48:50 +02:00
parent 7666b71ef9
commit 36d030b696
7 changed files with 59 additions and 21 deletions

View file

@ -8,9 +8,10 @@ enum FileAction: Stateable {
case createAttachmentThumbnail(messageId: String, localName: String) case createAttachmentThumbnail(messageId: String, localName: String)
case attachmentThumbnailCreated(messageId: String, thumbnailName: String) case attachmentThumbnailCreated(messageId: String, thumbnailName: String)
case copyGalleryItemsForUploading(items: [SharingGalleryItem])
case galleryItemsCopiedForUploading(newMessageIds: [String], localNames: [String])
case fetchItemsFromGallery case fetchItemsFromGallery
case itemsFromGalleryFetched(items: [SharingGalleryItem]) case itemsFromGalleryFetched(items: [SharingGalleryItem])
case copyGalleryItemsForUploading(items: [SharingGalleryItem])
case copyCameraCapturedForUploading(media: Data, type: SharingCameraMediaType)
case itemsCopiedForUploading(newMessageIds: [String], localNames: [String])
} }

View file

@ -16,5 +16,5 @@ enum SharingAction: Stateable {
case galleryItemsUpdated(items: [SharingGalleryItem]) case galleryItemsUpdated(items: [SharingGalleryItem])
case cameraCaptured(media: Data, type: SharingCameraMediaType) case cameraCaptured(media: Data, type: SharingCameraMediaType)
case flushCameraCaptured // case flushCameraCaptured
} }

View file

@ -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 { private extension FileProcessing {

View file

@ -104,7 +104,17 @@ final class FileMiddleware {
case .fileAction(.copyGalleryItemsForUploading(let items)): case .fileAction(.copyGalleryItemsForUploading(let items)):
return Future { promise in return Future { promise in
let ids = FileProcessing.shared.copyGalleryItemsForUploading(items: items) 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() .eraseToAnyPublisher()

View file

@ -64,11 +64,7 @@ final class SharingMiddleware {
} }
.eraseToAnyPublisher() .eraseToAnyPublisher()
case .sharingAction(.cameraCaptured(let media, let type)): case .fileAction(.itemsCopiedForUploading(let newMessageIds, let localNames)):
print("Camera captured: \(media.count)")
return Empty().eraseToAnyPublisher()
case .fileAction(.galleryItemsCopiedForUploading(let newMessageIds, let localNames)):
if let chat = state.conversationsState.currentChat { if let chat = state.conversationsState.currentChat {
return Just(.conversationAction(.sendMediaMessages( return Just(.conversationAction(.sendMediaMessages(
from: chat.account, from: chat.account,
@ -81,6 +77,17 @@ final class SharingMiddleware {
return Empty().eraseToAnyPublisher() 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)): case .sharingAction(.shareLocation(let lat, let lon)):
if let chat = state.conversationsState.currentChat { if let chat = state.conversationsState.currentChat {
let msg = "geo:\(lat),\(lon)" let msg = "geo:\(lat),\(lon)"

View file

@ -12,13 +12,13 @@ extension SharingState {
case .setGalleryAccess(let granted): case .setGalleryAccess(let granted):
state.isGalleryAccessGranted = granted state.isGalleryAccessGranted = granted
case .cameraCaptured(let media, let type): // case .cameraCaptured(let media, let type):
state.cameraCapturedMedia = media // state.cameraCapturedMedia = media
state.cameraCapturedMediaType = type // state.cameraCapturedMediaType = type
//
case .flushCameraCaptured: // case .flushCameraCaptured:
state.cameraCapturedMedia = Data() // state.cameraCapturedMedia = Data()
state.cameraCapturedMediaType = .photo // state.cameraCapturedMediaType = .photo
case .galleryItemsUpdated(let items): case .galleryItemsUpdated(let items):
state.galleryItems = items state.galleryItems = items

View file

@ -17,8 +17,8 @@ struct SharingState: Stateable {
var isCameraAccessGranted: Bool var isCameraAccessGranted: Bool
var isGalleryAccessGranted: Bool var isGalleryAccessGranted: Bool
var cameraCapturedMedia: Data // var cameraCapturedMedia: Data
var cameraCapturedMediaType: SharingCameraMediaType // var cameraCapturedMediaType: SharingCameraMediaType
var galleryItems: [SharingGalleryItem] var galleryItems: [SharingGalleryItem]
} }
@ -30,8 +30,8 @@ extension SharingState {
isCameraAccessGranted = false isCameraAccessGranted = false
isGalleryAccessGranted = false isGalleryAccessGranted = false
cameraCapturedMedia = Data() // cameraCapturedMedia = Data()
cameraCapturedMediaType = .photo // cameraCapturedMediaType = .photo
galleryItems = [] galleryItems = []
} }