continue reading sorted by last read

This commit is contained in:
Jared Turner
2020-06-01 15:29:18 +01:00
parent e99d7b8b29
commit 3ef6a7bfc4
3 changed files with 58 additions and 15 deletions

View File

@@ -75,10 +75,41 @@ class MainRouter < Router
t.get_continue_reading_entry username
}.select Entry
percentage = continue_reading_entries.map do |e|
percentage = continue_reading_entries.map { |e|
e.book.load_percentage username, e.title
pp e.book.load_last_read username, e.title
end
}
last_read = continue_reading_entries.map { |e|
e.book.get_last_read_for_continue_reading username, e
}
# Group values in a NamedTuple for easier sorting
cr_entries = continue_reading_entries.map_with_index { |e, i|
{
entry: e,
percentage: percentage[i],
# if you're ok with the NamedTuple approach we could remove the
# percentage and last_read vars above and just call the methods
# here eg.
# perecentage: e.book.load_percentage username, e.title
last_read: last_read[i]
}
}
# I couldn't get the sort to work where last_read type is `Time | Nil`
# so I'm creating a new variable with just the entries that have last_read
# even still, I have to do another workaround within the sort below :/
cr_entries_not_nil = cr_entries.select { |e| e[:last_read] }
cr_entries_not_nil.sort! { |a, b|
# 'if' ensures values aren't nil otherwise the compiler errors
# because it still thinks the NamedTuple `last_read` can be nil
# even though we only 'select'ed the objects which have last_read
# there's probably a better way to do this
if (a_time = a[:last_read]) && (b_time = b[:last_read])
b_time <=> a_time
end
}
# add `last_read == nil` entries AFTER sorted entries
continue_reading = cr_entries_not_nil + cr_entries.select { |e| e[:last_read].nil? }
layout "home"
rescue e