Skip to content

Commit

Permalink
Fixed image in not removed
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Nov 28, 2024
1 parent 164b473 commit 1bb2728
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 40 deletions.
22 changes: 10 additions & 12 deletions Data/Data/Repository/ExpenseRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ public class ExpenseRepository: ObservableObject {
return newExpense
}

public func deleteExpense(group: Groups, expense: Expense) async throws {
guard let userId = preference.user?.id else { return }

public func deleteExpense(group: Groups, expense: Expense) async throws -> Expense {
var updatedExpense = expense
updatedExpense.isActive = false // Make expense inactive
updatedExpense.updatedBy = userId
try await updateExpense(group: group, expense: updatedExpense, oldExpense: expense, type: .expenseDeleted)
updatedExpense.updatedBy = preference.user?.id ?? ""
updatedExpense.updatedAt = Timestamp()
return try await updateExpense(group: group, expense: updatedExpense, oldExpense: expense, type: .expenseDeleted)
}

public func updateExpenseWithImage(imageData: Data?, newImageUrl: String?, group: Groups, expense: (new: Expense, old: Expense), type: ActivityType) async throws -> Expense {
Expand All @@ -50,17 +49,16 @@ public class ExpenseRepository: ObservableObject {
let uploadedImageUrl = try await uploadImage(imageData: imageData, expense: updatedExpense)
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 to 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)
updatedExpense.imageUrl = nil
updatedExpense.imageUrl = ""
} else if let newImageUrl {
// If a new image URL is explicitly passed, update it
updatedExpense.imageUrl = newImageUrl
}

guard hasExpenseChanged(updatedExpense, oldExpense: expense.old) else { return updatedExpense }
try await updateExpense(group: group, expense: updatedExpense, oldExpense: expense.old, type: type)
return updatedExpense
return try await updateExpense(group: group, expense: updatedExpense, oldExpense: expense.old, type: type)
}

