Skip to content

Commit

Permalink
Add Recently Added to home
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredlt committed Jun 5, 2020
1 parent 49193b9 commit 808074e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/library.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ end
class Entry
property zip_path : String, book : Title, title : String,
size : String, pages : Int32, id : String, title_id : String,
encoded_path : String, encoded_title : String, mtime : Time
encoded_path : String, encoded_title : String, mtime : Time,
date_added : Time

def initialize(path, @book, @title_id, storage)
@zip_path = path
Expand All @@ -33,6 +34,7 @@ class Entry
file.close
@id = storage.get_id @zip_path, false
@mtime = File.info(@zip_path).modification_time
@date_added = load_date_added
end

def to_json(json : JSON::Builder)
Expand Down Expand Up @@ -89,6 +91,20 @@ class Entry
end
end
end

private def load_date_added
date_added = nil
TitleInfo.new @book.dir do |info|
info_da = info.date_added[@title]?
if info_da.nil?
date_added = info.date_added[@title] = ctime @zip_path
info.save
else
date_added = info_da
end
end
date_added.not_nil! # is it ok to set not_nil! here?
end
end

class Title
Expand Down Expand Up @@ -384,6 +400,7 @@ class TitleInfo
property cover_url = ""
property entry_cover_url = {} of String => String
property last_read = {} of String => Hash(String, Time)
property date_added = {} of String => Time

@[JSON::Field(ignore: true)]
property dir : String = ""
Expand Down Expand Up @@ -486,7 +503,7 @@ class Library
get_continue_reading_entry username, t
}.select Entry

continue_reading = continue_reading_entries.map_with_index { |e, i|
continue_reading = continue_reading_entries.map { |e|
{
entry: e,
percentage: e.book.load_percentage(username, e.title),
Expand All @@ -495,12 +512,26 @@ class Library
}

# Sort by by last_read, most recent first (nils at the end)
continue_reading.sort! do |a, b|
continue_reading.sort! { |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!
}[0..11]
end

def get_recently_added_entries(username)
entries = [] of Entry
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)
}
}
recently_added.sort! { |a, b| b[:entry].date_added <=> a[:entry].date_added }[0..11]
end


Expand Down
1 change: 1 addition & 0 deletions src/routes/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class MainRouter < Router
begin
username = get_username env
continue_reading = @context.library.get_continue_reading_entries username
recently_added = @context.library.get_recently_added_entries username

layout "home"
rescue e
Expand Down
22 changes: 22 additions & 0 deletions src/views/home.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@
</div>
<%- end -%>

<%- unless recently_added.empty? -%>
<h2 class="uk-title home-headings">Recently Added</h2>
<div id="item-container-continue" class="uk-child-width-1-4@m uk-child-width-1-2" uk-grid>
<%- recently_added.each do |ra| -%>
<div class="item" data-mtime="<%= ra[:entry].mtime.to_unix %>" data-progress="<%= ra[:percentage] %>" id="<%= ra[:entry].id %>">
<a class="acard">
<div class="uk-card uk-card-default" onclick="showModal(&quot;<%= ra[:entry].encoded_path %>&quot;, '<%= ra[:entry].pages %>', <%= (ra[:percentage] * 100).round(1) %>, &quot;<%= ra[:entry].book.encoded_display_name %>&quot;, &quot;<%= ra[:entry].encoded_display_name %>&quot;, '<%= ra[:entry].title_id %>', '<%= ra[:entry].id %>')">
<div class="uk-card-media-top">
<img data-src="<%= ra[:entry].cover_url %>" alt="" data-width data-height uk-img>
</div>
<div class="uk-card-body">
<div class="uk-card-badge uk-label"><%= (ra[:percentage] * 100).round(1) %>%</div>
<h3 class="uk-card-title break-word" data-title="<%= ra[:entry].display_name.gsub("\"", "&quot;") %>"><%= ra[:entry].display_name %></h3>
<p><%= ra[:entry].pages %> pages</p>
</div>
</div>
</a>
</div>
<%- end -%>
</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 808074e

Please sign in to comment.