From 695094724cbbb66c247f223c7b5500524463575d Mon Sep 17 00:00:00 2001 From: PaulNguyen Date: Fri, 18 Nov 2022 15:24:45 +0700 Subject: [PATCH 1/3] format amount with commas --- GoMoney.xcodeproj/project.pbxproj | 4 +++ GoMoney/Extensions/Double+Extension.swift | 13 ++++++++++ .../Scences/Home/View/RecentExpenseCell.swift | 25 ++++++++++++++----- 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 GoMoney/Extensions/Double+Extension.swift diff --git a/GoMoney.xcodeproj/project.pbxproj b/GoMoney.xcodeproj/project.pbxproj index 98a80e4..571559b 100644 --- a/GoMoney.xcodeproj/project.pbxproj +++ b/GoMoney.xcodeproj/project.pbxproj @@ -48,6 +48,7 @@ 085F7539291278100094A026 /* ConnectionService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 085F7538291278100094A026 /* ConnectionService.swift */; }; 086B4FB5291FFF01005C61F6 /* CountryData.plist in Resources */ = {isa = PBXBuildFile; fileRef = 086B4FB4291FFF01005C61F6 /* CountryData.plist */; }; 086C5D1A29150B2E00B6DE49 /* calculator.json in Resources */ = {isa = PBXBuildFile; fileRef = 086C5D1929150B2E00B6DE49 /* calculator.json */; }; + 0875F3092927778000D122C3 /* Double+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0875F3082927778000D122C3 /* Double+Extension.swift */; }; 0878DBA12924939D004E3FFD /* NewTagViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0878DBA02924939D004E3FFD /* NewTagViewController.swift */; }; 0878DBA42924AA15004E3FFD /* IconCollectionVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0878DBA32924AA15004E3FFD /* IconCollectionVC.swift */; }; 0878DBA62924AC43004E3FFD /* IconPickerCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0878DBA52924AC43004E3FFD /* IconPickerCell.swift */; }; @@ -210,6 +211,7 @@ 085F7538291278100094A026 /* ConnectionService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionService.swift; sourceTree = ""; }; 086B4FB4291FFF01005C61F6 /* CountryData.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = CountryData.plist; sourceTree = ""; }; 086C5D1929150B2E00B6DE49 /* calculator.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = calculator.json; sourceTree = ""; }; + 0875F3082927778000D122C3 /* Double+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+Extension.swift"; sourceTree = ""; }; 0878DBA02924939D004E3FFD /* NewTagViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTagViewController.swift; sourceTree = ""; }; 0878DBA32924AA15004E3FFD /* IconCollectionVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IconCollectionVC.swift; sourceTree = ""; }; 0878DBA52924AC43004E3FFD /* IconPickerCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IconPickerCell.swift; sourceTree = ""; }; @@ -726,6 +728,7 @@ 08C872EF28F6CAAB00DC859D /* UIView+Constraint.swift */, 08DB56E828F9035800170C60 /* StackView+Extension.swift */, 08DB56EA28F9053700170C60 /* UIColor+Extension.swift */, + 0875F3082927778000D122C3 /* Double+Extension.swift */, 08C1F25C28FE93E900590512 /* DateFormatter+Extension.swift */, 08C1F26928FFBAD500590512 /* UIImage+Extension.swift */, 08DEE9C52900FC4C00EA9A2A /* Date+Extension.swift */, @@ -1148,6 +1151,7 @@ 089CC756291A0C0A0070A108 /* BiometricService.swift in Sources */, 08B48B7C290293AE00344650 /* UIBarButtonItem+Extension.swift in Sources */, 08C8731928F865CA00DC859D /* SignUpPasswordVC.swift in Sources */, + 0875F3092927778000D122C3 /* Double+Extension.swift in Sources */, 08EEA00E291CC374003B35B8 /* Widget+Kind.swift in Sources */, 082BF97D28FD68480094FE94 /* Notification+Name.swift in Sources */, 083EA336290659660079605F /* TransactionTag.swift in Sources */, diff --git a/GoMoney/Extensions/Double+Extension.swift b/GoMoney/Extensions/Double+Extension.swift new file mode 100644 index 0000000..cf5bc10 --- /dev/null +++ b/GoMoney/Extensions/Double+Extension.swift @@ -0,0 +1,13 @@ +import Foundation + +extension Double { + func formatWithCommas() -> String { + let numberFormatter = NumberFormatter() + numberFormatter.numberStyle = .decimal + if let formattedNumber = numberFormatter.string(from: NSNumber(value: self)) { + return formattedNumber + } else { + return String(self) + } + } +} diff --git a/GoMoney/Scences/Home/View/RecentExpenseCell.swift b/GoMoney/Scences/Home/View/RecentExpenseCell.swift index 72c7dd5..9d7ca25 100644 --- a/GoMoney/Scences/Home/View/RecentExpenseCell.swift +++ b/GoMoney/Scences/Home/View/RecentExpenseCell.swift @@ -6,17 +6,14 @@ class RecentExpenseCell: UITableViewCell { var expense: Expense? { didSet { if let expense = expense { - icon.loadIcon(src: expense.tag?.icon) - labelName.text = expense.tag?.name - labelDate.text = expense.getDate(.occuredOn) - labelPrice.text = String(expense.amount) + bindView(transaction: expense) } } } lazy var icon = GMExpenseIcon() - lazy var labelName = GMLabel(text: "Gas", style: .regularBold) - lazy var labelDate = GMLabel(text: "20/10/2022", style: .small) + lazy var labelName = GMLabel(style: .regularBold) + lazy var labelDate = GMLabel(style: .small) lazy var labelPrice = GMLabel(style: .regularBold) lazy var stackInfo: UIStackView = { @@ -59,4 +56,20 @@ class RecentExpenseCell: UITableViewCell { labelPrice.centerYToView(self) labelPrice.anchor(top: topAnchor, right: rightAnchor, paddingRight: 4) } + + func bindView(transaction: Expense) { + icon.loadIcon(src: transaction.tag?.icon) + labelName.text = transaction.tag?.name + labelDate.text = transaction.getDate(.occuredOn) + let currency = SettingsManager.shared.getValue(for: .currencyUnit) + labelPrice.text = "\(transaction.amount.formatWithCommas()) \(currency)" + switch transaction.type { + case ExpenseType.income.rawValue: + labelPrice.textColor = K.Color.saving + case ExpenseType.expense.rawValue: + labelPrice.textColor = K.Color.debt + default: + break + } + } } From 76b95481ccbfa426316908ea674fd5f86947beaf Mon Sep 17 00:00:00 2001 From: PaulNguyen Date: Fri, 18 Nov 2022 16:07:40 +0700 Subject: [PATCH 2/3] format money chartview --- GoMoney/Extensions/Double+Extension.swift | 4 +++- GoMoney/Scences/Home/View/ChartView.swift | 16 ++++++++++--- .../Home/View/MonthlyExpenseView.swift | 24 ++++++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/GoMoney/Extensions/Double+Extension.swift b/GoMoney/Extensions/Double+Extension.swift index cf5bc10..8ee23fc 100644 --- a/GoMoney/Extensions/Double+Extension.swift +++ b/GoMoney/Extensions/Double+Extension.swift @@ -1,9 +1,11 @@ import Foundation extension Double { - func formatWithCommas() -> String { + func formatWithCommas(minFraction: Int = 0, maxFraction: Int = 0) -> String { let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .decimal + numberFormatter.minimumFractionDigits = minFraction + numberFormatter.maximumFractionDigits = maxFraction if let formattedNumber = numberFormatter.string(from: NSNumber(value: self)) { return formattedNumber } else { diff --git a/GoMoney/Scences/Home/View/ChartView.swift b/GoMoney/Scences/Home/View/ChartView.swift index 4edd915..8d292ae 100644 --- a/GoMoney/Scences/Home/View/ChartView.swift +++ b/GoMoney/Scences/Home/View/ChartView.swift @@ -88,9 +88,19 @@ class ChartView: UIView { { let savings = incomeSum - expenseSum - monthlyIncomes.amount.text = String(incomeSum) - monthlyExpenses.amount.text = String(expenseSum) - monthlySavings.amount.text = String(savings) + guard + let currency = SettingsManager.shared.getValue(for: .currencyUnit) as? String, + let unit = CurrencyUnit(rawValue: currency) + else { + return + } + + monthlyIncomes.amount.text = + "\(MoneyFormatter.formatShorter(amount: incomeSum, currency: unit)) \(currency)" + monthlyExpenses.amount.text = + "\(MoneyFormatter.formatShorter(amount: expenseSum, currency: unit)) \(currency)" + monthlySavings.amount.text = + "\(MoneyFormatter.formatShorter(amount: savings, currency: unit)) \(currency)" if savings >= 0 { monthlySavings.amount.textColor = K.Color.saving diff --git a/GoMoney/Scences/Home/View/MonthlyExpenseView.swift b/GoMoney/Scences/Home/View/MonthlyExpenseView.swift index 81dbfb0..9050e3d 100644 --- a/GoMoney/Scences/Home/View/MonthlyExpenseView.swift +++ b/GoMoney/Scences/Home/View/MonthlyExpenseView.swift @@ -1,8 +1,30 @@ import UIKit +class MoneyFormatter { + static func formatShorter(amount: Double, currency: CurrencyUnit) -> String { + switch currency { + case .dong: + switch amount { + case 1 ..< 1_000_000: + return amount.formatWithCommas() + default: + let million = amount / 1_000_000 + let formated = million.formatWithCommas(minFraction: 2, maxFraction: 2) + return "\(formated)tr" + } + + default: + return String(amount.formatWithCommas()) + } + } +} + class MonthlyExpenseView: UIView { lazy var label = GMLabel(style: .small) - lazy var amount = GMLabel(style: .largeBold) + lazy var amount = GMLabel(style: .largeBold) { + $0.textAlignment = .right + } + lazy var stackView: UIStackView = .build { [self] in $0.axis = .vertical $0.spacing = 8 From b7cd2c03809e4437d4309cd60d9da409d7c9abd5 Mon Sep 17 00:00:00 2001 From: PaulNguyen Date: Fri, 18 Nov 2022 16:13:59 +0700 Subject: [PATCH 3/3] seperate formatter to file --- GoMoney.xcodeproj/project.pbxproj | 12 ++++++++++++ GoMoney/Common/Currency/MoneyFormatter.swift | 18 ++++++++++++++++++ .../Home/View/MonthlyExpenseView.swift | 19 ------------------- 3 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 GoMoney/Common/Currency/MoneyFormatter.swift diff --git a/GoMoney.xcodeproj/project.pbxproj b/GoMoney.xcodeproj/project.pbxproj index 571559b..1e88f24 100644 --- a/GoMoney.xcodeproj/project.pbxproj +++ b/GoMoney.xcodeproj/project.pbxproj @@ -49,6 +49,7 @@ 086B4FB5291FFF01005C61F6 /* CountryData.plist in Resources */ = {isa = PBXBuildFile; fileRef = 086B4FB4291FFF01005C61F6 /* CountryData.plist */; }; 086C5D1A29150B2E00B6DE49 /* calculator.json in Resources */ = {isa = PBXBuildFile; fileRef = 086C5D1929150B2E00B6DE49 /* calculator.json */; }; 0875F3092927778000D122C3 /* Double+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0875F3082927778000D122C3 /* Double+Extension.swift */; }; + 0875F30D292783E200D122C3 /* MoneyFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0875F30C292783E200D122C3 /* MoneyFormatter.swift */; }; 0878DBA12924939D004E3FFD /* NewTagViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0878DBA02924939D004E3FFD /* NewTagViewController.swift */; }; 0878DBA42924AA15004E3FFD /* IconCollectionVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0878DBA32924AA15004E3FFD /* IconCollectionVC.swift */; }; 0878DBA62924AC43004E3FFD /* IconPickerCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0878DBA52924AC43004E3FFD /* IconPickerCell.swift */; }; @@ -212,6 +213,7 @@ 086B4FB4291FFF01005C61F6 /* CountryData.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = CountryData.plist; sourceTree = ""; }; 086C5D1929150B2E00B6DE49 /* calculator.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = calculator.json; sourceTree = ""; }; 0875F3082927778000D122C3 /* Double+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+Extension.swift"; sourceTree = ""; }; + 0875F30C292783E200D122C3 /* MoneyFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoneyFormatter.swift; sourceTree = ""; }; 0878DBA02924939D004E3FFD /* NewTagViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTagViewController.swift; sourceTree = ""; }; 0878DBA32924AA15004E3FFD /* IconCollectionVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IconCollectionVC.swift; sourceTree = ""; }; 0878DBA52924AC43004E3FFD /* IconPickerCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IconPickerCell.swift; sourceTree = ""; }; @@ -481,6 +483,14 @@ path = Cells; sourceTree = ""; }; + 0875F30B292783DB00D122C3 /* Currency */ = { + isa = PBXGroup; + children = ( + 0875F30C292783E200D122C3 /* MoneyFormatter.swift */, + ); + path = Currency; + sourceTree = ""; + }; 0878DB9D29248C54004E3FFD /* Tags */ = { isa = PBXGroup; children = ( @@ -778,6 +788,7 @@ 08C8730B28F7B1BD00DC859D /* Common */ = { isa = PBXGroup; children = ( + 0875F30B292783DB00D122C3 /* Currency */, 08F068A929221C79005C58EC /* AsyncImage */, 089CC74A2919D9580070A108 /* FileHelper */, 08DDEB402918A0FE00327474 /* RealmConverter */, @@ -1210,6 +1221,7 @@ 08C872CA28F6681600DC859D /* UIViewController+Extension.swift in Sources */, 08C872EA28F6C51200DC859D /* UserDefaultKey.swift in Sources */, 08C872A528F655CD00DC859D /* SceneDelegate.swift in Sources */, + 0875F30D292783E200D122C3 /* MoneyFormatter.swift in Sources */, 081DC8A2290274990005374F /* CategoryPickerInputView.swift in Sources */, 085F753129124DF50094A026 /* SettingsTableViewAccessoryCell.swift in Sources */, ); diff --git a/GoMoney/Common/Currency/MoneyFormatter.swift b/GoMoney/Common/Currency/MoneyFormatter.swift new file mode 100644 index 0000000..ea07936 --- /dev/null +++ b/GoMoney/Common/Currency/MoneyFormatter.swift @@ -0,0 +1,18 @@ +class MoneyFormatter { + static func formatShorter(amount: Double, currency: CurrencyUnit) -> String { + switch currency { + case .dong: + switch amount { + case 1 ..< 1_000_000: + return amount.formatWithCommas() + default: + let million = amount / 1_000_000 + let formated = million.formatWithCommas(minFraction: 2, maxFraction: 2) + return "\(formated)tr" + } + + default: + return String(amount.formatWithCommas()) + } + } +} diff --git a/GoMoney/Scences/Home/View/MonthlyExpenseView.swift b/GoMoney/Scences/Home/View/MonthlyExpenseView.swift index 9050e3d..f426ff9 100644 --- a/GoMoney/Scences/Home/View/MonthlyExpenseView.swift +++ b/GoMoney/Scences/Home/View/MonthlyExpenseView.swift @@ -1,24 +1,5 @@ import UIKit -class MoneyFormatter { - static func formatShorter(amount: Double, currency: CurrencyUnit) -> String { - switch currency { - case .dong: - switch amount { - case 1 ..< 1_000_000: - return amount.formatWithCommas() - default: - let million = amount / 1_000_000 - let formated = million.formatWithCommas(minFraction: 2, maxFraction: 2) - return "\(formated)tr" - } - - default: - return String(amount.formatWithCommas()) - } - } -} - class MonthlyExpenseView: UIView { lazy var label = GMLabel(style: .small) lazy var amount = GMLabel(style: .largeBold) {