Skip to content

Commit

Permalink
refactor continue reading into Library class
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredlt committed Jun 3, 2020
1 parent 13c0878 commit 4f5e05c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 46 deletions.
50 changes: 50 additions & 0 deletions src/library.cr
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,54 @@ class Library
end
@logger.debug "Scan completed"
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
45 changes: 1 addition & 44 deletions src/routes/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,51 +65,8 @@ class MainRouter < Router

get "/" do |env|
begin
titles = @context.library.titles
username = get_username env

# 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? }
continue_reading = @context.library.get_continue_reading_entries username

layout "home"
rescue e
Expand Down
3 changes: 1 addition & 2 deletions src/views/home.ecr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%- unless continue_reading_entries.empty? -%>
<%- unless continue_reading.empty? -%>
<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>
<%- continue_reading.each do |cr| -%>
Expand All @@ -20,7 +20,6 @@
</div>
<%- end -%>


<!-- TODO: DRY this code with calls in other ecr files? eg. title.ecr -->
<div id="modal" class="uk-flex-top" uk-modal>
<div class="uk-modal-dialog uk-margin-auto-vertical">
Expand Down

0 comments on commit 4f5e05c

Please sign in to comment.