Close StickerSetBox on error.

This commit is contained in:
John Preston 2021-06-17 11:06:17 +04:00
parent b6e77537e2
commit e0159e15b2
2 changed files with 41 additions and 12 deletions

View file

@ -63,12 +63,14 @@ public:
bool loaded() const; bool loaded() const;
bool notInstalled() const; bool notInstalled() const;
bool official() const; bool official() const;
rpl::producer<TextWithEntities> title() const; [[nodiscard]] rpl::producer<TextWithEntities> title() const;
QString shortName() const; [[nodiscard]] QString shortName() const;
void install(); void install();
rpl::producer<uint64> setInstalled() const; [[nodiscard]] rpl::producer<uint64> setInstalled() const;
rpl::producer<> updateControls() const; [[nodiscard]] rpl::producer<> updateControls() const;
[[nodiscard]] rpl::producer<Error> errors() const;
~Inner(); ~Inner();
@ -137,6 +139,7 @@ private:
rpl::event_stream<uint64> _setInstalled; rpl::event_stream<uint64> _setInstalled;
rpl::event_stream<> _updateControls; rpl::event_stream<> _updateControls;
rpl::event_stream<Error> _errors;
}; };
@ -186,6 +189,11 @@ void StickerSetBox::prepare() {
_controller->session().api().stickerSetInstalled(setId); _controller->session().api().stickerSetInstalled(setId);
closeBox(); closeBox();
}, lifetime()); }, lifetime());
_inner->errors(
) | rpl::start_with_next([=](Error error) {
handleError(error);
}, lifetime());
} }
void StickerSetBox::addStickers() { void StickerSetBox::addStickers() {
@ -198,6 +206,20 @@ void StickerSetBox::copyStickersLink() {
QGuiApplication::clipboard()->setText(url); 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<InformBox>(tr::lng_stickers_not_found(tr::now)));
break;
default: Unexpected("Error in StickerSetBox::handleError.");
}
}
void StickerSetBox::archiveStickers() { void StickerSetBox::archiveStickers() {
const auto weak = base::make_weak(_controller.get()); const auto weak = base::make_weak(_controller.get());
const auto setId = _set.c_inputStickerSetID().vid().v; const auto setId = _set.c_inputStickerSetID().vid().v;
@ -211,7 +233,7 @@ void StickerSetBox::archiveStickers() {
} }
if (result.type() == mtpc_messages_stickerSetInstallResultSuccess) { if (result.type() == mtpc_messages_stickerSetInstallResultSuccess) {
Ui::Toast::Show(tr::lng_stickers_has_been_archived(tr::now)); Ui::Toast::Show(tr::lng_stickers_has_been_archived(tr::now));
const auto &session = controller->session(); const auto &session = controller->session();
auto &order = session.data().stickers().setsOrderRef(); auto &order = session.data().stickers().setsOrderRef();
const auto index = order.indexOf(setId); const auto index = order.indexOf(setId);
@ -219,10 +241,10 @@ void StickerSetBox::archiveStickers() {
return; return;
} }
order.removeAt(index); order.removeAt(index);
session.local().writeInstalledStickers(); session.local().writeInstalledStickers();
session.local().writeArchivedStickers(); session.local().writeArchivedStickers();
session.data().stickers().notifyUpdated(); session.data().stickers().notifyUpdated();
} }
}).fail([](const MTP::Error &error) { }).fail([](const MTP::Error &error) {
@ -324,7 +346,7 @@ StickerSetBox::Inner::Inner(
gotSet(result); gotSet(result);
}).fail([=](const MTP::Error &error) { }).fail([=](const MTP::Error &error) {
_loaded = true; _loaded = true;
controller->show(Box<InformBox>(tr::lng_stickers_not_found(tr::now))); _errors.fire(Error::NotFound);
}).send(); }).send();
_controller->session().api().updateStickers(); _controller->session().api().updateStickers();
@ -419,8 +441,7 @@ void StickerSetBox::Inner::gotSet(const MTPmessages_StickerSet &set) {
}); });
if (_pack.isEmpty()) { if (_pack.isEmpty()) {
_controller->show( _errors.fire(Error::NotFound);
Box<InformBox>(tr::lng_stickers_not_found(tr::now)));
return; return;
} else { } else {
int32 rows = _pack.size() / kStickersPanelPerRow + ((_pack.size() % kStickersPanelPerRow) ? 1 : 0); int32 rows = _pack.size() / kStickersPanelPerRow + ((_pack.size() % kStickersPanelPerRow) ? 1 : 0);
@ -440,6 +461,10 @@ rpl::producer<> StickerSetBox::Inner::updateControls() const {
return _updateControls.events(); return _updateControls.events();
} }
rpl::producer<StickerSetBox::Error> StickerSetBox::Inner::errors() const {
return _errors.events();
}
void StickerSetBox::Inner::installDone( void StickerSetBox::Inner::installDone(
const MTPmessages_StickerSetInstallResult &result) { const MTPmessages_StickerSetInstallResult &result) {
auto &sets = _controller->session().data().stickers().setsRef(); auto &sets = _controller->session().data().stickers().setsRef();
@ -809,8 +834,7 @@ void StickerSetBox::Inner::install() {
)).done([=](const MTPmessages_StickerSetInstallResult &result) { )).done([=](const MTPmessages_StickerSetInstallResult &result) {
installDone(result); installDone(result);
}).fail([=](const MTP::Error &error) { }).fail([=](const MTP::Error &error) {
_controller->show( _errors.fire(Error::NotFound);
Box<InformBox>(tr::lng_stickers_not_found(tr::now)));
}).send(); }).send();
} }

View file

@ -38,11 +38,16 @@ protected:
void resizeEvent(QResizeEvent *e) override; void resizeEvent(QResizeEvent *e) override;
private: private:
enum class Error {
NotFound,
};
void updateTitleAndButtons(); void updateTitleAndButtons();
void updateButtons(); void updateButtons();
void addStickers(); void addStickers();
void copyStickersLink(); void copyStickersLink();
void archiveStickers(); void archiveStickers();
void handleError(Error error);
const not_null<Window::SessionController*> _controller; const not_null<Window::SessionController*> _controller;
MTPInputStickerSet _set; MTPInputStickerSet _set;