This commit is contained in:
kunfei
2023-07-30 22:21:57 +08:00
parent 73972fdea5
commit 261c94d758
4 changed files with 42 additions and 3 deletions

View File

@@ -52,7 +52,7 @@ class AllBookmarkViewModel(application: Application) : BaseViewModel(application
author = it.bookAuthor
outputStream.write("## ${it.bookName} ${it.bookAuthor}\n\n".toByteArray())
}
outputStream.write("#### ${it.chapterName}\n".toByteArray())
outputStream.write("#### ${it.chapterName}\n\n".toByteArray())
outputStream.write("###### 原文\n ${it.bookText}\n\n".toByteArray())
outputStream.write("###### 摘要\n ${it.content}\n\n".toByteArray())
}

View File

@@ -44,7 +44,10 @@ class TocActivity : VMBaseActivity<ActivityChapterListBinding, TocViewModel>(),
private val waitDialog by lazy { WaitDialog(this) }
private val exportDir = registerForActivityResult(HandleFileContract()) {
it.uri?.let { uri ->
viewModel.saveBookmark(uri)
when (it.requestCode) {
1 -> viewModel.saveBookmark(uri)
2 -> viewModel.saveBookmarkMd(uri)
}
}
}
@@ -157,7 +160,13 @@ class TocActivity : VMBaseActivity<ActivityChapterListBinding, TocViewModel>(),
viewModel.chapterListCallBack?.upChapterList(searchView?.query?.toString())
}
R.id.menu_export_bookmark -> exportDir.launch()
R.id.menu_export_bookmark -> exportDir.launch {
requestCode = 1
}
R.id.menu_export_md -> exportDir.launch {
requestCode = 2
}
R.id.menu_log -> showDialogFragment<AppLogDialog>()
}

View File

@@ -14,6 +14,7 @@ import io.legado.app.model.localBook.LocalBook
import io.legado.app.utils.FileDoc
import io.legado.app.utils.GSON
import io.legado.app.utils.createFileIfNotExist
import io.legado.app.utils.openOutputStream
import io.legado.app.utils.toastOnUi
import io.legado.app.utils.writeText
@@ -90,6 +91,30 @@ class TocViewModel(application: Application) : BaseViewModel(application) {
}
}
fun saveBookmarkMd(treeUri: Uri) {
execute {
val book = bookData.value
?: throw NoStackTraceException(context.getString(R.string.no_book))
val fileName = "bookmark-${book.name} ${book.author}.md"
val treeDoc = FileDoc.fromUri(treeUri, true)
val fileDoc = treeDoc.createFileIfNotExist(fileName)
.openOutputStream()
.getOrThrow()
fileDoc.use { outputStream ->
outputStream.write("## ${book.name} ${book.author}\n\n".toByteArray())
appDb.bookmarkDao.getByBook(book.name, book.author).forEach {
outputStream.write("#### ${it.chapterName}\n\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("导出成功")
}
}
interface ChapterListCallBack {
fun upChapterList(searchKey: String?)

View File

@@ -53,6 +53,11 @@
android:title="@string/export"
app:showAsAction="never" />
<item
android:id="@+id/menu_export_md"
android:title="@string/export_md"
app:showAsAction="never" />
</group>
<item