Xwite 3 anos atrás
pai
commit
a286995275

+ 9 - 2
app/src/main/java/io/legado/app/ui/book/import/BaseImportBookActivity.kt

@@ -14,6 +14,7 @@ import io.legado.app.help.config.AppConfig
 import io.legado.app.lib.dialogs.alert
 import io.legado.app.lib.dialogs.selector
 import io.legado.app.lib.theme.primaryTextColor
+import io.legado.app.model.localBook.LocalBook
 import io.legado.app.ui.book.read.ReadBookActivity
 import io.legado.app.ui.document.HandleFileContract
 import io.legado.app.utils.*
@@ -93,7 +94,7 @@ abstract class BaseImportBookActivity<VM : ViewModel> : VMBaseActivity<ActivityI
         }
     }
 
-    open fun onArchiveFileClick(fileDoc: FileDoc) {
+    protected fun onArchiveFileClick(fileDoc: FileDoc) {
         val fileNames = ArchiveUtils.getArchiveFilesName(fileDoc) {
             it.matches(AppPattern.bookFileRegex)
         }
@@ -122,11 +123,17 @@ abstract class BaseImportBookActivity<VM : ViewModel> : VMBaseActivity<ActivityI
         }
     }
 
-    open fun addArchiveToBookShelf(
+    /* 添加压缩包内指定文件到书架 */
+    private inline fun addArchiveToBookShelf(
         fileDoc: FileDoc,
         fileName: String,
         onSuccess: (String) -> Unit
     ) {
+        LocalBook.importArchiveFile(fileDoc.uri, fileName) {
+            it.contains(fileName)
+        }.firstOrNull()?.run {
+            onSuccess.invoke(bookUrl)
+        }
     }
 
     /* 提示是否重新导入所点击的压缩文件 */

+ 0 - 7
app/src/main/java/io/legado/app/ui/book/import/local/ImportBookActivity.kt

@@ -303,11 +303,4 @@ class ImportBookActivity : BaseImportBookActivity<ImportBookViewModel>(),
         }
     }
 
-    override fun addArchiveToBookShelf(
-        fileDoc: FileDoc,
-        fileName: String,
-        onSuccess: (String) -> Unit
-    ) {
-        viewModel.addArchiveToBookShelf(fileDoc, fileName, onSuccess)
-    }
 }

+ 0 - 12
app/src/main/java/io/legado/app/ui/book/import/local/ImportBookViewModel.kt

@@ -97,18 +97,6 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
         }
     }
 
-    fun addArchiveToBookShelf(
-        fileDoc: FileDoc,
-        fileName: String,
-        onSuccess: (String) -> Unit
-    ) {
-        LocalBook.importArchiveFile(fileDoc.uri, fileName) {
-            it.contains(fileName)
-        }.firstOrNull()?.run {
-            onSuccess.invoke(bookUrl)
-        }
-    }
-
     fun deleteDoc(uriList: HashSet<String>, finally: () -> Unit) {
         execute {
             uriList.forEach {

+ 40 - 1
app/src/main/java/io/legado/app/ui/book/import/remote/RemoteBookActivity.kt

@@ -9,7 +9,9 @@ import androidx.activity.viewModels
 import androidx.core.view.isGone
 import androidx.recyclerview.widget.LinearLayoutManager
 import io.legado.app.R
+import io.legado.app.data.appDb
 import io.legado.app.data.entities.Book
+import io.legado.app.help.config.AppConfig
 import io.legado.app.help.config.LocalConfig
 import io.legado.app.lib.theme.backgroundColor
 import io.legado.app.model.remote.RemoteBook
@@ -17,6 +19,8 @@ import io.legado.app.ui.about.AppLogDialog
 import io.legado.app.ui.book.import.BaseImportBookActivity
 import io.legado.app.ui.widget.SelectActionBar
 import io.legado.app.ui.widget.dialog.TextDialog
+import io.legado.app.utils.FileDoc
+import io.legado.app.utils.find
 import io.legado.app.utils.showDialogFragment
 import kotlinx.coroutines.delay
 import kotlinx.coroutines.flow.conflate
@@ -197,6 +201,41 @@ class RemoteBookActivity : BaseImportBookActivity<RemoteBookViewModel>(),
         showDialogFragment(TextDialog(getString(R.string.help), mdText, TextDialog.Mode.MD))
     }
 
-    override fun startRead(book: Book) = startReadBook(book.bookUrl)
+    private fun showRemoteBookDownloadAlert(
+        remoteBook: RemoteBook,
+        onDownloadFinish: (() -> Unit)? = null
+    ) {
+        alert(
+            R.string.draw,
+            R.string.archive_not_found
+        ) {
+            okButton {
+                viewModel.addToBookshelf(hashSetOf<RemoteBook>(remoteBook) {
+                    onDownloadFinish?.invoke()
+                }
+            }
+            noButton()
+        }
+    }
+
+    override fun startRead(remoteBook: RemoteBook) {
+        val downloadFileName = remoteBook.filename
+        if (!ArchiveUtils.isArchive(downloadFileName)) {
+            appDb.bookDao.getBookByFileName(downloadFileName)?.let {
+                startReadBook(it.bookUrl)
+            }
+        } else {
+            AppConfig.defaultBookTreeUri ?: return
+            val downloadArchiveFileDoc = FileDoc.fromUri(Uri.parse(AppConfig.defaultBookTreeUri), true)
+                .find(downloadFileName)
+            if (downloadArchiveFileDoc == null) {
+                showRemoteBookDownloadAlert(remoteBook) {
+                    startRead(remoteBook)
+                }
+            } else {
+                onArchiveFileClick(downloadArchiveFileDoc)
+            }
+        }
+    }
 
 }

+ 2 - 5
app/src/main/java/io/legado/app/ui/book/import/remote/RemoteBookAdapter.kt

@@ -7,7 +7,6 @@ import io.legado.app.R
 import io.legado.app.base.adapter.ItemViewHolder
 import io.legado.app.base.adapter.RecyclerAdapter
 import io.legado.app.constant.AppConst
-import io.legado.app.data.appDb
 import io.legado.app.data.entities.Book
 import io.legado.app.databinding.ItemImportBookBinding
 import io.legado.app.model.remote.RemoteBook
@@ -88,9 +87,7 @@ class RemoteBookAdapter(context: Context, val callBack: CallBack) :
                     callBack.upCountView()
                 } else {
                     /* 点击开始阅读 */
-                    appDb.bookDao.getBookByFileName(it.filename)?.let {
-                        callBack.startRead(it)
-                    }
+                    callBack.startRead(it)
                 }
             }
         }
