Verified Commit 44a6a94b authored by WorldTeacher's avatar WorldTeacher
Browse files

fix(database): improve error handling in setBookTracking and isBookTrackingEnabled functions

fix(main): simplify session end handling and improve user feedback on session status
parent 978b41e7
Loading
Loading
Loading
Loading
Loading
+8 −6
Original line number Diff line number Diff line
@@ -3562,8 +3562,7 @@ function Database:setBookTracking(file_path, enabled)
        logger.err("BookloreSync Database: Failed to prepare setBookTracking:", self.conn:errmsg())
        return false
    end
    local ok, err = stmt:bind(1, val)
    if ok then ok, err = stmt:bind(2, file_path) end
    local ok, err = pcall(function() stmt:bind(val, file_path) end)
    if not ok then
        logger.err("BookloreSync Database: Bind failed in setBookTracking:", err)
        stmt:close()
@@ -3587,15 +3586,18 @@ function Database:isBookTrackingEnabled(file_path)
        SELECT tracking_enabled FROM book_cache WHERE file_path = ?
    ]])
    if not stmt then return true end
    local ok, err = stmt:bind(1, file_path)
    local ok, err = pcall(function() stmt:bind(file_path) end)
    if not ok then
        stmt:close()
        return true
    end
    local row = stmt:first_row()
    local result = true
    for row in stmt:rows() do
        result = tonumber(row[1]) ~= 0
        break
    end
    stmt:close()
    if not row then return true end  -- not in cache yet → track by default
    return tonumber(row[1]) ~= 0
    return result  -- not in cache yet → track by default
end

return Database
+31 −6
Original line number Diff line number Diff line
@@ -2637,7 +2637,7 @@ function BookloreSync:addToMainMenu(menu_items)
                end,
            },
            {
                text = _("Fetch Hardcover Book IDs from Booklore->Hardcover"),
                text = _("Fetch Hardcover Book IDs"),
                help_text = _("Fetch metadata from the Booklore server for every matched book and store any Hardcover book IDs returned. For books without a Hardcover ID in Booklore, searches Hardcover by ISBN or title and lets you select the correct match."),
                keep_menu_open = true,
                callback = function()
@@ -3253,7 +3253,7 @@ function BookloreSync:endSession(options)
    
    if not self.current_session then
        self:logInfo("BookloreSync: No active session to end")
        return
        return "no_session"
    end
    
    self:logInfo("BookloreSync: ========== Ending session ==========")
@@ -3274,7 +3274,7 @@ function BookloreSync:endSession(options)
    if not valid then
        self:logInfo("BookloreSync: Session invalid -", reason)
        self.current_session = nil
        return
        return "invalid", reason
    end

    -- Per-book tracking check: if the user has disabled tracking for this
@@ -3284,7 +3284,7 @@ function BookloreSync:endSession(options)
        self:logInfo("BookloreSync: Tracking disabled for this book, discarding session:",
                     self.current_session.file_path)
        self.current_session = nil
        return
        return "tracking_disabled"
    end
    
    local progress_delta = end_progress - self.current_session.start_progress
@@ -3331,6 +3331,7 @@ function BookloreSync:endSession(options)
    end
    
    self.current_session = nil
    return success and "saved" or "db_error"
end

-- Event Handlers
@@ -3416,7 +3417,18 @@ function BookloreSync:onCloseDocument()
    -- toast and the network sync happen below (step 5), after rating and
    -- annotations have also been queued, so this call purely persists the
    -- session data and never triggers a premature sync.
    self:endSession({ silent = true, force_queue = true })
    local session_status, session_reason = self:endSession({ silent = true, force_queue = true })

    -- Show "criteria not met" feedback immediately when the session was
    -- discarded at close (not on suspend/resume paths which pass silent=true
    -- themselves; those don't reach this code path anyway).
    if session_status == "invalid" and not self.silent_messages then
        UIManager:show(InfoMessage:new{
            text    = _("Session not saved: criteria not met"),
            timeout = 3,
        })
        return false
    end

    -- ── Step 3: Queue / sync rating ───────────────────────────────────────
    self:logInfo(string.format(
@@ -3592,6 +3604,19 @@ function BookloreSync:onCloseDocument()
                })
            end
        end
    elseif session_status == "saved" and not self.silent_messages then
        -- Manual sync only mode: session was queued, inform the user.
        self:logInfo("BookloreSync: manual_sync_only — session queued, notifying user")
        local parts = { _("Booklore Sync: Session saved") }
        parts[#parts + 1] = _("S: 1 queued")
        if ann_queued_now > 0 then
            parts[#parts + 1] = T(_("A: %1 queued"), ann_queued_now)
        end
        parts[#parts + 1] = _("Use 'Sync Pending Now' to upload")
        UIManager:show(InfoMessage:new{
            text    = table.concat(parts, "\n"),
            timeout = 3,
        })
    end

    return false
@@ -3620,7 +3645,7 @@ function BookloreSync:connectNetwork()
    
    self:logInfo("BookloreSync: Attempting to connect to network")
    
    if not Device:isConnected() then
    if not NetworkMgr:isConnected() then
        self:logInfo("BookloreSync: Enabling WiFi")
        Device:setWifiState(true)
    end