Improve performance for library and title pages

This commit is contained in:
Alex Ling 2020-06-24 16:29:34 +00:00
parent 9d76ca8c24
commit c87b96dd0b
2 changed files with 24 additions and 2 deletions

View File

@ -374,7 +374,7 @@ class Title
end
def deep_read_page_count(username) : Int32
entries.map { |e| e.load_progress username }.sum +
load_progress_for_all_entries(username).sum +
titles.map { |t| t.deep_read_page_count username }.flatten.sum
end
@ -402,6 +402,28 @@ class Title
end
end
def load_progress_for_all_entries(username)
progress = {} of String => Int32
TitleInfo.new @dir do |info|
progress = info.progress[username]?
end
@entries.map do |e|
info_progress = 0
if progress && progress.has_key? e.title
info_progress = [progress[e.title], e.pages].min
end
info_progress
end
end
def load_percentage_for_all_entries(username)
progress = load_progress_for_all_entries username
@entries.map_with_index do |e, i|
progress[i] / e.pages
end
end
# === helper methods ===
# Gets the last read entry in the title. If the entry has been completed,

View File

@ -53,7 +53,7 @@ class MainRouter < Router
begin
title = (@context.library.get_title env.params.url["title"]).not_nil!
username = get_username env
percentage = title.entries.map &.load_percentage username
percentage = title.load_percentage_for_all_entries username
title_percentage = title.titles.map &.load_percentage username
layout "title"
rescue e