mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
书签添加导出
This commit is contained in:
parent
6d0f1ec02c
commit
0eb0ba34d8
@ -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")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
8
app/src/main/res/menu/bookmark.xml
Normal file
8
app/src/main/res/menu/bookmark.xml
Normal file
@ -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…
Reference in New Issue
Block a user