Add SortOptions.from_info_json

This commit is contained in:
Alex Ling 2020-07-15 15:33:26 +00:00
parent 69b06a8352
commit ba16c3db2f
2 changed files with 14 additions and 24 deletions

View File

@ -40,6 +40,16 @@ class SortOptions
self.new method, ascend
end
def self.from_info_json(dir, username)
opt = SortOptions.new
TitleInfo.new dir do |info|
if info.sort_by.has_key? username
opt = SortOptions.from_tuple info.sort_by[username]
end
end
opt
end
def to_tuple
{@method.to_s.underscore, ascend}
end
@ -489,7 +499,7 @@ class Title
# When `opt` is not nil, it saves the options to info.json
def sorted_entries(username, opt : SortOptions? = nil)
if opt.nil?
opt = load_sort_options username
opt = SortOptions.from_info_json @dir, username
else
TitleInfo.new @dir do |info|
info.sort_by[username] = opt.to_tuple
@ -528,16 +538,6 @@ class Title
ary
end
def load_sort_options(username)
opt = SortOptions.new
TitleInfo.new @dir do |info|
if info.sort_by.has_key? username
opt = SortOptions.from_tuple info.sort_by[username]
end
end
opt
end
# === helper methods ===
# Gets the last read entry in the title. If the entry has been completed,
@ -793,7 +793,7 @@ class Library
def sorted_titles(username, opt : SortOptions? = nil)
if opt.nil?
opt = load_sort_options username
opt = SortOptions.from_info_json @dir, username
else
TitleInfo.new @dir do |info|
info.sort_by[username] = opt.to_tuple
@ -825,14 +825,4 @@ class Library
ary
end
def load_sort_options(username)
opt = SortOptions.new
TitleInfo.new @dir do |info|
if info.sort_by.has_key? username
opt = SortOptions.from_tuple info.sort_by[username]
end
end
opt
end
end

View File

@ -41,7 +41,7 @@ class MainRouter < Router
begin
username = get_username env
sort_opt = @context.library.load_sort_options username
sort_opt = SortOptions.from_info_json @context.library.dir, username
get_sort_opt
titles = @context.library.sorted_titles username, sort_opt
@ -59,7 +59,7 @@ class MainRouter < Router
title = (@context.library.get_title env.params.url["title"]).not_nil!
username = get_username env
sort_opt = title.load_sort_options username
sort_opt = SortOptions.from_info_json title.dir, username
get_sort_opt
entries = title.sorted_entries username, sort_opt