Remove unreadPerCollection from the save and re-compute it directly in ImportFromLoreLibrary

Orionik [06-05-22 - 15:08]
Remove unreadPerCollection from the save and re-compute it directly in ImportFromLoreLibrary
Because the "read" flag was in the account wide save, if you read a book from one character, the count in the other character was wrong
Anyway, computing it in the ImportFromLoreLibrary has a very small impact performance wise and it avoid potential issues with the save

Remove local variables in ImportFromLoreLibrary as it wasn't compatible with addon overriding these functions (like LoreLibrary)
Filename
Deprecation.lua
Librarian.lua
diff --git a/Deprecation.lua b/Deprecation.lua
index 00b6418..a3456f6 100644
--- a/Deprecation.lua
+++ b/Deprecation.lua
@@ -159,13 +159,12 @@ end
 function Librarian:UpdateSavedVariables()
     -- before version 3.0 there was no saveVersion
     -- In this version, the bookId is now used as identifier instead of the title (because several book have the same title)
-    -- Also unreadPerCollections was added
     if not self.globalSavedVars.saveVersion then
         self.globalSavedVars.saveVersion = 1

         -- if this player doesn't know any book yet, no need to go further
         if next(self.books) ~= nil then
-            local GetLoreBookInfo, ReadLoreBook = GetLoreBookInfo, ReadLoreBook
+            local GetLoreBookInfo = GetLoreBookInfo
             local bookIndexes = {
                 categoryIndex = 0,
                 collectionIndex = 0,
@@ -215,10 +214,8 @@ function Librarian:UpdateSavedVariables()

     if not self.localSavedVars.saveVersion then
         self.localSavedVars.saveVersion = 1
-        self.localSavedVars.unreadPerCollections = {} -- erase whatever is already inside (just in case)
-        self.unreadPerCollections = self.localSavedVars.unreadPerCollections

-        local GetLoreBookInfo, ReadLoreBook = GetLoreBookInfo, ReadLoreBook
+        local GetLoreBookInfo = GetLoreBookInfo
         local bookIndexes = {
             categoryIndex = 0,
             collectionIndex = 0,
@@ -249,11 +246,6 @@ function Librarian:UpdateSavedVariables()
                         local newCharacterBook = { title = title, timeStamp = characterBook.timeStamp, bookId = bookId }
                         table.insert(self.characterBooks, newCharacterBook)
                     end
-
-                    local book = self:FindBook(bookId)
-                    if book and book.unread then
-                        self:AddUnreadBookInCollection(bookIndexes.categoryIndex, bookIndexes.collectionIndex)
-                    end
                 end
             end
         end
diff --git a/Librarian.lua b/Librarian.lua
index a0230e1..d4e8663 100644
--- a/Librarian.lua
+++ b/Librarian.lua
@@ -30,6 +30,7 @@ function Librarian:Initialize(...)
     ZO_SortFilterList.Initialize(self, ...)

     self.masterList = {}
+    self.unreadPerCollections = {}
     self.newBookCount = 0
     self.sortHeaderGroup:SelectHeaderByKey("timeStamp")

@@ -55,9 +56,6 @@ function Librarian:Initialize(...)
     self.characterBooks = self.localSavedVars.characterBooks
     self.characterBooksCache = {}

-    if not self.localSavedVars.unreadPerCollections then self.localSavedVars.unreadPerCollections = {} end
-    self.unreadPerCollections = self.localSavedVars.unreadPerCollections
-
     self.searchBox = GetControl(LibrarianFrame, "SearchBox")
     self.searchBox:SetHandler("OnTextChanged", function() self:OnSearchTextChanged() end)
     self.search = ZO_StringSearch:New()
@@ -136,7 +134,6 @@ function Librarian:AddLoreReaderUnreadToggle()
                 if book and IsSameBook(book, LORE_READER.titleText) then
                     self:ToggleReadBook(book)
                     KEYBIND_STRIP:UpdateKeybindButtonGroup(readerKeybinds)
-                    self:RefreshAllData()
                 end
             end
         }
@@ -255,7 +252,6 @@ function Librarian:AddLoreLibraryIcons()
                 if book then
                     self:ToggleReadBook(book)
                     KEYBIND_STRIP:UpdateKeybindButtonGroup(loreLibraryKeybinds)
-                    self:RefreshAllData()
                 end
             end
         end
@@ -325,7 +321,6 @@ function Librarian:InitializeKeybindStripDescriptors()
             callback = function()
                 local book = self:FindBook(self.mouseOverRow.data.bookId)
                 self:ToggleReadBook(book)
-                self:RefreshAllData()
             end,
         }
     }
