Skip to content

Commit

Permalink
Added currency identifier for more flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
adri567 committed Feb 5, 2023
1 parent 23d3843 commit 3a15db1
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
7 changes: 4 additions & 3 deletions Sources/CheesyChart/Extensions/Double+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ extension Double {
/// ```
/// Convert 1234.56 to $1,234.56
/// ```
private var currencyFormatter: NumberFormatter {
private func currencyFormatter(currencyIdentifier: String) -> NumberFormatter {
let formatter = NumberFormatter()
formatter.usesGroupingSeparator = true
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: currencyIdentifier)
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
return formatter
Expand All @@ -27,9 +28,9 @@ extension Double {
/// ```
/// Convert 1234.56 to "$1,234.56"
/// ```
func asCurrencyWithTwoDecimals() -> String {
func asCurrencyWithTwoDecimals(currencyIdentifier: String) -> String {
let number = NSNumber(value: self)
return currencyFormatter.string(from: number) ?? "$0.00"
return currencyFormatter(currencyIdentifier: currencyIdentifier).string(from: number) ?? "$0.00"
}

/// Converts a Double into string representation
Expand Down
2 changes: 1 addition & 1 deletion Sources/CheesyChart/HelperViews/ChartPriceLabelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public struct ChartPriceLabelView: View {
/// Only nesessary if we are using a custom view with a binding to track the draged label price. This is important if the user creates multiple buttons to switch different chart data. It could happen that some data has more chart data. If so the app crashes. To avoid it we are checking if the vm.point is higher that the chart data count. If yes, we asign a blank String otherwise the normal price.
/// - Returns: Formatted price as a String
private func checkInput() -> String {
return vm.point > setup.data.count ? "" : setup.data[vm.point].asCurrencyWithTwoDecimals()
return vm.point > setup.data.count ? "" : setup.data[vm.point].asCurrencyWithTwoDecimals(currencyIdentifier: setup.currencyIdentifier)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct PriceLabelView: View {

// MARK: - Body
var body: some View {
Text(setup.data[vm.point].asCurrencyWithTwoDecimals())
Text(setup.data[vm.point].asCurrencyWithTwoDecimals(currencyIdentifier: setup.currencyIdentifier))
.foregroundColor(setup.chartHeaderFontColor)
.font(.footnote)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/CheesyChart/Main/CheesyChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public struct CheesyChart: View {
}

private func handleGesture(value: DragGesture.Value) {
print(value.location.x)
vm.hide = true
vm.touchLocation = value.location
/// Calculates each width of one price
Expand Down
3 changes: 3 additions & 0 deletions Sources/CheesyChart/Setup/SetupChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SetupChart {
// MARK: - Properties
public var name: String
public var data: [Double]
public var currencyIdentifier: String
public var image: UIImage?

public var showChartHeader: Bool
Expand Down Expand Up @@ -56,6 +57,7 @@ public class SetupChart {
public init(
name: String = "",
data: [Double] = [],
currencyIdentifier: String = "en_US",
image: UIImage? = UIImage(systemName: "bitcoinsign.circle.fill"),

showChartHeader: Bool = false,
Expand Down Expand Up @@ -95,6 +97,7 @@ public class SetupChart {
) {
self.name = name
self.data = data
self.currencyIdentifier = currencyIdentifier
self.image = image

self.showChartHeader = showChartHeader
Expand Down

0 comments on commit 3a15db1

Please sign in to comment.