书签添加导出

pull/2587/head
kunfei 2 years ago
parent 6d0f1ec02c
commit 0eb0ba34d8
  1. 23
      app/src/main/java/io/legado/app/ui/book/bookmark/AllBookmarkActivity.kt
  2. 31
      app/src/main/java/io/legado/app/ui/book/bookmark/AllBookmarkViewModel.kt
  3. 6
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  4. 8
      app/src/main/res/menu/bookmark.xml

@ -1,10 +1,15 @@
package io.legado.app.ui.book.bookmark
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.Bookmark
import io.legado.app.databinding.ActivityAllBookmarkBinding
import io.legado.app.ui.document.HandleFileContract
import io.legado.app.utils.launch
import io.legado.app.utils.showDialogFragment
import io.legado.app.utils.viewbindingdelegate.viewBinding
@ -17,6 +22,11 @@ class AllBookmarkActivity : VMBaseActivity<ActivityAllBookmarkBinding, AllBookma
private val adapter by lazy {
BookmarkAdapter(this, this)
}
private val exportDir = registerForActivityResult(HandleFileContract()) {
it.uri?.let { uri ->
viewModel.saveToFile(uri)
}
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
initView()
@ -30,6 +40,18 @@ class AllBookmarkActivity : VMBaseActivity<ActivityAllBookmarkBinding, AllBookma
binding.recyclerView.adapter = adapter
}
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.bookmark, menu)
return super.onCompatCreateOptionsMenu(menu)
}
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_export -> exportDir.launch()
}
return super.onCompatOptionsItemSelected(item)
}
override fun onItemClick(bookmark: Bookmark, position: Int) {
showDialogFragment(BookmarkDialog(bookmark, position))
}
@ -44,4 +66,5 @@ class AllBookmarkActivity : VMBaseActivity<ActivityAllBookmarkBinding, AllBookma
}
adapter.removeItem(pos)
}
}

@ -1,9 +1,17 @@
package io.legado.app.ui.book.bookmark
import android.annotation.SuppressLint
import android.app.Application
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb
import io.legado.app.data.entities.Bookmark
import io.legado.app.utils.*
import java.io.File
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.*
class AllBookmarkViewModel(application: Application) : BaseViewModel(application) {
@ -22,4 +30,27 @@ class AllBookmarkViewModel(application: Application) : BaseViewModel(application
}
}
@Suppress("BlockingMethodInNonBlockingContext")
@SuppressLint("SimpleDateFormat")
fun saveToFile(treeUri: Uri) {
execute {
val dateFormat = SimpleDateFormat("yyMMddHHmmss")
if (treeUri.isContentScheme()) {
val doc = DocumentFile.fromTreeUri(context, treeUri)
?.createFile("", "bookmark-${dateFormat.format(Date())}")
doc?.let {
context.contentResolver.openOutputStream(doc.uri)!!.use {
GSON.writeToOutputStream(it, appDb.bookmarkDao.all)
}
}
} else {
val path = treeUri.path!!
val file = FileUtils.createFileIfNotExist(File(path), "bookmark-${dateFormat.format(Date())}")
FileOutputStream(file).use {
GSON.writeToOutputStream(it, appDb.bookmarkDao.all)
}
}
}
}
}

@ -276,7 +276,11 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
binding.webView.evaluateJavascript("document.documentElement.outerHTML") {
val html = StringEscapeUtils.unescapeJson(it)
.replace("^\"|\"$".toRegex(), "")
viewModel.readAloud(Jsoup.parse(html).textArray().joinToString("\n"))
viewModel.readAloud(
Jsoup.parse(html)
.textArray()
.joinToString("\n")
)
}
}
}

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_export"
android:title="@string/export" />
</menu>
Loading…
Cancel
Save