From 95f897165814c4ae8c75360bf19a5df1edd86770 Mon Sep 17 00:00:00 2001 From: valdok Date: Wed, 19 Jun 2024 13:42:13 +0300 Subject: [PATCH] wallet: option to pass widget events to custom handlers --- wallet/client/wallet_client.cpp | 34 ++++++++++++++++++++++++++++++++- wallet/client/wallet_client.h | 1 + wallet/core/wallet.cpp | 11 +++++++++++ wallet/core/wallet.h | 9 +++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/wallet/client/wallet_client.cpp b/wallet/client/wallet_client.cpp index 6214bab84..7713e89e0 100644 --- a/wallet/client/wallet_client.cpp +++ b/wallet/client/wallet_client.cpp @@ -681,7 +681,37 @@ namespace beam::wallet } } - wallet->ResumeAllTransactions(); + struct MyWidgetNotify :public Wallet::IWidgetNotify { + WalletClient& m_This; + MyWidgetNotify(WalletClient& x) :m_This(x) + { + auto w = m_This.m_wallet.lock(); + if (w) + w->m_pWidgetNotify = this; + } + ~MyWidgetNotify() + { + auto w = m_This.m_wallet.lock(); + if (w) + w->m_pWidgetNotify = nullptr; + } + + void OnWriteStream(const std::string& s, const Blob& b, uint32_t iStream) override + { + ByteBuffer buf; + b.Export(buf); + WalletClient& wc = m_This; + + m_This.postFunctionToClientContext([&wc, buf1 = std::move(buf), sName = s, iStream]() + { + std::string s2 = std::move(sName); + ByteBuffer buf2 = std::move(buf1); + wc.onWidgetWrite(std::move(s2), std::move(buf2), iStream); + }); + + } + + } wn(*this); updateClientState(getStatus()); @@ -699,6 +729,8 @@ namespace beam::wallet wallet->SetNodeEndpoint(nodeNetwork); wallet->AddMessageEndpoint(walletNetwork); + wallet->ResumeAllTransactions(); + updateMaxPrivacyStatsImpl(getStatus()); auto wallet_subscriber = make_unique(static_cast(this), wallet); diff --git a/wallet/client/wallet_client.h b/wallet/client/wallet_client.h index 675b85bf3..73b09f48f 100644 --- a/wallet/client/wallet_client.h +++ b/wallet/client/wallet_client.h @@ -225,6 +225,7 @@ namespace beam::wallet virtual void onGetChatList(const std::vector>& chats) {} virtual void onGetChatMessages(const std::vector& messages) {} virtual void onChatRemoved(const WalletID& counterpart) {} + virtual void onWidgetWrite(std::string&&, ByteBuffer&&, uint32_t iStream) {} #ifdef BEAM_ASSET_SWAP_SUPPORT void onDexOrdersChanged(ChangeAction, const std::vector&) override {} diff --git a/wallet/core/wallet.cpp b/wallet/core/wallet.cpp index 2a3ca3573..9d88bcea0 100644 --- a/wallet/core/wallet.cpp +++ b/wallet/core/wallet.cpp @@ -376,6 +376,9 @@ namespace beam::wallet pVal->m_BodyManager = std::move(buf); + if (w.m_pWidgetNotify) + w.m_pWidgetNotify->OnCreate(pVal->m_Key); + return pVal; } @@ -442,7 +445,12 @@ namespace beam::wallet auto pVal = w.Get(sName); if (pVal) + { + if (m_pWidgetNotify) + m_pWidgetNotify->OnDelete(sName); + w.m_Set.Delete(*pVal); + } if (x.empty()) { @@ -2298,6 +2306,9 @@ namespace beam::wallet void Wallet::WidgetRunner::Entry::WriteStream(const Blob& b, uint32_t iStream) { + if (m_Wallet.m_pWidgetNotify) + m_Wallet.m_pWidgetNotify->OnWriteStream(m_Key, b, iStream); + const char* szPrefix = ""; switch (iStream) { diff --git a/wallet/core/wallet.h b/wallet/core/wallet.h index bb0490557..c1e79492a 100644 --- a/wallet/core/wallet.h +++ b/wallet/core/wallet.h @@ -166,6 +166,15 @@ namespace beam::wallet void SetWidget(std::string&&, ByteBuffer&&); + struct IWidgetNotify + { + virtual void OnCreate(const std::string&) {} + virtual void OnDelete(const std::string&) {} + virtual void OnWriteStream(const std::string&, const Blob& b, uint32_t iStream) {} + }; + + IWidgetNotify* m_pWidgetNotify = nullptr; + bool IsWalletInSync() const; Height get_TipHeight() const;