@@ -147,6 +144,6 @@ class RemoteBookAdapter(context: Context, val callBack: CallBack) :
     interface CallBack {
         fun openDir(remoteBook: RemoteBook)
         fun upCountView()
-        fun startRead(book: Book)
+        fun startRead(remoteBook: RemoteBook)
     }
 }

+ 1 - 0
app/src/main/res/values-es-rES/strings.xml

@@ -1084,4 +1084,5 @@
     <string name="no_books_dir">没有设置书籍保存位置!</string>
     <string name="delete_alert">删除提醒</string>
     <string name="no_book_found_bookshelf">No book found in bookshelf, import again ?</string>
+    <string name="archive_not_found">Can not find archive selected, continue to download ?</string>
 </resources>

+ 1 - 0
app/src/main/res/values-ja-rJP/strings.xml

@@ -1087,4 +1087,5 @@
     <string name="no_books_dir">没有设置书籍保存位置!</string>
     <string name="delete_alert">删除提醒</string>
     <string name="no_book_found_bookshelf">No book found in bookshelf, import again ?</string>
+    <string name="archive_not_found">Can not find archive selected, continue to download ?</string>
 </resources>

+ 1 - 0
app/src/main/res/values-pt-rBR/strings.xml

@@ -1087,4 +1087,5 @@
     <string name="no_books_dir">没有设置书籍保存位置!</string>
     <string name="delete_alert">删除提醒</string>
     <string name="no_book_found_bookshelf">No book found in bookshelf, import again ?</string>
+    <string name="archive_not_found">Can not find archive selected, continue to download ?</string>
 </resources>

+ 1 - 0
app/src/main/res/values-zh-rHK/strings.xml

@@ -1084,4 +1084,5 @@
     <string name="no_books_dir">没有设置书籍保存位置!</string>
     <string name="delete_alert">删除提醒</string>
     <string name="no_book_found_bookshelf">未在书架上找到所选书籍, 是否重新导入?</string>
+    <string name="archive_not_found">未能在书籍保存目录找到压缩文件,是否重新导入?</string>
 </resources>

+ 1 - 0
app/src/main/res/values-zh-rTW/strings.xml

@@ -1086,4 +1086,5 @@
     <string name="no_books_dir">没有设置书籍保存位置!</string>
     <string name="delete_alert">删除提醒</string>
     <string name="no_book_found_bookshelf">未在书架上找到所选书籍, 是否重新导入?</string>
+    <string name="archive_not_found">未能在书籍保存目录找到压缩文件,是否重新导入?</string>
 </resources>

+ 1 - 0
app/src/main/res/values-zh/strings.xml

@@ -1086,4 +1086,5 @@
     <string name="no_books_dir">没有设置书籍保存位置!</string>
     <string name="delete_alert">删除提醒</string>
     <string name="no_book_found_bookshelf">未在书架上找到所选书籍, 是否重新导入?</string>
+    <string name="archive_not_found">未能在书籍保存目录找到压缩文件,是否重新导入?</string>
 </resources>

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -1087,4 +1087,5 @@
     <string name="no_books_dir">没有设置书籍保存位置!</string>
     <string name="delete_alert">删除提醒</string>
     <string name="no_book_found_bookshelf">No book found in bookshelf, import again ?</string>
+    <string name="archive_not_found">Can not find archive selected, continue to download ?</string>
 </resources>