Moved out ui callbacks of bar from data class from sponsored messages.

This commit is contained in:
23rd 2024-10-30 06:37:58 +03:00
parent 30dae049ff
commit 6237675744
3 changed files with 70 additions and 42 deletions

View file

@ -278,38 +278,52 @@ void SponsoredMessages::parse(
}); });
} }
void SponsoredMessages::fillTopBar( FullMsgId SponsoredMessages::fillTopBar(
not_null<History*> history, not_null<History*> history,
not_null<Ui::RpWidget*> widget, not_null<Ui::RpWidget*> widget) {
Fn<void()> hide) {
const auto it = _data.find(history); const auto it = _data.find(history);
if (it == end(_data)) { if (it != end(_data)) {
return;
}
auto &list = it->second; auto &list = it->second;
if (list.entries.empty()) { if (!list.entries.empty()) {
return; const auto &entry = list.entries.front();
}
auto &entry = list.entries.front();
if (!entry.optionalDestructionNotifier) {
entry.optionalDestructionNotifier = std::make_unique<rpl::lifetime>();
}
const auto fullId = entry.itemFullId; const auto fullId = entry.itemFullId;
entry.optionalDestructionNotifier->add(std::move(hide));
Ui::FillSponsoredMessageBar( Ui::FillSponsoredMessageBar(
widget, widget,
_session, _session,
fullId, fullId,
entry.sponsored.from, entry.sponsored.from,
entry.sponsored.textWithEntities); entry.sponsored.textWithEntities);
return fullId;
}
}
return {};
}
const auto viewLifetime = widget->lifetime().make_state<rpl::lifetime>(); rpl::producer<> SponsoredMessages::itemRemoved(const FullMsgId &fullId) {
widget->shownValue() | rpl::filter( if (IsServerMsgId(fullId.msg) || !fullId) {
rpl::mappers::_1 return rpl::never<>();
) | rpl::start_with_next([=, this](bool shown) { }
view(fullId); const auto history = _session->data().history(fullId.peer);
viewLifetime->destroy(); const auto it = _data.find(history);
}, *viewLifetime); if (it == end(_data)) {
return rpl::never<>();
}
auto &list = it->second;
const auto entryIt = ranges::find_if(list.entries, [&](const Entry &e) {
return e.itemFullId == fullId;
});
if (entryIt == end(list.entries)) {
return rpl::never<>();
}
if (!entryIt->optionalDestructionNotifier) {
entryIt->optionalDestructionNotifier
= std::make_unique<rpl::lifetime>();
entryIt->optionalDestructionNotifier->add([this, fullId] {
_itemRemoved.fire_copy(fullId);
});
}
return _itemRemoved.events(
) | rpl::filter(rpl::mappers::_1 == fullId) | rpl::to_empty;
} }
void SponsoredMessages::append( void SponsoredMessages::append(

View file

@ -104,10 +104,10 @@ public:
void clearItems(not_null<History*> history); void clearItems(not_null<History*> history);
[[nodiscard]] Details lookupDetails(const FullMsgId &fullId) const; [[nodiscard]] Details lookupDetails(const FullMsgId &fullId) const;
void clicked(const FullMsgId &fullId, bool isMedia, bool isFullscreen); void clicked(const FullMsgId &fullId, bool isMedia, bool isFullscreen);
void fillTopBar( [[nodiscard]] FullMsgId fillTopBar(
not_null<History*> history, not_null<History*> history,
not_null<Ui::RpWidget*> widget, not_null<Ui::RpWidget*> widget);
Fn<void()> hide); [[nodiscard]] rpl::producer<> itemRemoved(const FullMsgId &);
[[nodiscard]] AppendResult append(not_null<History*> history); [[nodiscard]] AppendResult append(not_null<History*> history);
void inject( void inject(
@ -167,6 +167,8 @@ private:
base::flat_map<not_null<History*>, Request> _requests; base::flat_map<not_null<History*>, Request> _requests;
base::flat_map<RandomId, Request> _viewRequests; base::flat_map<RandomId, Request> _viewRequests;
rpl::event_stream<FullMsgId> _itemRemoved;
rpl::lifetime _lifetime; rpl::lifetime _lifetime;
}; };

View file

@ -7648,21 +7648,33 @@ void HistoryWidget::createSponsoredMessageBar() {
object_ptr<Ui::RpWidget>(this)); object_ptr<Ui::RpWidget>(this));
_sponsoredMessageBar->entity()->resizeToWidth(_scroll->width()); _sponsoredMessageBar->entity()->resizeToWidth(_scroll->width());
auto destruction = [this] { const auto maybeFullId = session().sponsoredMessages().fillTopBar(
if (_sponsoredMessageBar) {
_sponsoredMessageBar->toggle(false, anim::type::normal);
_sponsoredMessageBar->shownValue(
) | rpl::start_with_next([=](bool shown) {
if (!shown) {
_sponsoredMessageBar = nullptr;
}
}, _sponsoredMessageBar->lifetime());
}
};
session().sponsoredMessages().fillTopBar(
_history, _history,
_sponsoredMessageBar->entity(), _sponsoredMessageBar->entity());
std::move(destruction)); session().sponsoredMessages().itemRemoved(
maybeFullId
) | rpl::start_with_next([this] {
_sponsoredMessageBar->shownValue() | rpl::filter(
!rpl::mappers::_1
) | rpl::start_with_next([this] {
_sponsoredMessageBar = nullptr;
}, _sponsoredMessageBar->lifetime());
_sponsoredMessageBar->toggle(false, anim::type::normal);
}, _sponsoredMessageBar->lifetime());
if (maybeFullId) {
const auto viewLifetime
= _sponsoredMessageBar->lifetime().make_state<rpl::lifetime>();
rpl::combine(
_sponsoredMessageBar->entity()->heightValue(),
_sponsoredMessageBar->heightValue()
) | rpl::filter(
rpl::mappers::_1 == rpl::mappers::_2
) | rpl::start_with_next([=] {
session().sponsoredMessages().view(maybeFullId);
viewLifetime->destroy();
}, *viewLifetime);
}
_sponsoredMessageBarHeight = 0; _sponsoredMessageBarHeight = 0;
_sponsoredMessageBar->heightValue( _sponsoredMessageBar->heightValue(