[Mint Token] Update validation rule for name property

Fixes checking for duplicate token name in a case insensitive manner

Redo the `ModelUtils.contains(model, roleName, value, mode =
Qt.CaseSensitive)` in C++; more speed and add ability to search case in/
sensitive

Some more smaller fixes/speedups

Fixes #11204
This commit is contained in:
Lukáš Tinkl 2023-06-27 21:20:32 +02:00 committed by Lukáš Tinkl
parent 2ff798b686
commit 9107f15a77
5 changed files with 25 additions and 17 deletions

View File

@ -34,7 +34,7 @@ SplitView {
enabledNetworks: NetworksModel.enabledNetworks enabledNetworks: NetworksModel.enabledNetworks
allNetworks: enabledNetworks allNetworks: enabledNetworks
accounts: WalletAccountsModel {} accounts: WalletAccountsModel {}
tokensModel: MintedTokensModel.mintedTokensModel tokensModel: isAssetBox.checked ? MintedTokensModel.mintedAssetsModel : MintedTokensModel.mintedCollectiblesModel
onPreviewClicked: logs.logEvent("CommunityNewTokenView::previewClicked") onPreviewClicked: logs.logEvent("CommunityNewTokenView::previewClicked")
} }

View File

@ -24,6 +24,8 @@ public:
Q_INVOKABLE QVariant get(QAbstractItemModel *model, int row, Q_INVOKABLE QVariant get(QAbstractItemModel *model, int row,
const QString &roleName) const; const QString &roleName) const;
Q_INVOKABLE bool contains(QAbstractItemModel *model, const QString &roleName, const QVariant &value, int mode = Qt::CaseSensitive) const;
static QObject* qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine) static QObject* qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
{ {
Q_UNUSED(engine); Q_UNUSED(engine);

View File

@ -55,8 +55,8 @@ QtObject {
return -1 return -1
} }
function contains(model, role, key) { function contains(model, roleName, value, mode = Qt.CaseSensitive) {
return indexOf(model, role, key) !== -1 return Internal.ModelUtils.contains(model, roleName, value, mode)
} }
function checkItemsEquality(itemA, itemB, roles) { function checkItemsEquality(itemA, itemB, roles) {

View File

@ -12,15 +12,8 @@ QStringList ModelUtilsInternal::roleNames(QAbstractItemModel *model) const
if (model == nullptr) if (model == nullptr)
return {}; return {};
QHash<int, QByteArray> roles = model->roleNames(); const auto roles = model->roleNames();
return {roles.cbegin(), roles.cend()};
QStringList strings;
strings.reserve(roles.size());
for (auto it = roles.begin(); it != roles.end(); ++it)
strings << QString::fromUtf8(it.value());
return strings;
} }
@ -40,8 +33,8 @@ QVariantMap ModelUtilsInternal::get(QAbstractItemModel *model, int row) const
if (model == nullptr) if (model == nullptr)
return map; return map;
QModelIndex modelIndex = model->index(row, 0); const auto modelIndex = model->index(row, 0);
QHash<int, QByteArray> roles = model->roleNames(); const auto roles = model->roleNames();
for (auto it = roles.begin(); it != roles.end(); ++it) for (auto it = roles.begin(); it != roles.end(); ++it)
map.insert(it.value(), model->data(modelIndex, it.key())); map.insert(it.value(), model->data(modelIndex, it.key()));
@ -54,3 +47,16 @@ QVariant ModelUtilsInternal::get(QAbstractItemModel *model,
{ {
return model->data(model->index(row, 0), roleByName(model, roleName)); return model->data(model->index(row, 0), roleByName(model, roleName));
} }
bool ModelUtilsInternal::contains(QAbstractItemModel* model,
const QString& roleName,
const QVariant& value,
int mode) const
{
if(!model) return false;
Qt::MatchFlags flags = Qt::MatchFixedString; // Qt::CaseInsensitive by default
if(mode == Qt::CaseSensitive) flags |= Qt::MatchCaseSensitive;
const auto indexes = model->match(model->index(0, 0), roleByName(model, roleName), value, 1, flags);
return !indexes.isEmpty();
}

View File

@ -127,14 +127,14 @@ StatusScrollView {
minLengthValidator.errorMessage: qsTr("Please name your token name (use A-Z and 0-9, hyphens and underscores only)") minLengthValidator.errorMessage: qsTr("Please name your token name (use A-Z and 0-9, hyphens and underscores only)")
regexValidator.errorMessage: qsTr("Your token name contains invalid characters (use A-Z and 0-9, hyphens and underscores only)") regexValidator.errorMessage: qsTr("Your token name contains invalid characters (use A-Z and 0-9, hyphens and underscores only)")
extraValidator.validate: function (value) { extraValidator.validate: function (value) {
// If minted failed, we can retry same deployment, so same name allowed // If minting failed, we can retry same deployment, so same name allowed
var allowRepeatedName = (root.isAssetView ? asset.deployState : collectible.deployState) === Constants.ContractTransactionStatus.Failed const allowRepeatedName = (root.isAssetView ? asset.deployState : collectible.deployState) === Constants.ContractTransactionStatus.Failed
if(allowRepeatedName) if(allowRepeatedName)
if(nameInput.text === root.referenceName) if(nameInput.text === root.referenceName)
return true return true
// Otherwise, no repeated names allowed: // Otherwise, no repeated names allowed:
return !SQUtils.ModelUtils.contains(root.tokensModel, "name", nameInput.text) return !SQUtils.ModelUtils.contains(root.tokensModel, "name", nameInput.text, Qt.CaseInsensitive)
} }
extraValidator.errorMessage: qsTr("You have used this token name before") extraValidator.errorMessage: qsTr("You have used this token name before")