Skip to content

Commit

Permalink
Delete video and image from storage when user remove it
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Jan 2, 2025
1 parent 7919351 commit 5843375
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Data/Data/Helper/Firebase/StorageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Data/Data/Repository/ExpenseRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Data/Data/Repository/FeedbackRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion Data/Data/Repository/GroupRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Data/Data/Repository/TransactionRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Data/Data/Repository/UserRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions Splito/UI/Home/Account/Feedback/FeedbackViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -140,10 +154,7 @@ extension FeedbackViewModel {
selectedAttachments.removeAll()
}
}
}

// MARK: - Helper Methods
extension FeedbackViewModel {
func upload(attachment: Attachment) {
if uploadedAttachmentIDs.contains(attachment.id) {
return
Expand Down

0 comments on commit 5843375

Please sign in to comment.