From e0159e15b281721bf7ceb3c05e77011340eef4f8 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 17 Jun 2021 11:06:17 +0400 Subject: [PATCH] Close StickerSetBox on error. --- .../SourceFiles/boxes/sticker_set_box.cpp | 48 ++++++++++++++----- Telegram/SourceFiles/boxes/sticker_set_box.h | 5 ++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/boxes/sticker_set_box.cpp b/Telegram/SourceFiles/boxes/sticker_set_box.cpp index b9f26c330..19db66a31 100644 --- a/Telegram/SourceFiles/boxes/sticker_set_box.cpp +++ b/Telegram/SourceFiles/boxes/sticker_set_box.cpp @@ -63,12 +63,14 @@ public: bool loaded() const; bool notInstalled() const; bool official() const; - rpl::producer title() const; - QString shortName() const; + [[nodiscard]] rpl::producer title() const; + [[nodiscard]] QString shortName() const; void install(); - rpl::producer setInstalled() const; - rpl::producer<> updateControls() const; + [[nodiscard]] rpl::producer setInstalled() const; + [[nodiscard]] rpl::producer<> updateControls() const; + + [[nodiscard]] rpl::producer errors() const; ~Inner(); @@ -137,6 +139,7 @@ private: rpl::event_stream _setInstalled; rpl::event_stream<> _updateControls; + rpl::event_stream _errors; }; @@ -186,6 +189,11 @@ void StickerSetBox::prepare() { _controller->session().api().stickerSetInstalled(setId); closeBox(); }, lifetime()); + + _inner->errors( + ) | rpl::start_with_next([=](Error error) { + handleError(error); + }, lifetime()); } void StickerSetBox::addStickers() { @@ -198,6 +206,20 @@ void StickerSetBox::copyStickersLink() { QGuiApplication::clipboard()->setText(url); } +void StickerSetBox::handleError(Error error) { + const auto guard = gsl::finally(crl::guard(this, [=] { + closeBox(); + })); + + switch (error) { + case Error::NotFound: + _controller->show( + Box(tr::lng_stickers_not_found(tr::now))); + break; + default: Unexpected("Error in StickerSetBox::handleError."); + } +} + void StickerSetBox::archiveStickers() { const auto weak = base::make_weak(_controller.get()); const auto setId = _set.c_inputStickerSetID().vid().v; @@ -211,7 +233,7 @@ void StickerSetBox::archiveStickers() { } if (result.type() == mtpc_messages_stickerSetInstallResultSuccess) { Ui::Toast::Show(tr::lng_stickers_has_been_archived(tr::now)); - + const auto &session = controller->session(); auto &order = session.data().stickers().setsOrderRef(); const auto index = order.indexOf(setId); @@ -219,10 +241,10 @@ void StickerSetBox::archiveStickers() { return; } order.removeAt(index); - + session.local().writeInstalledStickers(); session.local().writeArchivedStickers(); - + session.data().stickers().notifyUpdated(); } }).fail([](const MTP::Error &error) { @@ -324,7 +346,7 @@ StickerSetBox::Inner::Inner( gotSet(result); }).fail([=](const MTP::Error &error) { _loaded = true; - controller->show(Box(tr::lng_stickers_not_found(tr::now))); + _errors.fire(Error::NotFound); }).send(); _controller->session().api().updateStickers(); @@ -419,8 +441,7 @@ void StickerSetBox::Inner::gotSet(const MTPmessages_StickerSet &set) { }); if (_pack.isEmpty()) { - _controller->show( - Box(tr::lng_stickers_not_found(tr::now))); + _errors.fire(Error::NotFound); return; } else { int32 rows = _pack.size() / kStickersPanelPerRow + ((_pack.size() % kStickersPanelPerRow) ? 1 : 0); @@ -440,6 +461,10 @@ rpl::producer<> StickerSetBox::Inner::updateControls() const { return _updateControls.events(); } +rpl::producer StickerSetBox::Inner::errors() const { + return _errors.events(); +} + void StickerSetBox::Inner::installDone( const MTPmessages_StickerSetInstallResult &result) { auto &sets = _controller->session().data().stickers().setsRef(); @@ -809,8 +834,7 @@ void StickerSetBox::Inner::install() { )).done([=](const MTPmessages_StickerSetInstallResult &result) { installDone(result); }).fail([=](const MTP::Error &error) { - _controller->show( - Box(tr::lng_stickers_not_found(tr::now))); + _errors.fire(Error::NotFound); }).send(); } diff --git a/Telegram/SourceFiles/boxes/sticker_set_box.h b/Telegram/SourceFiles/boxes/sticker_set_box.h index a5c105531..711415190 100644 --- a/Telegram/SourceFiles/boxes/sticker_set_box.h +++ b/Telegram/SourceFiles/boxes/sticker_set_box.h @@ -38,11 +38,16 @@ protected: void resizeEvent(QResizeEvent *e) override; private: + enum class Error { + NotFound, + }; + void updateTitleAndButtons(); void updateButtons(); void addStickers(); void copyStickersLink(); void archiveStickers(); + void handleError(Error error); const not_null _controller; MTPInputStickerSet _set;