From d2ad7fef7775ec3fafb0b2c3c1ee8a8fb30064b2 Mon Sep 17 00:00:00 2001 From: Jared Turner Date: Fri, 29 May 2020 13:26:47 +0100 Subject: [PATCH] WIP last_read property for Entries --- src/library.cr | 22 ++++++++++++++++++++++ src/routes/main.cr | 1 + 2 files changed, 23 insertions(+) diff --git a/src/library.cr b/src/library.cr index a51996f..a7ef914 100644 --- a/src/library.cr +++ b/src/library.cr @@ -287,6 +287,15 @@ class Title else info.progress[username][entry] = page end + # should this be a separate method? + # 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 @@ -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? + last_read + end + def next_entry(current_entry_obj) idx = @entries.index current_entry_obj return nil if idx.nil? || idx == @entries.size - 1 @@ -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 = "" diff --git a/src/routes/main.cr b/src/routes/main.cr index 90df4df..c57ead7 100644 --- a/src/routes/main.cr +++ b/src/routes/main.cr @@ -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 end layout "home"