🐛 修复书架不同步、章节打不开与登录状态误判

- 书架改走月读 Shelf.syncAsync:收藏夹分页拉取并写入书库,支持加 / 删收藏
- 开书改用主题进度窗,修 ui/widgets/progressdialog 不存在导致的崩溃
- 章节首次下载回调传路径而不是表,修新章第一次打开必失败
- 站点按页加密的图换成提示页,不再整话放弃
- 源实例元表改为继承 SourceBase,修登录后仍显示未登录
This commit is contained in:
Garfield committed 2026-11-27 07:44:00 +08:00
1 parent 4a031e00c5
commit 875dc70861
4 files changed
+236 -144

No files matched your search

@@ -5,7 +5,8 @@ local Chapter = require("moonsource_bilimanga.chapter")
local Toc = require("source.toc")
local _ = require("moonsource_bilimanga.l10n")
local BiliManga = {}
local BiliManga = setmetatable({}, { __index = SourceBase })
BiliManga.__index = BiliManga
local SOURCE_ID
function BiliManga.meta()
@@ -15,10 +16,7 @@ end
function BiliManga.new()
local meta = BiliManga.meta()
SOURCE_ID = meta.id
local source = setmetatable({ id = meta.id, name = meta.name, type = meta.type }, {
__index = SourceBase,
})
return source
return setmetatable({ id = meta.id, name = meta.name, type = meta.type }, BiliManga)
end
function BiliManga:configured()
@@ -45,7 +43,7 @@ function BiliManga:getDetailAsync(identity, cb)
return
end
return Client.comicDetailLiteAsync(identity.stable_id, function(info, err)
return Client.comicDetailAsync(identity.stable_id, function(info, err)
if err then cb(nil, err); return end
local book = Mapper.book(info)
require("book.store").rememberDetail(book)
@@ -80,20 +78,22 @@ function BiliManga:refreshTocAsync(identity, cb)
end)
end
--- 起始章:调用方指定的章 > 本地待推进度 > 第一章。
---@param identity BookIdentity
---@param opts table
---@param toc table[]
---@return integer
local function startIndex(identity, opts, toc)
local idx = opts.chapter_idx
if not idx then
local pending = require("book.progress_db").get(identity.source_id, identity.stable_id)
idx = pending and (pending.chapter_idx or math.floor((pending.fraction or 0) * #toc) + 1)
end
return math.max(1, math.min(#toc, idx or 1))
end
function BiliManga:openBookAsync(identity, opts, cb)
opts = opts or {}
SOURCE_ID = identity.source_id or SOURCE_ID
local cancelled = false
local active = nil
local dialog = nil
local function startIndex(toc)
if identity.chapter_idx and identity.chapter_idx > 0 then
return math.min(identity.chapter_idx, #toc)
end
return 1
end
local function finish(path, idx)
local ok, err = require("book.store").touch(path, identity, {
chapter_idx = idx,
@@ -101,105 +101,55 @@ function BiliManga:openBookAsync(identity, opts, cb)
})
cb(ok and path or nil, err)
end
local function closeDialog()
if dialog then
pcall(dialog.dismiss)
dialog = nil
end
end
-- 检查缓存
local toc = Toc.read(SOURCE_ID, identity.stable_id)
local toc = Toc.read(identity.source_id, identity.stable_id)
if toc and #toc > 0 then
local idx = startIndex(toc)
local idx = startIndex(identity, opts, toc)
local path = Chapter.existing(identity, toc[idx])
if path then return SourceBase.defer(finish, path, idx) end
end
-- 离线检查
if not require("ui/network/manager"):isConnected() then
return SourceBase.defer(cb, nil, _("网络不可用,请先连接 Wi-Fi"))
end
-- 异步:拉目录 → 下载CBZ
local cancelled, active, dialog = false, nil, nil
local function closeDialog()
if dialog then dialog:close(); dialog = nil end
end
require("ui/uimanager"):nextTick(function()
local ProgressDialog = require("ui/widgets/progressdialog")
dialog = ProgressDialog:new{
title = identity.book and identity.book.title or "",
text = _("正在准备漫画…"),
progress = { min = 0, max = 1 },
if cancelled then return end
dialog = require("ui.themes.widgets.progress_dialog"):new{
title = identity.book and identity.book.title or identity.stable_id,
dismissable = false,
}
dialog:show()
toc = Toc.read(SOURCE_ID, identity.stable_id)
if not toc or #toc == 0 then
local retries = 0
local MAX_RETRIES = 3
local function loadTocWithRetry()
dialog:set(_("正在准备漫画…"))
local loaded = false
local toc_job = self:loadTocAsync(identity, function(fresh, err)
loaded = true
active = nil
if cancelled then return end
if not fresh then closeDialog(); cb(nil, err or _("加载目录失败")); return end
local idx = startIndex(identity, opts, fresh)
local materialized = false
local chapter_job = Chapter.materializeAsync(identity, fresh[idx], function(done, total)
if dialog and total > 0 then dialog:update(_("正在准备漫画…"), done, total) end
end, function(path, materialize_err)
materialized = true
active = nil
closeDialog()
if cancelled then return end
retries = retries + 1
if retries > MAX_RETRIES then
closeDialog()
cb(nil, _("加载目录失败"))
return
end
self:refreshTocAsync(identity, function(toc_data, toc_err)
if cancelled then return end
if not toc_data then
require("ui/uimanager"):scheduleIn(2, loadTocWithRetry)
return
end
toc = toc_data
local idx = startIndex(toc)
local chapter_job = Chapter.materializeAsync(identity, toc[idx],
function(done, total)
if cancelled then return end
dialog:update(_("正在准备漫画…"), done, total)
end,
function(path, materialize_err)
if cancelled then return end
closeDialog()
if not path then
cb(nil, materialize_err or _("章节图片解析失败"))
return
end
finish(path, idx)
end
)
active = chapter_job
end, true)
end
loadTocWithRetry()
else
local idx = startIndex(toc)
local chapter_job = Chapter.materializeAsync(identity, toc[idx],
function(done, total)
if cancelled then return end
dialog:update(_("正在准备漫画…"), done, total)
end,
function(path, materialize_err)
if cancelled then return end
closeDialog()
if not path then
cb(nil, materialize_err or _("章节图片解析失败"))
return
end
finish(path, idx)
end
)
active = chapter_job
end
if not path then cb(nil, materialize_err or _("章节图片解析失败")); return end
finish(path, idx)
end)
if not materialized then active = chapter_job end
end)
if not loaded then active = toc_job end
end)
return {
cancel = function()
cancelled = true
if active then active.cancel() end
closeDialog()
end,
}
return { cancel = function()
cancelled = true
if active then active.cancel() end
closeDialog()
end }
end
function BiliManga:prefetchChaptersAsync(identity, toc, from_idx, count, cb)
@@ -298,40 +248,103 @@ function BiliManga:putProgressAsync(identity, pos, cb)
return { cancel = function() end }
end
function BiliManga:syncBooksAsync(opts, cb)
local cfg = require("utils.settings").getSource("bilimanga")
if not cfg.cookie or cfg.cookie == "" then
return SourceBase.defer(cb, { data = {} })
end
BiliManga.shelf_add = true
Client.listFavoriteAsync(1, 100, function(wire, err)
if err then cb(nil, err); return end
local items = Mapper.favoriteItems(wire)
local books = {}
for i = 1, #items do
local book = Mapper.bookFromItem(items[i])
if book then
book.read_at = items[i].read_time or 0
book.inserted_at = items[i].fav_time or 0
--- 收藏夹即远端书架:Shelf.syncAsync 要的 shelfSyncAsync / addToShelfAsync / removeFromShelfAsync。
local Favorites = {}
Favorites.__index = Favorites
local FAVORITE_PAGE = 50
--- "2026-11-27 00:40:55"(北京时间)→ 时间戳;空串 / 格式不对为 0。
---@param text any
---@return integer
local function beijingTime(text)
local y, mo, d, h, mi, s = tostring(text or ""):match("^(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)$")
if not y then return 0 end
local as_local = os.time({ year = tonumber(y), month = tonumber(mo), day = tonumber(d),
hour = tonumber(h), min = tonumber(mi), sec = tonumber(s) })
local local_offset = os.time(os.date("*t", as_local)) - os.time(os.date("!*t", as_local))
return as_local + local_offset - 8 * 3600
end
--- 整个收藏夹(逐页拉,接口的 total 不可靠,按满页判断还有没有下一页)。
---@param cb fun(books: Book[]|nil, err: string|nil)
function Favorites:shelfSyncAsync(cb)
local books, page = {}, 1
local function nextPage()
self.job = Client.listFavoriteAsync(page, FAVORITE_PAGE, function(wire, err)
self.job = nil
if self.cancelled then return end
local items = wire and Mapper.favoriteItems(wire)
if not items then cb(nil, err or _("响应无效")); return end
for _idx, item in ipairs(items) do
local book = Mapper.bookFromItem(item)
if not book then cb(nil, _("响应无效")); return end
book.read_at = beijingTime(item.latest_read_time)
books[#books + 1] = book
end
end
cb({ data = books })
end)
if #items < FAVORITE_PAGE then cb(books); return end
page = page + 1
nextPage()
end)
end
nextPage()
return self
end
return { cancel = function() end }
function Favorites:addToShelfAsync(stable_id, cb)
self.job = Client.addFavoriteAsync(stable_id, cb)
return self
end
function Favorites:removeFromShelfAsync(stable_id, cb)
self.job = Client.deleteFavoriteAsync(stable_id, cb)
return self
end
function Favorites:cancel()
self.cancelled = true
if self.job then self.job.cancel() end
end
function BiliManga:syncBooksAsync(opts, cb)
if not Client.hasAccount() then return SourceBase.skipped(cb, "not_logged_in") end
local remote = setmetatable({ cancelled = false }, Favorites)
local job = require("source.shelf").syncAsync(self.id, remote, nil, opts, cb)
return { cancel = function()
remote:cancel()
job:cancel()
end }
end
function BiliManga:browseAsync(opts, cb)
if opts and opts.browse_type == "home" then
Client.homeHotAsync(function(wire, err)
if err then cb(nil, err); return end
local books = Mapper.searchResults(wire)
cb({ data = books })
opts = opts or {}
local limit = opts.limit or 60
local state = { cancelled = false, job = nil }
state.job = Client.allLabelAsync(function(wire, err)
if state.cancelled then return end
local labels = Mapper.labels(wire)
if err or not labels then cb(nil, err or _("响应无效")); return end
local order, style = Mapper.storeChoice(opts.node, labels)
local groups = Mapper.storeGroups(labels, order, style)
state.job = require("source.browse").pages(function(page, done)
return Client.classPageAsync({ order = order, style_id = style }, page, function(rows, page_err)
if state.cancelled then return end
if page_err then done(nil, page_err); return end
local books = Mapper.books(rows)
if not books then done(nil, _("响应无效")); return end
done(books, nil, #books == 15)
end)
end, limit, groups, function(result, reason)
state.job = nil
if not state.cancelled then cb(result, reason) end
end)
return { cancel = function() end }
end
return SourceBase.defer(cb, { data = {} })
end)
return { cancel = function()
state.cancelled = true
if state.job and state.job.cancel then state.job.cancel() end
end }
end
return BiliManga