Skip to content

Commit

Permalink
Fix amount with currency
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-amisha-i authored Jan 22, 2025
1 parent 142008f commit a6ed691
Show file tree
Hide file tree
Showing 28 changed files with 145 additions and 140 deletions.
8 changes: 6 additions & 2 deletions BaseStyle/BaseStyle/CustomUI/Buttons/CapsuleButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ public struct CapsuleButton: View {
private let buttonName: String
private let paddingHr: CGFloat
private let paddingVr: CGFloat
private let cornerRadius: Double
private let isEnabled: Bool

private let onClick: (() -> Void)?

public init(buttonName: String, isEnabled: Bool = true, paddingHr: CGFloat = 73, paddingVr: CGFloat = 12, onClick: (() -> Void)?) {
public init(buttonName: String, isEnabled: Bool = true, paddingHr: CGFloat = 73,
paddingVr: CGFloat = 12, cornerRadius: Double = 12, onClick: (() -> Void)?) {
self.buttonName = buttonName
self.paddingHr = paddingHr
self.paddingVr = paddingVr
self.cornerRadius = cornerRadius
self.isEnabled = isEnabled
self.onClick = onClick
}
Expand All @@ -37,7 +41,7 @@ public struct CapsuleButton: View {
.padding(.horizontal, paddingHr)
.padding(.vertical, paddingVr)
.background(primaryColor)
.cornerRadius(12)
.cornerRadius(cornerRadius)
}
.buttonStyle(.scale)
.disabled(!isEnabled)
Expand Down
19 changes: 8 additions & 11 deletions Data/Data/Extension/Double+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ 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
formatter.numberStyle = .decimal // Added decimal which includes comma separator
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2

if let formattedAmount = formatter.string(from: NSNumber(value: self)) {
amount = formattedAmount.hasPrefix("-") ? String(formattedAmount.dropFirst()) : formattedAmount
Expand All @@ -21,17 +24,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/AppUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct AppUser: Identifiable, Codable, Hashable, Sendable {
public var imageUrl: String?
public var deviceFcmToken: String?
public var loginType: LoginType
public var totalOweAmount: [String: Double]
public var totalOweAmount: [String: Double] /// [currency: balance]
public var isActive: Bool

public var fullName: String {
Expand Down
18 changes: 9 additions & 9 deletions Data/Data/Model/Currency/Currency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ public struct Currency: Decodable, Hashable {
public let symbol: String
public let region: String

public static var defaultCurrency = Currency(code: "INR", name: "Indian Rupee", symbol: "", region: "IN")
private static var defaultLocalCurrency = Currency(code: "INR", name: "Indian Rupee", symbol: "", region: "IN")

public static var defaultCurrency: Currency {
let allCurrencies = getAllCurrencies()
let currentLocal = Locale.current.region?.identifier
let currency = allCurrencies.first(where: { $0.region == currentLocal }) ??
(allCurrencies.first ?? defaultLocalCurrency)
return currency
}

public init(code: String, name: String, symbol: String, region: String) {
self.code = code
Expand All @@ -32,12 +40,4 @@ public struct Currency: Decodable, Hashable {
let currency = allCurrencies.first(where: { $0.code == code }) ?? defaultCurrency
return currency
}

public static func getCurrentLocalCurrency() -> Currency {
let allCurrencies = getAllCurrencies()
let currentLocal = Locale.current.region?.identifier
let currency = allCurrencies.first(where: { $0.region == currentLocal }) ??
(allCurrencies.first ?? defaultCurrency)
return currency
}
}
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
6 changes: 3 additions & 3 deletions Splito/UI/Home/Expense/AddExpenseViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class AddExpenseViewModel: BaseViewModel, ObservableObject {
self.router = router
self.groupId = groupId
self.expenseId = expenseId
self.selectedCurrency = Currency.getCurrentLocalCurrency()
self.selectedCurrency = Currency.defaultCurrency
super.init()
loadInitialData()
}
Expand Down 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
Loading

0 comments on commit a6ed691

Please sign in to comment.