diff --git a/td/telegram/BackgroundManager.cpp b/td/telegram/BackgroundManager.cpp index 608b77e70..f280b75fe 100644 --- a/td/telegram/BackgroundManager.cpp +++ b/td/telegram/BackgroundManager.cpp @@ -93,16 +93,12 @@ class GetBackgroundsQuery : public Td::ResultHandler { class InstallBackgroundQuery : public Td::ResultHandler { Promise promise_; - BackgroundId background_id_; - BackgroundType type_; public: explicit InstallBackgroundQuery(Promise &&promise) : promise_(std::move(promise)) { } void send(BackgroundId background_id, int64 access_hash, const BackgroundType &type) { - background_id_ = background_id; - type_ = type; send_query(G()->net_query_creator().create(create_storer(telegram_api::account_installWallPaper( telegram_api::make_object(background_id.get(), access_hash), get_input_wallpaper_settings(type))))); @@ -114,7 +110,6 @@ class InstallBackgroundQuery : public Td::ResultHandler { return on_error(id, result_ptr.move_as_error()); } - td->background_manager_->set_background_id(background_id_, type_); LOG_IF(INFO, !result_ptr.ok()) << "Receive false from account.installWallPaper"; promise_.set_value(Unit()); } @@ -528,10 +523,30 @@ BackgroundId BackgroundManager::set_background(BackgroundId background_id, const } LOG(INFO) << "Install " << background_id << " with " << type; - td_->create_handler(std::move(promise))->send(background_id, background->access_hash, type); + auto query_promise = PromiseCreator::lambda( + [actor_id = actor_id(this), background_id, type, promise = std::move(promise)](Result &&result) mutable { + send_closure(actor_id, &BackgroundManager::on_installed_background, background_id, type, std::move(result), + std::move(promise)); + }); + td_->create_handler(std::move(query_promise)) + ->send(background_id, background->access_hash, type); return BackgroundId(); } +void BackgroundManager::on_installed_background(BackgroundId background_id, BackgroundType type, Result &&result, + Promise &&promise) { + if (result.is_error()) { + return promise.set_error(result.move_as_error()); + } + + auto it = std::find(installed_background_ids_.begin(), installed_background_ids_.end(), background_id); + if (it == installed_background_ids_.end()) { + installed_background_ids_.insert(installed_background_ids_.begin(), background_id); + } + set_background_id(background_id, type); + promise.set_value(Unit()); +} + string BackgroundManager::get_background_database_key() { return "bg"; } diff --git a/td/telegram/BackgroundManager.h b/td/telegram/BackgroundManager.h index 79060c3ae..fb2bd5094 100644 --- a/td/telegram/BackgroundManager.h +++ b/td/telegram/BackgroundManager.h @@ -43,8 +43,6 @@ class BackgroundManager : public Actor { BackgroundId set_background(const td_api::InputBackground *input_background, const td_api::BackgroundType *background_type, Promise &&promise); - void set_background_id(BackgroundId background_id, const BackgroundType &type); - void remove_background(BackgroundId background_id, Promise &&promise); td_api::object_ptr get_background_object(BackgroundId background_id) const; @@ -109,6 +107,11 @@ class BackgroundManager : public Actor { BackgroundId set_background(BackgroundId background_id, const BackgroundType &type, Promise &&promise); + void on_installed_background(BackgroundId background_id, BackgroundType type, Result &&result, + Promise &&promise); + + void set_background_id(BackgroundId background_id, const BackgroundType &type); + void on_removed_background(BackgroundId background_id, Result &&result, Promise &&promise); void upload_background_file(FileId file_id, const BackgroundType &type, Promise &&promise);