Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@desktop/communities) Permissions, 'who holds' section - handling localiezed amounts #7392

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions ui/app/AppLayouts/Chat/controls/community/CollectiblesPanel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import QtQuick.Layouts 1.14
import StatusQ.Core 0.1
import StatusQ.Core.Theme 0.1
import StatusQ.Controls 0.1
import StatusQ.Controls.Validators 0.1

import shared.controls 1.0

ColumnLayout {
id: root
Expand All @@ -13,10 +14,16 @@ ColumnLayout {
property alias collectibleName: pickerButton.text
property url collectibleImage

property alias amount: amountInput.text
property alias amountText: amountInput.text
property alias amount: amountInput.amount
readonly property bool amountValid: amountInput.valid && amountInput.text.length > 0

signal pickerClicked

function setAmount(amount) {
amountInput.setAmount(amount)
}

spacing: 0

StatusPickerButton {
Expand Down Expand Up @@ -50,30 +57,14 @@ ColumnLayout {
StatusSwitch { id: specificAmountSwitch }
}

StatusInput {
AmountInput {
id: amountInput

visible: specificAmountSwitch.checked

Layout.fillWidth: true
Layout.topMargin: 8
visible: specificAmountSwitch.checked
minimumHeight: 36
maximumHeight: 36
topPadding: 0
bottomPadding: 0
font.pixelSize: 13
rightPadding: amountText.implicitWidth + amountText.anchors.rightMargin + leftPadding
input.placeholderText: "0"
validationMode: StatusInput.ValidationMode.IgnoreInvalidInput
validators: StatusFloatValidator { bottom: 0 }

StatusBaseText {
id: amountText
anchors.right: parent.right
anchors.rightMargin: 13
anchors.verticalCenter: parent.verticalCenter
text: qsTr("Amount")
color: Theme.palette.baseColor1
font.pixelSize: 13
}
allowDecimals: false
}
}
66 changes: 45 additions & 21 deletions ui/app/AppLayouts/Chat/controls/community/HoldingsDropdown.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ StatusDropdown {
property var store

property string tokenKey: ""
property string collectibleKey: ""

property real tokenAmount: 0

property string collectibleKey: ""
property real collectibleAmount: 1
property bool collectiblesSpecificAmount: false

Expand All @@ -36,6 +36,8 @@ StatusDropdown {
function reset() {
d.currentHoldingType = HoldingTypes.Type.Token
d.operator = SQ.Utils.Operators.None
d.tokenAmountText = ""
d.collectibleAmountText = ""

root.tokenKey = ""
root.collectibleKey = ""
Expand Down Expand Up @@ -96,6 +98,9 @@ StatusDropdown {
property int holdingsTabMode: HoldingsTabs.Mode.Add
property int extendedDropdownType: ExtendedDropdownContent.Type.Tokens

property string tokenAmountText: ""
property string collectibleAmountText: ""

property int currentHoldingType: HoldingTypes.Type.Token

property int operator: SQ.Utils.Operators.None
Expand Down Expand Up @@ -279,26 +284,17 @@ StatusDropdown {
id: tokensPanel

tokenName: d.defaultTokenNameText
amount: root.tokenAmount === 0 ? "" : root.tokenAmount.toString()
onAmountChanged: root.tokenAmount = Number(amount)
amountText: d.tokenAmountText
onAmountTextChanged: d.tokenAmountText = amountText

readonly property real effectiveAmount: amountValid ? amount : 0
onEffectiveAmountChanged: root.tokenAmount = effectiveAmount

onPickerClicked: {
d.extendedDropdownType = ExtendedDropdownContent.Type.Tokens
statesStack.push(d.extendedState)
}

Connections {
target: d

function onAddClicked() {
root.addToken(root.tokenKey, root.tokenAmount, d.operator)
}

function onUpdateClicked() {
root.updateToken(root.tokenKey, root.tokenAmount)
}
}

readonly property string tokenKey: root.tokenKey

onTokenKeyChanged: {
Expand All @@ -312,6 +308,23 @@ StatusDropdown {
tokensPanel.tokenImage = ""
}
}

Component.onCompleted: {
if (d.tokenAmountText.length === 0 && root.tokenAmount)
tokensPanel.setAmount(root.tokenAmount)
}

Connections {
target: d

function onAddClicked() {
root.addToken(root.tokenKey, root.tokenAmount, d.operator)
}

function onUpdateClicked() {
root.updateToken(root.tokenKey, root.tokenAmount)
}
}
}
}

Expand All @@ -322,9 +335,11 @@ StatusDropdown {
id: collectiblesPanel

collectibleName: d.defaultCollectibleNameText
amountText: d.collectibleAmountText
onAmountTextChanged: d.collectibleAmountText = amountText

amount: root.collectibleAmount === 0 ? "" : root.collectibleAmount.toString()
onAmountChanged: root.collectibleAmount = Number(amount)
readonly property real effectiveAmount: amountValid ? amount : 0
onEffectiveAmountChanged: root.collectibleAmount = effectiveAmount

specificAmount: root.collectiblesSpecificAmount
onSpecificAmountChanged: root.collectiblesSpecificAmount = specificAmount
Expand All @@ -334,15 +349,24 @@ StatusDropdown {
statesStack.push(d.extendedState)
}

Component.onCompleted: {
if (d.collectibleAmountText.length === 0 && root.collectibleAmount)
collectiblesPanel.setAmount(root.collectibleAmount)
}

function getAmount() {
return specificAmount ? effectiveAmount : 1
}

Connections {
target: d

function onAddClicked() {
root.addCollectible(root.collectibleKey, root.collectibleAmount, d.operator)
root.addCollectible(root.collectibleKey, collectiblesPanel.getAmount(), d.operator)
}

function onUpdateClicked() {
root.updateCollectible(root.collectibleKey, root.collectibleAmount)
root.updateCollectible(root.collectibleKey, collectiblesPanel.getAmount())
}
}

Expand Down Expand Up @@ -416,5 +440,5 @@ StatusDropdown {
}
}
}
}
}
}
32 changes: 10 additions & 22 deletions ui/app/AppLayouts/Chat/controls/community/TokensPanel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ import QtQuick.Layouts 1.14
import StatusQ.Core 0.1
import StatusQ.Core.Theme 0.1
import StatusQ.Controls 0.1
import StatusQ.Controls.Validators 0.1

import shared.controls 1.0

ColumnLayout {
id: root

property alias tokenName: pickerButton.text
property url tokenImage
property alias amount: amountInput.text
property alias amountText: amountInput.text
property alias amount: amountInput.amount
readonly property bool amountValid: amountInput.valid && amountInput.text.length > 0

signal pickerClicked

function setAmount(amount) {
amountInput.setAmount(amount)
}

spacing: 0

StatusPickerButton {
Expand All @@ -31,29 +38,10 @@ ColumnLayout {
onClicked: pickerClicked()
}

StatusInput {
AmountInput {
id: amountInput

Layout.fillWidth: true
Layout.topMargin: 8
minimumHeight: 36
maximumHeight: 36
topPadding: 0
bottomPadding: 0
font.pixelSize: 13
rightPadding: amountText.implicitWidth + amountText.anchors.rightMargin + leftPadding
input.placeholderText: "0"
validationMode: StatusInput.ValidationMode.IgnoreInvalidInput
validators: StatusFloatValidator { bottom: 0 }

StatusBaseText {
id: amountText
anchors.right: parent.right
anchors.rightMargin: 13
anchors.verticalCenter: parent.verticalCenter
text: qsTr("Amount")
color: Theme.palette.baseColor1
font.pixelSize: 13
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Flickable {
switch (type) {
case HoldingTypes.Type.Token:
case HoldingTypes.Type.Collectible:
return qsTr("%1 %2").arg(amount.toString()).arg(name)
return `${LocaleUtils.numberToLocaleString(amount)} ${name}`
case HoldingTypes.Type.Ens:
if (name)
return qsTr("ENS username on '%1' domain").arg(name)
Expand Down Expand Up @@ -182,9 +182,6 @@ Flickable {

const modelItem = tokensSelector.itemsModel.get(index)

dropdown.openFlow(HoldingsDropdown.FlowType.Update)
dropdown.setActiveTab(modelItem.type)

switch(modelItem.type) {
case HoldingTypes.Type.Token:
dropdown.tokenKey = modelItem.key
Expand All @@ -204,6 +201,9 @@ Flickable {
console.warn("Unsupported holdings type.")
}

dropdown.openFlow(HoldingsDropdown.FlowType.Update)
dropdown.setActiveTab(modelItem.type)

editedIndex = index
}
}
Expand Down
81 changes: 81 additions & 0 deletions ui/imports/shared/controls/AmountInput.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import QtQuick 2.14
import QtQuick.Layouts 1.14

import StatusQ.Core 0.1
import StatusQ.Core.Theme 0.1

import utils 1.0

Input {
id: root

property int maximumLength: 10
property var locale: Qt.locale()

readonly property alias amount: d.amount
readonly property bool valid: validationError.length === 0
property bool allowDecimals: true

validationErrorTopMargin: 8
fontPixelSize: 13
customHeight: 36
placeholderText: locale.zeroDigit

textField.rightPadding: labelText.implicitWidth + labelText.anchors.rightMargin
+ textField.leftPadding

function setAmount(amount) {
root.text = LocaleUtils.numberToLocaleString(amount, -1, root.locale)
}

QtObject {
id: d

property real amount: 0
}

validator: DoubleValidator {
id: doubleValidator

decimals: root.allowDecimals ? 100 : 0
bottom: 0
notation: DoubleValidator.StandardNotation
locale: root.locale.name
}

onTextChanged: {
if (!allowDecimals)
text = text.replace(root.locale.decimalPoint, "")
caybro marked this conversation as resolved.
Show resolved Hide resolved

if(text.length === 0) {
d.amount = 0
root.validationError = ""
return
}

if (text.length > root.maximumLength) {
root.validationError = qsTr("The maximum number of characters is %1").arg(root.maximumLength)
return
}

try {
d.amount = Number.fromLocaleString(root.locale, text) || 0
root.validationError = ""
} catch (err) {
root.validationError = qsTr("Invalid amount format")
}
}

StatusBaseText {
id: labelText

parent: root.textField

anchors.right: parent.right
anchors.rightMargin: 13
anchors.verticalCenter: parent.verticalCenter
text: qsTr("Amount")
color: Theme.palette.baseColor1
font.pixelSize: 13
}
}
1 change: 1 addition & 0 deletions ui/imports/shared/controls/qmldir
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
AddressInput 1.0 AddressInput.qml
AmountInput 1.0 AmountInput.qml
AssetAndAmountInput 1.0 AssetAndAmountInput.qml
AssetDelegate 1.0 AssetDelegate.qml
ContactSelector 1.0 ContactSelector.qml
Expand Down
22 changes: 22 additions & 0 deletions ui/imports/utils/LocaleUtils.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pragma Singleton

import QtQml 2.14

QtObject {
caybro marked this conversation as resolved.
Show resolved Hide resolved

function fractionalPartLength(num) {
if (Number.isInteger(num))
return 0

return num.toString().split('.')[1].length
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wonder if separator is always '.'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when using toString than yes, it's not localized

}

function numberToLocaleString(num, precision = -1, locale = null) {
locale = locale || Qt.locale()

if (precision === -1)
precision = fractionalPartLength(num)

return num.toLocaleString(locale, 'f', precision)
}
}
Loading