This commit is contained in:
kunfei 2023-07-30 21:15:37 +08:00
parent b7fcdb661e
commit 73972fdea5
13 changed files with 62 additions and 15 deletions

View File

@ -2,7 +2,6 @@ package io.legado.app.data.dao
import androidx.room.* import androidx.room.*
import io.legado.app.constant.BookType 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.Book
import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookGroup
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@ -12,14 +11,14 @@ interface BookDao {
fun flowByGroup(groupId: Long): Flow<List<Book>> { fun flowByGroup(groupId: Long): Flow<List<Book>> {
return when (groupId) { return when (groupId) {
BookGroup.IdRoot -> appDb.bookDao.flowRoot() BookGroup.IdRoot -> flowRoot()
BookGroup.IdAll -> appDb.bookDao.flowAll() BookGroup.IdAll -> flowAll()
BookGroup.IdLocal -> appDb.bookDao.flowLocal() BookGroup.IdLocal -> flowLocal()
BookGroup.IdAudio -> appDb.bookDao.flowAudio() BookGroup.IdAudio -> flowAudio()
BookGroup.IdNetNone -> appDb.bookDao.flowNetNoGroup() BookGroup.IdNetNone -> flowNetNoGroup()
BookGroup.IdLocalNone -> appDb.bookDao.flowLocalNoGroup() BookGroup.IdLocalNone -> flowLocalNoGroup()
BookGroup.IdError -> appDb.bookDao.flowUpdateError() BookGroup.IdError -> flowUpdateError()
else -> appDb.bookDao.flowByUserGroup(groupId) else -> flowByUserGroup(groupId)
} }
} }

View File

@ -10,7 +10,6 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.Bookmark import io.legado.app.data.entities.Bookmark
import io.legado.app.databinding.ActivityAllBookmarkBinding import io.legado.app.databinding.ActivityAllBookmarkBinding
import io.legado.app.ui.file.HandleFileContract import io.legado.app.ui.file.HandleFileContract
import io.legado.app.utils.launch
import io.legado.app.utils.showDialogFragment import io.legado.app.utils.showDialogFragment
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -25,7 +24,10 @@ class AllBookmarkActivity : VMBaseActivity<ActivityAllBookmarkBinding, AllBookma
} }
private val exportDir = registerForActivityResult(HandleFileContract()) { private val exportDir = registerForActivityResult(HandleFileContract()) {
it.uri?.let { uri -> 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 { override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { 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) return super.onCompatOptionsItemSelected(item)
} }

View File

@ -21,10 +21,10 @@ class AllBookmarkViewModel(application: Application) : BaseViewModel(application
/** /**
* 导出书签 * 导出书签
*/ */
fun saveToFile(treeUri: Uri) { fun exportBookmark(treeUri: Uri) {
execute { execute {
val dateFormat = SimpleDateFormat("yyMMddHHmmss", Locale.getDefault()) 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) val dirDoc = FileDoc.fromUri(treeUri, true)
dirDoc.createFileIfNotExist(fileName).openOutputStream().getOrThrow().use { dirDoc.createFileIfNotExist(fileName).openOutputStream().getOrThrow().use {
GSON.writeToOutputStream(it, appDb.bookmarkDao.all) 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("导出成功")
}
}
} }

View File

@ -76,7 +76,7 @@ class TocViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
val book = bookData.value val book = bookData.value
?: throw NoStackTraceException(context.getString(R.string.no_book)) ?: 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) val doc = FileDoc.fromUri(treeUri, true)
doc.createFileIfNotExist(fileName).writeText( doc.createFileIfNotExist(fileName).writeText(
GSON.toJson( GSON.toJson(

View File

@ -5,4 +5,8 @@
android:id="@+id/menu_export" android:id="@+id/menu_export"
android:title="@string/export" /> android:title="@string/export" />
<item
android:id="@+id/menu_export_md"
android:title="@string/export_md" />
</menu> </menu>

View File

@ -1124,4 +1124,5 @@
<string name="effective_replaces">起效的替换</string> <string name="effective_replaces">起效的替换</string>
<string name="export_book">导出书籍</string> <string name="export_book">导出书籍</string>
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string> <string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
<string name="export_md">导出(MD)</string>
</resources> </resources>

View File

@ -1127,4 +1127,5 @@
<string name="effective_replaces">起效的替换</string> <string name="effective_replaces">起效的替换</string>
<string name="export_book">导出书籍</string> <string name="export_book">导出书籍</string>
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string> <string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
<string name="export_md">导出(MD)</string>
</resources> </resources>

View File

@ -1127,4 +1127,5 @@
<string name="effective_replaces">起效的替换</string> <string name="effective_replaces">起效的替换</string>
<string name="export_book">导出书籍</string> <string name="export_book">导出书籍</string>
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string> <string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
<string name="export_md">导出(MD)</string>
</resources> </resources>

View File

@ -1123,4 +1123,5 @@ Còn </string>
<string name="effective_replaces">起效的替换</string> <string name="effective_replaces">起效的替换</string>
<string name="export_book">导出书籍</string> <string name="export_book">导出书籍</string>
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string> <string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
<string name="export_md">导出(MD)</string>
</resources> </resources>

View File

@ -1124,4 +1124,5 @@
<string name="effective_replaces">起效的替换</string> <string name="effective_replaces">起效的替换</string>
<string name="export_book">导出书籍</string> <string name="export_book">导出书籍</string>
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string> <string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
<string name="export_md">导出(MD)</string>
</resources> </resources>

View File

@ -1126,4 +1126,5 @@
<string name="effective_replaces">起效的替换</string> <string name="effective_replaces">起效的替换</string>
<string name="export_book">导出书籍</string> <string name="export_book">导出书籍</string>
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string> <string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
<string name="export_md">导出(MD)</string>
</resources> </resources>

View File

@ -1126,4 +1126,5 @@
<string name="effective_replaces">起效的替换</string> <string name="effective_replaces">起效的替换</string>
<string name="export_book">导出书籍</string> <string name="export_book">导出书籍</string>
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string> <string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
<string name="export_md">导出(MD)</string>
</resources> </resources>

View File

@ -1127,4 +1127,5 @@
<string name="effective_replaces">起效的替换</string> <string name="effective_replaces">起效的替换</string>
<string name="export_book">导出书籍</string> <string name="export_book">导出书籍</string>
<string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string> <string name="export_book_notification_content">正在导出(%1$s),还有%2$d本待导出</string>
<string name="export_md">导出(MD)</string>
</resources> </resources>