Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi currency support #98

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Data/Data.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
21F41B002D31311A00D37DBB /* CommentRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21F41AFF2D31311A00D37DBB /* CommentRepository.swift */; };
64E499C5CFAEA368EC21313F /* Pods_DataTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1571EA0A08D442FEF7C09424 /* Pods_DataTests.framework */; };
7EF3A291581F7EA20CB1042D /* Pods_Data.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E91B3E23688435064A60C0C4 /* Pods_Data.framework */; };
D81C61532D3542600044A1E6 /* Bundle+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D81C61522D3542600044A1E6 /* Bundle+Extension.swift */; };
D826C0DF2BDA95C900AAA449 /* Timestamp+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D826C0DE2BDA95C900AAA449 /* Timestamp+Extension.swift */; };
D83B15052B9996C0004A5F4F /* Groups.swift in Sources */ = {isa = PBXBuildFile; fileRef = D83B15042B9996C0004A5F4F /* Groups.swift */; };
D83B15092B999789004A5F4F /* GroupRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = D83B15082B999789004A5F4F /* GroupRepository.swift */; };
Expand All @@ -31,6 +32,8 @@
D8613F682BFDECC4008BD53D /* Query+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8613F672BFDECC4008BD53D /* Query+Extension.swift */; };
D865F8AE2BD7CB0B0084BD36 /* Array+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D865F8AD2BD7CB0B0084BD36 /* Array+Extension.swift */; };
D88721432B99F133009DC5BE /* StorageManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D88721422B99F133009DC5BE /* StorageManager.swift */; };
D888EA772D314D180003284B /* Currency.swift in Sources */ = {isa = PBXBuildFile; fileRef = D888EA762D314D130003284B /* Currency.swift */; };
D888EA792D34E6BF0003284B /* Currencies.json in Resources */ = {isa = PBXBuildFile; fileRef = D888EA782D34E6BF0003284B /* Currencies.json */; };
D8910E382BB6D1D300877CE0 /* ExpenseStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8910E372BB6D1D300877CE0 /* ExpenseStore.swift */; };
D895F2282CA297B900C2E4EB /* NetworkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D895F2272CA297B900C2E4EB /* NetworkManager.swift */; };
D89BC2802D1C3DB200CDE86B /* DateTimeHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D89BC27F2D1C3DB200CDE86B /* DateTimeHelper.swift */; };
Expand Down Expand Up @@ -90,6 +93,7 @@
5B14CF1A2EEF27479BF50566 /* Pods-DataTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DataTests.debug.xcconfig"; path = "Target Support Files/Pods-DataTests/Pods-DataTests.debug.xcconfig"; sourceTree = "<group>"; };
803094012FE4C155F2A347B6 /* Pods-DataTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DataTests.release.xcconfig"; path = "Target Support Files/Pods-DataTests/Pods-DataTests.release.xcconfig"; sourceTree = "<group>"; };
BED6C37AA3F8FD2A6350DE1C /* Pods-Data.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Data.debug.xcconfig"; path = "Target Support Files/Pods-Data/Pods-Data.debug.xcconfig"; sourceTree = "<group>"; };
D81C61522D3542600044A1E6 /* Bundle+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Bundle+Extension.swift"; sourceTree = "<group>"; };
D826C0DE2BDA95C900AAA449 /* Timestamp+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Timestamp+Extension.swift"; sourceTree = "<group>"; };
D83B15042B9996C0004A5F4F /* Groups.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Groups.swift; sourceTree = "<group>"; };
D83B15082B999789004A5F4F /* GroupRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupRepository.swift; sourceTree = "<group>"; };
Expand All @@ -99,6 +103,8 @@
D8613F672BFDECC4008BD53D /* Query+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Query+Extension.swift"; sourceTree = "<group>"; };
D865F8AD2BD7CB0B0084BD36 /* Array+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Array+Extension.swift"; sourceTree = "<group>"; };
D88721422B99F133009DC5BE /* StorageManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageManager.swift; sourceTree = "<group>"; };
D888EA762D314D130003284B /* Currency.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Currency.swift; sourceTree = "<group>"; };
D888EA782D34E6BF0003284B /* Currencies.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = Currencies.json; sourceTree = "<group>"; };
D8910E372BB6D1D300877CE0 /* ExpenseStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpenseStore.swift; sourceTree = "<group>"; };
D895F2272CA297B900C2E4EB /* NetworkManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkManager.swift; sourceTree = "<group>"; };
D89BC27F2D1C3DB200CDE86B /* DateTimeHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateTimeHelper.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -200,9 +206,19 @@
path = Repository;
sourceTree = "<group>";
};
D888EA7A2D34E6C50003284B /* Currency */ = {
isa = PBXGroup;
children = (
D888EA762D314D130003284B /* Currency.swift */,
D888EA782D34E6BF0003284B /* Currencies.json */,
);
path = Currency;
sourceTree = "<group>";
};
D89DBE262B88801F00E5F1BD /* Model */ = {
isa = PBXGroup;
children = (
D888EA7A2D34E6C50003284B /* Currency */,
D89DBE472B8CBE4C00E5F1BD /* AppUser.swift */,
D83B15042B9996C0004A5F4F /* Groups.swift */,
D85E86DD2BAB0292002EDF76 /* Expense.swift */,
Expand Down Expand Up @@ -249,6 +265,7 @@
D865F8AD2BD7CB0B0084BD36 /* Array+Extension.swift */,
D8A7CA7F2BA867F80014EC67 /* String+Extension.swift */,
D89C934D2BC694C200FACD16 /* Double+Extension.swift */,
D81C61522D3542600044A1E6 /* Bundle+Extension.swift */,
D826C0DE2BDA95C900AAA449 /* Timestamp+Extension.swift */,
213D0CC92C89DBC800D65C73 /* Notification+Extension.swift */,
D8613F672BFDECC4008BD53D /* Query+Extension.swift */,
Expand Down Expand Up @@ -453,6 +470,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D888EA792D34E6BF0003284B /* Currencies.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -545,11 +563,13 @@
D89DBE482B8CBE4C00E5F1BD /* AppUser.swift in Sources */,
D8D14A522BA0917D00F45FF2 /* SharedCode.swift in Sources */,
D89DBE462B8CBE0F00E5F1BD /* UserRepository.swift in Sources */,
D81C61532D3542600044A1E6 /* Bundle+Extension.swift in Sources */,
D83B15052B9996C0004A5F4F /* Groups.swift in Sources */,
D8CF5A252C199ADB0014E3AD /* TransactionRepository.swift in Sources */,
D8AC25BB2B7F327A00CEAAD3 /* SplitoPreference.swift in Sources */,
D8AC25C32B7F390B00CEAAD3 /* AppAssembly.swift in Sources */,
21D8D0832C0857F10061B365 /* Constants.swift in Sources */,
D888EA772D314D180003284B /* Currency.swift in Sources */,
D8AC25C12B7F38D300CEAAD3 /* Injector.swift in Sources */,
21F41AFE2D312EDE00D37DBB /* CommentStore.swift in Sources */,
D8D14A542BA092F500F45FF2 /* ShareCodeRepository.swift in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions Data/Data/Extension/Bundle+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Bundle+Extension.swift
// Data
//
// Created by Amisha Italiya on 21/02/24.
//

import Foundation

private class UIBundleFakeClass {}

public extension Bundle {
static var dataBundle: Bundle {
return Bundle(for: UIBundleFakeClass.self)
}
}
13 changes: 13 additions & 0 deletions Data/Data/Extension/Double+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ public extension Double {
}
}

func formattedCurrencyWithSign(_ sign: String) -> String {
let amount: String
let formatter = NumberFormatter()
formatter.locale = Locale.current

if let formattedAmount = formatter.string(from: NSNumber(value: self)) {
amount = formattedAmount
} else {
amount = String(format: "%.2f", self.rounded()) // Fallback to a basic decimal format
}
return sign + " " + amount
}
cp-amisha-i marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +22 to +33
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider edge cases in currency formatting.

The implementation needs improvements to handle various currency formatting scenarios:

  1. Negative numbers aren't handled consistently with formattedCurrency
  2. Fixed space between sign and amount might not work for all currencies
  3. No support for RTL languages where the sign might need to be on the right

Consider this improved implementation:

     func formattedCurrencyWithSign(_ sign: String) -> String {
         let amount: String
         let formatter = NumberFormatter()
         formatter.locale = Locale.current
+        formatter.minimumFractionDigits = 2
+        formatter.maximumFractionDigits = 2

         if let formattedAmount = formatter.string(from: NSNumber(value: self)) {
-            amount = formattedAmount
+            amount = formattedAmount.hasPrefix("-") ? 
+                String(formattedAmount.dropFirst()) : formattedAmount
         } else {
             amount = String(format: "%.2f", self.rounded())
         }
-        return sign + " " + amount
+        
+        // Handle RTL languages
+        let isRTL = Locale.current.languageCode?.matches(
+            NSLocale.characterDirection: .rightToLeft) ?? false
+        return isRTL ? amount + " " + sign : sign + " " + amount
     }

Committable suggestion skipped: line range outside the PR's diff.


var formattedCurrencyWithSign: String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
Expand Down
Loading
Loading