Skip to content

Commit

Permalink
fix platform list footer display logic (#174)
Browse files Browse the repository at this point in the history
* fix platform list footer display logic

* fix platform list footer display logic

* display trigger price for trigger market orders
  • Loading branch information
mike-dydx committed Aug 21, 2024
1 parent 089b0cf commit 2f29178
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 100 deletions.
48 changes: 11 additions & 37 deletions PlatformUI/PlatformUI/PlatformListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,6 @@ open class PlatformListViewModel: PlatformViewModeling {
contentChanged?()
}
}
public var header: PlatformViewModel? {
didSet {
contentChanged?()
}
}

public var footer: PlatformViewModel? {
didSet {
contentChanged?()
}
}

public var placeholder: PlatformViewModel? {
didSet {
contentChanged?()
}
}


public var width: CGFloat? {
Expand All @@ -46,50 +29,40 @@ open class PlatformListViewModel: PlatformViewModeling {
}
}

open var header: PlatformViewModel? { nil }
open var footer: PlatformViewModel? { nil }
open var placeholder: PlatformViewModel? { nil }

// contentChanged is required because the list view model returns a ForEach struct
// which does not observe the content change. Caller should supply a contentChanged block
// that manually triggers the parent view model's objectWillChange.send()

public var contentChanged: (() -> Void)?

public init(items: [PlatformViewModel] = [],
header: PlatformViewModel? = nil,
placeholder: PlatformViewModel? = nil,
intraItemSeparator: Bool = true,
firstListItemTopSeparator: Bool = false,
lastListItemBottomSeparator: Bool = false,
contentChanged: (() -> Void)? = nil) {
self.items = items
self.header = header
self.placeholder = placeholder
self.intraItemSeparator = intraItemSeparator
self.firstListItemTopSeparator = firstListItemTopSeparator
self.lastListItemBottomSeparator = lastListItemBottomSeparator
self.contentChanged = contentChanged
}

