Sort smartfeeds by date

This commit is contained in:
Mike Sharov 2022-08-03 11:09:40 -04:00
parent 38b699e70e
commit 9e7e7c8291
1 changed files with 22 additions and 16 deletions

View File

@ -185,33 +185,39 @@ static void SmartFeedNewitems (struct feed* smart_feed)
// Be smart and don't leak the smart feed.
// The items->data structures must not be freed, since a smart feed is only
// a pointer collection and does not contain allocated memory.
if (smart_feed->items) {
while (smart_feed->items->next) {
smart_feed->items = smart_feed->items->next;
free (smart_feed->items->prev);
}
free (smart_feed->items);
smart_feed->items = NULL;
while (smart_feed->items) {
struct newsitem* i2f = smart_feed->items;
smart_feed->items = smart_feed->items->next;
free (i2f);
}
for (struct feed* f = _feed_list; f; f = f->next) {
// Do not add the smart feed recursively. 8)
if (f == smart_feed)
continue;
for (struct newsitem * item = f->items; item; item = item->next) {
// If item is unread, add to smart feed.
for (struct newsitem* item = f->items; item; item = item->next) {
if (item->data->readstatus)
continue;
// If item is unread, add to smart feed.
struct newsitem* new_item = calloc (1, sizeof (struct newsitem));
new_item->data = item->data;
// Find insertion point, sorted with latest date first
struct newsitem* prev = NULL;
if (smart_feed->items && smart_feed->items->data->date >= new_item->data->date) {
prev = smart_feed->items;
while (prev->next && prev->data->date >= new_item->data->date)
prev = prev->next;
}
// Add to data structure.
if (!smart_feed->items)
smart_feed->items = new_item;
else {
new_item->prev = smart_feed->items;
while (new_item->prev->next)
new_item->prev = new_item->prev->next;
new_item->prev->next = new_item;
if (!prev) {
new_item->next = smart_feed->items;
smart_feed->items = new_item; // Add to beginning of the list
} else {
new_item->next = prev->next;
new_item->prev = prev;
prev->next = new_item;
}
}
}