diff --git a/src/library/library.cr b/src/library/library.cr index cba0421..3813f14 100644 --- a/src/library/library.cr +++ b/src/library/library.cr @@ -30,6 +30,41 @@ class Library @title_ids.map { |tid| self.get_title!(tid) } end + def sorted_titles(username, opt : SortOptions? = nil) + if opt.nil? + opt = SortOptions.from_info_json @dir, username + else + TitleInfo.new @dir do |info| + info.sort_by[username] = opt.to_tuple + info.save + end + end + + # This is a hack to bypass a compiler bug + ary = titles + + case opt.not_nil!.method + when .time_modified? + ary.sort! { |a, b| (a.mtime <=> b.mtime).or \ + compare_numerically a.title, b.title } + when .progress? + ary.sort! do |a, b| + (a.load_percentage(username) <=> b.load_percentage(username)).or \ + compare_numerically a.title, b.title + end + else + unless opt.method.auto? + Logger.warn "Unknown sorting method #{opt.not_nil!.method}. Using " \ + "Auto instead" + end + ary.sort! { |a, b| compare_numerically a.title, b.title } + end + + ary.reverse! unless opt.not_nil!.ascend + + ary + end + def deep_titles titles + titles.map { |t| t.deep_titles }.flatten end @@ -83,7 +118,7 @@ class Library cr_entries = deep_titles .map { |t| t.get_last_read_entry username } # Select elements with type `Entry` from the array and ignore all `Nil`s - .select(Entry)[0..11] + .select(Entry)[0...ENTRIES_IN_HOME_SECTIONS] .map { |e| # Get the last read time of the entry. If it hasn't been started, get # the last read time of the previous entry @@ -143,41 +178,20 @@ class Library end end - recently_added[0..11] + recently_added[0...ENTRIES_IN_HOME_SECTIONS] end - def sorted_titles(username, opt : SortOptions? = nil) - if opt.nil? - opt = SortOptions.from_info_json @dir, username - else - TitleInfo.new @dir do |info| - info.sort_by[username] = opt.to_tuple - info.save - end - end - - # This is a hack to bypass a compiler bug - ary = titles - - case opt.not_nil!.method - when .time_modified? - ary.sort! { |a, b| (a.mtime <=> b.mtime).or \ - compare_numerically a.title, b.title } - when .progress? - ary.sort! do |a, b| - (a.load_percentage(username) <=> b.load_percentage(username)).or \ - compare_numerically a.title, b.title - end - else - unless opt.method.auto? - Logger.warn "Unknown sorting method #{opt.not_nil!.method}. Using " \ - "Auto instead" - end - ary.sort! { |a, b| compare_numerically a.title, b.title } - end - - ary.reverse! unless opt.not_nil!.ascend - - ary + def get_start_reading_titles(username) + # Here we are not using `deep_titles` as it may cause unexpected behaviors + # For example, consider the following nested titles: + # - One Puch Man + # - Vol. 1 + # - Vol. 2 + # If we use `deep_titles`, the start reading section might include `Vol. 2` + # when the user hasn't started `Vol. 1` yet + titles + .select { |t| t.load_percentage(username) == 0 } + .sample(ENTRIES_IN_HOME_SECTIONS) + .shuffle end end diff --git a/src/routes/main.cr b/src/routes/main.cr index 5801297..4c55472 100644 --- a/src/routes/main.cr +++ b/src/routes/main.cr @@ -103,6 +103,7 @@ class MainRouter < Router continue_reading = @context .library.get_continue_reading_entries username recently_added = @context.library.get_recently_added_entries username + start_reading = @context.library.get_start_reading_titles username titles = @context.library.titles new_user = !titles.any? { |t| t.load_percentage(username) > 0 } empty_library = titles.size == 0 diff --git a/src/util/util.cr b/src/util/util.cr index 1c7330c..9019720 100644 --- a/src/util/util.cr +++ b/src/util/util.cr @@ -1,6 +1,7 @@ -IMGS_PER_PAGE = 5 -UPLOAD_URL_PREFIX = "/uploads" -STATIC_DIRS = ["/css", "/js", "/img", "/favicon.ico"] +IMGS_PER_PAGE = 5 +ENTRIES_IN_HOME_SECTIONS = 8 +UPLOAD_URL_PREFIX = "/uploads" +STATIC_DIRS = ["/css", "/js", "/img", "/favicon.ico"] def random_str UUID.random.to_s.gsub "-", "" diff --git a/src/views/home.html.ecr b/src/views/home.html.ecr index cb0f53e..4f2eae1 100644 --- a/src/views/home.html.ecr +++ b/src/views/home.html.ecr @@ -50,6 +50,17 @@ <%- end -%> + <%- unless start_reading.empty? -%> +

Start Reading

+
+ <%- start_reading.each do |t| -%> + <% item = t %> + <% progress = 0.0 %> + <%= render_component "card" %> + <%- end -%> +
+ <%- end -%> + <%- unless recently_added.empty? -%>

Recently Added