Skip to content

Commit

Permalink
move convenience inits to dydxViews, clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Feb 14, 2024
1 parent c15ce58 commit a045c30
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 46 deletions.
30 changes: 0 additions & 30 deletions PlatformUI/PlatformUI/Components/Labels/SignedAmount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import SwiftUI
import dydxFormatter

public class SignedAmountViewModel: PlatformViewModel, Hashable {
public enum ColoringOption {
Expand Down Expand Up @@ -35,35 +34,6 @@ public class SignedAmountViewModel: PlatformViewModel, Hashable {
self.negativeTextStyleKey = negativeTextStyleKey
}

public convenience init(amount: Double?, displayType: DisplayType, coloringOption: ColoringOption, positiveTextStyleKey: String, negativeTextStyleKey: String, shouldDisplaySignForPositiveNumbers: Bool = false) {
let formattedZero: String
let formattedText: String
let sign: PlatformUISign
switch displayType {
case .dollar:
formattedText = dydxFormatter.shared.dollarVolume(number: amount, shouldDisplaySignForPositiveNumbers: shouldDisplaySignForPositiveNumbers) ?? ""
formattedZero = dydxFormatter.shared.dollarVolume(number: 0.0, shouldDisplaySignForPositiveNumbers: shouldDisplaySignForPositiveNumbers) ?? ""
case .percent:
let digits = 2
formattedText = dydxFormatter.shared.percent(number: amount, digits: digits, shouldDisplayPlusSignForPositiveNumbers: shouldDisplaySignForPositiveNumbers) ?? ""
formattedZero = dydxFormatter.shared.percent(number: 0.0, digits: digits, shouldDisplayPlusSignForPositiveNumbers: shouldDisplaySignForPositiveNumbers) ?? ""
}
// special logic for when a value like -0.001 would render as "-$0.00" instead of "$0.00"
if formattedText == formattedZero {
sign = .none
} else if (amount ?? 0) > 0 {
sign = .plus
} else {
sign = .minus
}
self.init(text: formattedText,
sign: sign,
coloringOption: coloringOption,
positiveTextStyleKey: positiveTextStyleKey,
negativeTextStyleKey: negativeTextStyleKey)
}



public static func == (lhs: SignedAmountViewModel, rhs: SignedAmountViewModel) -> Bool {
lhs.text == rhs.text &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class dydxPortfolioPositionsViewPresenter: HostedViewPresenter<dydxPortfolioPosi
item.entryPrice = dydxFormatter.shared.dollar(number: position.entryPrice?.current, digits: configs.displayTickSizeDecimals?.intValue ?? 0)

item.unrealizedPnl = SignedAmountViewModel(amount: position.unrealizedPnl?.current?.doubleValue ?? 0, displayType: .dollar, coloringOption: .allText)
item.unrealizedPnlPercent = dydxFormatter.shared.percent(number: abs(position.unrealizedPnlPercent?.current?.doubleValue ?? 0), digits: 2)
item.unrealizedPnlPercent = dydxFormatter.shared.percent(number: position.unrealizedPnlPercent?.current?.doubleValue ?? 0, digits: 2)

if let url = asset.resources?.imageUrl {
item.logoUrl = URL(string: url)
Expand Down
4 changes: 4 additions & 0 deletions dydx/dydxViews/dydxViews.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
270BA8CF2A6F1470009212EA /* dydxDebugThemeViewerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 270BA8CE2A6F1470009212EA /* dydxDebugThemeViewerView.swift */; };
270E7E342A5F700B00136793 /* dydxOrderbookManagerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 270E7E332A5F700B00136793 /* dydxOrderbookManagerView.swift */; };
272030182A7812B900D233B9 /* UINavigationController+SwipeBackNavigation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 272030172A7812B900D233B9 /* UINavigationController+SwipeBackNavigation.swift */; };
273F50162B7C3F120034792A /* SignedAmountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 273F50152B7C3F120034792A /* SignedAmountView.swift */; };
2769090E2AAFD8030075B2D6 /* TransferInstanceViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2769090D2AAFD8030075B2D6 /* TransferInstanceViewModel.swift */; };
276909102AAFD8BE0075B2D6 /* dydxPortfolioTransfersViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2769090F2AAFD8BE0075B2D6 /* dydxPortfolioTransfersViewModel.swift */; };
277442972AD88C4900C91357 /* Satoshi-Bold.otf in Resources */ = {isa = PBXBuildFile; fileRef = 277442952AD88C4900C91357 /* Satoshi-Bold.otf */; };
Expand Down Expand Up @@ -498,6 +499,7 @@
270BA8CE2A6F1470009212EA /* dydxDebugThemeViewerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = dydxDebugThemeViewerView.swift; sourceTree = "<group>"; };
270E7E332A5F700B00136793 /* dydxOrderbookManagerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = dydxOrderbookManagerView.swift; sourceTree = "<group>"; };
272030172A7812B900D233B9 /* UINavigationController+SwipeBackNavigation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UINavigationController+SwipeBackNavigation.swift"; sourceTree = "<group>"; };
273F50152B7C3F120034792A /* SignedAmountView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignedAmountView.swift; sourceTree = "<group>"; };
2769090D2AAFD8030075B2D6 /* TransferInstanceViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransferInstanceViewModel.swift; sourceTree = "<group>"; };
2769090F2AAFD8BE0075B2D6 /* dydxPortfolioTransfersViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = dydxPortfolioTransfersViewModel.swift; sourceTree = "<group>"; };
277442952AD88C4900C91357 /* Satoshi-Bold.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Satoshi-Bold.otf"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -848,6 +850,7 @@
02031F022AC36CE60069E00D /* BuySellButton.swift */,
024FEB7D2ACB82A10087A55E /* NavHeader.swift */,
0273A15E2ACCDDB1001B89F5 /* SelectionBar.swift */,
273F50152B7C3F120034792A /* SignedAmountView.swift */,
);
path = Shared;
sourceTree = "<group>";
Expand Down Expand Up @@ -1945,6 +1948,7 @@
0273A15F2ACCDDB1001B89F5 /* SelectionBar.swift in Sources */,
02031F032AC36CE60069E00D /* BuySellButton.swift in Sources */,
02A5C858297FBCBE00FFE1F9 /* dydxTransferView.swift in Sources */,
273F50162B7C3F120034792A /* SignedAmountView.swift in Sources */,
0277A22528BD7B14005A51F8 /* WalletConnectionView.swift in Sources */,
025D22D428F6528F00C4ADAE /* dydxMarketStatsView.swift in Sources */,
02195E1F2A20229700F73C17 /* dydxMarketOrderbookView.swift in Sources */,
Expand Down
45 changes: 45 additions & 0 deletions dydx/dydxViews/dydxViews/Shared/SignedAmountView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// SignedAmountView.swift
// dydxViews
//
// Created by Michael Maguire on 2/13/24.
//

