Skip to content

Commit

Permalink
Fix default currency
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-amisha-i committed Jan 22, 2025
1 parent eeca86c commit ce76ca6
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
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 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
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class CreateGroupViewModel: BaseViewModel, ObservableObject {
private func createGroup() async -> Bool {
guard let userId = preference.user?.id else { return false }

let localCurrency = Currency.getCurrentLocalCurrency().code
let localCurrency = Currency.defaultCurrency.code
let memberBalance = GroupMemberBalance(id: userId, balanceByCurrency: [:])
let group = Groups(name: groupName.trimming(spaces: .leadingAndTrailing), createdBy: userId,
members: [userId], balances: [memberBalance], currencyCode: localCurrency)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ private struct GroupBalanceItemView: View {
var body: some View {
VStack(alignment: .leading, spacing: 20) {
HStack(spacing: 16) {
let totalOwed = memberBalance.totalOwedAmount.reduce(0) { $0 + $1.value }
let name = viewModel.getMemberName(id: memberBalance.id, needFullName: true)

HStack(spacing: 16) {
MemberProfileImageView(imageUrl: imageUrl)

if totalOwed == 0 {
if memberBalance.totalOwedAmount.allSatisfy({ $0.value == 0 }) {
Group {
Text(name)
.font(.subTitle2())
Expand Down Expand Up @@ -143,7 +142,7 @@ private struct GroupBalanceItemView: View {
}
.frame(maxWidth: .infinity, alignment: .leading)

if totalOwed != 0 {
if memberBalance.totalOwedAmount.allSatisfy({ $0.value != 0 }) {
ScrollToTopButton(
icon: "chevron.down", iconColor: primaryText, bgColor: container2Color,
showWithAnimation: true, size: (10, 7), isFirstGroupCell: memberBalance.isExpanded,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class GroupTransactionDetailViewModel: BaseViewModel, ObservableObject {
self.router = router
self.groupId = groupId
self.transactionId = transactionId
self.amountCurrency = "INR"
self.amountCurrency = Currency.defaultCurrency.code
super.init()

NotificationCenter.default.addObserver(self, selector: #selector(getUpdatedTransaction(notification:)), name: .updateTransaction, object: nil)
Expand Down Expand Up @@ -78,7 +78,7 @@ class GroupTransactionDetailViewModel: BaseViewModel, ObservableObject {
private func fetchTransaction() async {
do {
transaction = try await transactionRepository.fetchTransactionBy(groupId: groupId, transactionId: transactionId)
amountCurrency = transaction?.currencyCode ?? (group?.defaultCurrencyCode ?? "INR")
amountCurrency = transaction?.currencyCode ?? (group?.defaultCurrencyCode ?? Currency.defaultCurrency.code)
await setTransactionUsersData()
LogD("GroupTransactionDetailViewModel: \(#function) Payment fetched successfully.")
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GroupTotalsViewModel: BaseViewModel, ObservableObject {

@Published var showCurrencyPicker = false
@Published var supportedCurrencies: [Currency] = [Currency.defaultCurrency]
@Published var selectedCurrency: Currency = Currency.getCurrentLocalCurrency() {
@Published var selectedCurrency: Currency = Currency.defaultCurrency {
didSet {
filterDataForSelectedTab() // Recalculate data when currency changes
}
Expand Down

0 comments on commit ce76ca6

Please sign in to comment.