Add unread counter from feed to common counter.

This commit is contained in:
John Preston 2018-02-20 20:53:55 +03:00
parent 17a4d19beb
commit 336e691dbc
5 changed files with 72 additions and 27 deletions

View file

@ -359,17 +359,43 @@ void Feed::applyDialog(const MTPDdialogFeed &data) {
}
}
void Feed::changedInChatListHook(Dialogs::Mode list, bool added) {
if (list == Dialogs::Mode::All && unreadCount()) {
const auto mutedCount = _unreadMutedCount;
const auto nonMutedCount = unreadCount() - mutedCount;
const auto mutedDelta = added ? mutedCount : -mutedCount;
const auto nonMutedDelta = added ? nonMutedCount : -nonMutedCount;
App::histories().unreadIncrement(nonMutedDelta, false);
App::histories().unreadIncrement(mutedDelta, true);
}
}
void Feed::setUnreadCounts(int unreadNonMutedCount, int unreadMutedCount) {
if (unreadCountKnown()
&& (*_unreadCount == unreadNonMutedCount + unreadMutedCount)
&& (_unreadMutedCount == unreadMutedCount)) {
return;
}
const auto unreadNonMutedCountDelta = _unreadCount | [&](int count) {
return unreadNonMutedCount - (count - _unreadMutedCount);
};
const auto unreadMutedCountDelta = _unreadCount | [&](int count) {
return unreadMutedCount - _unreadMutedCount;
};
_unreadCount = unreadNonMutedCount + unreadMutedCount;
_unreadMutedCount = unreadMutedCount;
_unreadCountChanges.fire(unreadCount());
updateChatListEntry();
if (inChatList(Dialogs::Mode::All)) {
App::histories().unreadIncrement(
unreadNonMutedCountDelta ? *unreadNonMutedCountDelta : unreadNonMutedCount,
false);
App::histories().unreadIncrement(
unreadMutedCountDelta ? *unreadMutedCountDelta : unreadMutedCount,
true);
}
}
void Feed::setUnreadPosition(const MessagePosition &position) {
@ -384,14 +410,24 @@ void Feed::unreadCountChanged(
if (!unreadCountKnown()) {
return;
}
*_unreadCount = std::max(*_unreadCount + unreadCountDelta, 0);
_unreadMutedCount = snap(
_unreadMutedCount + mutedCountDelta,
0,
*_unreadCount);
accumulate_max(unreadCountDelta, -*_unreadCount);
*_unreadCount += unreadCountDelta;
mutedCountDelta = snap(
mutedCountDelta,
-_unreadMutedCount,
*_unreadCount - _unreadMutedCount);
_unreadMutedCount += mutedCountDelta;
_unreadCountChanges.fire(unreadCount());
updateChatListEntry();
if (inChatList(Dialogs::Mode::All)) {
App::histories().unreadIncrement(
unreadCountDelta,
false);
App::histories().unreadMuteChanged(mutedCountDelta, true);
}
}
MessagePosition Feed::unreadPosition() const {

View file

@ -67,6 +67,7 @@ public:
const QString &chatsListName() const override;
const base::flat_set<QString> &chatsListNameWords() const override;
const base::flat_set<QChar> &chatsListFirstLetters() const override;
void changedInChatListHook(Dialogs::Mode list, bool added) override;
void loadUserpic() override;
void paintUserpic(

View file

@ -162,6 +162,29 @@ int Histories::unreadBadge() const {
return _unreadFull - (Global::IncludeMuted() ? 0 : _unreadMuted);
}
int Histories::unreadMutedCount() const {
return _unreadMuted;
}
void Histories::unreadIncrement(int count, bool muted) {
_unreadFull += count;
if (muted) {
_unreadMuted += count;
}
if (!muted || Global::IncludeMuted()) {
Notify::unreadCounterUpdated();
}
}
void Histories::unreadMuteChanged(int count, bool muted) {
if (muted) {
_unreadMuted += count;
} else {
_unreadMuted -= count;
}
Notify::unreadCounterUpdated();
}
bool Histories::unreadOnlyMuted() const {
return Global::IncludeMuted() ? (_unreadMuted >= _unreadFull) : false;
}
@ -1642,9 +1665,6 @@ void History::setUnreadCount(int newUnreadCount) {
App::histories().unreadIncrement(
unreadCountDelta ? *unreadCountDelta : newUnreadCount,
mute());
if (!mute() || Global::IncludeMuted()) {
Notify::unreadCounterUpdated();
}
}
if (const auto main = App::main()) {
main->unreadCountChanged(this);
@ -2580,7 +2600,6 @@ void History::changedInChatListHook(Dialogs::Mode list, bool added) {
if (list == Dialogs::Mode::All && unreadCount()) {
const auto delta = added ? unreadCount() : -unreadCount();
App::histories().unreadIncrement(delta, mute());
Notify::unreadCounterUpdated();
}
}

View file

@ -72,23 +72,10 @@ public:
BasicAnimation _a_typings;
int unreadBadge() const;
int unreadMutedCount() const {
return _unreadMuted;
}
int unreadMutedCount() const;
bool unreadOnlyMuted() const;
void unreadIncrement(int32 count, bool muted) {
_unreadFull += count;
if (muted) {
_unreadMuted += count;
}
}
void unreadMuteChanged(int32 count, bool muted) {
if (muted) {
_unreadMuted += count;
} else {
_unreadMuted -= count;
}
}
void unreadIncrement(int count, bool muted);
void unreadMuteChanged(int count, bool muted);
struct SendActionAnimationUpdate {
History *history;

View file

@ -160,9 +160,11 @@ std::unique_ptr<PeerListState> ChannelsController::saveState() const {
auto result = PeerListController::saveState();
auto my = std::make_unique<SavedState>();
using Flag = Data::FeedUpdateFlag;
// Must not capture `this` here, because it dies before my->lifetime.
Auth().data().feedUpdated(
) | rpl::filter([=](const Data::FeedUpdate &update) {
return (update.feed == _feed) && (update.flag == Flag::Channels);
) | rpl::filter([feed = _feed](const Data::FeedUpdate &update) {
return (update.feed == feed) && (update.flag == Flag::Channels);
}) | rpl::start_with_next([state = result.get()] {
state->controllerState = nullptr;
}, my->lifetime);