dialogs_main_list.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "dialogs/dialogs_main_list.h"
  8. #include "data/data_changes.h"
  9. #include "data/data_session.h"
  10. #include "data/data_chat_filters.h"
  11. #include "main/main_session.h"
  12. #include "history/history_unread_things.h"
  13. #include "history/history.h"
  14. namespace Dialogs {
  15. MainList::MainList(
  16. not_null<Main::Session*> session,
  17. FilterId filterId,
  18. rpl::producer<int> pinnedLimit)
  19. : _filterId(filterId)
  20. , _all(SortMode::Date, filterId)
  21. , _pinned(filterId, 1) {
  22. _unreadState.known = true;
  23. std::move(
  24. pinnedLimit
  25. ) | rpl::start_with_next([=](int limit) {
  26. _pinned.setLimit(limit);
  27. }, _lifetime);
  28. session->changes().realtimeNameUpdates(
  29. ) | rpl::start_with_next([=](const Data::NameUpdate &update) {
  30. _all.peerNameChanged(_filterId, update.peer, update.oldFirstLetters);
  31. }, _lifetime);
  32. }
  33. bool MainList::empty() const {
  34. return _all.empty();
  35. }
  36. bool MainList::loaded() const {
  37. return _loaded;
  38. }
  39. void MainList::setLoaded(bool loaded) {
  40. if (_loaded == loaded) {
  41. return;
  42. }
  43. const auto recomputer = gsl::finally([&] {
  44. recomputeFullListSize();
  45. });
  46. const auto notifier = unreadStateChangeNotifier(true);
  47. _loaded = loaded;
  48. }
  49. void MainList::setAllAreMuted(bool allAreMuted) {
  50. if (_allAreMuted == allAreMuted) {
  51. return;
  52. }
  53. const auto notifier = unreadStateChangeNotifier(true);
  54. _allAreMuted = allAreMuted;
  55. }
  56. void MainList::setCloudListSize(int size) {
  57. if (_cloudListSize == size) {
  58. return;
  59. }
  60. _cloudListSize = size;
  61. recomputeFullListSize();
  62. }
  63. const rpl::variable<int> &MainList::fullSize() const {
  64. return _fullListSize;
  65. }
  66. void MainList::clear() {
  67. const auto recomputer = gsl::finally([&] {
  68. recomputeFullListSize();
  69. });
  70. const auto notifier = unreadStateChangeNotifier(true);
  71. _pinned.clear();
  72. _all.clear();
  73. _unreadState = UnreadState();
  74. _cloudUnreadState = UnreadState();
  75. _unreadState.known = true;
  76. _cloudUnreadState.known = true;
  77. _cloudListSize = 0;
  78. }
  79. RowsByLetter MainList::addEntry(Key key) {
  80. const auto result = _all.addToEnd(key);
  81. const auto unread = key.entry()->chatListUnreadState();
  82. unreadEntryChanged(unread, true);
  83. recomputeFullListSize();
  84. return result;
  85. }
  86. void MainList::removeEntry(Key key) {
  87. _all.remove(key);
  88. const auto unread = key.entry()->chatListUnreadState();
  89. unreadEntryChanged(unread, false);
  90. recomputeFullListSize();
  91. }
  92. void MainList::recomputeFullListSize() {
  93. _fullListSize = std::max(_all.size(), loaded() ? 0 : _cloudListSize);
  94. }
  95. void MainList::unreadStateChanged(
  96. const UnreadState &wasState,
  97. const UnreadState &nowState) {
  98. const auto useClouded = _cloudUnreadState.known && !loaded();
  99. const auto updateCloudUnread = _cloudUnreadState.known && wasState.known;
  100. const auto notify = !useClouded || wasState.known;
  101. const auto notifier = unreadStateChangeNotifier(notify);
  102. _unreadState += nowState - wasState;
  103. if (updateCloudUnread) {
  104. Assert(nowState.known);
  105. _cloudUnreadState += nowState - wasState;
  106. finalizeCloudUnread();
  107. }
  108. }
  109. void MainList::unreadEntryChanged(
  110. const Dialogs::UnreadState &state,
  111. bool added) {
  112. if (!state.messages
  113. && !state.chats
  114. && !state.marks
  115. && !state.mentions
  116. && !state.reactions) {
  117. return;
  118. }
  119. const auto updateCloudUnread = _cloudUnreadState.known && state.known;
  120. const auto notify = !_cloudUnreadState.known || loaded() || state.known;
  121. const auto notifier = unreadStateChangeNotifier(notify);
  122. if (added) {
  123. _unreadState += state;
  124. } else {
  125. _unreadState -= state;
  126. }
  127. if (updateCloudUnread) {
  128. if (added) {
  129. _cloudUnreadState += state;
  130. } else {
  131. _cloudUnreadState -= state;
  132. }
  133. finalizeCloudUnread();
  134. }
  135. }
  136. void MainList::updateCloudUnread(const MTPDdialogFolder &data) {
  137. const auto notifier = unreadStateChangeNotifier(!loaded());
  138. _cloudUnreadState.messages = data.vunread_muted_messages_count().v
  139. + data.vunread_unmuted_messages_count().v;
  140. _cloudUnreadState.chats = data.vunread_muted_peers_count().v
  141. + data.vunread_unmuted_peers_count().v;
  142. finalizeCloudUnread();
  143. _cloudUnreadState.known = true;
  144. }
  145. bool MainList::cloudUnreadKnown() const {
  146. return _cloudUnreadState.known;
  147. }
  148. void MainList::finalizeCloudUnread() {
  149. // Cloud state for archive folder always counts everything as muted.
  150. _cloudUnreadState.messagesMuted = _cloudUnreadState.messages;
  151. _cloudUnreadState.chatsMuted = _cloudUnreadState.chats;
  152. // We don't know the real value of marked chats counts in cloud unread.
  153. _cloudUnreadState.marksMuted = _cloudUnreadState.marks = 0;
  154. }
  155. UnreadState MainList::unreadState() const {
  156. const auto useCloudState = _cloudUnreadState.known && !loaded();
  157. auto result = useCloudState ? _cloudUnreadState : _unreadState;
  158. // We don't know the real value of marked chats counts in cloud unread.
  159. if (useCloudState) {
  160. result.marks = _unreadState.marks;
  161. result.marksMuted = _unreadState.marksMuted;
  162. }
  163. if (_allAreMuted) {
  164. result.messagesMuted = result.messages;
  165. result.chatsMuted = result.chats;
  166. result.marksMuted = result.marks;
  167. }
  168. #ifdef Q_OS_WIN
  169. [[maybe_unused]] volatile auto touch = 0
  170. + _unreadState.marks + _unreadState.marksMuted
  171. + _unreadState.messages + _unreadState.messagesMuted
  172. + _unreadState.chats + _unreadState.chatsMuted
  173. + _unreadState.reactions + _unreadState.reactionsMuted
  174. + _unreadState.mentions;
  175. #endif // Q_OS_WIN
  176. return result;
  177. }
  178. rpl::producer<UnreadState> MainList::unreadStateChanges() const {
  179. return _unreadStateChanges.events();
  180. }
  181. not_null<IndexedList*> MainList::indexed() {
  182. return &_all;
  183. }
  184. not_null<const IndexedList*> MainList::indexed() const {
  185. return &_all;
  186. }
  187. not_null<PinnedList*> MainList::pinned() {
  188. return &_pinned;
  189. }
  190. not_null<const PinnedList*> MainList::pinned() const {
  191. return &_pinned;
  192. }
  193. } // namespace Dialogs