feat(CPP): Saved addresses code review fixes

This commit is contained in:
MishkaRogachev 2022-09-08 16:24:50 +04:00 committed by Mikhail Rogachev
parent 8c312960cb
commit 95b1333758
12 changed files with 29 additions and 23 deletions

View File

@ -11,8 +11,6 @@
namespace Accounts = Status::StatusGo::Accounts; namespace Accounts = Status::StatusGo::Accounts;
using json = nlohmann::json;
namespace Status::StatusGo::Wallet { namespace Status::StatusGo::Wallet {
/// \brief Define a saved wallet address as returned by the corresponding API /// \brief Define a saved wallet address as returned by the corresponding API

View File

@ -49,9 +49,9 @@ SavedAddresses getSavedAddresses()
return data.is_null() ? nlohmann::json() : data; return data.is_null() ? nlohmann::json() : data;
} }
void saveAddress(const Accounts::EOAddress &address, const QString &name) void saveAddress(const SavedAddress &address)
{ {
std::vector<json> params = { SavedAddress({ address, name }) }; std::vector<json> params = { address };
json inputJson = { json inputJson = {
{"jsonrpc", "2.0"}, {"jsonrpc", "2.0"},
{"method", "wallet_addSavedAddress"}, {"method", "wallet_addSavedAddress"},

View File

@ -33,7 +33,7 @@ SavedAddresses getSavedAddresses();
/// \brief Add a new or update existing saved wallet address /// \brief Add a new or update existing saved wallet address
/// \see \c addSavedAddress /// \see \c addSavedAddress
/// \throws \c CallPrivateRpcError /// \throws \c CallPrivateRpcError
void saveAddress(const Accounts::EOAddress& address, const QString& name); void saveAddress(const SavedAddress &address);
/// \note status-go's GetEthereumChains@api.go which calls /// \note status-go's GetEthereumChains@api.go which calls
/// NetworkManager@client.go -> network.Manager.get() /// NetworkManager@client.go -> network.Manager.get()

View File

@ -4,8 +4,8 @@
#include <QtQmlIntegration> #include <QtQmlIntegration>
namespace Status::Wallet namespace Status::Wallet {
{
class SavedAddress : public QObject class SavedAddress : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -16,7 +16,8 @@ class SavedAddress : public QObject
Q_PROPERTY(QString name READ name CONSTANT) Q_PROPERTY(QString name READ name CONSTANT)
public: public:
SavedAddress(const QString& address = QString(), const QString& name = QString(), QObject *parent = nullptr); SavedAddress(const QString &address = QString(), const QString &name = QString(),
QObject *parent = nullptr);
const QString& address() const; const QString& address() const;
const QString& name() const; const QString& name() const;

View File

@ -15,7 +15,7 @@ class SavedAddressesController : public QObject
QML_ELEMENT QML_ELEMENT
QML_UNCREATABLE("C++ only") QML_UNCREATABLE("C++ only")
Q_PROPERTY(QAbstractListModel* savedAddresses READ savedAddresses CONSTANT) Q_PROPERTY(QAbstractListModel *savedAddresses READ savedAddresses CONSTANT)
public: public:
enum Error enum Error
@ -26,11 +26,11 @@ public:
}; };
Q_ENUM(Error) Q_ENUM(Error)
SavedAddressesController(QObject* parent = nullptr); explicit SavedAddressesController(QObject *parent = nullptr);
QAbstractListModel* savedAddresses() const; QAbstractListModel *savedAddresses() const;
Q_INVOKABLE void saveAddress(const QString& address, const QString& name); Q_INVOKABLE void saveAddress(const QString &address, const QString &name);
Q_INVOKABLE void refresh(); Q_INVOKABLE void refresh();
signals: signals:

View File

@ -37,8 +37,12 @@ public:
/// \note caller (QML) takes ownership of the returned object /// \note caller (QML) takes ownership of the returned object
/// \todo consider if complex approach of keeping ownership here and enforcing a unique instance /// \todo consider if complex approach of keeping ownership here and enforcing a unique instance
/// or not reusing the account list and make it singleton are better options /// or not reusing the account list and make it singleton are better options
Q_INVOKABLE [[nodiscard]] Status::Wallet::NewWalletAccountController* createNewWalletAccountController() const; Q_INVOKABLE [[nodiscard]] Status::Wallet::NewWalletAccountController *createNewWalletAccountController() const;
Q_INVOKABLE [[nodiscard]] Status::Wallet::SavedAddressesController* createSavedAddressesController() const; /// Separated controler for working with wallet saved addresses
/// \note caller (QML) takes ownership of the returned object
/// \todo consider if complex approach of keeping ownership here and enforcing a unique instance
/// or not reusing the saved addresses list and make it singleton are better options
Q_INVOKABLE [[nodiscard]] Status::Wallet::SavedAddressesController *createSavedAddressesController() const;
QAbstractListModel *accountsModel() const; QAbstractListModel *accountsModel() const;

View File

@ -23,6 +23,8 @@ Popup {
TextField { TextField {
id: addressFiled id: addressFiled
maximumLength: 42
validator: RegularExpressionValidator { regularExpression: /^0x[a-fA-F0-9]{40}$/ }
Layout.fillWidth: true Layout.fillWidth: true
} }
@ -38,7 +40,7 @@ Popup {
Button { Button {
text: qsTr("Confirm") text: qsTr("Confirm")
enabled: addressFiled.text.length && nameFiled.text.length enabled: addressFiled.length === addressFiled.maximumLength && nameFiled.text.length
onClicked: { onClicked: {
savedAddressesController.saveAddress(addressFiled.text, nameFiled.text); savedAddressesController.saveAddress(addressFiled.text, nameFiled.text);
addressFiled.clear(); addressFiled.clear();

View File

@ -23,12 +23,14 @@ Item {
Label { Label {
text: qsTr("Address") text: qsTr("Address")
horizontalAlignment: Text.AlignHCenter
font.bold: true font.bold: true
Layout.fillWidth: true Layout.fillWidth: true
} }
Label { Label {
text: qsTr("Name") text: qsTr("Name")
horizontalAlignment: Text.AlignHCenter
font.bold: true font.bold: true
Layout.fillWidth: true Layout.fillWidth: true
} }

View File

@ -12,7 +12,6 @@ Item {
required property WalletAccount account required property WalletAccount account
required property AccountAssetsController assetController required property AccountAssetsController assetController
required property SavedAddressesController savedAddressesController
ColumnLayout { ColumnLayout {
anchors.fill: parent anchors.fill: parent
@ -79,7 +78,7 @@ Item {
Loader { Loader {
active: SwipeView.isCurrentItem active: SwipeView.isCurrentItem
sourceComponent: SavedAddressesView { sourceComponent: SavedAddressesView {
savedAddressesController: root.savedAddressesController savedAddressesController: WalletController.createSavedAddressesController()
} }
} }
} }

View File

@ -38,7 +38,6 @@ PanelAndContentBase {
account: WalletController.currentAccount account: WalletController.currentAccount
assetController: panel.currentAssetController assetController: panel.currentAssetController
savedAddressesController: WalletController.createSavedAddressesController()
} }
} }
} }

View File

@ -3,7 +3,7 @@
namespace Status::Wallet namespace Status::Wallet
{ {
SavedAddress::SavedAddress(const QString& address, const QString& name, QObject *parent) SavedAddress::SavedAddress(const QString &address, const QString &name, QObject *parent)
: QObject(parent) : QObject(parent)
, m_address(address) , m_address(address)
, m_name(name) , m_name(name)

View File

@ -7,13 +7,13 @@
namespace Status::Wallet namespace Status::Wallet
{ {
SavedAddressesController::SavedAddressesController(QObject* parent) SavedAddressesController::SavedAddressesController(QObject *parent)
: QObject(parent) : QObject(parent)
, m_savedAddresses(Helpers::makeSharedQObject<SavedAddressesModel>("savedAddress")) , m_savedAddresses(Helpers::makeSharedQObject<SavedAddressesModel>("savedAddress"))
{ {
} }
QAbstractListModel* SavedAddressesController::savedAddresses() const QAbstractListModel *SavedAddressesController::savedAddresses() const
{ {
return m_savedAddresses.get(); return m_savedAddresses.get();
} }
@ -22,7 +22,8 @@ void SavedAddressesController::saveAddress(const QString &address, const QString
{ {
try try
{ {
StatusGo::Wallet::saveAddress(StatusGo::Accounts::EOAddress(address), name); StatusGo::Wallet::saveAddress(StatusGo::Wallet::SavedAddress(
{ StatusGo::Accounts::EOAddress(address), name }));
} }
catch(const StatusGo::CallPrivateRpcError& rpcError) catch(const StatusGo::CallPrivateRpcError& rpcError)
{ {
@ -41,7 +42,7 @@ void SavedAddressesController::refresh()
try try
{ {
for (const StatusGo::Wallet::SavedAddress& address: StatusGo::Wallet::getSavedAddresses()) for (const auto& address: StatusGo::Wallet::getSavedAddresses())
{ {
savedAddresses.push_back(std::make_shared<SavedAddress>(address.address.get(), address.name)); savedAddresses.push_back(std::make_shared<SavedAddress>(address.address.get(), address.name));
} }