From c4b73a72447cacda2fa6457f8348ae2518b88666 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 24 Jul 2024 18:05:33 -0400 Subject: [PATCH] address PR comments --- .../dydxNumberInputFormatter.swift | 11 ++++++---- .../dydxTakeProfitStopLossViewPresenter.swift | 2 +- .../dydxTargetLeverageViewBuilder.swift | 1 + .../dydxComponents/dydxSliderInputView.swift | 17 ++++++---------- .../dydxComponents/dydxSliderView.swift | 6 +++--- .../dydxTitledNumberField.swift | 20 +++++-------------- .../dydxCustomAmountViewModel.swift | 6 ++---- .../Trade/Margin/dydxTargetLeverageView.swift | 3 +-- 8 files changed, 26 insertions(+), 40 deletions(-) diff --git a/dydx/dydxFormatter/dydxFormatter/dydxNumberInputFormatter.swift b/dydx/dydxFormatter/dydxFormatter/dydxNumberInputFormatter.swift index 779701601..676785470 100644 --- a/dydx/dydxFormatter/dydxFormatter/dydxNumberInputFormatter.swift +++ b/dydx/dydxFormatter/dydxFormatter/dydxNumberInputFormatter.swift @@ -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() + } } } diff --git a/dydx/dydxPresenters/dydxPresenters/_v4/TakeProfitStopLoss/dydxTakeProfitStopLossViewPresenter.swift b/dydx/dydxPresenters/dydxPresenters/_v4/TakeProfitStopLoss/dydxTakeProfitStopLossViewPresenter.swift index b58de3423..9895e314f 100644 --- a/dydx/dydxPresenters/dydxPresenters/_v4/TakeProfitStopLoss/dydxTakeProfitStopLossViewPresenter.swift +++ b/dydx/dydxPresenters/dydxPresenters/_v4/TakeProfitStopLoss/dydxTakeProfitStopLossViewPresenter.swift @@ -128,7 +128,7 @@ private class dydxTakeProfitStopLossViewPresenter: HostedViewPresenter 0 { - viewModel?.customAmountViewModel?.sliderTextInput.precision = Int(-log10(stepSize)) + viewModel?.customAmountViewModel?.sliderTextInput.numberFormatter.fractionDigits = Int(-log10(stepSize)) } } diff --git a/dydx/dydxPresenters/dydxPresenters/_v4/Trade/Margin/dydxTargetLeverageViewBuilder.swift b/dydx/dydxPresenters/dydxPresenters/_v4/Trade/Margin/dydxTargetLeverageViewBuilder.swift index 3574e4d1a..2d4eee99e 100644 --- a/dydx/dydxPresenters/dydxPresenters/_v4/Trade/Margin/dydxTargetLeverageViewBuilder.swift +++ b/dydx/dydxPresenters/dydxPresenters/_v4/Trade/Margin/dydxTargetLeverageViewBuilder.swift @@ -63,6 +63,7 @@ private class dydxTargetLeverageViewPresenter: HostedViewPresenter PlatformView { @@ -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) diff --git a/dydx/dydxViews/dydxViews/Shared/dydxComponents/dydxSliderView.swift b/dydx/dydxViews/dydxViews/Shared/dydxComponents/dydxSliderView.swift index 91d4445d5..9f88d7b39 100644 --- a/dydx/dydxViews/dydxViews/Shared/dydxComponents/dydxSliderView.swift +++ b/dydx/dydxViews/dydxViews/Shared/dydxComponents/dydxSliderView.swift @@ -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 diff --git a/dydx/dydxViews/dydxViews/Shared/dydxComponents/dydxTitledNumberField.swift b/dydx/dydxViews/dydxViews/Shared/dydxComponents/dydxTitledNumberField.swift index d92427aa4..9398b9b78 100644 --- a/dydx/dydxViews/dydxViews/Shared/dydxComponents/dydxTitledNumberField.swift +++ b/dydx/dydxViews/dydxViews/Shared/dydxComponents/dydxTitledNumberField.swift @@ -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? @@ -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) @@ -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 @@ -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 } @@ -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 - } } diff --git a/dydx/dydxViews/dydxViews/_v4/TakeProfitStopLoss/Components/dydxCustomAmountViewModel.swift b/dydx/dydxViews/dydxViews/_v4/TakeProfitStopLoss/Components/dydxCustomAmountViewModel.swift index 763719660..825da84e1 100644 --- a/dydx/dydxViews/dydxViews/_v4/TakeProfitStopLoss/Components/dydxCustomAmountViewModel.swift +++ b/dydx/dydxViews/dydxViews/_v4/TakeProfitStopLoss/Components/dydxCustomAmountViewModel.swift @@ -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 { @@ -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 }() diff --git a/dydx/dydxViews/dydxViews/_v4/Trade/Margin/dydxTargetLeverageView.swift b/dydx/dydxViews/dydxViews/_v4/Trade/Margin/dydxTargetLeverageView.swift index 34c5fe217..643d65e7e 100644 --- a/dydx/dydxViews/dydxViews/_v4/Trade/Margin/dydxTargetLeverageView.swift +++ b/dydx/dydxViews/dydxViews/_v4/Trade/Margin/dydxTargetLeverageView.swift @@ -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()