Skip to content

Commit

Permalink
add memo warning to transfer input
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Jun 5, 2024
1 parent e0450f9 commit 22eba76
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 10 deletions.
4 changes: 4 additions & 0 deletions PlatformUI/PlatformUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
02F38BF22A9AAE3700969E06 /* CircularProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02F38BF12A9AAE3700969E06 /* CircularProgressView.swift */; };
1C811E336064517E256D1290 /* Pods_iOS_PlatformUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F6793D8074DF2E7FE4592138 /* Pods_iOS_PlatformUITests.framework */; };
27044F882BBB2ADF004C750D /* Text+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27044F872BBB2ADF004C750D /* Text+Ext.swift */; };
274C47EA2C0FC6CF000212C3 /* EdgeInsets+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 274C47E92C0FC6CF000212C3 /* EdgeInsets+Ext.swift */; };
277E7AC62BBF3BE8009F95DE /* InlineAlert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 277E7AC52BBF3BE8009F95DE /* InlineAlert.swift */; };
27E6A7322AB8D5F600026CB5 /* SwipeActionsViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27E6A7312AB8D5F600026CB5 /* SwipeActionsViewModifier.swift */; };
6488BBDC296F6AEA0096502F /* TabItemViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6488BBDB296F6AEA0096502F /* TabItemViewModel.swift */; };
Expand Down Expand Up @@ -135,6 +136,7 @@
02F16FE128B53A200085DC58 /* SampleStyleLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SampleStyleLabel.swift; sourceTree = "<group>"; };
02F38BF12A9AAE3700969E06 /* CircularProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CircularProgressView.swift; sourceTree = "<group>"; };
27044F872BBB2ADF004C750D /* Text+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Text+Ext.swift"; sourceTree = "<group>"; };
274C47E92C0FC6CF000212C3 /* EdgeInsets+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "EdgeInsets+Ext.swift"; sourceTree = "<group>"; };
277E7AC52BBF3BE8009F95DE /* InlineAlert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InlineAlert.swift; sourceTree = "<group>"; };
27E6A7312AB8D5F600026CB5 /* SwipeActionsViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwipeActionsViewModifier.swift; sourceTree = "<group>"; };
366BD14FE1ED4F2AF21D924E /* Pods-iOS-PlatformUI.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iOS-PlatformUI.release.xcconfig"; path = "Target Support Files/Pods-iOS-PlatformUI/Pods-iOS-PlatformUI.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -375,6 +377,7 @@
isa = PBXGroup;
children = (
27044F872BBB2ADF004C750D /* Text+Ext.swift */,
274C47E92C0FC6CF000212C3 /* EdgeInsets+Ext.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -642,6 +645,7 @@
6488BBDC296F6AEA0096502F /* TabItemViewModel.swift in Sources */,
27044F882BBB2ADF004C750D /* Text+Ext.swift in Sources */,
0258B9EE2991C9FF0098E1BE /* PlatformWebView.swift in Sources */,
274C47EA2C0FC6CF000212C3 /* EdgeInsets+Ext.swift in Sources */,
02F38BF22A9AAE3700969E06 /* CircularProgressView.swift in Sources */,
02E2C93028A1C8A400F7C3BE /* PlatformUI.docc in Sources */,
02B120B528A430DF00281498 /* ThemeViewModifiers.swift in Sources */,
Expand Down
12 changes: 6 additions & 6 deletions PlatformUI/PlatformUI/Components/Buttons/PlatformButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ public enum PlatformButtonState {
}

public enum PlatformButtonType {
case defaultType(fillWidth: Bool), iconType, pill, small
case defaultType(fillWidth: Bool = true, padding: EdgeInsets = .init(all: 14)), iconType, pill, small
}

