Skip to content

Commit

Permalink
Fixed: group spending summary amount not shown with sign
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Jan 22, 2025
1 parent de0cc38 commit f46ca5f
Show file tree
Hide file tree
Showing 19 changed files with 37 additions and 43 deletions.
16 changes: 5 additions & 11 deletions Data/Data/Extension/Double+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

public extension Double {

func formattedCurrencyWithSign(_ code: String?) -> String {
func formattedCurrency(_ code: String?, _ showSign: Bool = false) -> String {
let amount: String
let formatter = NumberFormatter()
formatter.locale = Locale.current
Expand All @@ -21,17 +21,11 @@ public extension Double {
}

let currencySymbol = Currency.getCurrencyFromCode(code).symbol
return currencySymbol.isEmpty ? amount : (currencySymbol + " " + amount)
}

var formattedCurrency: String {
let formatter = NumberFormatter()
formatter.locale = Locale.current

if let formattedAmount = formatter.string(from: NSNumber(value: self)) {
return formattedAmount.hasPrefix("-") ? String(formattedAmount.dropFirst()) : formattedAmount
if showSign {
let sign = self < 0 ? "-" : ""
return currencySymbol.isEmpty ? "\(sign)\(amount)" : "\(sign)\(currencySymbol) \(amount)"
} else {
return String(format: "%.2f", self.rounded()) // Fallback to a basic decimal format
return currencySymbol.isEmpty ? amount : (currencySymbol + " " + amount)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Data/Data/Model/Expense.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public struct Expense: Codable, Hashable, Identifiable {

// Calculated properties for better UI representation
public var formattedAmount: String {
return amount.formattedCurrencyWithSign(currencyCode)
return amount.formattedCurrency(currencyCode)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Splito/UI/Home/ActivityLog/ActivityLogView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ private struct ActivityListCellView: View {
return ""
case .expenseAdded, .expenseUpdated, .expenseDeleted, .expenseRestored:
let action = (amount > 0 ? "get back" : "owe")
return (amount == 0) ? "You do not owe anything" : "You \(action) \(amount.formattedCurrencyWithSign(activityLog.amountCurrency))"
return (amount == 0) ? "You do not owe anything" : "You \(action) \(amount.formattedCurrency(activityLog.amountCurrency))"
case .transactionAdded, .transactionUpdated, .transactionDeleted, .transactionRestored:
let action = (amount > 0 ? "paid" : "received")
return (amount == 0) ? "You do not owe anything" : "You \(action) \(amount.formattedCurrencyWithSign(activityLog.amountCurrency))"
return (amount == 0) ? "You do not owe anything" : "You \(action) \(amount.formattedCurrency(activityLog.amountCurrency))"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Splito/UI/Home/Expense/AddExpenseViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,11 @@ extension AddExpenseViewModel {
let amountDescription = totalSharedAmount < expenseAmount ? "short" : "over"
let differenceAmount = totalSharedAmount < expenseAmount ? (expenseAmount - totalSharedAmount) : (totalSharedAmount - expenseAmount)
showAlertFor(title: "Error!",
message: "The amounts do not add up to the total cost of \(expenseAmount.formattedCurrencyWithSign(selectedCurrency.code)). You are \(amountDescription) by \(differenceAmount.formattedCurrencyWithSign(selectedCurrency.code)).")
message: "The amounts do not add up to the total cost of \(expenseAmount.formattedCurrency(selectedCurrency.code)). You are \(amountDescription) by \(differenceAmount.formattedCurrency(selectedCurrency.code)).")
return false
} else if selectedPayers.count > 1 && totalPaidAmount != expenseAmount {
showAlertFor(title: "Error",
message: "The total of everyone's paid shares (\(totalPaidAmount.formattedCurrencyWithSign(selectedCurrency.code))) is different than the total cost (\(expenseAmount.formattedCurrencyWithSign(selectedCurrency.code)))")
message: "The total of everyone's paid shares (\(totalPaidAmount.formattedCurrency(selectedCurrency.code))) is different than the total cost (\(expenseAmount.formattedCurrency(selectedCurrency.code)))")
return false
} else if selectedPayers.count == 1 && selectedPayers.values.reduce(0, +) != expenseAmount {
showAlertFor(title: "Error",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ struct MemberCellView: View {
.foregroundStyle(primaryText)

if let splitAmount {
Text(splitAmount.formattedCurrencyWithSign(amountCurrency))
Text(splitAmount.formattedCurrency(amountCurrency))
.font(.body3(12))
.foregroundStyle(disableText)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ private struct SplitOptionsBottomView: View {
switch viewModel.selectedTab {
case .equally:
let selectedCurrency = viewModel.selectedCurrency
BottomInfoCardView(title: "\(viewModel.splitAmount.formattedCurrencyWithSign(selectedCurrency))/person",
BottomInfoCardView(title: "\(viewModel.splitAmount.formattedCurrency(selectedCurrency))/person",
value: "\(viewModel.selectedMembers.count) people",
memberCount: viewModel.selectedMembers.count, isAllSelected: viewModel.isAllSelected,
isForEqualSplit: true, onAllBtnTap: viewModel.handleAllBtnAction)
case .fixedAmount:
let selectedCurrency = viewModel.selectedCurrency
BottomInfoCardView(title: "\(viewModel.totalFixedAmount.formattedCurrencyWithSign(selectedCurrency)) of \(viewModel.expenseAmount.formattedCurrencyWithSign(selectedCurrency))",
value: "\((viewModel.expenseAmount - viewModel.totalFixedAmount).formattedCurrencyWithSign(selectedCurrency)) left")
BottomInfoCardView(title: "\(viewModel.totalFixedAmount.formattedCurrency(selectedCurrency)) of \(viewModel.expenseAmount.formattedCurrency(selectedCurrency))",
value: "\((viewModel.expenseAmount - viewModel.totalFixedAmount).formattedCurrency(selectedCurrency)) left")
case .percentage:
BottomInfoCardView(title: "\(String(format: "%.0f", viewModel.totalPercentage))% of 100%",
value: "\(String(format: "%.0f", 100 - viewModel.totalPercentage))% left")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class ExpenseSplitOptionsViewModel: BaseViewModel, ObservableObject {
let amountDescription = totalFixedAmount < expenseAmount ? "short" : "over"
let differenceAmount = totalFixedAmount < expenseAmount ? (expenseAmount - totalFixedAmount) : (totalFixedAmount - expenseAmount)

showAlertFor(title: "Whoops!", message: "The amounts do not add up to the total cost of \(expenseAmount.formattedCurrencyWithSign(selectedCurrency)). You are \(amountDescription) by \(differenceAmount.formattedCurrencyWithSign(selectedCurrency)).")
showAlertFor(title: "Whoops!", message: "The amounts do not add up to the total cost of \(expenseAmount.formattedCurrency(selectedCurrency)). You are \(amountDescription) by \(differenceAmount.formattedCurrency(selectedCurrency)).")
return completion(false)
}
case .percentage:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ struct ChooseMultiplePayerView: View {
.scrollBounceBehavior(.basedOnSize)

let currency = viewModel.amountCurrency
BottomInfoCardView(title: "\(viewModel.totalAmount.formattedCurrencyWithSign(currency)) of \(viewModel.expenseAmount.formattedCurrencyWithSign(currency))",
value: "\((viewModel.expenseAmount - viewModel.totalAmount).formattedCurrencyWithSign(currency)) left")
BottomInfoCardView(title: "\(viewModel.totalAmount.formattedCurrency(currency)) of \(viewModel.expenseAmount.formattedCurrency(currency))",
value: "\((viewModel.expenseAmount - viewModel.totalAmount).formattedCurrency(currency)) left")
}
}
.background(surfaceColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ChooseMultiplePayerViewModel: BaseViewModel, ObservableObject {
let differenceAmount = totalAmount < expenseAmount ? (expenseAmount - totalAmount) : (totalAmount - expenseAmount)

showAlertFor(title: "Whoops!",
message: "The payment values do not add up to the total cost of \(expenseAmount.formattedCurrencyWithSign(amountCurrency)). You are \(amountDescription) by \(differenceAmount.formattedCurrencyWithSign(amountCurrency)).")
message: "The payment values do not add up to the total cost of \(expenseAmount.formattedCurrency(amountCurrency)). You are \(amountDescription) by \(differenceAmount.formattedCurrency(amountCurrency)).")
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ private struct ExpenseInfoView: View {
MemberProfileImageView(imageUrl: userData.imageUrl, height: SUB_IMAGE_HEIGHT, scaleEffect: 0.6)

if let splitTo = expense?.splitTo, splitTo.contains(userData.id) {
Text("\(memberName.localized) paid \(paidAmount.formattedCurrencyWithSign(currencyCode)) and \(owes.localized) \(splitAmount)")
Text("\(memberName.localized) paid \(paidAmount.formattedCurrency(currencyCode)) and \(owes.localized) \(splitAmount)")
} else {
Text("\(memberName.localized) paid \(paidAmount.formattedCurrencyWithSign(currencyCode))")
Text("\(memberName.localized) paid \(paidAmount.formattedCurrency(currencyCode))")
}
} else if let splitTo = expense?.splitTo, splitTo.contains(userData.id) {
MemberProfileImageView(imageUrl: userData.imageUrl, height: SUB_IMAGE_HEIGHT, scaleEffect: 0.6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject {
func getSplitAmount(for member: String) -> String {
guard let expense else { return "" }
let finalAmount = expense.getTotalSplitAmountOf(member: member)
return finalAmount.formattedCurrencyWithSign(expense.currencyCode)
return finalAmount.formattedCurrency(expense.currencyCode)
}

@objc private func getUpdatedExpense(notification: Notification) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ private struct GroupBalanceItemView: View {
let negativeAmounts = memberBalance.totalOwedAmount.filter { $0.value < 0 }

let positiveText = positiveAmounts.map { currency, amount in
amount.formattedCurrencyWithSign(currency)
amount.formattedCurrency(currency)
}.joined(separator: " + ")

let negativeText = negativeAmounts.map { currency, amount in
abs(amount).formattedCurrencyWithSign(currency) // Use `abs` for positive display
abs(amount).formattedCurrency(currency) // Use `abs` for positive display
}.joined(separator: " + ")

VStack(alignment: .leading, spacing: 0) {
Expand Down Expand Up @@ -199,7 +199,7 @@ private struct GroupBalanceItemMemberView: View {

Group {
Text("\(owedMemberName.capitalized) \(owesText.localized) ")
+ Text(amount.formattedCurrencyWithSign(currency))
+ Text(amount.formattedCurrency(currency))
.foregroundColor(hasDue ? errorColor : successColor)
+ Text(" to \(owesMemberName)")
}
Expand Down Expand Up @@ -240,7 +240,7 @@ private struct GroupBalanceItemMemberView: View {

private func generateReminderText(owedMemberName: String, owesText: String, amount: Double,
currency: String, owesMemberName: String) -> String {
let formattedAmount = amount.formattedCurrencyWithSign(currency)
let formattedAmount = amount.formattedCurrency(currency)
let groupName = viewModel.group?.name ?? ""
let deepLink = "\(Constants.groupBaseUrl)\(viewModel.groupId)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private struct GroupMemberCellView: View {
Text(isBorrowed ? "you owe" : "owes you")
.font(.caption1())

Text(abs(memberBalance.balance).formattedCurrencyWithSign(memberBalance.currencyCode))
Text(abs(memberBalance.balance).formattedCurrency(memberBalance.currencyCode))
.font(.body1())
}
.lineLimit(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private struct TransactionItemView: View {
Spacer()

let currencyCode = transactionWithUser.transaction.currencyCode
Text("\(transactionWithUser.transaction.amount.formattedCurrencyWithSign(currencyCode))")
Text("\(transactionWithUser.transaction.amount.formattedCurrency(currencyCode))")
.font(.subTitle2())
.foregroundStyle(transactionWithUser.payer?.id == preference.user?.id ? successColor : transactionWithUser.receiver?.id == preference.user?.id ? errorColor : primaryText)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private struct TransactionSummaryView: View {
.padding(.bottom, 8)
}

Text(amount?.formattedCurrencyWithSign(currency) ?? 0.0.formattedCurrencyWithSign(currency))
Text(amount?.formattedCurrency(currency) ?? 0.0.formattedCurrency(currency))
.font(.Header2())
.foregroundStyle(primaryText)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private struct GroupSummaryAmountView: View {

Spacer()

Text(amount.formattedCurrencyWithSign(currencySymbol))
Text(amount.formattedCurrency(currencySymbol, true))
.font(.body1())
.foregroundStyle(amount == 0 ? lowestText : fontColor)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private struct GroupMemberCellView: View {
Text(isBorrowed ? "owes" : "gets back")
.font(.caption1())

Text(amount.formattedCurrencyWithSign(currency))
Text(amount.formattedCurrency(currency))
.font(.body1())
+ Text(balance.count > 1 ? "*" : "")
.font(.body1())
Expand Down
8 changes: 4 additions & 4 deletions Splito/UI/Home/Groups/Group/GroupExpenseListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ struct GroupExpenseItemView: View {
Text(isBorrowed ? "you borrowed" : "you lent")
.font(.caption1())

Text(amount.formattedCurrencyWithSign(expense.currencyCode))
Text(amount.formattedCurrency(expense.currencyCode))
.font(.body1())
} else {
Text("not involved")
Expand Down Expand Up @@ -349,7 +349,7 @@ private struct GroupExpenseHeaderOverallView: View {
.multilineTextAlignment(.trailing)

let spendingText = viewModel.currentMonthSpending.map { (currency, amount) in
abs(amount).formattedCurrencyWithSign(currency)
abs(amount).formattedCurrency(currency)
}.joined(separator: " + ")

Text(spendingText)
Expand Down Expand Up @@ -377,15 +377,15 @@ private struct GroupExpenseMemberOweView: View {
Group {
Text("\(name.localized) owes you ")
.foregroundColor(disableText)
+ Text(amount.formattedCurrencyWithSign(currency))
+ Text(amount.formattedCurrency(currency))
.foregroundColor(successColor)
}
.font(.body3())
} else if amount < 0 {
Group {
Text("You owe \(name.localized) ")
.foregroundColor(disableText)
+ Text(amount.formattedCurrencyWithSign(currency))
+ Text(amount.formattedCurrency(currency))
.foregroundColor(errorColor)
}.font(.body3())
}
Expand Down
6 changes: 3 additions & 3 deletions Splito/UI/Home/Groups/GroupListWithDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private struct GroupListCellView: View {
Text(isBorrowed ? "you owe" : "you are owed")
.font(.caption1())

Text(userBalance.value.formattedCurrencyWithSign(userBalance.key))
Text(userBalance.value.formattedCurrency(userBalance.key))
.font(.body1())
+ Text(group.userBalance.count > 1 ? "*" : "")
.font(.body1())
Expand Down Expand Up @@ -206,15 +206,15 @@ private struct GroupExpenseMemberOweView: View {
Group {
Text("\(name.localized) owes you ")
.foregroundColor(disableText)
+ Text(amount.formattedCurrencyWithSign(currency))
+ Text(amount.formattedCurrency(currency))
.foregroundColor(successColor)
}
.font(.body3())
} else if amount < 0 {
Group {
Text("You owe \(name.localized) ")
.foregroundColor(disableText)
+ Text(amount.formattedCurrencyWithSign(currency))
+ Text(amount.formattedCurrency(currency))
.foregroundColor(errorColor)
}
.font(.body3())
Expand Down

0 comments on commit f46ca5f

Please sign in to comment.