tdesktop/Telegram/SourceFiles/dialogs/dialogs_main_list.cpp

221 lines
5.4 KiB
C++
Raw Normal View History

2019-04-22 14:22:39 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "dialogs/dialogs_main_list.h"
#include "data/data_changes.h"
#include "data/data_session.h"
#include "data/data_chat_filters.h"
#include "main/main_session.h"
#include "history/history_unread_things.h"
2019-04-22 14:22:39 +00:00
#include "history/history.h"
namespace Dialogs {
MainList::MainList(
not_null<Main::Session*> session,
FilterId filterId,
rpl::producer<int> pinnedLimit)
: _filterId(filterId)
2020-03-17 13:04:30 +00:00
, _all(SortMode::Date, filterId)
, _pinned(filterId, 1) {
_unreadState.known = true;
2019-04-22 14:22:39 +00:00
std::move(
pinnedLimit
) | rpl::start_with_next([=](int limit) {
_pinned.setLimit(limit);
}, _lifetime);
session->changes().realtimeNameUpdates(
) | rpl::start_with_next([=](const Data::NameUpdate &update) {
_all.peerNameChanged(_filterId, update.peer, update.oldFirstLetters);
2019-04-22 14:22:39 +00:00
}, _lifetime);
}
bool MainList::empty() const {
return _all.empty();
}
bool MainList::loaded() const {
return _loaded;
}
void MainList::setLoaded(bool loaded) {
if (_loaded == loaded) {
return;
}
2020-03-18 13:39:08 +00:00
const auto recomputer = gsl::finally([&] {
recomputeFullListSize();
});
const auto notifier = unreadStateChangeNotifier(true);
2019-04-22 14:22:39 +00:00
_loaded = loaded;
}
void MainList::setAllAreMuted(bool allAreMuted) {
if (_allAreMuted == allAreMuted) {
return;
}
const auto notifier = unreadStateChangeNotifier(true);
_allAreMuted = allAreMuted;
}
void MainList::setCloudListSize(int size) {
if (_cloudListSize == size) {
return;
}
_cloudListSize = size;
recomputeFullListSize();
}
const rpl::variable<int> &MainList::fullSize() const {
return _fullListSize;
2019-04-22 14:22:39 +00:00
}
void MainList::clear() {
2020-03-18 13:39:08 +00:00
const auto recomputer = gsl::finally([&] {
recomputeFullListSize();
});
const auto notifier = unreadStateChangeNotifier(true);
_pinned.clear();
2019-04-22 14:22:39 +00:00
_all.clear();
_unreadState = UnreadState();
_cloudUnreadState = UnreadState();
_unreadState.known = true;
_cloudUnreadState.known = true;
_cloudListSize = 0;
}
RowsByLetter MainList::addEntry(Key key) {
const auto result = _all.addToEnd(key);
const auto unread = key.entry()->chatListUnreadState();
unreadEntryChanged(unread, true);
recomputeFullListSize();
return result;
}
void MainList::removeEntry(Key key) {
_all.remove(key);
const auto unread = key.entry()->chatListUnreadState();
unreadEntryChanged(unread, false);
recomputeFullListSize();
}
void MainList::recomputeFullListSize() {
_fullListSize = std::max(_all.size(), loaded() ? 0 : _cloudListSize);
2019-04-22 14:22:39 +00:00
}
void MainList::unreadStateChanged(
const UnreadState &wasState,
const UnreadState &nowState) {
2020-03-16 12:12:40 +00:00
const auto useClouded = _cloudUnreadState.known && !loaded();
const auto updateCloudUnread = _cloudUnreadState.known && wasState.known;
2020-03-16 12:12:40 +00:00
const auto notify = !useClouded || wasState.known;
const auto notifier = unreadStateChangeNotifier(notify);
_unreadState += nowState - wasState;
if (updateCloudUnread) {
Assert(nowState.known);
_cloudUnreadState += nowState - wasState;
finalizeCloudUnread();
}
2019-04-22 14:22:39 +00:00
}
void MainList::unreadEntryChanged(
const Dialogs::UnreadState &state,
bool added) {
if (!state.messages
&& !state.chats
&& !state.marks
&& !state.mentions
&& !state.reactions) {
return;
}
const auto updateCloudUnread = _cloudUnreadState.known && state.known;
2020-03-18 13:39:08 +00:00
const auto notify = !_cloudUnreadState.known || loaded() || state.known;
const auto notifier = unreadStateChangeNotifier(notify);
if (added) {
_unreadState += state;
} else {
_unreadState -= state;
}
if (updateCloudUnread) {
if (added) {
_cloudUnreadState += state;
} else {
_cloudUnreadState -= state;
}
finalizeCloudUnread();
}
}
void MainList::updateCloudUnread(const MTPDdialogFolder &data) {
const auto notifier = unreadStateChangeNotifier(!loaded());
_cloudUnreadState.messages = data.vunread_muted_messages_count().v
+ data.vunread_unmuted_messages_count().v;
_cloudUnreadState.chats = data.vunread_muted_peers_count().v
+ data.vunread_unmuted_peers_count().v;
finalizeCloudUnread();
_cloudUnreadState.known = true;
}
bool MainList::cloudUnreadKnown() const {
return _cloudUnreadState.known;
}
void MainList::finalizeCloudUnread() {
// Cloud state for archive folder always counts everything as muted.
_cloudUnreadState.messagesMuted = _cloudUnreadState.messages;
_cloudUnreadState.chatsMuted = _cloudUnreadState.chats;
// We don't know the real value of marked chats counts in cloud unread.
_cloudUnreadState.marksMuted = _cloudUnreadState.marks = 0;
2019-04-22 14:22:39 +00:00
}
UnreadState MainList::unreadState() const {
const auto useCloudState = _cloudUnreadState.known && !loaded();
auto result = useCloudState ? _cloudUnreadState : _unreadState;
// We don't know the real value of marked chats counts in cloud unread.
if (useCloudState) {
result.marks = _unreadState.marks;
result.marksMuted = _unreadState.marksMuted;
}
if (_allAreMuted) {
result.messagesMuted = result.messages;
result.chatsMuted = result.chats;
result.marksMuted = result.marks;
}
return result;
}
rpl::producer<UnreadState> MainList::unreadStateChanges() const {
return _unreadStateChanges.events();
2019-04-22 14:22:39 +00:00
}
not_null<IndexedList*> MainList::indexed() {
return &_all;
2019-04-22 14:22:39 +00:00
}
2020-02-07 09:43:12 +00:00
not_null<const IndexedList*> MainList::indexed() const {
return &_all;
2019-04-22 14:22:39 +00:00
}
not_null<PinnedList*> MainList::pinned() {
return &_pinned;
}
not_null<const PinnedList*> MainList::pinned() const {
return &_pinned;
}
} // namespace Dialogs