Verified Commit 1f28f0aa authored by WorldTeacher's avatar WorldTeacher
Browse files

feat(sync): add progress sync gestures

added gestures to push / pull progress for the active book
parent c4f59ce9
Loading
Loading
Loading
Loading
+258 −128
Original line number Diff line number Diff line
@@ -698,6 +698,18 @@ function BookloreSync:registerDispatcherActions()
        title = _("BookLoreSync: Sync from Booklore Shelf"),
        general = true,
    })
    Dispatcher:registerAction("booklore_pull_current_book_progress", {
        category = "none",
        event = "PullBookloreCurrentBookProgress",
        title = _("BookLoreSync: Pull Current Book Progress"),
        general = true,
    })
    Dispatcher:registerAction("booklore_push_current_book_progress", {
        category = "none",
        event = "PushBookloreCurrentBookProgress",
        title = _("BookLoreSync: Push Current Book Progress"),
        general = true,
    })
end

---BookloreSync:onSyncBookloreShelf.
@@ -716,6 +728,87 @@ function BookloreSync:onSyncBookloreShelf()
    return true
end

---BookloreSync:hasOpenDocumentForProgressSync.
function BookloreSync:hasOpenDocumentForProgressSync()
    return self.ui and self.ui.document and self.ui.document.file and self.ui.document.file ~= ""
end

---BookloreSync:onPullBookloreCurrentBookProgress.
function BookloreSync:onPullBookloreCurrentBookProgress()
    if not self:hasOpenDocumentForProgressSync() then
        UIManager:show(InfoMessage:new{
            text = _("Open a book to pull progress"),
            timeout = 2,
        })
        return true
    end
    if not self.progress_sync_enabled then
        UIManager:show(InfoMessage:new{
            text = _("Progress sync is disabled"),
            timeout = 2,
        })
        return true
    end

    self:_requestWifi(_("pull current book progress"), function()
        self:pullKoreaderProgress(true)
    end, function()
        UIManager:show(InfoMessage:new{
            text = _("WiFi not enabled - pull skipped"),
            timeout = 2,
        })
    end)
    return true
end

---BookloreSync:onPushBookloreCurrentBookProgress.
function BookloreSync:onPushBookloreCurrentBookProgress()
    if not self:hasOpenDocumentForProgressSync() then
        UIManager:show(InfoMessage:new{
            text = _("Open a book to push progress"),
            timeout = 2,
        })
        return true
    end
    if not self.progress_sync_enabled then
        UIManager:show(InfoMessage:new{
            text = _("Progress sync is disabled"),
            timeout = 2,
        })
        return true
    end

    self:_requestWifi(_("push current book progress"), function()
        local payload = self:buildCurrentKoreaderProgressPayload(os.time())
        if not payload then
            UIManager:show(InfoMessage:new{
                text = _("Could not build current progress payload"),
                timeout = 2,
            })
            return
        end

        local ok, reason = self:pushCurrentKoreaderProgressDirect(payload)
        if ok then
            UIManager:show(InfoMessage:new{
                text = _("Current book progress pushed"),
                timeout = 2,
            })
        else
            UIManager:show(InfoMessage:new{
                text = T(_("Failed to push progress: %1"), tostring(reason or "unknown error")),
                timeout = 2,
            })
        end
    end, function()
        UIManager:show(InfoMessage:new{
            text = _("WiFi not enabled - push skipped"),
            timeout = 2,
        })
    end)
    return true
end

---BookloreSync:onToggleBookloreSync.
function BookloreSync:onToggleBookloreSync()
    self:toggleSync()