import PlatformUI
import dydxFormatter

public extension SignedAmountViewModel {
convenience init(text: String? = nil, sign: PlatformUISign = .plus, coloringOption: ColoringOption) {
self.init(text: text,
sign: sign,
coloringOption: coloringOption,
positiveTextStyleKey: ThemeSettings.positiveTextStyleKey,
negativeTextStyleKey: ThemeSettings.negativeTextStyleKey)
}

convenience init(amount: Double?, displayType: DisplayType, coloringOption: ColoringOption, shouldDisplaySignForPositiveNumbers: Bool = false) {
let formattedZero: String?
let formattedText: String?
let sign: PlatformUISign
switch displayType {
case .dollar:
formattedText = dydxFormatter.shared.dollarVolume(number: amount?.magnitude, shouldDisplaySignForPositiveNumbers: shouldDisplaySignForPositiveNumbers)
formattedZero = dydxFormatter.shared.dollarVolume(number: 0.0, shouldDisplaySignForPositiveNumbers: shouldDisplaySignForPositiveNumbers)
case .percent:
let digits = 2
formattedText = dydxFormatter.shared.percent(number: amount?.magnitude, digits: digits, shouldDisplayPlusSignForPositiveNumbers: shouldDisplaySignForPositiveNumbers)
formattedZero = dydxFormatter.shared.percent(number: 0.0, digits: digits, shouldDisplayPlusSignForPositiveNumbers: shouldDisplaySignForPositiveNumbers)
}
// special logic for when a value like -0.001 would render as "-$0.00" instead of "$0.00"
if formattedText == formattedZero {
sign = .none
} else if (amount ?? 0) > 0 {
sign = .plus
} else {
sign = .minus
}
self.init(text: formattedText,
sign: sign,
coloringOption: coloringOption)
}
}
15 changes: 0 additions & 15 deletions dydx/dydxViews/dydxViews/Themes/dydxThemes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,6 @@ struct GradientTypeModifier: ViewModifier {
}
}

public extension SignedAmountViewModel {
convenience init(text: String? = nil, sign: PlatformUISign = .plus, coloringOption: ColoringOption) {
self.init(text: text, sign: sign, coloringOption: coloringOption, positiveTextStyleKey: ThemeSettings.positiveTextStyleKey, negativeTextStyleKey: ThemeSettings.negativeTextStyleKey)
}

convenience init(amount: Double?, displayType: DisplayType, coloringOption: ColoringOption, shouldDisplaySignForPositiveNumbers: Bool = false) {
self.init(amount: amount,
displayType: displayType,
coloringOption: coloringOption,
positiveTextStyleKey: ThemeSettings.positiveTextStyleKey,
negativeTextStyleKey: ThemeSettings.negativeTextStyleKey,
shouldDisplaySignForPositiveNumbers: shouldDisplaySignForPositiveNumbers)
}
}

private extension UIWindow {

/// Unload all views and add back.
Expand Down

0 comments on commit a045c30

Please sign in to comment.