From 584337526b6837fa2afbbf3207a8e4a76b7601ba Mon Sep 17 00:00:00 2001 From: NiraliSonani Date: Thu, 2 Jan 2025 17:34:58 +0530 Subject: [PATCH] Delete video and image from storage when user remove it --- Data/Data/Helper/Firebase/StorageManager.swift | 6 +++--- Data/Data/Repository/ExpenseRepository.swift | 2 +- Data/Data/Repository/FeedbackRepository.swift | 4 ++++ Data/Data/Repository/GroupRepository.swift | 2 +- .../Data/Repository/TransactionRepository.swift | 2 +- Data/Data/Repository/UserRepository.swift | 2 +- .../Account/Feedback/FeedbackViewModel.swift | 17 ++++++++++++++--- 7 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Data/Data/Helper/Firebase/StorageManager.swift b/Data/Data/Helper/Firebase/StorageManager.swift index 6755927f..32557422 100644 --- a/Data/Data/Helper/Firebase/StorageManager.swift +++ b/Data/Data/Helper/Firebase/StorageManager.swift @@ -72,15 +72,15 @@ public class StorageManager: ObservableObject { } public func updateImage(for type: ImageStoreType, id: String, url: String, imageData: Data) async throws -> String? { - try await deleteImage(imageUrl: url) + try await deleteAttachment(attachmentUrl: url) // Upload the new image asynchronously return try await uploadAttachment(for: type, id: id, attachmentData: imageData) } - public func deleteImage(imageUrl: String) async throws { + public func deleteAttachment(attachmentUrl: String) async throws { do { - let storageRef = storage.reference(forURL: imageUrl) + let storageRef = storage.reference(forURL: attachmentUrl) try await storageRef.delete() LogD("StorageManager: \(#function) Image deleted successfully.") } catch { diff --git a/Data/Data/Repository/ExpenseRepository.swift b/Data/Data/Repository/ExpenseRepository.swift index 04df05f7..14d61422 100644 --- a/Data/Data/Repository/ExpenseRepository.swift +++ b/Data/Data/Repository/ExpenseRepository.swift @@ -50,7 +50,7 @@ public class ExpenseRepository: ObservableObject { updatedExpense.imageUrl = uploadedImageUrl } else if let currentUrl = updatedExpense.imageUrl, newImageUrl == nil { // If there's a current image URL and we want to remove it, delete the image and set imageUrl empty - try await storageManager.deleteImage(imageUrl: currentUrl) + try await storageManager.deleteAttachment(attachmentUrl: currentUrl) updatedExpense.imageUrl = "" } else if let newImageUrl { // If a new image URL is explicitly passed, update it diff --git a/Data/Data/Repository/FeedbackRepository.swift b/Data/Data/Repository/FeedbackRepository.swift index ef5de026..68bfd64d 100644 --- a/Data/Data/Repository/FeedbackRepository.swift +++ b/Data/Data/Repository/FeedbackRepository.swift @@ -20,4 +20,8 @@ public class FeedbackRepository: ObservableObject { public func uploadAttachment(attachmentId: String, attachmentData: Data, attachmentType: StorageManager.AttachmentType) async throws -> String? { return try await storageManager.uploadAttachment(for: .feedback, id: attachmentId, attachmentData: attachmentData, attachmentType: attachmentType) } + + public func deleteAttachment(attachmentUrl: String) async throws { + try await storageManager.deleteAttachment(attachmentUrl: attachmentUrl) + } } diff --git a/Data/Data/Repository/GroupRepository.swift b/Data/Data/Repository/GroupRepository.swift index 1b471ec0..5d167586 100644 --- a/Data/Data/Repository/GroupRepository.swift +++ b/Data/Data/Repository/GroupRepository.swift @@ -58,7 +58,7 @@ public class GroupRepository: ObservableObject { updatedGroup.imageUrl = uploadedImageUrl } else if let currentUrl = group.imageUrl, newImageUrl == nil { // If there's a current image URL and we want to remove it, delete the image and set imageUrl empty - try await storageManager.deleteImage(imageUrl: currentUrl) + try await storageManager.deleteAttachment(attachmentUrl: currentUrl) updatedGroup.imageUrl = "" } else if let newImageUrl = newImageUrl { // If a new image URL is explicitly passed, update it diff --git a/Data/Data/Repository/TransactionRepository.swift b/Data/Data/Repository/TransactionRepository.swift index e277ede1..4383e24b 100644 --- a/Data/Data/Repository/TransactionRepository.swift +++ b/Data/Data/Repository/TransactionRepository.swift @@ -56,7 +56,7 @@ public class TransactionRepository: ObservableObject { updatedTransaction.imageUrl = uploadedImageUrl } else if let currentUrl = updatedTransaction.imageUrl, newImageUrl == nil { // If there's a current image URL and we want to remove it, delete the image and set imageUrl empty - try await storageManager.deleteImage(imageUrl: currentUrl) + try await storageManager.deleteAttachment(attachmentUrl: currentUrl) updatedTransaction.imageUrl = "" } else if let newImageUrl { // If a new image URL is explicitly passed, update it diff --git a/Data/Data/Repository/UserRepository.swift b/Data/Data/Repository/UserRepository.swift index af48f57f..ed1cf05f 100644 --- a/Data/Data/Repository/UserRepository.swift +++ b/Data/Data/Repository/UserRepository.swift @@ -61,7 +61,7 @@ public class UserRepository: ObservableObject { if let currentUrl = user.imageUrl, newImageUrl == nil { newUser.imageUrl = newImageUrl - _ = try await storageManager.deleteImage(imageUrl: currentUrl) + _ = try await storageManager.deleteAttachment(attachmentUrl: currentUrl) return try await performImageAction(imageData: imageData, user: newUser) } else { return try await performImageAction(imageData: imageData, user: newUser) diff --git a/Splito/UI/Home/Account/Feedback/FeedbackViewModel.swift b/Splito/UI/Home/Account/Feedback/FeedbackViewModel.swift index 13a444fb..e478ba24 100644 --- a/Splito/UI/Home/Account/Feedback/FeedbackViewModel.swift +++ b/Splito/UI/Home/Account/Feedback/FeedbackViewModel.swift @@ -110,13 +110,27 @@ extension FeedbackViewModel { } if let urlIndex = attachmentsUrls.firstIndex(where: { $0.id == attachment.id }) { + let attachmentUrl = attachmentsUrls[urlIndex].url attachmentsUrls.remove(at: urlIndex) + deleteAttachment(attachmentUrl: attachmentUrl) } selectedAttachments.remove(at: index) uploadingAttachments.removeAll(where: { $0.id == attachment.id }) } + func deleteAttachment(attachmentUrl: String) { + Task { + do { + try await feedbackRepository.deleteAttachment(attachmentUrl: attachmentUrl) + LogD("FeedbackViewModel: \(#function) Attachment deleted successfully.") + } catch { + LogE("FeedbackViewModel: \(#function) Failed to delete attachment: \(error)") + showToastFor(toast: ToastPrompt(type: .error, title: "Error", message: "Failed to remove attachment.")) + } + } + } + func onRetryAttachment(_ attachment: Attachment) { guard let index = selectedAttachments.firstIndex(where: { $0.id == attachment.id }) else { return @@ -140,10 +154,7 @@ extension FeedbackViewModel { selectedAttachments.removeAll() } } -} -// MARK: - Helper Methods -extension FeedbackViewModel { func upload(attachment: Attachment) { if uploadedAttachmentIDs.contains(attachment.id) { return