private func uploadImage(imageData: Data, expense: Expense) async throws -> String {
Expand All @@ -76,10 +74,10 @@ public class ExpenseRepository: ObservableObject {
oldExpense.splitData != expense.splitData || oldExpense.isActive != expense.isActive
}

public func updateExpense(group: Groups, expense: Expense, oldExpense: Expense, type: ActivityType) async throws {
guard let groupId = group.id else { return }
try await store.updateExpense(groupId: groupId, expense: expense)
public func updateExpense(group: Groups, expense: Expense, oldExpense: Expense, type: ActivityType) async throws -> Expense {
try await store.updateExpense(groupId: group.id ?? "", expense: expense)
try await addActivityLogForExpense(group: group, expense: expense, oldExpense: oldExpense, type: type)
return expense
}

private func addActivityLogForExpense(group: Groups, expense: Expense, oldExpense: Expense, type: ActivityType) async throws {
Expand Down
19 changes: 9 additions & 10 deletions Data/Data/Repository/TransactionRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public class TransactionRepository: ObservableObject {
var updatedTransaction = transaction
updatedTransaction.isActive = false // Make transaction inactive
updatedTransaction.updatedBy = preference.user?.id ?? ""
try await updateTransaction(group: group, transaction: updatedTransaction, oldTransaction: transaction,
members: (payer, receiver), type: .transactionDeleted)
return updatedTransaction
updatedTransaction.updatedAt = Timestamp()
return try await updateTransaction(group: group, transaction: updatedTransaction, oldTransaction: transaction,
members: (payer, receiver), type: .transactionDeleted)
}

public func updateTransactionWithImage(imageData: Data?, newImageUrl: String?, group: Groups, transaction: (new: Transactions, old: Transactions), members: (payer: AppUser, receiver: AppUser)) async throws -> Transactions {
Expand All @@ -51,17 +51,16 @@ public class TransactionRepository: ObservableObject {
let uploadedImageUrl = try await uploadImage(imageData: imageData, transaction: updatedTransaction)
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 to 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)
updatedTransaction.imageUrl = nil
updatedTransaction.imageUrl = ""
} else if let newImageUrl {
// If a new image URL is explicitly passed, update it
updatedTransaction.imageUrl = newImageUrl
}

guard hasTransactionChanged(updatedTransaction, oldTransaction: transaction.old) else { return updatedTransaction }
try await updateTransaction(group: group, transaction: updatedTransaction, oldTransaction: transaction.old, members: members, type: .transactionUpdated)
return updatedTransaction
return try await updateTransaction(group: group, transaction: updatedTransaction, oldTransaction: transaction.old, members: members, type: .transactionUpdated)
}

private func uploadImage(imageData: Data, transaction: Transactions) async throws -> String {
Expand All @@ -78,11 +77,11 @@ public class TransactionRepository: ObservableObject {
}

public func updateTransaction(group: Groups, transaction: Transactions, oldTransaction: Transactions,
members: (payer: AppUser, receiver: AppUser), type: ActivityType) async throws {
guard let groupId = group.id else { return }
try await store.updateTransaction(groupId: groupId, transaction: transaction)
members: (payer: AppUser, receiver: AppUser), type: ActivityType) async throws -> Transactions {
try await store.updateTransaction(groupId: group.id ?? "", transaction: transaction)
try await addActivityLogForTransaction(group: group, transaction: transaction, oldTransaction: oldTransaction,
type: type, members: members)
return transaction
}

private func addActivityLogForTransaction(group: Groups, transaction: Transactions, oldTransaction: Transactions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Data
import SwiftUI
import BaseStyle
import FirebaseFirestore

class ExpenseDetailsViewModel: BaseViewModel, ObservableObject {

Expand Down Expand Up @@ -146,8 +147,7 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject {
return
}

guard var expense, let userId = preference.user?.id,
validateUserPermission(operationText: "restored", action: "restored"),
guard var expense, let userId = preference.user?.id, validateUserPermission(operationText: "restored", action: "restored"),
validateGroupMembers(action: "restored") else { return }

Task { [weak self] in
Expand All @@ -156,8 +156,9 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject {
self.viewState = .loading
expense.isActive = true
expense.updatedBy = userId
expense.updatedAt = Timestamp()

try await self.expenseRepository.updateExpense(group: group, expense: expense,
self.expense = try await self.expenseRepository.updateExpense(group: group, expense: expense,
oldExpense: expense, type: .expenseRestored)
await self.updateGroupMemberBalance(updateType: .Add)

Expand Down Expand Up @@ -188,8 +189,8 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject {
Task {
do {
viewState = .loading
try await expenseRepository.deleteExpense(group: group, expense: expense)
NotificationCenter.default.post(name: .deleteExpense, object: expense)
self.expense = try await expenseRepository.deleteExpense(group: group, expense: expense)
NotificationCenter.default.post(name: .deleteExpense, object: self.expense)
await self.updateGroupMemberBalance(updateType: .Delete)

viewState = .initial
Expand Down
7 changes: 4 additions & 3 deletions Splito/UI/Home/Expense/Note/AddNoteViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AddNoteViewModel: BaseViewModel, ObservableObject {
var updatedExpense = expense
updatedExpense.note = note

try await expenseRepository.updateExpense(group: group, expense: updatedExpense, oldExpense: expense, type: .expenseUpdated)
updatedExpense = try await expenseRepository.updateExpense(group: group, expense: updatedExpense, oldExpense: expense, type: .expenseUpdated)
NotificationCenter.default.post(name: .updateExpense, object: updatedExpense)

showLoader = false
Expand All @@ -83,8 +83,9 @@ class AddNoteViewModel: BaseViewModel, ObservableObject {

var updatedPayment = payment
updatedPayment.note = note
try await transactionRepository.updateTransaction(group: group, transaction: updatedPayment, oldTransaction: payment,
members: members, type: .transactionUpdated)
updatedPayment = try await transactionRepository.updateTransaction(group: group, transaction: updatedPayment,
oldTransaction: payment, members: members,
type: .transactionUpdated)
NotificationCenter.default.post(name: .updateTransaction, object: updatedPayment)

showLoader = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Data
import Foundation
import FirebaseFirestore

class GroupTransactionDetailViewModel: BaseViewModel, ObservableObject {

Expand Down Expand Up @@ -154,14 +154,15 @@ class GroupTransactionDetailViewModel: BaseViewModel, ObservableObject {
}

Task { [weak self] in
guard let self, let payer = getMemberDataBy(id: transaction.payerId),
guard let self, let userId = preference.user?.id, let payer = getMemberDataBy(id: transaction.payerId),
let receiver = getMemberDataBy(id: transaction.receiverId) else { return }
do {
self.viewState = .loading
transaction.isActive = true
transaction.updatedBy = preference.user?.id ?? ""
transaction.updatedBy = userId
transaction.updatedAt = Timestamp()

try await self.transactionRepository.updateTransaction(group: group, transaction: transaction, oldTransaction: transaction, members: (payer, receiver), type: .transactionRestored)
self.transaction = try await self.transactionRepository.updateTransaction(group: group, transaction: transaction, oldTransaction: transaction, members: (payer, receiver), type: .transactionRestored)
await self.updateGroupMemberBalance(updateType: .Add)

self.viewState = .initial
Expand Down
8 changes: 2 additions & 6 deletions Splito/UI/Home/Groups/Group/GroupHomeViewModelExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,11 @@ extension GroupHomeViewModel {
}

private func deleteExpense(expense: Expense) {
guard let group, let expenseId = expense.id, let userId = preference.user?.id,
validateGroupMembers(expense: expense) else { return }
guard let group, let expenseId = expense.id, validateGroupMembers(expense: expense) else { return }

Task {
do {
var deletedExpense = expense
deletedExpense.updatedBy = userId

try await expenseRepository.deleteExpense(group: group, expense: deletedExpense)
let deletedExpense = try await expenseRepository.deleteExpense(group: group, expense: expense)
await updateGroupMemberBalance(expense: deletedExpense, updateType: .Delete)
LogD("GroupHomeViewModel: \(#function) Expense deleted successfully.")
} catch {
Expand Down

0 comments on commit 1bb2728

Please sign in to comment.