If opening an unread story show only unread.

This commit is contained in:
John Preston 2023-07-21 18:21:34 +04:00
parent 06e49c6813
commit c0b7577db9
2 changed files with 29 additions and 11 deletions

View file

@ -669,6 +669,9 @@ void Controller::rebuildFromContext(
storyId.peer, storyId.peer,
&StoriesSourceInfo::id); &StoriesSourceInfo::id);
if (i != end(sources)) { if (i != end(sources)) {
if (_cachedSourcesList.empty()) {
_showingUnreadSources = source && (source->readTill < id);
}
rebuildCachedSourcesList(sources, (i - begin(sources))); rebuildCachedSourcesList(sources, (i - begin(sources)));
showSiblings(&user->session()); showSiblings(&user->session());
if (int(sources.end() - i) < kPreloadUsersCount) { if (int(sources.end() - i) < kPreloadUsersCount) {
@ -1294,6 +1297,8 @@ void Controller::rebuildCachedSourcesList(
int index) { int index) {
Expects(index >= 0 && index < lists.size()); Expects(index >= 0 && index < lists.size());
const auto currentPeerId = lists[index].id;
// Remove removed. // Remove removed.
_cachedSourcesList.erase(ranges::remove_if(_cachedSourcesList, [&]( _cachedSourcesList.erase(ranges::remove_if(_cachedSourcesList, [&](
PeerId id) { PeerId id) {
@ -1301,7 +1306,7 @@ void Controller::rebuildCachedSourcesList(
}), end(_cachedSourcesList)); }), end(_cachedSourcesList));
// Find current, full rebuild if can't find. // Find current, full rebuild if can't find.
const auto i = ranges::find(_cachedSourcesList, lists[index].id); const auto i = ranges::find(_cachedSourcesList, currentPeerId);
if (i == end(_cachedSourcesList)) { if (i == end(_cachedSourcesList)) {
_cachedSourcesList.clear(); _cachedSourcesList.clear();
} else { } else {
@ -1310,38 +1315,50 @@ void Controller::rebuildCachedSourcesList(
if (_cachedSourcesList.empty()) { if (_cachedSourcesList.empty()) {
// Full rebuild. // Full rebuild.
const auto predicate = [&](const Data::StoriesSourceInfo &info) {
return !_showingUnreadSources
|| (info.unreadCount > 0)
|| (info.id == currentPeerId);
};
_cachedSourcesList = lists _cachedSourcesList = lists
| ranges::views::filter(predicate)
| ranges::views::transform(&Data::StoriesSourceInfo::id) | ranges::views::transform(&Data::StoriesSourceInfo::id)
| ranges::to_vector; | ranges::to_vector;
_cachedSourceIndex = index; _cachedSourceIndex = ranges::find(_cachedSourcesList, currentPeerId)
- begin(_cachedSourcesList);
} else if (ranges::equal( } else if (ranges::equal(
lists, lists,
_cachedSourcesList, _cachedSourcesList,
ranges::equal_to(), ranges::equal_to(),
&Data::StoriesSourceInfo::id)) { &Data::StoriesSourceInfo::id)) {
// No rebuild needed. // No rebuild needed.
_cachedSourceIndex = index;
} else { } else {
// All that go before the current push to front. // All that go before the current push to front.
for (auto before = index; before > 0;) { for (auto before = index; before > 0;) {
const auto peerId = lists[--before].id; const auto &info = lists[--before];
if (!ranges::contains(_cachedSourcesList, peerId)) { if (_showingUnreadSources && !info.unreadCount) {
continue;
} else if (!ranges::contains(_cachedSourcesList, info.id)) {
_cachedSourcesList.insert( _cachedSourcesList.insert(
begin(_cachedSourcesList), begin(_cachedSourcesList),
peerId); info.id);
++_cachedSourceIndex; ++_cachedSourceIndex;
} }
} }
// All that go after the current push to back. // All that go after the current push to back.
for (auto after = index + 1, count = int(lists.size()); after != count; ++after) { for (auto after = index + 1, count = int(lists.size())
const auto peerId = lists[after].id; ; after != count
if (!ranges::contains(_cachedSourcesList, peerId)) { ; ++after) {
_cachedSourcesList.push_back(peerId); const auto &info = lists[after];
if (_showingUnreadSources && !info.unreadCount) {
continue;
} else if (!ranges::contains(_cachedSourcesList, info.id)) {
_cachedSourcesList.push_back(info.id);
} }
} }
} }
Ensures(_cachedSourcesList.size() == lists.size()); Ensures(_cachedSourcesList.size() <= lists.size());
Ensures(_cachedSourceIndex >= 0 Ensures(_cachedSourceIndex >= 0
&& _cachedSourceIndex < _cachedSourcesList.size()); && _cachedSourceIndex < _cachedSourcesList.size());
} }

View file

@ -266,6 +266,7 @@ private:
std::vector<PeerId> _cachedSourcesList; std::vector<PeerId> _cachedSourcesList;
int _cachedSourceIndex = -1; int _cachedSourceIndex = -1;
bool _showingUnreadSources = false;
ViewsSlice _viewsSlice; ViewsSlice _viewsSlice;
rpl::event_stream<> _moreViewsLoaded; rpl::event_stream<> _moreViewsLoaded;