@@ -3240,6 +3333,9 @@ function BookloreSync:addToMainMenu(menu_items)
    local progress_sync_menu = {
        text = _("Progress Sync"),
        help_text = _("Configure KOReader document-position sync with Booklore."),
        sub_item_table = {
            {
                text = _("General"),
                sub_item_table = {
                    {
                        text = _("Automatically sync progress"),
@@ -3290,7 +3386,32 @@ function BookloreSync:addToMainMenu(menu_items)
                        keep_menu_open = true,
                    },
                    {
                text = _("Sync behavior"),
                        text = _("Pull progress now"),
                        help_text = _("Immediately pull progress for the currently open book."),
                        enabled_func = function()
                            return self.progress_sync_enabled
                        end,
                        callback = function()
                            self:onPullBookloreCurrentBookProgress()
                        end,
                        keep_menu_open = true,
                    },
                    {
                        text = _("Push progress now"),
                        help_text = _("Immediately push progress for the currently open book."),
                        enabled_func = function()
                            return self.progress_sync_enabled
                        end,
                        callback = function()
                            self:onPushBookloreCurrentBookProgress()
                        end,
                        keep_menu_open = true,
                    },
                },
                keep_menu_open = true,
            },
            {
                text = _("Conflict handling"),
                help_text = _("Control how incoming progress updates are applied."),
                enabled_func = function()
                    return self.progress_sync_enabled
@@ -3369,15 +3490,18 @@ function BookloreSync:addToMainMenu(menu_items)
                },
                keep_menu_open = true,
            },
            {
                text = _("Update frequency"),
                enabled_func = function()
                    return self.progress_sync_enabled
                end,
                sub_item_table = {
                    {
                        text = _("Periodic page-turn updates"),
                        help_text = _("Enable or disable periodic page-turn progress updates. Book-open and book-close positions are always stored."),
                        checked_func = function()
                            return self.progress_sync_page_updates_enabled
                        end,
                enabled_func = function()
                    return self.progress_sync_enabled
                end,
                        keep_menu_open = true,
                        callback = function()
                            self.progress_sync_page_updates_enabled = not self.progress_sync_page_updates_enabled
@@ -3397,7 +3521,7 @@ function BookloreSync:addToMainMenu(menu_items)
                        end,
                        help_text = _("How many page turns before a periodic sync update is queued. Minimum is 1 when periodic updates are enabled."),
                        enabled_func = function()
                    return self.progress_sync_enabled and self.progress_sync_page_updates_enabled
                            return self.progress_sync_page_updates_enabled
                        end,
                        keep_menu_open = true,
                        callback = function()
@@ -3425,15 +3549,21 @@ function BookloreSync:addToMainMenu(menu_items)
                            UIManager:show(widget)
                        end,
                    },
                },
                keep_menu_open = true,
            },
            {
                text = _("Advanced"),
                enabled_func = function()
                    return self.progress_sync_enabled
                end,
                sub_item_table = {
                    {
                        text = _("Normalize session locations"),
                        help_text = _("When enabled, start/end session locations are converted to the server page-count scale so the displayed book page is correct instead of using KOReader-provided pages. This needs page count metadata from the server, which can be re-synced in Sync & Cache -> Advanced. Progress percentages are unchanged."),
                        checked_func = function()
                            return self.progress_sync_normalize_session_locations
                        end,
                enabled_func = function()
                    return self.progress_sync_enabled
                end,
                        callback = function()
                            self.progress_sync_normalize_session_locations = not self.progress_sync_normalize_session_locations
                            self.settings:saveSetting("progress_sync_normalize_session_locations", self.progress_sync_normalize_session_locations)
@@ -3453,9 +3583,6 @@ function BookloreSync:addToMainMenu(menu_items)
                        checked_func = function()
                            return self.progress_sync_set_read_on_rating
                        end,
                enabled_func = function()
                    return self.progress_sync_enabled
                end,
                        callback = function()
                            self.progress_sync_set_read_on_rating = not self.progress_sync_set_read_on_rating
                            self.settings:saveSetting("progress_sync_set_read_on_rating", self.progress_sync_set_read_on_rating)
@@ -3468,6 +3595,9 @@ function BookloreSync:addToMainMenu(menu_items)
                        keep_menu_open = true,
                    },
                },
                keep_menu_open = true,
            },
        },
    }

    local annotations_menu = Settings:buildAnnotationsMenu(self)