Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Sep 25, 2023
1 parent f724c08 commit 3601ba1
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/src/main/java/io/legado/app/data/dao/BookDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.room.*
import io.legado.app.constant.BookType
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookGroup
import io.legado.app.data.entities.BookSource
import kotlinx.coroutines.flow.Flow

@Dao
Expand Down Expand Up @@ -81,6 +82,11 @@ interface BookDao {
@Query("SELECT * FROM books WHERE name = :name and author = :author")
fun getBook(name: String, author: String): Book?

@Query("""select distinct bs.* from books, book_sources bs
where origin == bookSourceUrl and origin not like '${BookType.localTag}%'
and origin not like '${BookType.webDavTag}%'""")
fun getAllUseBookSource(): List<BookSource>

@Query("SELECT * FROM books WHERE name = :name and origin = :origin")
fun getBookByOrigin(name: String, origin: String): Book?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookGroup
import io.legado.app.data.entities.BookSource
import io.legado.app.databinding.ActivityArrangeBookBinding
import io.legado.app.databinding.DialogEditTextBinding
import io.legado.app.help.DirectLinkUpload
import io.legado.app.help.book.contains
import io.legado.app.help.book.isLocal
import io.legado.app.help.config.AppConfig
Expand All @@ -31,6 +33,7 @@ import io.legado.app.lib.theme.primaryTextColor
import io.legado.app.ui.book.group.GroupManageDialog
import io.legado.app.ui.book.group.GroupSelectDialog
import io.legado.app.ui.book.info.BookInfoActivity
import io.legado.app.ui.file.HandleFileContract
import io.legado.app.ui.widget.SelectActionBar
import io.legado.app.ui.widget.dialog.WaitDialog
import io.legado.app.ui.widget.recycler.DragSelectTouchHelper
Expand Down Expand Up @@ -70,6 +73,23 @@ class BookshelfManageActivity :
}
private var books: List<Book>? = null
private val waitDialog by lazy { WaitDialog(this) }
private val exportDir = registerForActivityResult(HandleFileContract()) {
it.uri?.let { uri ->
alert(R.string.export_success) {
if (uri.toString().isAbsUrl()) {
setMessage(DirectLinkUpload.getSummary())
}
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
editView.hint = getString(R.string.path)
editView.setText(uri.toString())
}
customView { alertBinding.root }
okButton {
sendToClip(uri.toString())
}
}
}
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
viewModel.groupId = intent.getLongExtra("groupId", -1)
Expand Down Expand Up @@ -255,6 +275,17 @@ class BookshelfManageActivity :
adapter.notifyItemRangeChanged(0, adapter.itemCount)
}

R.id.menu_export_all_use_book_source -> viewModel.saveAllUseBookSourceToFile { file ->
exportDir.launch {
mode = HandleFileContract.EXPORT
fileData = HandleFileContract.FileData(
"bookSource.json",
file,
"application/json"
)
}
}

else -> if (item.groupId == R.id.menu_group) {
viewModel.groupName = item.title.toString()
upTitle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ import io.legado.app.help.config.AppConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON
import io.legado.app.utils.stackTraceStr
import io.legado.app.utils.toastOnUi
import io.legado.app.utils.writeToOutputStream
import kotlinx.coroutines.delay
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream


class BookshelfManageViewModel(application: Application) : BaseViewModel(application) {
Expand Down Expand Up @@ -50,6 +58,26 @@ class BookshelfManageViewModel(application: Application) : BaseViewModel(applica
}
}

@Suppress("BlockingMethodInNonBlockingContext")
fun saveAllUseBookSourceToFile(success: (file: File) -> Unit) {
execute {
val path = "${context.filesDir}/shareBookSource.json"
FileUtils.delete(path)
val file = FileUtils.createFileWithReplace(path)
val sources = appDb.bookDao.getAllUseBookSource()
FileOutputStream(file).use { out ->
BufferedOutputStream(out, 64 * 1024).use {
GSON.writeToOutputStream(it, sources)
}
}
file
}.onSuccess {
success.invoke(it)
}.onError {
context.toastOnUi(it.stackTraceStr)
}
}

fun changeSource(books: List<Book>, source: BookSource) {
batchChangeSourceCoroutine?.cancel()
batchChangeSourceCoroutine = execute {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/menu/bookshelf_manage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

</item>

<item
android:id="@+id/menu_export_all_use_book_source"
android:title="@string/export_all_use_book_source" />

<item
android:id="@+id/menu_open_book_info_by_click_title"
android:checkable="true"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es-rES/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1130,4 +1130,5 @@
<string name="export_wait">等待导出</string>
<string name="default_home_page">默认主页</string>
<string name="show_bookshelf_fast_scroller">显示快速滚动条</string>
<string name="export_all_use_book_source">导出所有书的书源</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ja-rJP/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1133,4 +1133,5 @@
<string name="export_wait">等待导出</string>
<string name="default_home_page">默认主页</string>
<string name="show_bookshelf_fast_scroller">显示快速滚动条</string>
<string name="export_all_use_book_source">导出所有书的书源</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1133,4 +1133,5 @@
<string name="export_wait">等待导出</string>
<string name="default_home_page">默认主页</string>
<string name="show_bookshelf_fast_scroller">显示快速滚动条</string>
<string name="export_all_use_book_source">导出所有书的书源</string>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1128,4 +1128,5 @@ Còn </string>
<string name="open_book_info_by_click_title">点击书名打开详情</string>
<string name="export_wait">等待导出</string>
<string name="show_bookshelf_fast_scroller">显示快速滚动条</string>
</resources>
<string name="export_all_use_book_source">导出所有书的书源</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1129,4 +1129,5 @@
<string name="open_book_info_by_click_title">点击书名打开详情</string>
<string name="export_wait">等待导出</string>
<string name="show_bookshelf_fast_scroller">显示快速滚动条</string>
<string name="export_all_use_book_source">导出所有书的书源</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1131,4 +1131,5 @@
<string name="open_book_info_by_click_title">点击书名打开详情</string>
<string name="export_wait">等待导出</string>
<string name="show_bookshelf_fast_scroller">显示快速滚动条</string>
<string name="export_all_use_book_source">导出所有书的书源</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1131,4 +1131,5 @@
<string name="open_book_info_by_click_title">点击书名打开详情</string>
<string name="export_wait">等待导出</string>
<string name="show_bookshelf_fast_scroller">显示快速滚动条</string>
<string name="export_all_use_book_source">导出所有书的书源</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1133,4 +1133,5 @@
<string name="export_wait">等待导出</string>
<string name="default_home_page">默认主页</string>
<string name="show_bookshelf_fast_scroller">显示快速滚动条</string>
<string name="export_all_use_book_source">导出所有书的书源</string>
</resources>

0 comments on commit 3601ba1

Please sign in to comment.