-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IOS-8236 Token selector view and buy from action buttons
- Loading branch information
1 parent
9836302
commit ebbb504
Showing
28 changed files
with
846 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,3 +50,9 @@ extension Array { | |
} | ||
} | ||
} | ||
|
||
extension Array { | ||
var isNotEmpty: Bool { | ||
return !isEmpty | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Tangem/Modules/Main/ActionButtons/Buy/Builders/ActionButtonsTokenSelectorItemBuilder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// ActionButtonsTokenSelectorItemBuilder.swift | ||
// TangemApp | ||
// | ||
// Created by GuitarKitty on 01.11.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
struct ActionButtonsTokenSelectorItemBuilder: TokenSelectorItemBuilder { | ||
func map(from walletModel: WalletModel, isDisabled: Bool) -> ActionButtonsTokenSelectorItem { | ||
let tokenIconInfo = TokenIconInfoBuilder().build(from: walletModel.tokenItem, isCustom: walletModel.isCustom) | ||
|
||
return ActionButtonsTokenSelectorItem( | ||
id: walletModel.id, | ||
tokenIconInfo: tokenIconInfo, | ||
name: walletModel.tokenItem.name, | ||
symbol: walletModel.tokenItem.currencySymbol, | ||
balance: walletModel.balance, | ||
fiatBalance: walletModel.fiatBalance, | ||
isDisabled: isDisabled, | ||
walletModel: walletModel | ||
) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Tangem/Modules/Main/ActionButtons/Buy/BuyTokenSelectorStrings.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// BuyTokenSelectorLocalizable.swift | ||
// TangemApp | ||
// | ||
// Created by GuitarKitty on 05.11.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
struct BuyTokenSelectorStrings: TokenSelectorLocalizable { | ||
let availableTokensListTitle = "My tokens" | ||
let unavailableTokensListTitle = "Unavailable to purchase" | ||
let emptySearchMessage = "Token not found in your portfolio? Check the Markets to find and add it for purchase" | ||
let emptyTokensMessage: String? = nil | ||
} |
66 changes: 66 additions & 0 deletions
66
Tangem/Modules/Main/ActionButtons/Buy/Coordinator/ActionButtonsBuyCoordinator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// ActionButtonsBuyCoordinator.swift | ||
// TangemApp | ||
// | ||
// Created by GuitarKitty on 01.11.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
final class ActionButtonsBuyCoordinator< | ||
Builder: TokenSelectorItemBuilder | ||
>: ObservableObject, Identifiable where Builder.TokenModel == ActionButtonsTokenSelectorItem { | ||
@Injected(\.exchangeService) private var exchangeService: ExchangeService | ||
|
||
private(set) var tokenSelectorViewModel: TokenSelectorViewModel< | ||
ActionButtonsTokenSelectorItem, | ||
ActionButtonsTokenSelectorItemBuilder | ||
>? | ||
|
||
private let expressTokensListAdapter: ExpressTokensListAdapter | ||
private let tokenSorter: BuyTokenAvailabilitySorter | ||
private let tokenSelectorItemBuilder: Builder | ||
private let openBuy: (URL) -> Void | ||
|
||
init( | ||
expressTokensListAdapter: some ExpressTokensListAdapter, | ||
tokenSorter: some BuyTokenAvailabilitySorter = CommonBuyTokenAvailabilitySorter(), | ||
tokenSelectorItemBuilder: Builder = ActionButtonsTokenSelectorItemBuilder(), | ||
openBuy: @escaping (URL) -> Void | ||
) { | ||
self.expressTokensListAdapter = expressTokensListAdapter | ||
self.tokenSorter = tokenSorter | ||
self.tokenSelectorItemBuilder = tokenSelectorItemBuilder | ||
self.openBuy = openBuy | ||
|
||
tokenSelectorViewModel = makeTokenSelectorViewModel() | ||
} | ||
|
||
func openBuy(for token: ActionButtonsTokenSelectorItem) { | ||
guard | ||
let buyUrl = exchangeService.getBuyUrl( | ||
currencySymbol: token.symbol, | ||
amountType: token.walletModel.amountType, | ||
blockchain: token.walletModel.blockchainNetwork.blockchain, | ||
walletAddress: token.walletModel.defaultAddress | ||
) | ||
else { | ||
return | ||
} | ||
|
||
openBuy(buyUrl) | ||
} | ||
|
||
private func makeTokenSelectorViewModel() -> TokenSelectorViewModel< | ||
ActionButtonsTokenSelectorItem, | ||
ActionButtonsTokenSelectorItemBuilder | ||
> { | ||
TokenSelectorViewModel( | ||
tokenSelectorItemBuilder: ActionButtonsTokenSelectorItemBuilder(), | ||
strings: BuyTokenSelectorStrings(), | ||
expressTokensListAdapter: expressTokensListAdapter, | ||
sortModels: tokenSorter.sortModels(walletModels:) | ||
) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
Tangem/Modules/Main/ActionButtons/Buy/Coordinator/ActionButtonsBuyCoordinatorView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// ActionButtonsBuyCoordinatorView.swift | ||
// TangemApp | ||
// | ||
// Created by GuitarKitty on 01.11.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ActionButtonsBuyView: View { | ||
@ObservedObject var coordinator: ActionButtonsBuyCoordinator<ActionButtonsTokenSelectorItemBuilder> | ||
@Environment(\.dismiss) private var dismiss | ||
|
||
var body: some View { | ||
NavigationView { | ||
content | ||
.navigationTitle(Localization.commonBuy) | ||
.navigationBarTitleDisplayMode(.inline) | ||
.toolbar { | ||
ToolbarItem(placement: .topBarLeading) { | ||
CloseButton(dismiss: dismiss.callAsFunction) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
private var content: some View { | ||
if let tokenSelectorViewModel = coordinator.tokenSelectorViewModel { | ||
TokenSelectorView( | ||
viewModel: tokenSelectorViewModel, | ||
tokenCellContent: { token in | ||
ActionButtonsTokenSelectItemView(model: token) | ||
.onTapGesture { | ||
coordinator.openBuy(for: token) | ||
} | ||
}, | ||
emptySearchContent: { | ||
Text(tokenSelectorViewModel.strings.emptySearchMessage) | ||
.font(Fonts.Regular.caption2) | ||
.foregroundStyle(Colors.Text.tertiary) | ||
.multilineTextAlignment(.center) | ||
.animation(.default, value: tokenSelectorViewModel.searchText) | ||
} | ||
) | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Tangem/Modules/Main/ActionButtons/Buy/Services/CommonBuyTokenAvailabilitySorter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// CommonBuyTokenAvailabilitySorter.swift | ||
// TangemApp | ||
// | ||
// Created by GuitarKitty on 01.11.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
protocol BuyTokenAvailabilitySorter { | ||
func sortModels(walletModels: [WalletModel]) -> (availableModels: [WalletModel], unavailableModels: [WalletModel]) | ||
} | ||
|
||
struct CommonBuyTokenAvailabilitySorter: BuyTokenAvailabilitySorter { | ||
@Injected(\.exchangeService) private var exchangeService: ExchangeService | ||
|
||
func sortModels(walletModels: [WalletModel]) -> (availableModels: [WalletModel], unavailableModels: [WalletModel]) { | ||
walletModels.reduce(into: (availableModels: [WalletModel](), unavailableModels: [WalletModel]())) { result, walletModel in | ||
if exchangeService.canBuy( | ||
walletModel.tokenItem.currencySymbol, | ||
amountType: walletModel.amountType, | ||
blockchain: walletModel.blockchainNetwork.blockchain | ||
) { | ||
result.availableModels.append(walletModel) | ||
} else { | ||
result.unavailableModels.append(walletModel) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.