mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-05 04:15:35 -04:00
continue reading sorted by last read
This commit is contained in:
parent
e99d7b8b29
commit
3ef6a7bfc4
@ -287,10 +287,7 @@ class Title
|
|||||||
else
|
else
|
||||||
info.progress[username][entry] = page
|
info.progress[username][entry] = page
|
||||||
end
|
end
|
||||||
# should this be a separate method?
|
# save last_read timestamp
|
||||||
# eg. def save_last_read(username, entry)
|
|
||||||
# if so, we would need to open the json file twice every
|
|
||||||
# time we save. Does that matter?
|
|
||||||
if info.last_read[username]?.nil?
|
if info.last_read[username]?.nil?
|
||||||
info.last_read[username] = {entry => Time.utc}
|
info.last_read[username] = {entry => Time.utc}
|
||||||
else
|
else
|
||||||
@ -336,7 +333,6 @@ class Title
|
|||||||
last_read = info.last_read[username][entry]
|
last_read = info.last_read[username][entry]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return nil if last_read.nil?
|
|
||||||
last_read
|
last_read
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -346,6 +342,12 @@ class Title
|
|||||||
@entries[idx + 1]
|
@entries[idx + 1]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def previous_entry(current_entry_obj)
|
||||||
|
idx = @entries.index current_entry_obj
|
||||||
|
return nil if idx.nil? || idx == 0
|
||||||
|
@entries[idx - 1]
|
||||||
|
end
|
||||||
|
|
||||||
def get_continue_reading_entry(username)
|
def get_continue_reading_entry(username)
|
||||||
in_progress_entries = @entries.select do |e|
|
in_progress_entries = @entries.select do |e|
|
||||||
load_progress(username, e.title) > 0
|
load_progress(username, e.title) > 0
|
||||||
@ -360,6 +362,16 @@ class Title
|
|||||||
latest_read_entry
|
latest_read_entry
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: More concise title?
|
||||||
|
def get_last_read_for_continue_reading(username, entry_obj)
|
||||||
|
last_read = 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 = previous_entry(entry_obj)
|
||||||
|
return load_last_read username, previous_entry.title if previous_entry
|
||||||
|
end
|
||||||
|
last_read
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class TitleInfo
|
class TitleInfo
|
||||||
|
@ -75,10 +75,41 @@ class MainRouter < Router
|
|||||||
t.get_continue_reading_entry username
|
t.get_continue_reading_entry username
|
||||||
}.select Entry
|
}.select Entry
|
||||||
|
|
||||||
percentage = continue_reading_entries.map do |e|
|
percentage = continue_reading_entries.map { |e|
|
||||||
e.book.load_percentage username, e.title
|
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"
|
layout "home"
|
||||||
rescue e
|
rescue e
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
<%- unless continue_reading_entries.empty? -%>
|
<%- unless continue_reading_entries.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_entries.each_with_index do |e, i| -%>
|
<%- continue_reading.each do |cr| -%>
|
||||||
<div class="item" data-mtime="<%= e.mtime.to_unix %>" data-progress="<%= percentage[i] %>" id="<%= e.id %>">
|
<div class="item" data-mtime="<%= cr[:entry].mtime.to_unix %>" data-progress="<%= cr[:percentage] %>" id="<%= cr[:entry].id %>">
|
||||||
<a class="acard">
|
<a class="acard">
|
||||||
<div class="uk-card uk-card-default" onclick="showModal("<%= e.encoded_path %>", '<%= e.pages %>', <%= (percentage[i] * 100).round(1) %>, "<%= e.book.encoded_display_name %>", "<%= e.encoded_display_name %>", '<%= e.title_id %>', '<%= e.id %>')">
|
<div class="uk-card uk-card-default" onclick="showModal("<%= cr[:entry].encoded_path %>", '<%= cr[:entry].pages %>', <%= (cr[:percentage] * 100).round(1) %>, "<%= cr[:entry].book.encoded_display_name %>", "<%= cr[:entry].encoded_display_name %>", '<%= cr[:entry].title_id %>', '<%= cr[:entry].id %>')">
|
||||||
<div class="uk-card-media-top">
|
<div class="uk-card-media-top">
|
||||||
<img data-src="<%= e.cover_url %>" alt="" data-width data-height uk-img>
|
<img data-src="<%= cr[:entry].cover_url %>" alt="" data-width data-height uk-img>
|
||||||
</div>
|
</div>
|
||||||
<div class="uk-card-body">
|
<div class="uk-card-body">
|
||||||
<div class="uk-card-badge uk-label"><%= (percentage[i] * 100).round(1) %>%</div>
|
<div class="uk-card-badge uk-label"><%= (cr[:percentage] * 100).round(1) %>%</div>
|
||||||
<h3 class="uk-card-title break-word" data-title="<%= e.display_name.gsub("\"", """) %>"><%= e.display_name %></h3>
|
<h3 class="uk-card-title break-word" data-title="<%= cr[:entry].display_name.gsub("\"", """) %>"><%= cr[:entry].display_name %></h3>
|
||||||
<p><%= e.pages %> pages</p>
|
<p><%= cr[:entry].pages %> pages</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user