-
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 3f789e9
Showing
27 changed files
with
865 additions
and
112 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 = Localization.exchangeTokensAvailableTokensHeader | ||
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 | ||
} |
61 changes: 61 additions & 0 deletions
61
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,61 @@ | ||
// | ||
// ActionButtonsBuyCoordinator.swift | ||
// TangemApp | ||
// | ||
// Created by GuitarKitty on 01.11.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
final class ActionButtonsBuyCoordinator: ObservableObject, Identifiable { | ||
@Injected(\.exchangeService) private var exchangeService: ExchangeService | ||
|
||
private(set) var tokenSelectorViewModel: TokenSelectorViewModel< | ||
ActionButtonsTokenSelectorItem, | ||
ActionButtonsTokenSelectorItemBuilder | ||
>? | ||
|
||
private let expressTokensListAdapter: ExpressTokensListAdapter | ||
private let tokenSorter: BuyTokenAvailabilitySorter | ||
private let openBuy: (URL) -> Void | ||
|
||
init( | ||
expressTokensListAdapter: some ExpressTokensListAdapter, | ||
tokenSorter: some BuyTokenAvailabilitySorter = CommonBuyTokenAvailabilitySorter(), | ||
openBuy: @escaping (URL) -> Void | ||
) { | ||
self.expressTokensListAdapter = expressTokensListAdapter | ||
self.tokenSorter = tokenSorter | ||
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:) | ||
) | ||
} | ||
} |
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) | ||
} | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
Tangem/Modules/Main/ActionButtons/Buy/View/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,47 @@ | ||
// | ||
// 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 | ||
@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) { | ||
coordinator.openBuy(for: token) | ||
} | ||
}, | ||
emptySearchContent: { | ||
Text(tokenSelectorViewModel.strings.emptySearchMessage) | ||
.style(Fonts.Regular.caption1, color: Colors.Text.tertiary) | ||
.multilineTextAlignment(.center) | ||
.animation(.default, value: tokenSelectorViewModel.searchText) | ||
} | ||
) | ||
} | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
Tangem/Modules/Main/ActionButtons/Models/ActionButtonsTokenSelectorItem.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,18 @@ | ||
// | ||
// ActionButtonsTokenSelectorItem.swift | ||
// TangemApp | ||
// | ||
// Created by GuitarKitty on 05.11.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
struct ActionButtonsTokenSelectorItem: Identifiable, Equatable { | ||
let id: Int | ||
let tokenIconInfo: TokenIconInfo | ||
let name: String | ||
let symbol: String | ||
let balance: String | ||
let fiatBalance: String | ||
let isDisabled: Bool | ||
let walletModel: WalletModel | ||
} |
Oops, something went wrong.