From ee75abd4a0c8cdb79f48d8bc9c1c6e2f01d38562 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Sun, 18 Dec 2022 18:04:00 +0200 Subject: [PATCH] Statistics: show correct number of notes in a book (#9928) --- .../apps/reader/modules/readerbookmark.lua | 40 +++++++++++++------ .../apps/reader/modules/readerhighlight.lua | 23 ++++------- plugins/statistics.koplugin/main.lua | 25 +++++++----- 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/frontend/apps/reader/modules/readerbookmark.lua b/frontend/apps/reader/modules/readerbookmark.lua index 28d3c323a..4b83fe6c1 100644 --- a/frontend/apps/reader/modules/readerbookmark.lua +++ b/frontend/apps/reader/modules/readerbookmark.lua @@ -933,10 +933,10 @@ function ReaderBookmark:isBookmarkAdded(item) end function ReaderBookmark:removeHighlight(item) - self:removeBookmark(item) if item.pos0 then self.ui:handleEvent(Event:new("Unhighlight", item)) else -- dogear bookmark, update it in case we removed a bookmark for current page + self:removeBookmark(item) self:setDogearVisibility(self:getCurrentPageNumber()) end end @@ -947,13 +947,19 @@ function ReaderBookmark:removeBookmark(item, reset_auto_text_only) -- not search on one side of one it found on page, where item could be. -- Fallback to do a full scan. local index = self:getBookmarkIndexBinarySearch(item) or self:getBookmarkIndexFullScan(item) - local v = self.bookmarks[index] + local bookmark = self.bookmarks[index] if reset_auto_text_only then - if self:isBookmarkAutoText(v) then - v.text = nil + if self:isBookmarkAutoText(bookmark) then + bookmark.text = nil end else - self.ui:handleEvent(Event:new("BookmarkRemoved", v)) + local bookmark_type = item.type or self:getBookmarkType(bookmark) + if bookmark_type == "highlight" then + self.ui:handleEvent(Event:new("DelHighlight")) + elseif bookmark_type == "note" then + self.ui:handleEvent(Event:new("DelNote")) + end + self.ui:handleEvent(Event:new("BookmarkRemoved", bookmark)) table.remove(self.bookmarks, index) self.view.footer:onUpdateFooter(self.view.footer_visible) end @@ -987,6 +993,7 @@ function ReaderBookmark:setBookmarkNote(item, from_highlight, is_new_note, new_t bm.text = self:getBookmarkAutoText(bm) end bookmark = util.tableDeepCopy(bm) + bookmark.type = self:getBookmarkType(bookmark) bookmark.text_orig = bm.text or bm.notes bookmark.mandatory = self:getBookmarkPageString(bm.page) self.ui:handleEvent(Event:new("BookmarkEdited", bm)) @@ -1031,11 +1038,22 @@ function ReaderBookmark:setBookmarkNote(item, from_highlight, is_new_note, new_t text = _("Save"), is_enter_default = true, callback = function() - local value = self.input:getInputValue() + local value = self.input:getInputText() if value == "" then -- blank input resets the 'text' field to auto-text value = self:getBookmarkAutoText(bookmark) end bookmark.text = value or bookmark.notes + local bookmark_type = bookmark.type + bookmark.type = self:getBookmarkType(bookmark) + if bookmark_type ~= bookmark.type then + if bookmark_type == "highlight" then + self.ui:handleEvent(Event:new("DelHighlight")) + self.ui:handleEvent(Event:new("AddNote")) + else + self.ui:handleEvent(Event:new("AddHighlight")) + self.ui:handleEvent(Event:new("DelNote")) + end + end local index = self:getBookmarkIndexBinarySearch(bookmark) or self:getBookmarkIndexFullScan(bookmark) local bm = self.bookmarks[index] bm.text = value @@ -1047,7 +1065,6 @@ function ReaderBookmark:setBookmarkNote(item, from_highlight, is_new_note, new_t UIManager:setDirty(self.dialog, "ui") -- refresh note marker end else - bookmark.type = self:getBookmarkType(bookmark) bookmark.text_orig = bookmark.text bookmark.text = DISPLAY_PREFIX[bookmark.type] .. bookmark.text self.refresh() @@ -1292,15 +1309,14 @@ function ReaderBookmark:getNumberOfBookmarks() end function ReaderBookmark:getNumberOfHighlightsAndNotes() -- for Statistics plugin - local highlights = 0 -- Statistics show highlights+notes total amount + local highlights = 0 local notes = 0 for _, v in ipairs(self.bookmarks) do local bm_type = self:getBookmarkType(v) - if bm_type ~= "bookmark" then + if bm_type == "highlight" then highlights = highlights + 1 - if bm_type == "note" then - notes = notes + 1 - end + elseif bm_type == "note" then + notes = notes + 1 end end return highlights, notes diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index 69371e93e..60c70c2c7 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -590,7 +590,7 @@ function ReaderHighlight:clear(clear_id) -- might happen if scheduled and run after document is closed return end - if self.ui.document.info.has_pages then + if self.ui.paging then self.view.highlight.temp = {} else self.ui.document:clearSelection() @@ -637,7 +637,7 @@ function ReaderHighlight:onTap(_, ges) local cleared = self.hold_pos and self:clear() -- We only care about potential taps on existing highlights, not on taps that closed a highlight menu. if not cleared and ges then - if self.ui.document.info.has_pages then + if self.ui.paging then return self:onTapPageSavedHighlight(ges) else return self:onTapXPointerSavedHighlight(ges) @@ -725,10 +725,7 @@ function ReaderHighlight:onTapXPointerSavedHighlight(ges) end function ReaderHighlight:updateHighlight(page, index, side, direction, move_by_char) - if self.ui.document.info.has_pages then -- we do this only if it's epub file - return - end - + if self.ui.paging then return end local highlight = self.view.highlight.saved[page][index] local highlight_time = highlight.datetime local highlight_beginning = highlight.pos0 @@ -1108,7 +1105,7 @@ function ReaderHighlight:onHold(arg, ges) end end - if self.ui.document.info.has_pages then + if self.ui.paging then self.view.highlight.temp[self.hold_pos.page] = self.selected_text.sboxes -- Unfortunately, getWordFromPosition() may not return good coordinates, -- so refresh the whole page @@ -1338,9 +1335,7 @@ function ReaderHighlight:getSelectedWordContext(nb_words) end function ReaderHighlight:viewSelectionHTML(debug_view, no_css_files_buttons) - if self.ui.document.info.has_pages then - return - end + if self.ui.paging then return end if self.selected_text and self.selected_text.pos0 and self.selected_text.pos1 then -- For available flags, see the "#define WRITENODEEX_*" in crengine/src/lvtinydom.cpp -- Start with valid and classic displayed HTML (with only block nodes indented), @@ -1670,7 +1665,7 @@ function ReaderHighlight:onUnhighlight(bookmark_item) sel_text = cleanupSelectedText(self.selected_text.text) sel_pos0 = self.selected_text.pos0 end - if self.ui.document.info.has_pages then -- We can safely use page + if self.ui.paging then -- We can safely use page -- As we may have changed spaces and hyphens handling in the extracted -- text over the years, check text identities with them removed local sel_text_cleaned = sel_text:gsub("[ -]", ""):gsub("\xC2\xAD", "") @@ -1828,7 +1823,6 @@ function ReaderHighlight:addNote(text) self:editHighlight(page, index, true, text) UIManager:close(self.edit_highlight_dialog) self.edit_highlight_dialog = nil - self.ui:handleEvent(Event:new("AddNote")) end function ReaderHighlight:lookupWikipedia() @@ -1860,7 +1854,6 @@ function ReaderHighlight:onHighlightDictLookup() end function ReaderHighlight:deleteHighlight(page, i, bookmark_item) - self.ui:handleEvent(Event:new("DelHighlight")) logger.dbg("delete highlight", page, i) -- The per-page table is a pure array local removed = table.remove(self.view.highlight.saved[page], i) @@ -1872,7 +1865,7 @@ function ReaderHighlight:deleteHighlight(page, i, bookmark_item) self.ui.bookmark:removeBookmark(bookmark_item) else self.ui.bookmark:removeBookmark({ - page = self.ui.document.info.has_pages and page or removed.pos0, + page = self.ui.paging and page or removed.pos0, datetime = removed.datetime, }) end @@ -1883,7 +1876,7 @@ end function ReaderHighlight:editHighlight(page, i, is_new_note, text) local item = self.view.highlight.saved[page][i] self.ui.bookmark:setBookmarkNote({ - page = self.ui.document.info.has_pages and page or item.pos0, + page = self.ui.paging and page or item.pos0, datetime = item.datetime, }, true, is_new_note, text) end diff --git a/plugins/statistics.koplugin/main.lua b/plugins/statistics.koplugin/main.lua index aca136499..194a85297 100644 --- a/plugins/statistics.koplugin/main.lua +++ b/plugins/statistics.koplugin/main.lua @@ -191,7 +191,6 @@ function ReaderStatistics:initData() self.data.md5 = self:partialMd5(self.document.file) end -- Update these numbers to what's actually stored in the settings - -- (not that "notes" is invalid and does not represent edited highlights) self.data.highlights, self.data.notes = self.ui.bookmark:getNumberOfHighlightsAndNotes() self.id_curr_book = self:getIdBookDB() self.book_read_pages, self.book_read_time = self:getPageTimeTotalStats(self.id_curr_book) @@ -1408,7 +1407,7 @@ function ReaderStatistics:getCurrentStat() local current_duration, current_pages = self:getCurrentBookStats() local conn = SQ3.open(db_location) - local highlights, notes = conn:rowexec(string.format("SELECT highlights, notes FROM book WHERE id = %d;", id_book)) -- luacheck: no unused + local highlights, notes = conn:rowexec(string.format("SELECT highlights, notes FROM book WHERE id = %d;", id_book)) local sql_stmt = [[ SELECT count(*) FROM ( @@ -1551,9 +1550,9 @@ function ReaderStatistics:getCurrentStat() { _("Pages read"), string.format("%d (%d%%)", total_read_pages, Math.round(100*total_read_pages/self.data.pages)) }, { _("Average time per page"), avg_page_time_string, separator = true }, - -- Highlights - { _("Book highlights"), tonumber(highlights) } - -- { _("Book notes"), tonumber(notes) }, -- not accurate, don't show it + -- Highlights and notes + { _("Book highlights"), tonumber(highlights) }, + { _("Book notes"), tonumber(notes) }, } end @@ -1577,7 +1576,7 @@ function ReaderStatistics:getBookStat(id_book) -- Show "?" when these values are not known (they will be -- fixed next time this book is opened). highlights = highlights and tonumber(highlights) or "?" - notes = notes and tonumber(notes) or "?" -- luacheck: no unused + notes = notes and tonumber(notes) or "?" sql_stmt = [[ SELECT count(*) @@ -1668,8 +1667,8 @@ function ReaderStatistics:getBookStat(id_book) { _("Average time per page"), datetime.secondsToClockDuration(user_duration_format, avg_time_per_page, false, true), separator = true }, -- Highlights - { _("Book highlights"), highlights } - -- { _("Book notes"), notes }, -- not accurate, don't show it + { _("Book highlights"), highlights }, + { _("Book notes"), notes }, } end @@ -2557,9 +2556,7 @@ end function ReaderStatistics:onDelHighlight() if self.settings.is_enabled then - if self.data.highlights > 0 then - self.data.highlights = self.data.highlights - 1 - end + self.data.highlights = self.data.highlights - 1 end end @@ -2569,6 +2566,12 @@ function ReaderStatistics:onAddNote() end end +function ReaderStatistics:onDelNote() + if self.settings.is_enabled then + self.data.notes = self.data.notes - 1 + end +end + -- Triggered by auto_save_settings_interval_minutes function ReaderStatistics:onSaveSettings() if not self:isDocless() then