mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-03 11:25:29 -04:00
refactor continue reading into Library class
This commit is contained in:
parent
13c0878357
commit
4f5e05c008
@ -477,4 +477,54 @@ class Library
|
|||||||
end
|
end
|
||||||
@logger.debug "Scan completed"
|
@logger.debug "Scan completed"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_continue_reading_entries(username)
|
||||||
|
# map: get the continue-reading entry or nil for each Title
|
||||||
|
# select: select only entries (and ignore Nil's) from the array
|
||||||
|
# produced by map
|
||||||
|
continue_reading_entries = titles.map { |t|
|
||||||
|
get_continue_reading_entry username, t
|
||||||
|
}.select Entry
|
||||||
|
|
||||||
|
continue_reading = continue_reading_entries.map_with_index { |e, i|
|
||||||
|
{
|
||||||
|
entry: e,
|
||||||
|
percentage: e.book.load_percentage(username, e.title),
|
||||||
|
last_read: get_relevant_last_read(username, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Sort by by last_read, most recent first (nils at the end)
|
||||||
|
continue_reading.sort! do |a, b|
|
||||||
|
next 0 if a[:last_read].nil? && b[:last_read].nil?
|
||||||
|
next 1 if a[:last_read].nil?
|
||||||
|
next -1 if b[:last_read].nil?
|
||||||
|
b[:last_read].not_nil! <=> a[:last_read].not_nil!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
private def get_continue_reading_entry(username, title)
|
||||||
|
in_progress_entries = title.entries.select do |e|
|
||||||
|
title.load_progress(username, e.title) > 0
|
||||||
|
end
|
||||||
|
return nil if in_progress_entries.empty?
|
||||||
|
|
||||||
|
latest_read_entry = in_progress_entries[-1]
|
||||||
|
if title.load_progress(username, latest_read_entry.title) ==
|
||||||
|
latest_read_entry.pages
|
||||||
|
title.next_entry latest_read_entry
|
||||||
|
else
|
||||||
|
latest_read_entry
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private def get_relevant_last_read(username, entry_obj)
|
||||||
|
last_read = entry_obj.book.load_last_read username, entry_obj.title
|
||||||
|
if last_read.nil? # grab from previous entry if current entry hasn't been started yet
|
||||||
|
previous_entry = entry_obj.book.previous_entry(entry_obj)
|
||||||
|
return entry_obj.book.load_last_read username, previous_entry.title if previous_entry
|
||||||
|
end
|
||||||
|
last_read
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -65,51 +65,8 @@ class MainRouter < Router
|
|||||||
|
|
||||||
get "/" do |env|
|
get "/" do |env|
|
||||||
begin
|
begin
|
||||||
titles = @context.library.titles
|
|
||||||
username = get_username env
|
username = get_username env
|
||||||
|
continue_reading = @context.library.get_continue_reading_entries username
|
||||||
# map: get the on-deck entry or nil for each Title
|
|
||||||
# select: select only entries (and ignore Nil's) from the array
|
|
||||||
# produced by map
|
|
||||||
continue_reading_entries = titles.map { |t|
|
|
||||||
t.get_continue_reading_entry username
|
|
||||||
}.select Entry
|
|
||||||
|
|
||||||
percentage = continue_reading_entries.map { |e|
|
|
||||||
e.book.load_percentage username, e.title
|
|
||||||
}
|
|
||||||
|
|
||||||
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"
|
layout "home"
|
||||||
rescue e
|
rescue e
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<%- unless continue_reading_entries.empty? -%>
|
<%- unless continue_reading.empty? -%>
|
||||||
<h2 class="uk-title home-headings">Continue Reading</h2>
|
<h2 class="uk-title home-headings">Continue Reading</h2>
|
||||||
<div id="item-container-continue" class="uk-child-width-1-4@m uk-child-width-1-2" uk-grid>
|
<div id="item-container-continue" class="uk-child-width-1-4@m uk-child-width-1-2" uk-grid>
|
||||||
<%- continue_reading.each do |cr| -%>
|
<%- continue_reading.each do |cr| -%>
|
||||||
@ -20,7 +20,6 @@
|
|||||||
</div>
|
</div>
|
||||||
<%- end -%>
|
<%- end -%>
|
||||||
|
|
||||||
|
|
||||||
<!-- TODO: DRY this code with calls in other ecr files? eg. title.ecr -->
|
<!-- TODO: DRY this code with calls in other ecr files? eg. title.ecr -->
|
||||||
<div id="modal" class="uk-flex-top" uk-modal>
|
<div id="modal" class="uk-flex-top" uk-modal>
|
||||||
<div class="uk-modal-dialog uk-margin-auto-vertical">
|
<div class="uk-modal-dialog uk-margin-auto-vertical">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user