Add button to allow search in only title or text+title

Orionik [07-25-22 - 14:26]
Add button to allow search in only title or text+title
Copy previous save in other save var before doing the deprecation in order to allow a rollback in case of a mistake
Reduce the size of the text in the UI in order to be able to fully display the title and the category of each book
Filename
Deprecation.lua
Librarian.lua
Librarian.txt
Librarian.xml
lang/en.lua
lang/fr.lua
diff --git a/Deprecation.lua b/Deprecation.lua
index c92b36b..1a75a46 100644
--- a/Deprecation.lua
+++ b/Deprecation.lua
@@ -1,3 +1,15 @@
+local function DeepCopy(originalTable)
+    local copyTable
+    if type(originalTable) == 'table' then
+        copyTable = {}
+        for key, value in pairs(originalTable) do
+            copyTable[key] = DeepCopy(value)
+        end
+    else
+        copyTable = originalTable
+    end
+    return copyTable
+end

 local function GetNextBookIndex(bookIndexes)
     if bookIndexes.bookIndex >= bookIndexes.totalBooks then
@@ -162,6 +174,10 @@ function Librarian:UpdateSavedVariables()
     if not self.globalSavedVars.saveVersion then
         self.globalSavedVars.saveVersion = 1

+        -- let's copy the current save into another file just in case something wrong happen during deprecation
+        local backupGlobalSavedVars = ZO_SavedVars:NewAccountWide("Librarian_SavedVariables_Backup", 1, nil, {}, nil)
+        backupGlobalSavedVars.books = DeepCopy(self.books)
+
         -- if this player doesn't know any book yet, no need to go further
         if next(self.books) ~= nil then
             local GetLoreBookInfo = GetLoreBookInfo
@@ -215,6 +231,10 @@ function Librarian:UpdateSavedVariables()
     if not self.localSavedVars.saveVersion then
         self.localSavedVars.saveVersion = 1

+        -- let's copy the current save into another file just in case something wrong happen during deprecation
+        local backupLocalSavedVars = ZO_SavedVars:New("Librarian_SavedVariables_Backup", 1, nil, {}, nil)
+        backupLocalSavedVars.characterBooks = DeepCopy(self.characterBooks)
+
         local GetLoreBookInfo = GetLoreBookInfo
         local bookIndexes = {
             categoryIndex = 0,
diff --git a/Librarian.lua b/Librarian.lua
index 34b03e1..2731080 100644
--- a/Librarian.lua
+++ b/Librarian.lua
@@ -19,6 +19,7 @@ local ENTRY_SORT_KEYS =
     ["title"] = { },
     ["unread"] = { tiebreaker = "timeStamp" },
     ["timeStamp"] = { tiebreaker = "title" },
+    ["category"] = { tiebreaker = "title" },
     ["wordCount"] = { tiebreaker = "title" }
 }

@@ -80,6 +81,23 @@ function Librarian:Initialize(...)
     ZO_CheckButton_SetToggleFunction(showAllBooks, OnShowAllBooksClicked)
     ZO_CheckButton_SetCheckState(showAllBooks, GetShowAllBooks())

+    local function OnSearchOnlyTitleClicked(checkButton, isChecked)
+        if isChecked then
+            LibrarianFrameSearchLabel:SetText(GetString(LIBRARIAN_TITLE_SEARCH))
+        else
+            LibrarianFrameSearchLabel:SetText(GetString(LIBRARIAN_FULLTEXT_SEARCH))
+        end
+        self.searchOnlyTitle = isChecked
+
+        if self.searchBox:GetText() ~= "" then
+            self:RefreshFilters()
+        end
+    end
+
+    local searchOnlyTitleControl = LibrarianFrameSearchOnlyTitle
+    ZO_CheckButton_SetToggleFunction(searchOnlyTitleControl, OnSearchOnlyTitleClicked)
+    self.searchOnlyTitle = false
+
     self:RefreshData()
     self:InitializeKeybindStripDescriptors()
     self:InitializeScene()
@@ -473,6 +491,10 @@ function Librarian:BuildMasterList()
                 data.category = GetLoreCollectionInfo(data.categoryIndex, data.collectionIndex)
             end

+            if not data.category or data.category == "" then
+                data.category = GetString(LIBRARIAN_NO_CATEGORY)
+            end
+
             if not data.wordCount then
                 local wordCount = 0
                 if data.body then
