Skip to content

Commit 8c13a71

Browse files
update: format amount with commas (#57)
* format amount with commas * format money chartview * seperate formatter to file
1 parent 731736b commit 8c13a71

File tree

6 files changed

+85
-10
lines changed

6 files changed

+85
-10
lines changed

GoMoney.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
085F7539291278100094A026 /* ConnectionService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 085F7538291278100094A026 /* ConnectionService.swift */; };
4949
086B4FB5291FFF01005C61F6 /* CountryData.plist in Resources */ = {isa = PBXBuildFile; fileRef = 086B4FB4291FFF01005C61F6 /* CountryData.plist */; };
5050
086C5D1A29150B2E00B6DE49 /* calculator.json in Resources */ = {isa = PBXBuildFile; fileRef = 086C5D1929150B2E00B6DE49 /* calculator.json */; };
51+
0875F3092927778000D122C3 /* Double+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0875F3082927778000D122C3 /* Double+Extension.swift */; };
52+
0875F30D292783E200D122C3 /* MoneyFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0875F30C292783E200D122C3 /* MoneyFormatter.swift */; };
5153
0878DBA12924939D004E3FFD /* NewTagViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0878DBA02924939D004E3FFD /* NewTagViewController.swift */; };
5254
0878DBA42924AA15004E3FFD /* IconCollectionVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0878DBA32924AA15004E3FFD /* IconCollectionVC.swift */; };
5355
0878DBA62924AC43004E3FFD /* IconPickerCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0878DBA52924AC43004E3FFD /* IconPickerCell.swift */; };
@@ -210,6 +212,8 @@
210212
085F7538291278100094A026 /* ConnectionService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionService.swift; sourceTree = "<group>"; };
211213
086B4FB4291FFF01005C61F6 /* CountryData.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = CountryData.plist; sourceTree = "<group>"; };
212214
086C5D1929150B2E00B6DE49 /* calculator.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = calculator.json; sourceTree = "<group>"; };
215+
0875F3082927778000D122C3 /* Double+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+Extension.swift"; sourceTree = "<group>"; };
216+
0875F30C292783E200D122C3 /* MoneyFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoneyFormatter.swift; sourceTree = "<group>"; };
213217
0878DBA02924939D004E3FFD /* NewTagViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTagViewController.swift; sourceTree = "<group>"; };
214218
0878DBA32924AA15004E3FFD /* IconCollectionVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IconCollectionVC.swift; sourceTree = "<group>"; };
215219
0878DBA52924AC43004E3FFD /* IconPickerCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IconPickerCell.swift; sourceTree = "<group>"; };
@@ -479,6 +483,14 @@
479483
path = Cells;
480484
sourceTree = "<group>";
481485
};
486+
0875F30B292783DB00D122C3 /* Currency */ = {
487+
isa = PBXGroup;
488+
children = (
489+
0875F30C292783E200D122C3 /* MoneyFormatter.swift */,
490+
);
491+
path = Currency;
492+
sourceTree = "<group>";
493+
};
482494
0878DB9D29248C54004E3FFD /* Tags */ = {
483495
isa = PBXGroup;
484496
children = (
@@ -726,6 +738,7 @@
726738
08C872EF28F6CAAB00DC859D /* UIView+Constraint.swift */,
727739
08DB56E828F9035800170C60 /* StackView+Extension.swift */,
728740
08DB56EA28F9053700170C60 /* UIColor+Extension.swift */,
741+
0875F3082927778000D122C3 /* Double+Extension.swift */,
729742
08C1F25C28FE93E900590512 /* DateFormatter+Extension.swift */,
730743
08C1F26928FFBAD500590512 /* UIImage+Extension.swift */,
731744
08DEE9C52900FC4C00EA9A2A /* Date+Extension.swift */,
@@ -775,6 +788,7 @@
775788
08C8730B28F7B1BD00DC859D /* Common */ = {
776789
isa = PBXGroup;
777790
children = (
791+
0875F30B292783DB00D122C3 /* Currency */,
778792
08F068A929221C79005C58EC /* AsyncImage */,
779793
089CC74A2919D9580070A108 /* FileHelper */,
780794
08DDEB402918A0FE00327474 /* RealmConverter */,
@@ -1148,6 +1162,7 @@
11481162
089CC756291A0C0A0070A108 /* BiometricService.swift in Sources */,
11491163
08B48B7C290293AE00344650 /* UIBarButtonItem+Extension.swift in Sources */,
11501164
08C8731928F865CA00DC859D /* SignUpPasswordVC.swift in Sources */,
1165+
0875F3092927778000D122C3 /* Double+Extension.swift in Sources */,
11511166
08EEA00E291CC374003B35B8 /* Widget+Kind.swift in Sources */,
11521167
082BF97D28FD68480094FE94 /* Notification+Name.swift in Sources */,
11531168
083EA336290659660079605F /* TransactionTag.swift in Sources */,
@@ -1206,6 +1221,7 @@
12061221
08C872CA28F6681600DC859D /* UIViewController+Extension.swift in Sources */,
12071222
08C872EA28F6C51200DC859D /* UserDefaultKey.swift in Sources */,
12081223
08C872A528F655CD00DC859D /* SceneDelegate.swift in Sources */,
1224+
0875F30D292783E200D122C3 /* MoneyFormatter.swift in Sources */,
12091225
081DC8A2290274990005374F /* CategoryPickerInputView.swift in Sources */,
12101226
085F753129124DF50094A026 /* SettingsTableViewAccessoryCell.swift in Sources */,
12111227
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class MoneyFormatter {
2+
static func formatShorter(amount: Double, currency: CurrencyUnit) -> String {
3+
switch currency {
4+
case .dong:
5+
switch amount {
6+
case 1 ..< 1_000_000:
7+
return amount.formatWithCommas()
8+
default:
9+
let million = amount / 1_000_000
10+
let formated = million.formatWithCommas(minFraction: 2, maxFraction: 2)
11+
return "\(formated)tr"
12+
}
13+
14+
default:
15+
return String(amount.formatWithCommas())
16+
}
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Foundation
2+
3+
extension Double {
4+
func formatWithCommas(minFraction: Int = 0, maxFraction: Int = 0) -> String {
5+
let numberFormatter = NumberFormatter()
6+
numberFormatter.numberStyle = .decimal
7+
numberFormatter.minimumFractionDigits = minFraction
8+
numberFormatter.maximumFractionDigits = maxFraction
9+
if let formattedNumber = numberFormatter.string(from: NSNumber(value: self)) {
10+
return formattedNumber
11+
} else {
12+
return String(self)
13+
}
14+
}
15+
}

GoMoney/Scences/Home/View/ChartView.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,19 @@ class ChartView: UIView {
8888
{
8989
let savings = incomeSum - expenseSum
9090

91-
monthlyIncomes.amount.text = String(incomeSum)
92-
monthlyExpenses.amount.text = String(expenseSum)
93-
monthlySavings.amount.text = String(savings)
91+
guard
92+
let currency = SettingsManager.shared.getValue(for: .currencyUnit) as? String,
93+
let unit = CurrencyUnit(rawValue: currency)
94+
else {
95+
return
96+
}
97+
98+
monthlyIncomes.amount.text =
99+
"\(MoneyFormatter.formatShorter(amount: incomeSum, currency: unit)) \(currency)"
100+
monthlyExpenses.amount.text =
101+
"\(MoneyFormatter.formatShorter(amount: expenseSum, currency: unit)) \(currency)"
102+
monthlySavings.amount.text =
103+
"\(MoneyFormatter.formatShorter(amount: savings, currency: unit)) \(currency)"
94104

95105
if savings >= 0 {
96106
monthlySavings.amount.textColor = K.Color.saving

GoMoney/Scences/Home/View/MonthlyExpenseView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import UIKit
22

33
class MonthlyExpenseView: UIView {
44
lazy var label = GMLabel(style: .small)
5-
lazy var amount = GMLabel(style: .largeBold)
5+
lazy var amount = GMLabel(style: .largeBold) {
6+
$0.textAlignment = .right
7+
}
8+
69
lazy var stackView: UIStackView = .build { [self] in
710
$0.axis = .vertical
811
$0.spacing = 8

GoMoney/Scences/Home/View/RecentExpenseCell.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ class RecentExpenseCell: UITableViewCell {
66
var expense: Expense? {
77
didSet {
88
if let expense = expense {
9-
icon.loadIcon(src: expense.tag?.icon)
10-
labelName.text = expense.tag?.name
11-
labelDate.text = expense.getDate(.occuredOn)
12-
labelPrice.text = String(expense.amount)
9+
bindView(transaction: expense)
1310
}
1411
}
1512
}
1613

1714
lazy var icon = GMExpenseIcon()
18-
lazy var labelName = GMLabel(text: "Gas", style: .regularBold)
19-
lazy var labelDate = GMLabel(text: "20/10/2022", style: .small)
15+
lazy var labelName = GMLabel(style: .regularBold)
16+
lazy var labelDate = GMLabel(style: .small)
2017
lazy var labelPrice = GMLabel(style: .regularBold)
2118

2219
lazy var stackInfo: UIStackView = {
@@ -59,4 +56,20 @@ class RecentExpenseCell: UITableViewCell {
5956
labelPrice.centerYToView(self)
6057
labelPrice.anchor(top: topAnchor, right: rightAnchor, paddingRight: 4)
6158
}
59+
60+
func bindView(transaction: Expense) {
61+
icon.loadIcon(src: transaction.tag?.icon)
62+
labelName.text = transaction.tag?.name
63+
labelDate.text = transaction.getDate(.occuredOn)
64+
let currency = SettingsManager.shared.getValue(for: .currencyUnit)
65+
labelPrice.text = "\(transaction.amount.formatWithCommas()) \(currency)"
66+
switch transaction.type {
67+
case ExpenseType.income.rawValue:
68+
labelPrice.textColor = K.Color.saving
69+
case ExpenseType.expense.rawValue:
70+
labelPrice.textColor = K.Color.debt
71+
default:
72+
break
73+
}
74+
}
6275
}

0 commit comments

Comments
 (0)