public class PlatformButtonViewModel<Content: PlatformViewModeling>: PlatformViewModel {
@Published public var action: () -> ()
@Published public var content: Content
@Published public var type: PlatformButtonType
@Published public var state: PlatformButtonState

public init(content: Content,
type: PlatformButtonType = .defaultType(fillWidth: true),
type: PlatformButtonType = .defaultType(),
state: PlatformButtonState = .primary,
action: @escaping () -> ()) {
self.action = action
self.content = content
self.type = type
self.state = state
self.action = action
}

public override func createView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> PlatformView {
Expand All @@ -40,7 +40,7 @@ public class PlatformButtonViewModel<Content: PlatformViewModeling>: PlatformVie
return AnyView(
Group {
switch self.type {
case .defaultType(let fillWidth):
case .defaultType(let fillWidth, let padding):
let button = Button(action: self.action) {
HStack {
if fillWidth {
Expand All @@ -55,7 +55,7 @@ public class PlatformButtonViewModel<Content: PlatformViewModeling>: PlatformVie
}
.buttonStyle(BorderlessButtonStyle())
.disabled(disabled)
.padding(.all, 14)
.padding(padding)
.if(fillWidth) { view in
view.frame(maxWidth: .infinity)
}
Expand Down
18 changes: 18 additions & 0 deletions PlatformUI/PlatformUI/Components/Extensions/EdgeInsets+Ext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// EdgeInsets+ext.swift
// PlatformUI
//
// Created by Michael Maguire on 6/4/24.
//

import SwiftUI

public extension EdgeInsets {
init(all: CGFloat) {
self.init(top: all, leading: all, bottom: all, trailing: all)
}

init(horizontal: CGFloat, vertical: CGFloat) {
self.init(top: vertical, leading: horizontal, bottom: vertical, trailing: horizontal)
}
}
11 changes: 11 additions & 0 deletions PlatformUI/PlatformUI/Components/Input/PlatformInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ open class PlatformTextInputViewModel: PlatformValueInputViewModel {

private var inputType: InputType

/// Prefer to set `value` directly if forcing is not needed
/// - Parameters:
/// - value: value to set
/// - shouldForce: whether setting shoudl happen even if input is focused
/// we need to refactor how we do inputs to be more flexible
public final func programmaticallySet(value: String) {
input = value
self.valueChanged(value: self.input)
updateView()
}

override open var value: String? {
didSet {
if !focused {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Abacus
import dydxStateManager
import dydxFormatter
import Combine
import KeyboardObserving

protocol dydxTransferOutViewPresenterProtocol: HostedViewPresenterProtocol {
var viewModel: dydxTransferOutViewModel? { get }
Expand Down Expand Up @@ -76,6 +77,10 @@ class dydxTransferOutViewPresenter: HostedViewPresenter<dydxTransferOutViewModel
AbacusStateManager.shared.transfer(input: address, type: .address)
}

viewModel.memoBox = .init { value in
AbacusStateManager.shared.transfer(input: value, type: .memo)
}

self.viewModel = viewModel

attachChildren(workers: childPresenters)
Expand Down Expand Up @@ -153,6 +158,8 @@ class dydxTransferOutViewPresenter: HostedViewPresenter<dydxTransferOutViewModel
viewModel?.amountBox?.objectWillChange.send()

viewModel?.addressInput?.value = transferInput.address

viewModel?.memoBox?.value = transferInput.memo
}

private func updateChainsTokensViewModel() {
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 @@ -171,6 +171,7 @@
2729123E2C06A775003F3EA0 /* Introspect in Frameworks */ = {isa = PBXBuildFile; productRef = 2729123D2C06A775003F3EA0 /* Introspect */; };
272912402C06A780003F3EA0 /* KeyboardObserving in Frameworks */ = {isa = PBXBuildFile; productRef = 2729123F2C06A780003F3EA0 /* KeyboardObserving */; };
273F50162B7C3F120034792A /* SignedAmountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 273F50152B7C3F120034792A /* SignedAmountView.swift */; };
274C47F02C0FC9A4000212C3 /* MemoBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 274C47EF2C0FC9A4000212C3 /* MemoBox.swift */; };
27685F4D2B9FCAE200F37DE2 /* Satoshi-Medium.otf in Resources */ = {isa = PBXBuildFile; fileRef = 27685F402B9FCAD300F37DE2 /* Satoshi-Medium.otf */; };
2769090E2AAFD8030075B2D6 /* TransferInstanceViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2769090D2AAFD8030075B2D6 /* TransferInstanceViewModel.swift */; };
276909102AAFD8BE0075B2D6 /* dydxPortfolioTransfersViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2769090F2AAFD8BE0075B2D6 /* dydxPortfolioTransfersViewModel.swift */; };
Expand Down Expand Up @@ -540,6 +541,7 @@
2728CE1A2BBCD2AB004C9323 /* dydxGainLossInputViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = dydxGainLossInputViewModel.swift; sourceTree = "<group>"; };
273F50152B7C3F120034792A /* SignedAmountView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignedAmountView.swift; sourceTree = "<group>"; };
2742C04D2BF6897A00E13C09 /* dydxAnalytics.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = dydxAnalytics.framework; sourceTree = BUILT_PRODUCTS_DIR; };
274C47EF2C0FC9A4000212C3 /* MemoBox.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoBox.swift; sourceTree = "<group>"; };
27685F402B9FCAD300F37DE2 /* Satoshi-Medium.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Satoshi-Medium.otf"; 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>"; };
Expand Down Expand Up @@ -641,6 +643,7 @@
02714C9C29E0C78600CC1C44 /* ChainsComboBox.swift */,
02714C9E29E0C7C500CC1C44 /* TokensComboBox.swift */,
02F99D1429E0CC9D0009B3E8 /* TransferAmountBox.swift */,
274C47EF2C0FC9A4000212C3 /* MemoBox.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -1997,6 +2000,7 @@
02428C192962E219000D7929 /* dydxMarketFundingDurationsView.swift in Sources */,
023AB3B42BEAD53D005230B2 /* dydxMarginModeView.swift in Sources */,
02860A9129C15E670079E644 /* dydxOnboardScanView.swift in Sources */,
274C47F02C0FC9A4000212C3 /* MemoBox.swift in Sources */,
02F99F4029E50D3A0009B3E8 /* ChevronBackButton.swift in Sources */,
02F6E7112A8292FC0018F00C /* dydxProfileFeesView.swift in Sources */,
27F624132BBDB4D400AB6D1A /* dydxTakeProfitStopLossInputAreaViewModel.swift in Sources */,
Expand Down
115 changes: 115 additions & 0 deletions dydx/dydxViews/dydxViews/_v4/Transfer/Components/MemoBox.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// MemoBox.swift
// dydxViews
//
// Created by Michael Maguire on 6/4/24.
//

import SwiftUI
import PlatformUI
import Utilities
import dydxFormatter

public class MemoBoxModel: PlatformTextInputViewModel {
@Published public var pasteAction: (() -> Void)?

public init(onEdited: ((String?) -> Void)?) {
super.init(label: DataLocalizer.localize(path: "APP.GENERAL.MEMO"),
placeHolder: DataLocalizer.localize(path: "APP.DIRECT_TRANSFER_MODAL.REQUIRED_FOR_CEX"),
onEdited: onEdited)
}

private var memoWarning: InlineAlertViewModel? {
guard value?.isEmpty != false else { return nil }
return InlineAlertViewModel(.init(title: nil,
body: DataLocalizer.localize(path: "ERRORS.TRANSFER_MODAL.TRANSFER_WITHOUT_MEMO"),
level: .warning))
}

public override var valueAccessoryView: AnyView? {
set {}
get { memoInputAccessory }
}

private var memoInputAccessory: AnyView? {
ZStack {
let shouldShowCancel = value?.isEmpty == false
if shouldShowCancel {
let content = Image("icon_cancel", bundle: .dydxView)
.resizable()
.templateColor(.textSecondary)
.frame(width: 9, height: 9)
.padding(.all, 10)
.borderAndClip(style: .circle, borderColor: .layer6)
.wrappedViewModel

PlatformButtonViewModel(content: content,
type: .iconType,
state: .secondary) {[weak self] in
self?.programmaticallySet(value: "")
}
.createView()

}
let content = Text(localizerPathKey: "APP.GENERAL.PASTE")
.themeColor(foreground: .textSecondary)
.themeFont(fontType: .base, fontSize: .small)
.wrappedViewModel

PlatformButtonViewModel(content: content,
type: .defaultType(fillWidth: false,
padding: .init(horizontal: 8, vertical: 6)),
state: .secondary ) {[weak self] in
guard let pastedString = UIPasteboard.general.string else { return }
self?.programmaticallySet(value: pastedString)
}
.createView()
.opacity(shouldShowCancel ? 0 : 1) // hide it with opacity so that it sizes correctly all the timem
}
.wrappedInAnyView()

}

public override func createView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> PlatformView {
VStack(spacing: 12) {
super.createView(parentStyle: parentStyle)
.makeInput()
memoWarning?.createView(parentStyle: parentStyle)
}
.wrappedViewModel
.createView(parentStyle: parentStyle)
}

public static var previewValue: MemoBoxModel = {
let vm = MemoBoxModel(onEdited: nil)
return vm
}()
}

#if DEBUG
struct MemoBox_Previews_Dark: PreviewProvider {
@StateObject static var themeSettings = ThemeSettings.shared

static var previews: some View {
ThemeSettings.applyDarkTheme()
ThemeSettings.applyStyles()
return MemoBoxModel.previewValue
.createView()
// .edgesIgnoringSafeArea(.bottom)
.previewLayout(.sizeThatFits)
}
}

struct MemoBox_Previews_Light: PreviewProvider {
@StateObject static var themeSettings = ThemeSettings.shared

static var previews: some View {
ThemeSettings.applyLightTheme()
ThemeSettings.applyStyles()
return MemoBoxModel.previewValue
.createView()
// .edgesIgnoringSafeArea(.bottom)
.previewLayout(.sizeThatFits)
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class dydxTransferOutViewModel: PlatformViewModel {
text: DataLocalizer.localize(path: "APP.GENERAL.DYDX_CHAIN"))
@Published public var tokensComboBox: TokensComboBoxModel? =
TokensComboBoxModel(label: DataLocalizer.localize(path: "APP.GENERAL.ASSET"))
@Published public var memoBox: MemoBoxModel?

@Published public var ctaButton: dydxTradeInputCtaButtonViewModel? = dydxTradeInputCtaButtonViewModel()
@Published public var validationViewModel: dydxValidationViewModel? = dydxValidationViewModel()
Expand All @@ -42,16 +43,18 @@ public class dydxTransferOutViewModel: PlatformViewModel {
public override func createView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> PlatformView {
PlatformView(viewModel: self, parentStyle: parentStyle, styleKey: styleKey) { [weak self] style in
guard let self = self else { return AnyView(PlatformView.nilView) }

return AnyView(
VStack {
Group {
VStack(spacing: 12) {
self.chainsComboBox?.createView(parentStyle: style)
self.addressInput?.createView(parentStyle: style)
.makeInput()
HStack(spacing: 12) {
self.addressInput?.createView(parentStyle: style)
.makeInput()
self.chainsComboBox?.createView(parentStyle: style)
}
self.tokensComboBox?.createView(parentStyle: style)
self.amountBox?.createView(parentStyle: style)
self.memoBox?.createView(parentStyle: style)
}
}

Expand Down
2 changes: 2 additions & 0 deletions dydx/dydxViews/dydxViews/_v4/Transfer/dydxTransferView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import SwiftUI
import PlatformUI
import Utilities
import KeyboardObserving

public class dydxTransferViewModel: PlatformViewModel {
@Published public var sections = dydxTransferSectionsViewModel()
Expand Down Expand Up @@ -62,6 +63,7 @@ public class dydxTransferViewModel: PlatformViewModel {
}
.frame(minHeight: UIScreen.main.bounds.size.height - topPadding - (self.safeAreaInsets?.top ?? 0) - (self.safeAreaInsets?.bottom ?? 0))
}
.keyboardObserving()
.keyboardAccessory(background: .layer3, parentStyle: style)
.frame(minWidth: 0, maxWidth: .infinity)
.padding([.leading, .trailing])
Expand Down

0 comments on commit 22eba76

Please sign in to comment.