@@ -543,11 +565,7 @@ function Librarian:SetupBookRow(control, data)
     control.title:SetText(data.title)

     control.category.normalColor = ZO_NORMAL_TEXT
-    if data.category then
-        control.category:SetText(data.category)
-    else
-        control.category:SetText(GetString(LIBRARIAN_NO_CATEGORY))
-    end
+    control.category:SetText(data.category)

     control.wordCount.normalColor = ZO_NORMAL_TEXT
     control.wordCount:SetText(data.wordCount)
@@ -562,7 +580,7 @@ function Librarian:ProcessBookEntry(stringSearch, data, searchTerm, cache)
         return true
     end

-    if data.body and zo_plainstrfind(data.body:lower(), lowerSearchTerm) then
+    if not self.searchOnlyTitle and data.body and zo_plainstrfind(data.body:lower(), lowerSearchTerm) then
         return true
     end

diff --git a/Librarian.txt b/Librarian.txt
index 17f0cd3..5f3cd3c 100644
--- a/Librarian.txt
+++ b/Librarian.txt
@@ -2,7 +2,7 @@
 ## Author: Orionik, |c4EFFF6Calia1120|r, Flamage
 ## Version: 1.0
 ## APIVersion: 101034
-## SavedVariables: Librarian_SavedVariables
+## SavedVariables: Librarian_SavedVariables Librarian_SavedVariables_Backup
 ## DependsOn: LibAddonMenu-2.0

 ; Load language strings
diff --git a/Librarian.xml b/Librarian.xml
index a9fc1bb..8646a00 100644
--- a/Librarian.xml
+++ b/Librarian.xml
@@ -17,16 +17,16 @@
                     <Dimensions x="30" y="30" />
                     <Anchor point="TOPLEFT" relativeTo="$(parent)" offsetX="19" />
                 </Texture>
-                <Label name="$(parent)Found" font="ZoFontWinH3" wrapMode="ELLIPSIS">
+                <Label name="$(parent)Found" font="ZoFontGame" wrapMode="ELLIPSIS">
                     <Anchor point="TOPLEFT" relativeTo="$(parent)" offsetX="70" offsetY="1" />
                 </Label>
-                <Label name="$(parent)WordCount" font="ZoFontWinH3" wrapMode="ELLIPSIS">
+                <Label name="$(parent)WordCount" font="ZoFontGame" wrapMode="ELLIPSIS">
                     <Anchor point="TOPRIGHT" relativeTo="$(parent)" offsetY="1" />
                 </Label>
-                <Label name="$(parent)Category" font="ZoFontWinH3" wrapMode="ELLIPSIS">
-                    <Anchor point="TOPLEFT" relativeTo="$(parent)" offsetX="650" offsetY="1" />
+                <Label name="$(parent)Category" font="ZoFontGame" wrapMode="ELLIPSIS">
+                    <Anchor point="TOPLEFT" relativeTo="$(parent)" offsetX="620" offsetY="1" />
                 </Label>
-                <Label name="$(parent)Title" font="ZoFontWinH3" wrapMode="ELLIPSIS">
+                <Label name="$(parent)Title" font="ZoFontGame" wrapMode="ELLIPSIS">
                     <Anchor point="TOPLEFT" relativeTo="$(parent)" offsetX="200" offsetY="1" />
                 </Label>
             </Controls>
@@ -63,6 +63,11 @@
                 <Label name="$(parent)SearchLabel" text="LIBRARIAN_FULLTEXT_SEARCH" font="ZoFontGameLargeBold" color="INTERFACE_COLOR_TYPE_TEXT_COLORS:INTERFACE_TEXT_COLOR_NORMAL">
                     <Anchor point="TOPRIGHT" relativePoint="TOPLEFT" relativeTo="$(parent)Search" offsetX="-5" offsetY="3"/>
                 </Label>
+
+                <Button name="$(parent)SearchOnlyTitle" inherits="ZO_CheckButton">
+                    <Anchor point="TOPRIGHT" relativePoint="TOPLEFT" relativeTo="$(parent)SearchLabel" offsetX="-5" offsetY="2" />
+                </Button>
+
                 <Control name="$(parent)Headers">
                     <Anchor point="TOPLEFT" relativeTo="$(parent)" offsetY="40" />
                     <Anchor point="TOPRIGHT" relativeTo="$(parent)" offsetY="40" />
