status-desktop/ui/app/AppLayouts/Profile/helpers/ProfileShowcaseModels.qml

262 lines
7.7 KiB
QML
Raw Normal View History

import QtQml 2.15
import StatusQ 0.1
import StatusQ.Core.Utils 0.1
import SortFilterProxyModel 0.2
import utils 1.0
QObject {
id: root
// GENERAL
readonly property bool dirty: communities.dirty || accounts.dirty || collectibles.dirty || socialLinks.dirty
function revert() {
communities.revert()
accounts.revert()
collectibles.revert()
socialLinks.revert()
}
// COMMUNITIES
// Input models
property alias communitiesSourceModel: communities.sourceModel
property string communitiesSearcherText
// Output models
readonly property alias communitiesVisibleModel: communities.visibleModel
readonly property alias communitiesHiddenModel: communities.hiddenModel
// Methods
function communitiesCurrentState() {
return communities.currentState()
}
function setCommunityVisibility(key, visibility) {
communities.setVisibility(key, visibility)
}
function changeCommunityPosition(key, to) {
communities.changePosition(key, to)
}
// ACCOUNTS
// Input models
property alias accountsSourceModel: accounts.sourceModel
property string accountsSearcherText
// Output models
readonly property alias accountsVisibleModel: accounts.visibleModel
readonly property alias accountsHiddenModel: accounts.hiddenModel
// Methods
function accountsCurrentState() {
return accounts.currentState()
}
function setAccountVisibility(key, visibility) {
accounts.setVisibility(key, visibility)
}
function changeAccountPosition(key, to) {
accounts.changePosition(key, to)
}
// Other
readonly property alias visibleAccountsList:
visibleAccountsConnections.visibleAccountsList
readonly property alias accountsVisibilityMap:
visibleAccountsConnections.accountsVisibilityMap
// COLLECTIBLES
// Input models
property alias collectiblesSourceModel: collectiblesFilter.sourceModel
property string collectiblesSearcherText
// Output models
readonly property alias collectiblesVisibleModel: collectibles.visibleModel
readonly property alias collectiblesHiddenModel: collectibles.hiddenModel
// Methods
function collectiblesCurrentState() {
return collectibles.currentState()
}
function setCollectibleVisibility(key, visibility) {
collectibles.setVisibility(key, visibility)
}
function changeCollectiblePosition(from, to) {
collectibles.changePosition(from, to)
}
// SOCIAL LINKS
// Input models
property alias socialLinksSourceModel: socialLinks.sourceModel
// Output models
readonly property alias socialLinksVisibleModel: socialLinks.visibleModel
// Methods
function appendSocialLink(obj) {
socialLinks.append(obj)
}
function updateSocialLink(index, obj) {
socialLinks.update(index, obj)
}
function removeSocialLink(index) {
socialLinks.remove(index)
}
function changeSocialLinkPosition(from, to) {
socialLinks.changePosition(from, to)
}
function socialLinksCurrentState(roleNames) {
return socialLinks.currentState(roleNames)
}
// The complete preferences models json current state:
function buildJSONModelsCurrentState() {
return JSON.stringify({
"communities": communitiesCurrentState(),
"accounts": accountsCurrentState(),
"collectibles": collectiblesCurrentState(),
"socialLinks": socialLinksCurrentState(["url", "text", "showcaseVisibility", "showcasePosition"])
// TODO: Assets --> Issue #13492
})
}
ProfileShowcaseDirtyState {
id: communities
function getMemberRole(memberRole) {
return ProfileUtils.getMemberRoleText(memberRole)
}
searcherFilter: FastExpressionFilter {
expression: {
root.communitiesSearcherText
return (name.toLowerCase().includes(root.communitiesSearcherText.toLowerCase()) ||
communities.getMemberRole(memberRole).toLowerCase().includes(root.communitiesSearcherText.toLowerCase()))
}
expectedRoles: ["name", "memberRole"]
}
}
ProfileShowcaseDirtyState {
id: accounts
searcherFilter: FastExpressionFilter {
expression: {
root.accountsSearcherText
return (address.toLowerCase().includes(root.accountsSearcherText.toLowerCase()) ||
name.toLowerCase().includes(root.accountsSearcherText.toLowerCase()) ||
preferredSharingChainShortNames.toLowerCase().includes(root.accountsSearcherText.toLowerCase()))
}
expectedRoles: ["address", "name", "preferredSharingChainShortNames"]
}
}
ProfileShowcaseDirtyState {
id: collectibles
sourceModel: collectiblesFilter
searcherFilter: FastExpressionFilter {
expression: {
root.collectiblesSearcherText
return (name.toLowerCase().includes(root.collectiblesSearcherText.toLowerCase()) ||
uid.toLowerCase().includes(root.collectiblesSearcherText.toLowerCase()) ||
communityName.toLowerCase().includes(root.collectiblesSearcherText.toLowerCase()) ||
collectionName.toLowerCase().includes(root.collectiblesSearcherText.toLowerCase()))
}
expectedRoles: ["name", "uid", "collectionName", "communityName"]
}
}
ProfileShowcaseDirtyState {
id: socialLinks
}
SortFilterProxyModel {
id: collectiblesFilter
delayed: true
proxyRoles: FastExpressionRole {
name: "maxVisibility"
// singletons cannot be used in expressions
readonly property int hidden: Constants.ShowcaseVisibility.NoOne
function getMaxVisibility(ownershipModel) {
const visibilityMap = root.accountsVisibilityMap
const accounts = ModelUtils.modelToFlatArray(ownershipModel, "accountAddress")
const visibilities = accounts.map(a => visibilityMap[a.toLowerCase()]).filter(
v => v !== undefined)
return visibilities.length ? Math.min(...visibilities) : hidden
}
expression: {
return getMaxVisibility(model.ownership)
}
expectedRoles: ["ownership"]
}
filters: ValueFilter {
roleName: "maxVisibility"
value: Constants.ShowcaseVisibility.NoOne
inverted: true
}
}
Connections {
id: visibleAccountsConnections
target: accounts.visibleModel
property var visibleAccountsList: []
property var accountsVisibilityMap: ({})
function updateAccountsList() {
const keysAndVisibility = ModelUtils.modelToArray(
accounts.visibleModel, ["showcaseKey", "showcaseVisibility"])
visibleAccountsList = keysAndVisibility.map(e => e.showcaseKey)
accountsVisibilityMap = keysAndVisibility.reduce(
(acc, val) => Object.assign(
acc, {[val.showcaseKey]: val.showcaseVisibility}), {})
}
function onDataChanged() {
updateAccountsList()
}
function onRowsInserted() {
updateAccountsList()
}
function onRowsRemoved() {
updateAccountsList()
}
function onModelReset() {
updateAccountsList()
}
Component.onCompleted: updateAccountsList()
}
}