This commit is contained in:
Horis 2024-01-20 11:26:11 +08:00
parent f55cade258
commit 9e9d670262
3 changed files with 100 additions and 84 deletions

View File

@ -25,6 +25,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.MainScope import kotlinx.coroutines.MainScope
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import splitties.init.appCtx import splitties.init.appCtx
@ -567,6 +568,29 @@ object ReadBook : CoroutineScope by MainScope() {
} }
} }
/**
* 注册回调
*/
fun register(cb: CallBack) {
callBack?.notifyBookChanged()
callBack = cb
}
/**
* 取消注册回调
*/
fun unregister(cb: CallBack) {
if (callBack === cb) {
callBack = null
}
msg = null
preDownloadTask?.cancel()
downloadScope.coroutineContext.cancelChildren()
coroutineContext.cancelChildren()
downloadedChapters.clear()
downloadFailChapters.clear()
}
interface CallBack { interface CallBack {
fun upMenuView() fun upMenuView()

View File

@ -117,7 +117,6 @@ import io.legado.app.utils.toastOnUi
import io.legado.app.utils.visible import io.legado.app.utils.visible
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -235,8 +234,7 @@ class ReadBookActivity : BaseReadBookActivity(),
binding.cursorRight.setOnTouchListener(this) binding.cursorRight.setOnTouchListener(this)
window.setBackgroundDrawable(null) window.setBackgroundDrawable(null)
upScreenTimeOut() upScreenTimeOut()
ReadBook.callBack?.notifyBookChanged() ReadBook.register(this)
ReadBook.callBack = this
onBackPressedDispatcher.addCallback(this) { onBackPressedDispatcher.addCallback(this) {
if (isShowingSearchResult) { if (isShowingSearchResult) {
exitSearchMenu() exitSearchMenu()
@ -267,22 +265,22 @@ class ReadBookActivity : BaseReadBookActivity(),
override fun onPostCreate(savedInstanceState: Bundle?) { override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState) super.onPostCreate(savedInstanceState)
viewModel.initData(intent) { viewModel.initData(intent) {
upMenu() initDataSuccess()
if (reloadContent) {
reloadContent = false
ReadBook.loadContent(resetPageOffset = false)
}
} }
} }
override fun onNewIntent(intent: Intent?) { override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent) super.onNewIntent(intent)
viewModel.initData(intent ?: return) { viewModel.initData(intent ?: return) {
upMenu() initDataSuccess()
if (reloadContent) { }
reloadContent = false }
ReadBook.loadContent(resetPageOffset = false)
} private fun initDataSuccess() {
upMenu()
if (reloadContent) {
reloadContent = false
ReadBook.loadContent(resetPageOffset = false)
} }
} }
@ -1226,48 +1224,47 @@ class ReadBookActivity : BaseReadBookActivity(),
} }
override fun payAction() { override fun payAction() {
ReadBook.book?.let { book -> val book = ReadBook.book ?: return
if (book.isLocal) return if (book.isLocal) return
val chapter = appDb.bookChapterDao.getChapter(book.bookUrl, ReadBook.durChapterIndex) val chapter = appDb.bookChapterDao.getChapter(book.bookUrl, ReadBook.durChapterIndex)
if (chapter == null) { if (chapter == null) {
toastOnUi("no chapter") toastOnUi("no chapter")
return return
} }
alert(R.string.chapter_pay) { alert(R.string.chapter_pay) {
setMessage(chapter.title) setMessage(chapter.title)
yesButton { yesButton {
Coroutine.async { Coroutine.async {
val source = val source =
ReadBook.bookSource ?: throw NoStackTraceException("no book source") ReadBook.bookSource ?: throw NoStackTraceException("no book source")
val payAction = source.getContentRule().payAction val payAction = source.getContentRule().payAction
if (payAction.isNullOrBlank()) { if (payAction.isNullOrBlank()) {
throw NoStackTraceException("no pay action") throw NoStackTraceException("no pay action")
}
val analyzeRule = AnalyzeRule(book, source)
analyzeRule.setBaseUrl(chapter.url)
analyzeRule.chapter = chapter
analyzeRule.evalJS(payAction).toString()
}.onSuccess {
if (it.isAbsUrl()) {
startActivity<WebViewActivity> {
putExtra("title", getString(R.string.chapter_pay))
putExtra("url", it)
IntentData.put(it, ReadBook.bookSource?.getHeaderMap(true))
}
} else if (it.isTrue()) {
//购买成功后刷新目录
ReadBook.book?.let {
ReadBook.curTextChapter = null
BookHelp.delContent(book, chapter)
viewModel.loadChapterList(book)
}
}
}.onError {
AppLog.put("执行购买操作出错\n${it.localizedMessage}", it, true)
} }
val analyzeRule = AnalyzeRule(book, source)
analyzeRule.setBaseUrl(chapter.url)
analyzeRule.chapter = chapter
analyzeRule.evalJS(payAction).toString()
}.onSuccess {
if (it.isAbsUrl()) {
startActivity<WebViewActivity> {
putExtra("title", getString(R.string.chapter_pay))
putExtra("url", it)
IntentData.put(it, ReadBook.bookSource?.getHeaderMap(true))
}
} else if (it.isTrue()) {
//购买成功后刷新目录
ReadBook.book?.let {
ReadBook.curTextChapter = null
BookHelp.delContent(book, chapter)
viewModel.loadChapterList(book)
}
}
}.onError {
AppLog.put("执行购买操作出错\n${it.localizedMessage}", it, true)
} }
noButton()
} }
noButton()
} }
} }
@ -1364,13 +1361,13 @@ class ReadBookActivity : BaseReadBookActivity(),
TIP_COLOR -> { TIP_COLOR -> {
ReadTipConfig.tipColor = color ReadTipConfig.tipColor = color
postEvent(EventBus.TIP_COLOR, "") postEvent(EventBus.TIP_COLOR, "")
postEvent(EventBus.UP_CONFIG, true) postEvent(EventBus.UP_CONFIG, false)
} }
TIP_DIVIDER_COLOR -> { TIP_DIVIDER_COLOR -> {
ReadTipConfig.tipDividerColor = color ReadTipConfig.tipDividerColor = color
postEvent(EventBus.TIP_COLOR, "") postEvent(EventBus.TIP_COLOR, "")
postEvent(EventBus.UP_CONFIG, true) postEvent(EventBus.UP_CONFIG, false)
} }
} }
} }
@ -1484,24 +1481,24 @@ class ReadBookActivity : BaseReadBookActivity(),
} }
override fun finish() { override fun finish() {
ReadBook.book?.let { val book = ReadBook.book ?: return super.finish()
if (!ReadBook.inBookshelf) {
if (!AppConfig.showAddToShelfAlert) { if (ReadBook.inBookshelf) {
viewModel.removeFromBookshelf { super.finish() } return super.finish()
} else { }
alert(title = getString(R.string.add_to_bookshelf)) {
setMessage(getString(R.string.check_add_bookshelf, it.name)) if (!AppConfig.showAddToShelfAlert) {
okButton { viewModel.removeFromBookshelf { super.finish() }
ReadBook.inBookshelf = true } else {
setResult(Activity.RESULT_OK) alert(title = getString(R.string.add_to_bookshelf)) {
} setMessage(getString(R.string.check_add_bookshelf, book.name))
noButton { viewModel.removeFromBookshelf { super.finish() } } okButton {
} ReadBook.inBookshelf = true
setResult(Activity.RESULT_OK)
} }
} else { noButton { viewModel.removeFromBookshelf { super.finish() } }
super.finish()
} }
} ?: super.finish() }
} }
override fun onDestroy() { override fun onDestroy() {
@ -1510,15 +1507,7 @@ class ReadBookActivity : BaseReadBookActivity(),
textActionMenu.dismiss() textActionMenu.dismiss()
popupAction.dismiss() popupAction.dismiss()
binding.readView.onDestroy() binding.readView.onDestroy()
ReadBook.msg = null ReadBook.unregister(this)
if (ReadBook.callBack === this) {
ReadBook.callBack = null
}
ReadBook.preDownloadTask?.cancel()
ReadBook.downloadScope.coroutineContext.cancelChildren()
ReadBook.coroutineContext.cancelChildren()
ReadBook.downloadedChapters.clear()
ReadBook.downloadFailChapters.clear()
if (!BuildConfig.DEBUG) { if (!BuildConfig.DEBUG) {
Backup.autoBack(this) Backup.autoBack(this)
} }
@ -1540,7 +1529,7 @@ class ReadBookActivity : BaseReadBookActivity(),
readView.upBg() readView.upBg()
readView.upStyle() readView.upStyle()
readView.upBgAlpha() readView.upBgAlpha()
if (it) { if (it) { // 更新内容排版布局
if (isInitFinish) { if (isInitFinish) {
ReadBook.loadContent(resetPageOffset = false) ReadBook.loadContent(resetPageOffset = false)
} else { } else {

View File

@ -98,12 +98,12 @@ const readingRecent = ref({
chapterPos: 0, chapterPos: 0,
}); });
const shelfWrapper = ref(null); const shelfWrapper = ref(null);
const { showLoading, closeLoading, loadingWrapper } = useLoading( const { showLoading, closeLoading, loadingWrapper, isLoading } = useLoading(
shelfWrapper, shelfWrapper,
"正在获取书籍信息", "正在获取书籍信息",
); );
const books = ref([]); const books = shallowRef([]);
const search = ref(""); const search = ref("");
const isSearching = ref(false); const isSearching = ref(false);
@ -131,10 +131,13 @@ const searchBook = () => {
API.search( API.search(
search.value, search.value,
(data) => { (data) => {
closeLoading(); if (isLoading) {
closeLoading();
}
try { try {
store.setSearchBooks(JSON.parse(data)); store.setSearchBooks(JSON.parse(data));
store.searchBooks.forEach((item) => books.value.push(item)); books.value = store.searchBooks
//store.searchBooks.forEach((item) => books.value.push(item));
} catch (e) { } catch (e) {
ElMessage.error("后端数据错误"); ElMessage.error("后端数据错误");
throw e; throw e;