2024-10-30 15:18:24 +00:00
|
|
|
import QtQuick 2.15
|
|
|
|
|
|
|
|
import StatusQ.Core.Utils 0.1
|
|
|
|
|
|
|
|
QObject {
|
|
|
|
id: root
|
2024-11-07 09:10:10 +00:00
|
|
|
objectName: "DAppsModel"
|
2024-10-30 15:18:24 +00:00
|
|
|
// RoleNames
|
|
|
|
// name: string
|
|
|
|
// url: string
|
|
|
|
// iconUrl: string
|
|
|
|
// topic: string
|
|
|
|
// connectorId: int
|
|
|
|
// accountAddressses: [{address: string}]
|
|
|
|
// chains: string
|
|
|
|
// rawSessions: [{session: object}]
|
|
|
|
readonly property ListModel model: ListModel {}
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
// Appending a new DApp to the model
|
|
|
|
// Required properties: url, topic, connectorId, accountAddresses
|
|
|
|
// Optional properties: name, iconUrl, chains, rawSessions
|
2024-10-30 15:18:24 +00:00
|
|
|
function append(dapp) {
|
|
|
|
try {
|
2024-11-07 09:10:10 +00:00
|
|
|
let {name, url, iconUrl, topic, accountAddresses, connectorId, rawSessions } = dapp
|
|
|
|
if (!url || !topic || !connectorId || !accountAddresses) {
|
2024-10-30 15:18:24 +00:00
|
|
|
console.warn("DAppsModel - Failed to append dapp, missing required fields", JSON.stringify(dapp))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
name = name || ""
|
|
|
|
iconUrl = iconUrl || ""
|
|
|
|
accountAddresses = accountAddresses || []
|
|
|
|
rawSessions = rawSessions || []
|
|
|
|
|
2024-10-30 15:18:24 +00:00
|
|
|
root.model.append({
|
|
|
|
name,
|
|
|
|
url,
|
|
|
|
iconUrl,
|
|
|
|
topic,
|
|
|
|
connectorId,
|
|
|
|
accountAddresses,
|
|
|
|
rawSessions
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
console.warn("DAppsModel - Failed to append dapp", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove(topic) {
|
2024-11-07 09:10:10 +00:00
|
|
|
const { dapp, index, sessionIndex } = findDapp(topic)
|
|
|
|
if (!dapp) {
|
|
|
|
console.warn("DAppsModel - Failed to remove dapp, not found", topic)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dapp.rawSessions.count === 1) {
|
|
|
|
root.model.remove(index)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const rawSession = dapp.rawSessions.get(sessionIndex)
|
|
|
|
dapp.rawSessions.remove(sessionIndex)
|
|
|
|
if (rawSession.topic == dapp.topic) {
|
|
|
|
root.model.setProperty(index, "topic", dapp.rawSessions.get(0).topic)
|
2024-10-30 15:18:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear() {
|
|
|
|
root.model.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
function getByTopic(topic) {
|
2024-11-07 09:10:10 +00:00
|
|
|
const dappTemplate = (dapp) => {
|
|
|
|
return {
|
|
|
|
name: dapp.name,
|
|
|
|
url: dapp.url,
|
|
|
|
iconUrl: dapp.iconUrl,
|
|
|
|
topic: dapp.topic,
|
|
|
|
connectorId: dapp.connectorId,
|
|
|
|
accountAddresses: dapp.accountAddresses,
|
|
|
|
rawSessions: dapp.rawSessions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const { dapp } = findDapp(topic)
|
|
|
|
if (!dapp) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return dappTemplate(dapp)
|
|
|
|
}
|
|
|
|
|
|
|
|
function findDapp(topic) {
|
2024-10-30 15:18:24 +00:00
|
|
|
for (let i = 0; i < root.model.count; i++) {
|
|
|
|
const dapp = root.model.get(i)
|
2024-11-07 09:10:10 +00:00
|
|
|
for (let j = 0; j < dapp.rawSessions.count; j++) {
|
|
|
|
if (dapp.rawSessions.get(j).topic == topic) {
|
|
|
|
return { dapp, index: i, sessionIndex: j }
|
|
|
|
break
|
2024-10-30 15:18:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
|
|
|
|
return { dapp: null, index: -1, sessionIndex: -1 }
|
2024-10-30 15:18:24 +00:00
|
|
|
}
|
|
|
|
}
|