@@ -357,14 +352,13 @@ function Librarian:InitializeScene()
 end

 function Librarian:ImportFromLoreLibrary()
+    self.unreadPerCollections = {}
     local hasImportedBooks = false
     local chatEnabled = self.settings.chatEnabled
     local alertEnabled = self.settings.alertEnabled
     self.settings.chatEnabled = true
     self.settings.alertEnabled = false
     local identifier = "LibrarianLoreImport"
-    local GetLoreCategoryInfo, GetLoreCollectionInfo = GetLoreCategoryInfo, GetLoreCollectionInfo
-    local GetLoreBookInfo, ReadLoreBook = GetLoreBookInfo, ReadLoreBook
     local em = GetEventManager()
     local function endSearch()
         em:UnregisterForUpdate(identifier)
@@ -372,6 +366,8 @@ function Librarian:ImportFromLoreLibrary()
         self.settings.alertEnabled = alertEnabled
         if hasImportedBooks then
             self:RefreshAllData()
+        else
+            self:RefreshLoreLibraryData()
         end
     end

@@ -397,9 +393,13 @@ function Librarian:ImportFromLoreLibrary()
         bookIndex = bookIndex + 1
         local title, _, known, bookId = GetLoreBookInfo(categoryIndex, collectionIndex, bookIndex)
         if known then
-            if not self:FindCharacterBook(bookId) then
-                book = { bookId = bookId, title = title }
-                self:AddBook(book, false)
+            local book = self:FindBook(bookId)
+            local characterBook = self:FindCharacterBook(bookId)
+            if book and characterBook and book.unread then
+                self:AddUnreadBookInCollection(categoryIndex, collectionIndex)
+            elseif not characterBook then
+                local newBook = { bookId = bookId, title = title }
+                self:AddBook(newBook, false)
                 hasImportedBooks = true
             end
         end
@@ -410,8 +410,7 @@ function Librarian:ImportFromLoreLibrary()
         local gettime = GetGameTimeMilliseconds
         local start = gettime()
         -- do as much as possible in 5ms
-        while true do
-            if ((gettime() - start) >= 5) or not step() then break end
+        while ((gettime() - start) < 5) and step() do
         end
     end
     em:RegisterForUpdate(identifier, 0, steps)
@@ -593,7 +592,10 @@ end
 function Librarian:AddBook(book, refreshDataRightAway)
     if not self:FindCharacterBook(book.bookId) then
         local bookTitle = book.title -- storing it because AddBookToGlobalSave will delete it if we can retrieve it thanks to ESO API
-        if not self:FindBook(book.bookId) then
+        local foundBook = self:FindBook(book.bookId)
+        if foundBook then
+            book = foundBook
+        else
             self:AddBookToGlobalSave(book)
         end

@@ -649,6 +651,8 @@ function Librarian:ToggleReadBook(book)
             self.unreadPerCollections[categoryIndex][collectionIndex] = self.unreadPerCollections[categoryIndex][collectionIndex] - 1
         end
     end
+
+    self:RefreshAllData()
 end

 function Librarian:Toggle()
@@ -675,7 +679,10 @@ end

 function Librarian:RefreshAllData()
     self:RefreshData()
+    self:RefreshLoreLibraryData()
+end

+function Librarian:RefreshLoreLibraryData()
     if self.settings.showUnreadIndicatorInLoreLibrary then
         local selectedCategoryIndex = LORE_LIBRARY:GetSelectedCategoryIndex()
         local selectedCollectionIndex = LORE_LIBRARY:GetSelectedCollectionIndex()