Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Jul 24, 2024
1 parent dc5d5dd commit c4b73a7
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 40 deletions.
11 changes: 7 additions & 4 deletions dydx/dydxFormatter/dydxFormatter/dydxNumberInputFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import Foundation

/// a number formatter that also supports rounding to nearest 10/100/1000/etc
/// formatter is intended for user inputs, so group separator is omitted, i.e. the "," in "1,000"
public class dydxNumberInputFormatter: NumberFormatter {
public class dydxNumberInputFormatter: NumberFormatter, ObservableObject {

/// if greater than 0, numbers will be rounded to nearest 10, 100, 1000, etc. If less than 0 numbers will be rounded to nearest 0.1, 0.01, .001
var fractionDigits: Int {
public var fractionDigits: Int {
get {
maximumFractionDigits
}
set {
maximumFractionDigits = newValue
minimumFractionDigits = newValue
if maximumFractionDigits != newValue || minimumFractionDigits != newValue {
maximumFractionDigits = newValue
minimumFractionDigits = newValue
objectWillChange.send()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private class dydxTakeProfitStopLossViewPresenter: HostedViewPresenter<dydxTakeP
viewModel?.customAmountViewModel?.sliderTextInput.minValue = market?.configs?.minOrderSize?.doubleValue.magnitude ?? 0
// abacus stepSizeDecimals is not accurate for 10/100/1000 precision
if let stepSize = market?.configs?.stepSize?.doubleValue, stepSize > 0 {
viewModel?.customAmountViewModel?.sliderTextInput.precision = Int(-log10(stepSize))
viewModel?.customAmountViewModel?.sliderTextInput.numberFormatter.fractionDigits = Int(-log10(stepSize))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private class dydxTargetLeverageViewPresenter: HostedViewPresenter<dydxTargetLev
Router.shared?.navigate(to: RoutingRequest(path: "/action/dismiss"), animated: true, completion: nil)
}

self.viewModel?.sliderTextInput.numberFormatter.fractionDigits = 2
self.viewModel?.sliderTextInput.$value
.removeDuplicates()
.sink(receiveValue: { [weak self] value in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,18 @@ public class dydxSliderInputViewModel: PlatformViewModel {
@Published public var accessoryTitle: String?
@Published public var minValue: Double = 0
@Published public var maxValue: Double = 0
/// number of decimals after the decimal place to display
@Published public var precision: Int = 0
@Published public private(set) var valueAsString: String = ""
@Published public var value: Double? {
didSet {
valueAsString = value.map { numberFormatter.string(from: $0 as NSNumber) ?? "" } ?? ""
}
}

var numberFormatter: dydxNumberInputFormatter {
dydxNumberInputFormatter(fractionDigits: precision)
}
@Published public private(set) var numberFormatter = dydxNumberInputFormatter()

init(title: String?, accessoryTitle: String? = nil, precision: Int) {
init(title: String?, accessoryTitle: String? = nil) {
self.title = title
self.accessoryTitle = accessoryTitle
self.precision = precision
}

public override func createView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> PlatformView {
Expand All @@ -47,16 +42,16 @@ private struct dydxSliderTextInput: View {
@ObservedObject var viewModel: dydxSliderInputViewModel

var slider: some View {
dydxSlider(minValue: $viewModel.minValue,
maxValue: $viewModel.maxValue,
precision: $viewModel.precision,
dydxSlider(minValue: viewModel.minValue,
maxValue: viewModel.maxValue,
precision: viewModel.numberFormatter.fractionDigits,
value: $viewModel.value)
}

var textInput: some View {
dydxTitledNumberField(title: viewModel.title,
accessoryTitle: viewModel.accessoryTitle,
precision: viewModel.precision,
numberFormatter: viewModel.numberFormatter,
minValue: viewModel.minValue,
maxValue: viewModel.maxValue,
value: $viewModel.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import SwiftUI
import PlatformUI

struct dydxSlider: View {
@Binding var minValue: Double
@Binding var maxValue: Double
@Binding var precision: Int
var minValue: Double
var maxValue: Double
var precision: Int
@Binding var value: Double?

private let thumbRadius: CGFloat = 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import PlatformUI
struct dydxTitledNumberField: View {
let title: String?
let accessoryTitle: String?
let precision: Int
let numberFormatter: dydxNumberInputFormatter
let minValue: Double
let maxValue: Double
@Binding var value: Double?
Expand Down Expand Up @@ -54,7 +54,7 @@ struct dydxTitledNumberField: View {
actualValue: $value,
minValue: minValue,
maxValue: maxValue,
precision: precision)
numberFormatter: numberFormatter)
.themeColor(foreground: .textPrimary)
.themeFont(fontType: .base, fontSize: .medium)
.truncationMode(.middle)
Expand Down Expand Up @@ -82,14 +82,10 @@ private struct NumberTextField: View {

let minValue: Double
let maxValue: Double
let precision: Int

private var numberFormatter: dydxNumberInputFormatter {
dydxNumberInputFormatter(fractionDigits: precision)
}
let numberFormatter: dydxNumberInputFormatter

private var keyboardType: UIKeyboardType {
precision > 0 ? .decimalPad : .numberPad
numberFormatter.fractionDigits > 0 ? .decimalPad : .numberPad
}

@ViewBuilder
Expand Down Expand Up @@ -117,7 +113,7 @@ private struct NumberTextField: View {
guard let value = value else {
return nil
}
let multiplier = pow(10.0, Double(precision))
let multiplier = pow(10.0, Double(numberFormatter.fractionDigits))
let formattedValue = (value * multiplier).rounded() / multiplier
return formattedValue
}
Expand All @@ -144,10 +140,4 @@ private struct NumberTextField: View {
private func clamp(_ value: Double) -> Double {
min(max(value, minValue), maxValue)
}

private func formatNumber(_ value: Double) -> Double {
let multiplier = pow(10.0, Double(precision))
let formattedValue = (value * multiplier).rounded() / multiplier
return formattedValue
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public class dydxCustomAmountViewModel: PlatformViewModel {
}

@Published public var sliderTextInput = dydxSliderInputViewModel(
title: DataLocalizer.localize(path: "APP.GENERAL.AMOUNT"),
precision: 2
title: DataLocalizer.localize(path: "APP.GENERAL.AMOUNT")
)

private var onOffSwitch: some View {
Expand All @@ -44,8 +43,7 @@ public class dydxCustomAmountViewModel: PlatformViewModel {
let vm = dydxCustomAmountViewModel()
vm.isOn = true
vm.sliderTextInput = dydxSliderInputViewModel(title: DataLocalizer.shared?.localize(path: "APP.GENERAL.AMOUNT", params: nil) ?? "",
accessoryTitle: "ETH",
precision: 2)
accessoryTitle: "ETH")
return vm
}()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public class dydxTargetLeverageViewModel: PlatformViewModel {
@Published public var selectedOptionIndex: Int?
@Published public var optionSelectedAction: ((LeverageTextAndValue) -> Void)?
@Published public var sliderTextInput = dydxSliderInputViewModel(
title: DataLocalizer.localize(path: "APP.TRADE.TARGET_LEVERAGE"),
precision: 2
title: DataLocalizer.localize(path: "APP.TRADE.TARGET_LEVERAGE")
)
@Published public var ctaButton: dydxTargetLeverageCtaButtonViewModel? = dydxTargetLeverageCtaButtonViewModel()

Expand Down

0 comments on commit c4b73a7

Please sign in to comment.