diff --git a/PlatformUI/PlatformUI/PlatformListViewModel.swift b/PlatformUI/PlatformUI/PlatformListViewModel.swift index cea3fa82a..db0b27479 100644 --- a/PlatformUI/PlatformUI/PlatformListViewModel.swift +++ b/PlatformUI/PlatformUI/PlatformListViewModel.swift @@ -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? { @@ -53,43 +36,33 @@ open class PlatformListViewModel: PlatformViewModeling { 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 var header: PlatformViewModel? { nil } + open var footer: PlatformViewModel? { nil } + open var placeholder: PlatformViewModel? { nil } + 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( @@ -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) { diff --git a/dydx/dydxPresenters/dydxPresenters/_v4/Portfolio/Components/dydxPortfolioOrdersViewPresenter.swift b/dydx/dydxPresenters/dydxPresenters/_v4/Portfolio/Components/dydxPortfolioOrdersViewPresenter.swift index d343ecccd..ad239e602 100644 --- a/dydx/dydxPresenters/dydxPresenters/_v4/Portfolio/Components/dydxPortfolioOrdersViewPresenter.swift +++ b/dydx/dydxPresenters/dydxPresenters/_v4/Portfolio/Components/dydxPortfolioOrdersViewPresenter.swift @@ -36,6 +36,8 @@ class dydxPortfolioOrdersViewPresenter: HostedViewPresenter 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) @@ -142,6 +141,7 @@ public class dydxPortfolioFeesListViewModel: PlatformListViewModel { .padding(.horizontal, 8) .themeFont(fontSize: .smaller) .themeColor(foreground: .textTertiary) + .wrappedViewModel } } diff --git a/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioFillsView.swift b/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioFillsView.swift index c99abcddb..daff7232c 100644 --- a/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioFillsView.swift +++ b/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioFillsView.swift @@ -11,13 +11,13 @@ 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, @@ -25,8 +25,6 @@ public class dydxPortfolioFillsViewModel: PlatformListViewModel { firstListItemTopSeparator: true, lastListItemBottomSeparator: true, contentChanged: contentChanged) - self.placeholder = _placeholder - self.header = createHeader().wrappedViewModel self.width = UIScreen.main.bounds.width - 16 } @@ -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() @@ -54,6 +53,7 @@ public class dydxPortfolioFillsViewModel: PlatformListViewModel { .padding(.bottom, 16) .themeFont(fontSize: .small) .themeColor(foreground: .textTertiary) + .wrappedViewModel } } diff --git a/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioFundingView.swift b/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioFundingView.swift index f5c05dfcc..a3fca8def 100644 --- a/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioFundingView.swift +++ b/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioFundingView.swift @@ -141,13 +141,13 @@ 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, @@ -155,8 +155,6 @@ public class dydxPortfolioFundingViewModel: PlatformListViewModel { firstListItemTopSeparator: true, lastListItemBottomSeparator: true, contentChanged: contentChanged) - self.placeholder = _placeholder - self.header = createHeader().wrappedViewModel self.width = UIScreen.main.bounds.width - 16 } @@ -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() @@ -184,6 +183,7 @@ public class dydxPortfolioFundingViewModel: PlatformListViewModel { .padding(.bottom, 16) .themeFont(fontSize: .small) .themeColor(foreground: .textTertiary) + .wrappedViewModel } } diff --git a/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioOrdersView.swift b/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioOrdersView.swift index a2078329b..c870b3518 100644 --- a/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioOrdersView.swift +++ b/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioOrdersView.swift @@ -181,13 +181,14 @@ 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, @@ -195,10 +196,6 @@ public class dydxPortfolioOrdersViewModel: PlatformListViewModel { 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 { @@ -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")) @@ -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 } } diff --git a/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioPositionsView.swift b/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioPositionsView.swift index 1c7e81e50..fe7b13d50 100644 --- a/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioPositionsView.swift +++ b/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioPositionsView.swift @@ -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 } @@ -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")) @@ -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 } } diff --git a/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioTransfersViewModel.swift b/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioTransfersViewModel.swift index bd2be86d9..171bff968 100644 --- a/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioTransfersViewModel.swift +++ b/dydx/dydxViews/dydxViews/_v4/Portfolio/Components/Sections/dydxPortfolioTransfersViewModel.swift @@ -10,17 +10,16 @@ import PlatformUI import Utilities public class dydxPortfolioTransfersViewModel: 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, - placeholder: _placeholder, intraItemSeparator: true, firstListItemTopSeparator: true, lastListItemBottomSeparator: true,