diff --git a/src/library.cr b/src/library.cr index 335a3e2..6764655 100644 --- a/src/library.cr +++ b/src/library.cr @@ -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 @@ -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) @@ -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 @@ -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 = "" @@ -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), @@ -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 diff --git a/src/routes/main.cr b/src/routes/main.cr index c2f3f1f..e1ce580 100644 --- a/src/routes/main.cr +++ b/src/routes/main.cr @@ -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 diff --git a/src/views/home.ecr b/src/views/home.ecr index bbd8541..e1f8243 100644 --- a/src/views/home.ecr +++ b/src/views/home.ecr @@ -20,6 +20,28 @@ <%- end -%> +<%- unless recently_added.empty? -%> +

Recently Added

+
+ <%- recently_added.each do |ra| -%> +
+ +
+
+ +
+
+
<%= (ra[:percentage] * 100).round(1) %>%
+

"><%= ra[:entry].display_name %>

+

<%= ra[:entry].pages %> pages

+
+
+
+
+ <%- end -%> +
+<%- end -%> +