2024-05-16 17:57:37 +00:00
|
|
|
import QtQuick 2.15
|
|
|
|
|
2024-08-05 13:41:20 +00:00
|
|
|
import StatusQ 0.1
|
2024-05-21 10:42:50 +00:00
|
|
|
import StatusQ.Core.Utils 0.1
|
|
|
|
|
2024-08-05 13:41:20 +00:00
|
|
|
import SortFilterProxyModel 0.2
|
|
|
|
|
2024-07-23 14:26:55 +00:00
|
|
|
import AppLayouts.Wallet.services.dapps 1.0
|
|
|
|
|
2024-05-21 10:42:50 +00:00
|
|
|
import shared.stores 1.0
|
|
|
|
|
2024-05-16 17:57:37 +00:00
|
|
|
import utils 1.0
|
|
|
|
|
2024-05-21 10:42:50 +00:00
|
|
|
QObject {
|
2024-05-16 17:57:37 +00:00
|
|
|
id: root
|
|
|
|
|
2024-05-31 09:34:59 +00:00
|
|
|
required property WalletConnectSDKBase sdk
|
2024-05-21 10:42:50 +00:00
|
|
|
required property DAppsStore store
|
2024-07-23 14:26:55 +00:00
|
|
|
required property var supportedAccountsModel
|
2024-05-16 17:57:37 +00:00
|
|
|
|
2024-08-05 13:41:20 +00:00
|
|
|
property string selectedAddress: ""
|
|
|
|
|
|
|
|
readonly property SortFilterProxyModel dappsModel: SortFilterProxyModel {
|
|
|
|
objectName: "DAppsModelFiltered"
|
|
|
|
sourceModel: d.dappsModel
|
|
|
|
|
|
|
|
filters: FastExpressionFilter {
|
|
|
|
enabled: !!root.selectedAddress
|
|
|
|
|
|
|
|
function isAddressIncluded(accountAddressesSubModel, selectedAddress) {
|
|
|
|
const addresses = ModelUtils.modelToFlatArray(accountAddressesSubModel, "address")
|
|
|
|
return addresses.includes(root.selectedAddress)
|
|
|
|
}
|
|
|
|
expression: isAddressIncluded(model.accountAddresses, root.selectedAddress)
|
|
|
|
|
|
|
|
expectedRoles: "accountAddresses"
|
|
|
|
}
|
|
|
|
}
|
2024-05-16 17:57:37 +00:00
|
|
|
|
2024-05-21 10:42:50 +00:00
|
|
|
function updateDapps() {
|
|
|
|
d.updateDappsModel()
|
2024-05-16 17:57:37 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 10:42:50 +00:00
|
|
|
QObject {
|
2024-05-16 17:57:37 +00:00
|
|
|
id: d
|
|
|
|
|
2024-05-21 10:42:50 +00:00
|
|
|
property ListModel dappsModel: ListModel {
|
|
|
|
id: dapps
|
2024-08-05 13:41:20 +00:00
|
|
|
objectName: "DAppsModel"
|
2024-05-16 17:57:37 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 10:42:50 +00:00
|
|
|
property var dappsListReceivedFn: null
|
|
|
|
property var getActiveSessionsFn: null
|
|
|
|
function updateDappsModel()
|
2024-05-16 17:57:37 +00:00
|
|
|
{
|
2024-05-21 10:42:50 +00:00
|
|
|
dappsListReceivedFn = (dappsJson) => {
|
2024-08-05 13:41:20 +00:00
|
|
|
root.store.dappsListReceived.disconnect(dappsListReceivedFn);
|
2024-05-21 10:42:50 +00:00
|
|
|
dapps.clear();
|
|
|
|
|
|
|
|
let dappsList = JSON.parse(dappsJson);
|
|
|
|
for (let i = 0; i < dappsList.length; i++) {
|
2024-08-05 13:41:20 +00:00
|
|
|
const cachedEntry = dappsList[i];
|
|
|
|
let accountAddresses = cachedEntry.accountAddresses
|
|
|
|
if (!accountAddresses) {
|
|
|
|
accountAddresses = [{address: ''}];
|
|
|
|
}
|
|
|
|
|
|
|
|
const dappEntryWithRequiredRoles = {
|
|
|
|
description: cachedEntry.description,
|
|
|
|
url: cachedEntry.url,
|
|
|
|
name: cachedEntry.name,
|
|
|
|
iconUrl: cachedEntry.url,
|
|
|
|
accountAddresses: cachedEntry.accountAddresses
|
|
|
|
}
|
2024-05-21 10:42:50 +00:00
|
|
|
dapps.append(dappsList[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
root.store.dappsListReceived.connect(dappsListReceivedFn);
|
|
|
|
|
|
|
|
// triggers a potential fast response from store.dappsListReceived
|
|
|
|
if (!store.getDapps()) {
|
|
|
|
console.warn("Failed retrieving dapps from persistence")
|
|
|
|
root.store.dappsListReceived.disconnect(dappsListReceivedFn);
|
|
|
|
}
|
2024-05-16 17:57:37 +00:00
|
|
|
|
2024-05-21 10:42:50 +00:00
|
|
|
getActiveSessionsFn = () => {
|
2024-08-05 13:41:20 +00:00
|
|
|
sdk.getActiveSessions((allSessionsAllProfiles) => {
|
2024-05-21 10:42:50 +00:00
|
|
|
root.store.dappsListReceived.disconnect(dappsListReceivedFn);
|
2024-05-16 17:57:37 +00:00
|
|
|
|
2024-08-05 13:41:20 +00:00
|
|
|
const dAppsMap = {}
|
|
|
|
const topics = []
|
|
|
|
const sessions = DAppsHelpers.filterActiveSessionsForKnownAccounts(allSessionsAllProfiles, supportedAccountsModel)
|
|
|
|
for (const sessionID in sessions) {
|
|
|
|
const session = sessions[sessionID]
|
|
|
|
const dapp = session.peer.metadata
|
2024-06-19 15:17:49 +00:00
|
|
|
if (!!dapp.icons && dapp.icons.length > 0) {
|
2024-05-21 10:42:50 +00:00
|
|
|
dapp.iconUrl = dapp.icons[0]
|
2024-06-19 15:17:49 +00:00
|
|
|
} else {
|
|
|
|
dapp.iconUrl = ""
|
2024-05-21 10:42:50 +00:00
|
|
|
}
|
2024-08-05 13:41:20 +00:00
|
|
|
const accounts = DAppsHelpers.getAccountsInSession(session)
|
|
|
|
const existingDApp = dAppsMap[dapp.url]
|
|
|
|
if (existingDApp) {
|
|
|
|
// In Qt5.15.2 this is the way to make a "union" of two arrays
|
|
|
|
// more modern syntax (ES-6) is not available yet
|
|
|
|
const combinedAddresses = new Set(existingDApp.accountAddresses.concat(dapp.accountAddresses));
|
|
|
|
existingDApp.accountAddresses = Array.from(combinedAddresses);
|
|
|
|
} else {
|
|
|
|
dapp.accountAddresses = accounts
|
|
|
|
dAppsMap[dapp.url] = dapp
|
|
|
|
}
|
|
|
|
|
|
|
|
topics.push(sessionID)
|
2024-05-21 10:42:50 +00:00
|
|
|
}
|
2024-08-05 13:41:20 +00:00
|
|
|
|
2024-07-23 14:26:55 +00:00
|
|
|
// TODO #15075: on SDK dApps refresh update the model that has data source from persistence instead of using reset
|
2024-06-19 15:17:49 +00:00
|
|
|
dapps.clear();
|
2024-08-05 13:41:20 +00:00
|
|
|
|
|
|
|
// Iterate dAppsMap and fill dapps
|
|
|
|
for (const topic in dAppsMap) {
|
|
|
|
const dAppEntry = dAppsMap[topic];
|
|
|
|
// Due to ListModel converting flat array to empty nested ListModel
|
|
|
|
// having array of key value pair fixes the problem
|
|
|
|
dAppEntry.accountAddresses = dAppEntry.accountAddresses.filter(account => (!!account)).map(account => ({address: account}));
|
|
|
|
dapps.append(dAppEntry);
|
2024-05-16 17:57:37 +00:00
|
|
|
}
|
2024-06-27 14:15:17 +00:00
|
|
|
|
|
|
|
root.store.updateWalletConnectSessions(JSON.stringify(topics))
|
2024-05-16 17:57:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-21 10:42:50 +00:00
|
|
|
if (root.sdk.sdkReady) {
|
|
|
|
getActiveSessionsFn()
|
|
|
|
} else {
|
|
|
|
let conn = root.sdk.sdkReadyChanged.connect(() => {
|
|
|
|
if (root.sdk.sdkReady) {
|
|
|
|
getActiveSessionsFn()
|
|
|
|
}
|
|
|
|
});
|
2024-05-16 17:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-29 14:39:56 +00:00
|
|
|
}
|