open func createView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> AnyView {
guard items.count > 0 else {
let cell = Group {
if let placeholder = self.placeholder {
placeholder.createView(parentStyle: parentStyle)
} else {
PlatformView.nilView
}
}
.frame(width: width)
return AnyView(cell)
}

let itemsOrPlaceholder = items.count > 0 ? items : [placeholder ?? .init(bodyBuilder: nil)]
let list: [PlatformViewModel]
if let header, let footer {
list = [header] + items + [footer]
list = [header] + itemsOrPlaceholder + [footer]
} else if let header {
list = [header] + items
list = [header] + itemsOrPlaceholder
} else if let footer {
list = items + [footer]
list = itemsOrPlaceholder + [footer]
} else {
list = items
list = itemsOrPlaceholder
}

return AnyView(
Expand All @@ -99,7 +72,8 @@ open class PlatformListViewModel: PlatformViewModeling {
let cell =
Group {
// render the item if it is a header or a footer and the index is first or last
if (item === list.first && self?.header != nil) || (item === list.last && self?.footer != nil) {
// or if items is empty (and placeholder is being displayed)
if (item === list.first && self?.header != nil) || (item === list.last && self?.footer != nil) || self?.items.isEmpty != false {
item.createView(parentStyle: parentStyle)
} else {
VStack(alignment: .leading, spacing: 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class dydxPortfolioOrdersViewPresenter: HostedViewPresenter<dydxPortfolioOrdersV

AbacusStateManager.shared.state.onboarded
.sink { [weak self] onboarded in
// TODO: remove once isolated markets is supported and force released
self?.viewModel?.shouldDisplayIsolatedOrdersWarning = onboarded
if onboarded {
self?.viewModel?.placeholderText = DataLocalizer.localize(path: "APP.GENERAL.PLACEHOLDER_NO_ORDERS")
} else {
Expand Down Expand Up @@ -97,8 +99,10 @@ class dydxPortfolioOrdersViewPresenter: HostedViewPresenter<dydxPortfolioOrdersV
item.filledSize = dydxFormatter.shared.localFormatted(number: filledSize, digits: configs.displayStepSizeDecimals?.intValue ?? 1)
if let tickSize = configs.displayTickSizeDecimals?.intValue {
switch order.type {
case .market, .stopmarket, .takeprofitmarket:
case .market:
item.price = DataLocalizer.localize(path: "APP.GENERAL.MARKET")
case .stopmarket, .takeprofitmarket:
item.price = dydxFormatter.shared.dollar(number: order.triggerPrice?.doubleValue, digits: tickSize)
default:
item.price = dydxFormatter.shared.dollar(number: order.price, digits: tickSize)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class dydxPortfolioPositionsViewPresenter: HostedViewPresenter<dydxPortfolioPosi

AbacusStateManager.shared.state.onboarded
.sink { [weak self] onboarded in
// TODO: remove once isolated markets is supported and force released
self?.viewModel?.shouldDisplayIsolatedPositionsWarning = onboarded
if onboarded {
self?.viewModel?.placeholderText = DataLocalizer.localize(path: "APP.GENERAL.PLACEHOLDER_NO_POSITIONS")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,12 @@ public class dydxPortfolioFeesListViewModel: PlatformListViewModel {

public init() {
super.init()
self.placeholder = PlatformView.nilViewModel
self.header = createHeader().wrappedViewModel
self.width = UIScreen.main.bounds.width - 16
}

private func createHeader() -> some View {
HStack {
public override var header: PlatformViewModel? {
guard items.count > 0 else { return nil }
return HStack {
Text(DataLocalizer.localize(path: "APP.GENERAL.TIER"))
.leftAligned()
.frame(width: 70)
Expand All @@ -142,6 +141,7 @@ public class dydxPortfolioFeesListViewModel: PlatformListViewModel {
.padding(.horizontal, 8)
.themeFont(fontSize: .smaller)
.themeColor(foreground: .textTertiary)
.wrappedViewModel
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ import PlatformUI
import Utilities

public class dydxPortfolioFillsViewModel: PlatformListViewModel {
@Published public var placeholderText: String? {
didSet {
_placeholder.text = placeholderText
}
}
@Published public var placeholderText: String?

private let _placeholder = PlaceholderViewModel()
public override var placeholder: PlatformViewModel? {
let vm = PlaceholderViewModel()
vm.text = placeholderText
return vm
}

public init(items: [PlatformViewModel] = [], contentChanged: (() -> Void)? = nil) {
super.init(items: items,
intraItemSeparator: true,
firstListItemTopSeparator: true,
lastListItemBottomSeparator: true,
contentChanged: contentChanged)
self.placeholder = _placeholder
self.header = createHeader().wrappedViewModel
self.width = UIScreen.main.bounds.width - 16
}

Expand All @@ -39,8 +37,9 @@ public class dydxPortfolioFillsViewModel: PlatformListViewModel {
return vm
}

private func createHeader() -> some View {
HStack {
public override var header: PlatformViewModel? {
guard items.count > 0 else { return nil }
return HStack {
HStack {
Text(DataLocalizer.localize(path: "APP.GENERAL.TIME"))
Spacer()
Expand All @@ -54,6 +53,7 @@ public class dydxPortfolioFillsViewModel: PlatformListViewModel {
.padding(.bottom, 16)
.themeFont(fontSize: .small)
.themeColor(foreground: .textTertiary)
.wrappedViewModel
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,20 @@ public class dydxPortfolioFundingItemViewModel: PlatformViewModel {
}

public class dydxPortfolioFundingViewModel: PlatformListViewModel {
@Published public var placeholderText: String? {
didSet {
_placeholder.text = placeholderText
}
}
@Published public var placeholderText: String?

private let _placeholder = PlaceholderViewModel()
public override var placeholder: PlatformViewModel? {
let vm = PlaceholderViewModel()
vm.text = placeholderText
return vm
}

public init(items: [PlatformViewModel] = [], contentChanged: (() -> Void)? = nil) {
super.init(items: items,
intraItemSeparator: true,
firstListItemTopSeparator: true,
lastListItemBottomSeparator: true,
contentChanged: contentChanged)
self.placeholder = _placeholder
self.header = createHeader().wrappedViewModel
self.width = UIScreen.main.bounds.width - 16
}

Expand All @@ -169,8 +167,9 @@ public class dydxPortfolioFundingViewModel: PlatformListViewModel {
return vm
}

private func createHeader() -> some View {
HStack {
public override var header: PlatformViewModel? {
guard items.count > 0 else { return nil }
return HStack {
HStack {
Text(DataLocalizer.localize(path: "APP.GENERAL.TIME"))
Spacer()
Expand All @@ -184,6 +183,7 @@ public class dydxPortfolioFundingViewModel: PlatformListViewModel {
.padding(.bottom, 16)
.themeFont(fontSize: .small)
.themeColor(foreground: .textTertiary)
.wrappedViewModel
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,21 @@ public class dydxPortfolioOrderItemViewModel: PlatformViewModel {
}

public class dydxPortfolioOrdersViewModel: PlatformListViewModel {
@Published public var placeholderText: String? {
didSet {
_placeholder.text = placeholderText
}
}
@Published public var shouldDisplayIsolatedOrdersWarning: Bool = false
@Published public var placeholderText: String?

private let _placeholder = PlaceholderViewModel()
public override var placeholder: PlatformViewModel? {
let vm = PlaceholderViewModel()
vm.text = placeholderText
return vm
}

public init(items: [PlatformViewModel] = [], contentChanged: (() -> Void)? = nil) {
super.init(items: items,
intraItemSeparator: true,
firstListItemTopSeparator: true,
lastListItemBottomSeparator: true,
contentChanged: contentChanged)
self.placeholder = _placeholder
self.header = createHeader().wrappedViewModel
self.footer = createFooter().wrappedViewModel
self.width = UIScreen.main.bounds.width - 16
}

public static var previewValue: dydxPortfolioOrdersViewModel {
Expand All @@ -210,8 +207,9 @@ public class dydxPortfolioOrdersViewModel: PlatformListViewModel {
return vm
}

private func createHeader() -> some View {
HStack {
public override var header: PlatformViewModel? {
guard items.count > 0 else { return nil }
return HStack {
Text(DataLocalizer.localize(path: "APP.GENERAL.STATUS_FILL"))
Spacer()
Text(DataLocalizer.localize(path: "APP.GENERAL.PRICE_TYPE"))
Expand All @@ -220,16 +218,19 @@ public class dydxPortfolioOrdersViewModel: PlatformListViewModel {
.padding(.bottom, 16)
.themeFont(fontSize: .small)
.themeColor(foreground: .textTertiary)
.wrappedViewModel
}

private func createFooter() -> some View {
Text(localizerPathKey: "APP.GENERAL.ISOLATED_POSITION_ORDERS_COMING_SOON")
public override var footer: PlatformViewModel? {
guard shouldDisplayIsolatedOrdersWarning else { return nil }
return Text(localizerPathKey: "APP.GENERAL.ISOLATED_POSITION_ORDERS_COMING_SOON")
.multilineTextAlignment(.center)
.padding(.horizontal, 16)
.themeFont(fontSize: .small)
.themeColor(foreground: .textTertiary)
.padding(.top, 12)
.padding(.bottom, 16)
.wrappedViewModel
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,21 +320,26 @@ public class dydxPortfolioPositionItemViewModel: PlatformViewModel {
}

public class dydxPortfolioPositionsViewModel: PlatformListViewModel {
@Published public var placeholderText: String? {
didSet {
_placeholder.text = placeholderText
}
}
// TODO: remove once isolated markets is supported and force released
@Published public var shouldDisplayIsolatedPositionsWarning: Bool = false
@Published public var placeholderText: String?

private let _placeholder = PlaceholderViewModel()
public override var placeholder: PlatformViewModel? {
let vm = PlaceholderViewModel()
vm.text = placeholderText
return vm
}

public override init(items: [PlatformViewModel] = [], header: PlatformViewModel? = nil, placeholder: PlatformViewModel? = nil, intraItemSeparator: Bool = false, firstListItemTopSeparator: Bool = false, lastListItemBottomSeparator: Bool = false, contentChanged: (() -> Void)? = nil) {
super.init(items: items, header: header, placeholder: placeholder, intraItemSeparator: intraItemSeparator, firstListItemTopSeparator: firstListItemTopSeparator, lastListItemBottomSeparator: lastListItemBottomSeparator, contentChanged: contentChanged)
self.placeholder = _placeholder
if dydxBoolFeatureFlag.enable_isolated_margins.isEnabled == false {
self.header = createHeader().wrappedViewModel
}
self.footer = createFooter().wrappedViewModel
public override init(items: [PlatformViewModel] = [],
intraItemSeparator: Bool = false,
firstListItemTopSeparator: Bool = false,
lastListItemBottomSeparator: Bool = false,
contentChanged: (() -> Void)? = nil) {
super.init(items: items,
intraItemSeparator: intraItemSeparator,
firstListItemTopSeparator: firstListItemTopSeparator,
lastListItemBottomSeparator: lastListItemBottomSeparator,
contentChanged: contentChanged)
self.width = UIScreen.main.bounds.width - 32
}

Expand All @@ -347,8 +352,9 @@ public class dydxPortfolioPositionsViewModel: PlatformListViewModel {
return vm
}

private func createHeader() -> some View {
HStack {
public override var header: PlatformViewModel? {
guard dydxBoolFeatureFlag.enable_isolated_margins.isEnabled == false, !items.isEmpty else { return nil }
return HStack {
Text(DataLocalizer.localize(path: "APP.GENERAL.DETAILS"))
Spacer()
Text(DataLocalizer.localize(path: "APP.GENERAL.INDEX_ENTRY"))
Expand All @@ -362,16 +368,19 @@ public class dydxPortfolioPositionsViewModel: PlatformListViewModel {
.padding(.horizontal, 16)
.themeFont(fontSize: .small)
.themeColor(foreground: .textTertiary)
.wrappedViewModel
}

private func createFooter() -> some View {
Text(localizerPathKey: "APP.GENERAL.ISOLATED_POSITIONS_COMING_SOON")
public override var footer: PlatformViewModel? {
guard shouldDisplayIsolatedPositionsWarning else { return nil }
return Text(localizerPathKey: "APP.GENERAL.ISOLATED_POSITIONS_COMING_SOON")
.multilineTextAlignment(.center)
.padding(.horizontal, 16)
.themeFont(fontSize: .small)
.themeColor(foreground: .textTertiary)
.padding(.top, 12)
.padding(.bottom, 16)
.wrappedViewModel
}
}

Expand Down
Loading

0 comments on commit 2f29178

Please sign in to comment.