mirror of
https://github.com/gedoor/legado.git
synced 2024-07-19 01:17:25 +08:00
优化
This commit is contained in:
parent
b7fcdb661e
commit
73972fdea5
@ -2,7 +2,6 @@ package io.legado.app.data.dao
|
||||
|
||||
import androidx.room.*
|
||||
import io.legado.app.constant.BookType
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.data.entities.Book
|
||||
import io.legado.app.data.entities.BookGroup
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
@ -12,14 +11,14 @@ interface BookDao {
|
||||
|
||||
fun flowByGroup(groupId: Long): Flow<List<Book>> {
|
||||
return when (groupId) {
|
||||
BookGroup.IdRoot -> appDb.bookDao.flowRoot()
|
||||
BookGroup.IdAll -> appDb.bookDao.flowAll()
|
||||
BookGroup.IdLocal -> appDb.bookDao.flowLocal()
|
||||
BookGroup.IdAudio -> appDb.bookDao.flowAudio()
|
||||
BookGroup.IdNetNone -> appDb.bookDao.flowNetNoGroup()
|
||||
BookGroup.IdLocalNone -> appDb.bookDao.flowLocalNoGroup()
|
||||
BookGroup.IdError -> appDb.bookDao.flowUpdateError()
|
||||
else -> appDb.bookDao.flowByUserGroup(groupId)
|
||||
BookGroup.IdRoot -> flowRoot()
|
||||
BookGroup.IdAll -> flowAll()
|
||||
BookGroup.IdLocal -> flowLocal()
|
||||
BookGroup.IdAudio -> flowAudio()
|
||||
BookGroup.IdNetNone -> flowNetNoGroup()
|
||||
BookGroup.IdLocalNone -> flowLocalNoGroup()
|
||||
BookGroup.IdError -> flowUpdateError()
|
||||
else -> flowByUserGroup(groupId)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,6 @@ import io.legado.app.data.appDb
|
||||
import io.legado.app.data.entities.Bookmark
|
||||
import io.legado.app.databinding.ActivityAllBookmarkBinding
|
||||
import io.legado.app.ui.file.HandleFileContract
|
||||
import io.legado.app.utils.launch
|
||||
import io.legado.app.utils.showDialogFragment
|
||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||
import kotlinx.coroutines.launch
|
||||
@ -25,7 +24,10 @@ class AllBookmarkActivity : VMBaseActivity<ActivityAllBookmarkBinding, AllBookma
|
||||
}
|
||||
private val exportDir = registerForActivityResult(HandleFileContract()) {
|
||||
it.uri?.let { uri ->
|
||||
viewModel.saveToFile(uri)
|
||||
when (it.requestCode) {
|
||||
1 -> viewModel.exportBookmark(uri)
|
||||
2 -> viewModel.exportBookmarkMd(uri)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,7 +52,13 @@ class AllBookmarkActivity : VMBaseActivity<ActivityAllBookmarkBinding, AllBookma
|
||||
|
||||
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.menu_export -> exportDir.launch()
|
||||
R.id.menu_export -> exportDir.launch {
|
||||
requestCode = 1
|
||||
}
|
||||
|
||||
R.id.menu_export_md -> exportDir.launch {
|
||||
requestCode = 2
|
||||
}
|
||||
}
|
||||
return super.onCompatOptionsItemSelected(item)
|
||||
}
|
||||
|
@ -21,10 +21,10 @@ class AllBookmarkViewModel(application: Application) : BaseViewModel(application
|
||||
/**
|
||||
* 导出书签
|
||||
*/
|
||||
fun saveToFile(treeUri: Uri) {
|
||||
fun exportBookmark(treeUri: Uri) {
|
||||
execute {
|
||||
val dateFormat = SimpleDateFormat("yyMMddHHmmss", Locale.getDefault())
|
||||
val fileName = "bookmark-${dateFormat.format(Date())}"
|
||||
val fileName = "bookmark-${dateFormat.format(Date())}.json"
|
||||
val dirDoc = FileDoc.fromUri(treeUri, true)
|
||||
dirDoc.createFileIfNotExist(fileName).openOutputStream().getOrThrow().use {
|
||||
GSON.writeToOutputStream(it, appDb.bookmarkDao.all)
|
||||
@ -36,4 +36,32 @@ class AllBookmarkViewModel(application: Application) : BaseViewModel(application
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun exportBookmarkMd(treeUri: Uri) {
|
||||
execute {
|
||||
val dateFormat = SimpleDateFormat("yyMMddHHmmss", Locale.getDefault())
|
||||
val fileName = "bookmark-${dateFormat.format(Date())}.md"
|
||||
val dirDoc = FileDoc.fromUri(treeUri, true)
|
||||
val fileDoc = dirDoc.createFileIfNotExist(fileName).openOutputStream().getOrThrow()
|
||||
fileDoc.use { outputStream ->
|
||||
var name = ""
|
||||
var author = ""
|
||||
appDb.bookmarkDao.all.forEach {
|
||||
if (it.bookName != name && it.bookAuthor != author) {
|
||||
name = it.bookName
|
||||
author = it.bookAuthor
|
||||
outputStream.write("## ${it.bookName} ${it.bookAuthor}\n\n".toByteArray())
|
||||
}
|
||||
outputStream.write("#### ${it.chapterName}\n".toByteArray())
|
||||
outputStream.write("###### 原文\n ${it.bookText}\n\n".toByteArray())
|
||||
outputStream.write("###### 摘要\n ${it.content}\n\n".toByteArray())
|
||||
}
|
||||
}
|
||||
}.onError {
|
||||
AppLog.put("导出失败\n${it.localizedMessage}", it, true)
|
||||
}.onSuccess {
|
||||
context.toastOnUi("导出成功")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -76,7 +76,7 @@ class TocViewModel(application: Application) : BaseViewModel(application) {
|
||||
execute {
|
||||
val book = bookData.value
|
||||
?: throw NoStackTraceException(context.getString(R.string.no_book))
|
||||
val fileName = "bookmark-${book.name} ${book.author}"
|
||||
val fileName = "bookmark-${book.name} ${book.author}.json"
|
||||
val doc = FileDoc.fromUri(treeUri, true)
|
||||
doc.createFileIfNotExist(fileName).writeText(
|
||||
GSON.toJson(
|
||||
|
@ -5,4 +5,8 @@
|
||||
android:id="@+id/menu_export"
|
||||
android:title="@string/export" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_export_md"
|
||||
android:title="@string/export_md" />
|
||||
|
||||
</menu>
|
@ -1124,4 +1124,5 @@
|
||||
<string name="effective_replaces">起效的替换</string>
|
||||
<string name="export_book">导出书籍</string>
|
||||
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
|
||||
<string name="export_md">导出(MD)</string>
|
||||
</resources>
|
||||
|
@ -1127,4 +1127,5 @@
|
||||
<string name="effective_replaces">起效的替换</string>
|
||||
<string name="export_book">导出书籍</string>
|
||||
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
|
||||
<string name="export_md">导出(MD)</string>
|
||||
</resources>
|
||||
|
@ -1127,4 +1127,5 @@
|
||||
<string name="effective_replaces">起效的替换</string>
|
||||
<string name="export_book">导出书籍</string>
|
||||
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
|
||||
<string name="export_md">导出(MD)</string>
|
||||
</resources>
|
||||
|
@ -1123,4 +1123,5 @@ Còn </string>
|
||||
<string name="effective_replaces">起效的替换</string>
|
||||
<string name="export_book">导出书籍</string>
|
||||
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
|
||||
<string name="export_md">导出(MD)</string>
|
||||
</resources>
|
@ -1124,4 +1124,5 @@
|
||||
<string name="effective_replaces">起效的替换</string>
|
||||
<string name="export_book">导出书籍</string>
|
||||
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
|
||||
<string name="export_md">导出(MD)</string>
|
||||
</resources>
|
||||
|
@ -1126,4 +1126,5 @@
|
||||
<string name="effective_replaces">起效的替换</string>
|
||||
<string name="export_book">导出书籍</string>
|
||||
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
|
||||
<string name="export_md">导出(MD)</string>
|
||||
</resources>
|
||||
|
@ -1126,4 +1126,5 @@
|
||||
<string name="effective_replaces">起效的替换</string>
|
||||
<string name="export_book">导出书籍</string>
|
||||
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
|
||||
<string name="export_md">导出(MD)</string>
|
||||
</resources>
|
||||
|
@ -1127,4 +1127,5 @@
|
||||
<string name="effective_replaces">起效的替换</string>
|
||||
<string name="export_book">导出书籍</string>
|
||||
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
|
||||
<string name="export_md">导出(MD)</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user