mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-04-16 06:07:06 +02:00
Moved out ui callbacks of bar from data class from sponsored messages.
This commit is contained in:
parent
30dae049ff
commit
6237675744
3 changed files with 70 additions and 42 deletions
|
@ -278,38 +278,52 @@ void SponsoredMessages::parse(
|
|||
});
|
||||
}
|
||||
|
||||
void SponsoredMessages::fillTopBar(
|
||||
FullMsgId SponsoredMessages::fillTopBar(
|
||||
not_null<History*> history,
|
||||
not_null<Ui::RpWidget*> widget,
|
||||
Fn<void()> hide) {
|
||||
not_null<Ui::RpWidget*> widget) {
|
||||
const auto it = _data.find(history);
|
||||
if (it != end(_data)) {
|
||||
auto &list = it->second;
|
||||
if (!list.entries.empty()) {
|
||||
const auto &entry = list.entries.front();
|
||||
const auto fullId = entry.itemFullId;
|
||||
Ui::FillSponsoredMessageBar(
|
||||
widget,
|
||||
_session,
|
||||
fullId,
|
||||
entry.sponsored.from,
|
||||
entry.sponsored.textWithEntities);
|
||||
return fullId;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
rpl::producer<> SponsoredMessages::itemRemoved(const FullMsgId &fullId) {
|
||||
if (IsServerMsgId(fullId.msg) || !fullId) {
|
||||
return rpl::never<>();
|
||||
}
|
||||
const auto history = _session->data().history(fullId.peer);
|
||||
const auto it = _data.find(history);
|
||||
if (it == end(_data)) {
|
||||
return;
|
||||
return rpl::never<>();
|
||||
}
|
||||
auto &list = it->second;
|
||||
if (list.entries.empty()) {
|
||||
return;
|
||||
const auto entryIt = ranges::find_if(list.entries, [&](const Entry &e) {
|
||||
return e.itemFullId == fullId;
|
||||
});
|
||||
if (entryIt == end(list.entries)) {
|
||||
return rpl::never<>();
|
||||
}
|
||||
auto &entry = list.entries.front();
|
||||
if (!entry.optionalDestructionNotifier) {
|
||||
entry.optionalDestructionNotifier = std::make_unique<rpl::lifetime>();
|
||||
if (!entryIt->optionalDestructionNotifier) {
|
||||
entryIt->optionalDestructionNotifier
|
||||
= std::make_unique<rpl::lifetime>();
|
||||
entryIt->optionalDestructionNotifier->add([this, fullId] {
|
||||
_itemRemoved.fire_copy(fullId);
|
||||
});
|
||||
}
|
||||
const auto fullId = entry.itemFullId;
|
||||
entry.optionalDestructionNotifier->add(std::move(hide));
|
||||
Ui::FillSponsoredMessageBar(
|
||||
widget,
|
||||
_session,
|
||||
fullId,
|
||||
entry.sponsored.from,
|
||||
entry.sponsored.textWithEntities);
|
||||
|
||||
const auto viewLifetime = widget->lifetime().make_state<rpl::lifetime>();
|
||||
widget->shownValue() | rpl::filter(
|
||||
rpl::mappers::_1
|
||||
) | rpl::start_with_next([=, this](bool shown) {
|
||||
view(fullId);
|
||||
viewLifetime->destroy();
|
||||
}, *viewLifetime);
|
||||
return _itemRemoved.events(
|
||||
) | rpl::filter(rpl::mappers::_1 == fullId) | rpl::to_empty;
|
||||
}
|
||||
|
||||
void SponsoredMessages::append(
|
||||
|
|
|
@ -104,10 +104,10 @@ public:
|
|||
void clearItems(not_null<History*> history);
|
||||
[[nodiscard]] Details lookupDetails(const FullMsgId &fullId) const;
|
||||
void clicked(const FullMsgId &fullId, bool isMedia, bool isFullscreen);
|
||||
void fillTopBar(
|
||||
[[nodiscard]] FullMsgId fillTopBar(
|
||||
not_null<History*> history,
|
||||
not_null<Ui::RpWidget*> widget,
|
||||
Fn<void()> hide);
|
||||
not_null<Ui::RpWidget*> widget);
|
||||
[[nodiscard]] rpl::producer<> itemRemoved(const FullMsgId &);
|
||||
|
||||
[[nodiscard]] AppendResult append(not_null<History*> history);
|
||||
void inject(
|
||||
|
@ -167,6 +167,8 @@ private:
|
|||
base::flat_map<not_null<History*>, Request> _requests;
|
||||
base::flat_map<RandomId, Request> _viewRequests;
|
||||
|
||||
rpl::event_stream<FullMsgId> _itemRemoved;
|
||||
|
||||
rpl::lifetime _lifetime;
|
||||
|
||||
};
|
||||
|
|
|
@ -7648,21 +7648,33 @@ void HistoryWidget::createSponsoredMessageBar() {
|
|||
object_ptr<Ui::RpWidget>(this));
|
||||
|
||||
_sponsoredMessageBar->entity()->resizeToWidth(_scroll->width());
|
||||
auto destruction = [this] {
|
||||
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(
|
||||
const auto maybeFullId = session().sponsoredMessages().fillTopBar(
|
||||
_history,
|
||||
_sponsoredMessageBar->entity(),
|
||||
std::move(destruction));
|
||||
_sponsoredMessageBar->entity());
|
||||
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;
|
||||
_sponsoredMessageBar->heightValue(
|
||||
|
|
Loading…
Add table
Reference in a new issue