Skip to content

Commit

Permalink
wallet: option to pass widget events to custom handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
valdok committed Jun 19, 2024
1 parent 7957542 commit 95f8971
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
34 changes: 33 additions & 1 deletion wallet/client/wallet_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -699,6 +729,8 @@ namespace beam::wallet
wallet->SetNodeEndpoint(nodeNetwork);
wallet->AddMessageEndpoint(walletNetwork);

wallet->ResumeAllTransactions();

updateMaxPrivacyStatsImpl(getStatus());

auto wallet_subscriber = make_unique<WalletSubscriber>(static_cast<IWalletObserver*>(this), wallet);
Expand Down
1 change: 1 addition & 0 deletions wallet/client/wallet_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ namespace beam::wallet
virtual void onGetChatList(const std::vector<std::pair<beam::wallet::WalletID, bool>>& chats) {}
virtual void onGetChatMessages(const std::vector<InstantMessage>& 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<DexOrder>&) override {}
Expand Down
11 changes: 11 additions & 0 deletions wallet/core/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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)
{
Expand Down
9 changes: 9 additions & 0 deletions wallet/core/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 95f8971

Please sign in to comment.