2024-10-15 19:26:12 +00:00
import QtQuick 2.15
import QtQuick . Layouts 1.15
import QtQml . Models 2.15
2024-08-06 16:04:22 +00:00
import SortFilterProxyModel 0.2
import StatusQ . Popups 0.1
import StatusQ . Popups . Dialog 0.1
import StatusQ . Controls 0.1
import StatusQ . Components 0.1
import StatusQ . Core 0.1
import StatusQ . Core . Theme 0.1
import StatusQ . Core . Utils 0.1
import StatusQ 0.1
import utils 1.0
import AppLayouts . Wallet . controls 1.0
import AppLayouts . Wallet . adaptors 1.0
import AppLayouts . Wallet . panels 1.0
2024-08-13 12:03:35 +00:00
import AppLayouts . Wallet . stores 1.0
2024-08-06 16:04:22 +00:00
StatusStackModal {
id: root
2024-08-13 12:03:35 +00:00
// required data
required property var buyProvidersModel
required property var isBuyProvidersModelLoading
2024-08-06 16:04:22 +00:00
required property BuyCryptoParamsForm buyCryptoInputParamsForm
2024-08-13 12:03:35 +00:00
required property var plainTokensBySymbolModel
required property var groupedAccountAssetsModel
required property var walletAccountsModel
required property var networksModel
required property bool areTestNetworksEnabled
required property string currentCurrency
signal fetchProviders ( )
signal fetchProviderUrl ( string uuid ,
string providerID ,
bool isRecurrent ,
string selectedWalletAddress ,
int chainID ,
string symbol )
// FIXME handling error in case the response is not successful
function providerUrlReady ( uuid , url ) {
if ( uuid === d . uuid ) {
d . urlIsBeingFetched = false
if ( ! ! d . selectedProviderEntry . item && ! ! url )
Global . openLinkWithConfirmation ( url , d . selectedProviderEntry . item . hostname )
root . close ( )
}
}
2024-08-06 16:04:22 +00:00
QtObject {
id: d
2024-08-13 12:03:35 +00:00
// States to track requests
property string uuid
property bool urlIsBeingFetched
2024-08-06 16:04:22 +00:00
readonly property var buyButton: StatusButton {
height: root . finishButton . height
visible: ! ! root . replaceItem
borderColor: "transparent"
2024-08-13 12:03:35 +00:00
text: qsTr ( "Buy via %1" ) . arg ( ! ! d . selectedProviderEntry . item ? d.selectedProviderEntry.item.name: "" )
loading: d . urlIsBeingFetched
2024-08-06 16:04:22 +00:00
onClicked: {
2024-08-13 12:03:35 +00:00
if ( ! ! d . selectedProviderEntry . item && ! ! d . selectedTokenEntry . item ) {
d . fetchProviderUrl (
2024-08-06 16:04:22 +00:00
root . buyCryptoInputParamsForm . selectedProviderId ,
buyCryptoProvidersListPanel . currentTabIndex ,
root . buyCryptoInputParamsForm . selectedWalletAddress ,
root . buyCryptoInputParamsForm . selectedNetworkChainId ,
2024-08-13 12:03:35 +00:00
d . selectedTokenEntry . item . symbol
2024-08-06 16:04:22 +00:00
)
}
}
enabled: root . buyCryptoInputParamsForm . filledCorrectly
}
2024-08-13 12:03:35 +00:00
readonly property ModelEntry selectedAccountEntry: ModelEntry {
sourceModel: root . walletAccountsModel
key: "address"
value: root . buyCryptoInputParamsForm . selectedWalletAddress
}
readonly property ModelEntry selectedTokenEntry: ModelEntry {
sourceModel: root . plainTokensBySymbolModel
key: "key"
2024-11-25 15:42:43 +00:00
value: root . buyCryptoInputParamsForm . selectedTokenKey || Constants . ethToken
2024-08-13 12:03:35 +00:00
}
readonly property ModelEntry selectedProviderEntry: ModelEntry {
id: selectedProviderEntry
sourceModel: root . buyProvidersModel
key: "id"
value: root . buyCryptoInputParamsForm . selectedProviderId
}
function fetchProviderUrl (
providerID ,
isRecurrent ,
accountAddress = "" ,
chainID = 0 ,
symbol = "" ) {
// Identify new search with a different uuid
d . uuid = Utils . uuid ( )
d . urlIsBeingFetched = true
root . fetchProviderUrl ( d . uuid , providerID , isRecurrent ,
accountAddress , chainID , symbol )
}
// used to filter items based on search string in the token selector
2024-09-10 13:32:31 +00:00
2024-08-13 12:03:35 +00:00
readonly property var tokenSelectorViewAdaptor: TokenSelectorViewAdaptor {
assetsModel: root . groupedAccountAssetsModel
plainTokensBySymbolModel: root . plainTokensBySymbolModel
flatNetworksModel: root . networksModel
currentCurrency: root . currentCurrency
showAllTokens: true
enabledChainIds: root . buyCryptoInputParamsForm . selectedNetworkChainId !== - 1 ? [ root . buyCryptoInputParamsForm . selectedNetworkChainId ] : [ ]
accountAddress: root . buyCryptoInputParamsForm . selectedWalletAddress
}
readonly property var buyCryptoAdaptor: BuyCryptoModalAdaptor {
networksModel: root . networksModel
areTestNetworksEnabled: root . areTestNetworksEnabled
processedTokenSelectorAssetsModel: d . tokenSelectorViewAdaptor . outputAssetsModel
selectedProviderSupportedAssetsArray: {
if ( ! ! d . selectedProviderEntry . item && ! ! d . selectedProviderEntry . item . supportedAssets )
return ModelUtils . modelToFlatArray ( d . selectedProviderEntry . item . supportedAssets , "key" )
return null
}
selectedChainId: root . buyCryptoInputParamsForm . selectedNetworkChainId
}
2024-08-06 16:04:22 +00:00
}
width: 560
height: 515
2024-10-15 19:26:12 +00:00
padding: Theme . xlPadding
2024-11-25 15:42:43 +00:00
stackTitle: ! ! root . buyCryptoInputParamsForm . selectedTokenKey ? qsTr ( "Ways to buy %1 for %2" ) . arg ( d . selectedTokenEntry . item . name ) . arg ( ! ! d . selectedAccountEntry . item ? d.selectedAccountEntry.item.name: "" ) : qsTr ( "Ways to buy assets for %1" ) . arg ( ! ! d . selectedAccountEntry . item ? d.selectedAccountEntry.item.name: "" )
2024-08-06 16:04:22 +00:00
rightButtons: [ d . buyButton , finishButton ]
finishButton: StatusButton {
text: qsTr ( "Done" )
onClicked: root . close ( )
}
2024-08-13 12:03:35 +00:00
onOpened: root . fetchProviders ( )
2024-08-06 16:04:22 +00:00
onClosed: {
// reset the view
2024-08-13 12:03:35 +00:00
d . uuid = ""
d . urlIsBeingFetched = false
2024-08-06 16:04:22 +00:00
root . replaceItem = undefined
buyCryptoProvidersListPanel . currentTabIndex = 0
root . buyCryptoInputParamsForm . resetFormData ( )
}
stackItems: [
BuyCryptoProvidersListPanel {
id: buyCryptoProvidersListPanel
2024-08-13 12:03:35 +00:00
providersLoading: root . isBuyProvidersModelLoading
providersModel: root . buyProvidersModel
2024-08-06 16:04:22 +00:00
selectedProviderId: root . buyCryptoInputParamsForm . selectedProviderId
2024-08-13 12:03:35 +00:00
isUrlBeingFetched: d . urlIsBeingFetched
2024-08-06 16:04:22 +00:00
onProviderSelected: {
root . buyCryptoInputParamsForm . selectedProviderId = id
2024-08-13 12:03:35 +00:00
if ( ! ! d . selectedProviderEntry . item ) {
if ( d . selectedProviderEntry . item . urlsNeedParameters ) {
2024-08-06 16:04:22 +00:00
root . replace ( selectParamsPanel )
} else {
2024-08-13 12:03:35 +00:00
d . fetchProviderUrl ( d . selectedProviderEntry . item . id , currentTabIndex )
2024-08-06 16:04:22 +00:00
}
}
}
}
]
Component {
id: selectParamsPanel
SelectParamsForBuyCryptoPanel {
2024-08-13 12:03:35 +00:00
objectName: "selectParamsPanel"
assetsModel: d . buyCryptoAdaptor . filteredAssetsModel
selectedProvider: d . selectedProviderEntry . item
2024-08-06 16:04:22 +00:00
selectedTokenKey: root . buyCryptoInputParamsForm . selectedTokenKey
selectedNetworkChainId: root . buyCryptoInputParamsForm . selectedNetworkChainId
2024-08-13 12:03:35 +00:00
filteredFlatNetworksModel: d . buyCryptoAdaptor . filteredFlatNetworksModel
2024-08-06 16:04:22 +00:00
onNetworkSelected: {
if ( root . buyCryptoInputParamsForm . selectedNetworkChainId !== chainId ) {
root . buyCryptoInputParamsForm . selectedNetworkChainId = chainId
}
}
onTokenSelected: {
if ( root . buyCryptoInputParamsForm . selectedTokenKey !== tokensKey ) {
root . buyCryptoInputParamsForm . selectedTokenKey = tokensKey
}
}
}
}
}