Group Recently Added by neighbouring Title

This commit is contained in:
Jared Turner
2020-06-07 18:39:05 +01:00
parent 5ed2a8affa
commit aaf0a3c6af
2 changed files with 59 additions and 14 deletions
+35 -6
View File
@@ -525,15 +525,44 @@ class Library
titles.each do |t|
t.entries.each { |e| entries << e }
end
recently_added = entries.map { |e|
{
entry: e,
percentage: e.book.load_percentage(username, e.title)
entries.sort! { |a, b| b.date_added <=> a.date_added }
# Only grab entries form the last 3 months
# otherwise we will be grouping neighbours by Title for the entire
# library every time. Is this premature optimisation or reasonable optimisation?
entries.select! { |e| e.date_added > 3.months.ago }
i = 0
recently_added = [] of NamedTuple(entry: Entry, percentage: Float64, grouped_count: Int32)
while i <= entries.size - 1
grouped_count = neighbouring_title_count(entries, i)
if grouped_count > 1
# delete grouped entries (except for first)
entries.delete_at(i+1..i+grouped_count-1)
end
recently_added << {
entry: entries[i],
percentage: entries[i].book.load_percentage(username, entries[i].title),
grouped_count: grouped_count
}
}
recently_added.sort! { |a, b| b[:entry].date_added <=> a[:entry].date_added }[0..11]
i += 1
end
recently_added[0..11]
end
private def neighbouring_title_count(entries, index : Int32)
count = 1
book_title = entries[index].book.title
while index < entries.size - 1
if book_title == entries[index+1].book.title # is it ok to compare via book.title?
count += 1
else
break
end
index += 1
end
count
end
private def get_continue_reading_entry(username, title)
in_progress_entries = title.entries.select do |e|