Browse Source

Merge remote-tracking branch 'origin/master'

kunfei 3 years ago
parent
commit
4a53b85b66

+ 10 - 4
app/src/main/java/io/legado/app/model/localBook/EpubFile.kt

@@ -74,10 +74,16 @@ class EpubFile(var book: Book) {
     private var fileDescriptor: ParcelFileDescriptor? = null
     private var epubBook: EpubBook? = null
         get() {
-            if (field != null && fileDescriptor != null) {
-                return field
+            if (field == null || fileDescriptor == null) {
+                field = readEpub()
+            }
+            return field
+        }
+    private var epubBookContents: List<Resource>?
+        get() {
+            if (field == null || fileDescriptor == null) {
+                field = epubBook.contents
             }
-            field = readEpub()
             return field
         }
 
@@ -147,7 +153,7 @@ class EpubFile(var book: Book) {
             val includeNextChapterResource = !endFragmentId.isNullOrBlank()
             /*一些书籍依靠href索引的resource会包含多个章节,需要依靠fragmentId来截取到当前章节的内容*/
             /*注:这里较大增加了内容加载的时间,所以首次获取内容后可存储到本地cache,减少重复加载*/
-            for (res in epubBook.contents) {
+            for (res in epubBookContents) {
                 if (!findChapterFirstSource) {
                     if (currentChapterFirstResourceHref != res.href) continue
                     findChapterFirstSource = true

+ 8 - 4
app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt

@@ -7,6 +7,8 @@ import android.view.ViewGroup
 import androidx.appcompat.widget.Toolbar
 import androidx.fragment.app.viewModels
 import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.Lifecycle.repeatOnLifecycle
+import androidx.lifecycle.Lifecycle.State.STARTED
 import androidx.recyclerview.widget.GridLayoutManager
 import io.legado.app.R
 import io.legado.app.base.BaseDialogFragment
@@ -67,10 +69,12 @@ class ChangeCoverDialog() : BaseDialogFragment(R.layout.dialog_change_cover),
     }
 
     private fun initData() {
-        lifecycleScope.launchWhenStarted {
-            viewModel.dataFlow.conflate().collect {
-                adapter.setItems(it)
-                delay(1000)
+        lifecycleScope.launch {
+            repeatOnLifecycle(STARTED) {
+                viewModel.dataFlow.conflate().collect {
+                    adapter.setItems(it)
+                    delay(1000)
+                }
             }
         }
     }

+ 8 - 4
app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt

@@ -10,6 +10,8 @@ import androidx.appcompat.widget.Toolbar
 import androidx.core.os.bundleOf
 import androidx.fragment.app.viewModels
 import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.Lifecycle.repeatOnLifecycle
+import androidx.lifecycle.Lifecycle.State.STARTED
 import androidx.recyclerview.widget.LinearLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import io.legado.app.R
@@ -181,10 +183,12 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_
             }
             binding.toolBar.menu.applyTint(requireContext())
         }
-        lifecycleScope.launchWhenStarted {
-            viewModel.searchDataFlow.conflate().collect {
-                adapter.setItems(it)
-                delay(1000)
+        lifecycleScope.launch {
+            repeatOnLifecycle(STARTED) {
+                viewModel.searchDataFlow.conflate().collect {
+                    adapter.setItems(it)
+                    delay(1000)
+                }
             }
         }
         launch {

+ 8 - 4
app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt

@@ -10,6 +10,8 @@ import androidx.core.os.bundleOf
 import androidx.core.view.isVisible
 import androidx.fragment.app.viewModels
 import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.Lifecycle.repeatOnLifecycle
+import androidx.lifecycle.Lifecycle.State.STARTED
 import androidx.recyclerview.widget.LinearLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import io.legado.app.R
@@ -202,10 +204,12 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c
             }
             binding.toolBar.menu.applyTint(requireContext())
         }
-        lifecycleScope.launchWhenStarted {
-            viewModel.searchDataFlow.conflate().collect {
-                searchBookAdapter.setItems(it)
-                delay(1000)
+        lifecycleScope.launch {
+            repeatOnLifecycle(STARTED) {
+                viewModel.searchDataFlow.conflate().collect {
+                    searchBookAdapter.setItems(it)
+                    delay(1000)
+                }
             }
         }
         launch {

+ 4 - 4
app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt

@@ -155,12 +155,12 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
                             appDb.bookDao.getBook(book.name, book.author)?.let {
                                 inBookshelf = true
                             }
-                            bookData.postValue(book)
+                            bookData.postValue(it)
                             if (inBookshelf) {
-                                appDb.bookDao.update(book)
+                                appDb.bookDao.update(it)
                             }
                             if (it.isWebFile) {
-                                loadWebFile(book, bookSource, scope)
+                                loadWebFile(it, scope)
                             } else {
                                 loadChapter(it, scope)
                             }
@@ -234,7 +234,6 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
 
     private fun loadWebFile(
         book: Book,
-        bookSource: BookSource,
         scope: CoroutineScope = viewModelScope
     ) {
         execute(scope) {
@@ -263,6 +262,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
                LocalBook.saveBookFile(webFile.url, webFile.name, bookSource)
            }
        }.onSuccess {
+           @Suppress("unchecked_cast")
            success?.invoke(it as T)
        }.onError {
            context.toastOnUi("ImportWebFileError\n${it.localizedMessage}")