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
@@ -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()
@@ -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,24 +265,24 @@ 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) {
initDataSuccess()
}
}
private fun initDataSuccess() {
upMenu() upMenu()
if (reloadContent) { if (reloadContent) {
reloadContent = false reloadContent = false
ReadBook.loadContent(resetPageOffset = false) ReadBook.loadContent(resetPageOffset = false)
} }
} }
}
override fun onWindowFocusChanged(hasFocus: Boolean) { override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus) super.onWindowFocusChanged(hasFocus)
@@ -1226,7 +1224,7 @@ 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) {
@@ -1269,7 +1267,6 @@ class ReadBookActivity : BaseReadBookActivity(),
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,13 +1481,17 @@ class ReadBookActivity : BaseReadBookActivity(),
} }
override fun finish() { override fun finish() {
ReadBook.book?.let { val book = ReadBook.book ?: return super.finish()
if (!ReadBook.inBookshelf) {
if (ReadBook.inBookshelf) {
return super.finish()
}
if (!AppConfig.showAddToShelfAlert) { if (!AppConfig.showAddToShelfAlert) {
viewModel.removeFromBookshelf { super.finish() } viewModel.removeFromBookshelf { super.finish() }
} else { } else {
alert(title = getString(R.string.add_to_bookshelf)) { alert(title = getString(R.string.add_to_bookshelf)) {
setMessage(getString(R.string.check_add_bookshelf, it.name)) setMessage(getString(R.string.check_add_bookshelf, book.name))
okButton { okButton {
ReadBook.inBookshelf = true ReadBook.inBookshelf = true
setResult(Activity.RESULT_OK) setResult(Activity.RESULT_OK)
@@ -1498,10 +1499,6 @@ class ReadBookActivity : BaseReadBookActivity(),
noButton { viewModel.removeFromBookshelf { super.finish() } } noButton { viewModel.removeFromBookshelf { super.finish() } }
} }
} }
} else {
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 {
+6 -3
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) => {
if (isLoading) {
closeLoading(); 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;