@@ -94,7 +99,7 @@
                                 ZO_SortHeader_Initialize(self, GetString(LIBRARIAN_SORT_TYPE_CATEGORY), "category", ZO_SORT_ORDER_UP, TEXT_ALIGN_LEFT, "ZoFontGameLargeBold")
                             </OnInitialized>
                             <Anchor point="TOPRIGHT" relativeTo="$(parent)WordCount" relativePoint="TOPLEFT" />
-                            <Dimensions x="200" y="32" />
+                            <Dimensions x="230" y="32" />
                         </Control>
                         <Control name="$(parent)Title" inherits="ZO_SortHeader">
                             <OnInitialized>
diff --git a/lang/en.lua b/lang/en.lua
index 32d8c02..160e8ea 100644
--- a/lang/en.lua
+++ b/lang/en.lua
@@ -5,7 +5,7 @@ local strings = {

     -- Main Window
     LIBRARIAN_WINDOW_TITLE_LIBRARIAN = "Librarian",
-    LIBRARIAN_SORT_TYPE_UNREAD = "Unread",
+    LIBRARIAN_SORT_TYPE_UNREAD = "Read",
     LIBRARIAN_SORT_TYPE_FOUND = "Found",
     LIBRARIAN_SORT_TYPE_TITLE = "Title",
     LIBRARIAN_SORT_TYPE_CATEGORY = "Category",
@@ -18,6 +18,7 @@ local strings = {
     LIBRARIAN_NEW_BOOK_FOUND = "Book added to librarian",
     LIBRARIAN_NEW_BOOK_FOUND_WITH_TITLE = "Book added to librarian: %s",
     LIBRARIAN_FULLTEXT_SEARCH = "Full-text Search:",
+    LIBRARIAN_TITLE_SEARCH = "Only title Search:",
     LIBRARIAN_SEARCH_HINT = "Enter text to search for.",
     LIBRARIAN_NO_CATEGORY = "No Category",
     LIBRARIAN_GO_TO_CATEGORY = "Go to Category",
diff --git a/lang/fr.lua b/lang/fr.lua
index 98bc7ab..58bd7e4 100644
--- a/lang/fr.lua
+++ b/lang/fr.lua
@@ -2,7 +2,7 @@ SafeAddString(SI_BINDING_NAME_LIBRARIAN_TOGGLE_LIBRARIAN,			"Afficher/Masquer Li
 SafeAddString(SI_BINDING_NAME_LIBRARIAN_RELOAD_UI,					"Reload UI", 1)

 SafeAddString(LIBRARIAN_WINDOW_TITLE_LIBRARIAN,						"Librarian", 1)
-SafeAddString(LIBRARIAN_SORT_TYPE_UNREAD,							"Non lu", 1)
+SafeAddString(LIBRARIAN_SORT_TYPE_UNREAD,							"Lu", 1)
 SafeAddString(LIBRARIAN_SORT_TYPE_FOUND,							"Trouvé", 1)
 SafeAddString(LIBRARIAN_SORT_TYPE_TITLE,							"Titre", 1)
 SafeAddString(LIBRARIAN_SORT_TYPE_CATEGORY,							"Catégorie", 1)
@@ -14,7 +14,8 @@ SafeAddString(LIBRARIAN_UNREAD_COUNT,								"%s (%d Non lu)", 1)
 SafeAddString(LIBRARIAN_SHOW_ALL_BOOKS,								"Montrer les livres pour tous les personnages", 1)
 SafeAddString(LIBRARIAN_NEW_BOOK_FOUND,								"Livre ajouté à Librarian", 1)
 SafeAddString(LIBRARIAN_NEW_BOOK_FOUND_WITH_TITLE,					"Livre ajouté à Librarian: %s", 1)
-SafeAddString(LIBRARIAN_FULLTEXT_SEARCH,							"Recherche:", 1)
+SafeAddString(LIBRARIAN_FULLTEXT_SEARCH,							"Recherche titre + contenu:", 1)
+SafeAddString(LIBRARIAN_TITLE_SEARCH,							    "Recherche titre seulement:", 1)
 SafeAddString(LIBRARIAN_SEARCH_HINT,								"Texte à rechercher.", 1)
 SafeAddString(LIBRARIAN_NO_CATEGORY,								"Pas de catégorie", 1)
 SafeAddString(LIBRARIAN_GO_TO_CATEGORY,								"Ouvrir la catégorie", 1)