Skip to content

Commit

Permalink
WIP last_read property for Entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredlt committed May 29, 2020
1 parent ddb6a86 commit d2ad7fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/library.cr
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ class Title
else
info.progress[username][entry] = page
end
# should this be a separate method?

This comment has been minimized.

Copy link
@hkalexling

hkalexling May 29, 2020

Member

I think leaving it like this is fine. Having to call two methods when updating the progress is troublesome.

# eg. def save_last_read(username, entry)
# if so, we would need to open the json file twice every
# time we save. Does that matter?
if info.last_read[username]?.nil?
info.last_read[username] = {entry => Time.utc}
else
info.last_read[username][entry] = Time.utc
end
info.save
end
end
Expand Down Expand Up @@ -319,6 +328,18 @@ class Title
read_pages / total_pages
end

def load_last_read(username, entry)
last_read = nil
TitleInfo.new @dir do |info|
unless info.last_read[username]?.nil? ||
info.last_read[username][entry]?.nil?
last_read = info.last_read[username][entry]
end
end
return nil if last_read.nil?

This comment has been minimized.

Copy link
@hkalexling

hkalexling May 29, 2020

Member

This line is redundant. If last_read is nil, the next line would just return nil.

last_read
end

def next_entry(current_entry_obj)
idx = @entries.index current_entry_obj
return nil if idx.nil? || idx == @entries.size - 1
Expand Down Expand Up @@ -350,6 +371,7 @@ class TitleInfo
property entry_display_name = {} of String => String
property cover_url = ""
property entry_cover_url = {} of String => String
property last_read = {} of String => Hash(String, Time)

@[JSON::Field(ignore: true)]
property dir : String = ""
Expand Down
1 change: 1 addition & 0 deletions src/routes/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class MainRouter < Router

percentage = continue_reading_entries.map do |e|
e.book.load_percentage username, e.title
pp e.book.load_last_read username, e.title

This comment has been minimized.

Copy link
@hkalexling

hkalexling May 29, 2020

Member

It failed to build because here percentage got assigned the value returned by Entry#load_last_read, which is of type Time. If you wish to print out the last read time of all continue reading entries, you could do

continue_reading_entries.map do |e|
  pp e.book.load_last_read username, e.title
end

outside of the percentage block.

This comment has been minimized.

Copy link
@jaredlt

jaredlt May 29, 2020

Author Collaborator

Ah gosh, it was staring me in the face! Thanks for being a second pair of eyes :)

end

layout "home"
Expand Down

0 comments on commit d2ad7fe

Please sign in to comment.