优化数据库的关联更新,添加pdfview界面的更多选项功能
This commit is contained in:
parent
9dfa05ef97
commit
456bbf5ecc
@ -33,4 +33,7 @@ interface BookmarkDao {
|
|||||||
|
|
||||||
@Query("DELETE FROM bookmarks WHERE filePath = :filePath AND pageNumber = :pageNumber")
|
@Query("DELETE FROM bookmarks WHERE filePath = :filePath AND pageNumber = :pageNumber")
|
||||||
suspend fun deleteByPage(filePath: String, pageNumber: Int)
|
suspend fun deleteByPage(filePath: String, pageNumber: Int)
|
||||||
|
|
||||||
|
@Query("UPDATE bookmarks SET filePath = :newFilePath WHERE filePath = :oldFilePath")
|
||||||
|
suspend fun updateFilePath(oldFilePath: String, newFilePath: String)
|
||||||
}
|
}
|
||||||
@ -11,7 +11,8 @@ import androidx.room.PrimaryKey
|
|||||||
entity = PdfDocumentEntity::class,
|
entity = PdfDocumentEntity::class,
|
||||||
parentColumns = ["filePath"],
|
parentColumns = ["filePath"],
|
||||||
childColumns = ["filePath"],
|
childColumns = ["filePath"],
|
||||||
onDelete = ForeignKey.CASCADE
|
onDelete = ForeignKey.CASCADE,
|
||||||
|
onUpdate = ForeignKey.CASCADE
|
||||||
)],
|
)],
|
||||||
indices = [Index(value = ["filePath"])]
|
indices = [Index(value = ["filePath"])]
|
||||||
)
|
)
|
||||||
@ -19,7 +20,7 @@ data class BookmarkEntity(
|
|||||||
@PrimaryKey(autoGenerate = true)
|
@PrimaryKey(autoGenerate = true)
|
||||||
val id: Long = 0,
|
val id: Long = 0,
|
||||||
|
|
||||||
val filePath: String, // 关联PdfDocumentEntity的fileHash
|
val filePath: String, // 关联PdfDocumentEntity的filePath
|
||||||
val pageNumber: Int, // 页码(从0开始)
|
val pageNumber: Int, // 页码(从0开始)
|
||||||
val label: String, // 书签标签
|
val label: String, // 书签标签
|
||||||
val positionX: Float = 0f, // 页面内X位置
|
val positionX: Float = 0f, // 页面内X位置
|
||||||
|
|||||||
@ -11,7 +11,8 @@ import androidx.room.PrimaryKey
|
|||||||
entity = PdfDocumentEntity::class,
|
entity = PdfDocumentEntity::class,
|
||||||
parentColumns = ["filePath"],
|
parentColumns = ["filePath"],
|
||||||
childColumns = ["filePath"],
|
childColumns = ["filePath"],
|
||||||
onDelete = ForeignKey.CASCADE
|
onDelete = ForeignKey.CASCADE,
|
||||||
|
onUpdate = ForeignKey.CASCADE
|
||||||
)],
|
)],
|
||||||
indices = [Index(value = ["filePath"])]
|
indices = [Index(value = ["filePath"])]
|
||||||
)
|
)
|
||||||
|
|||||||
@ -11,7 +11,8 @@ import androidx.room.PrimaryKey
|
|||||||
entity = PdfDocumentEntity::class,
|
entity = PdfDocumentEntity::class,
|
||||||
parentColumns = ["filePath"],
|
parentColumns = ["filePath"],
|
||||||
childColumns = ["filePath"],
|
childColumns = ["filePath"],
|
||||||
onDelete = ForeignKey.CASCADE
|
onDelete = ForeignKey.CASCADE,
|
||||||
|
onUpdate = ForeignKey.CASCADE
|
||||||
)],
|
)],
|
||||||
indices = [Index(value = ["filePath"])]
|
indices = [Index(value = ["filePath"])]
|
||||||
)
|
)
|
||||||
|
|||||||
@ -124,6 +124,9 @@ class PdfRepository private constructor(context: Context) {
|
|||||||
// 书签相关操作
|
// 书签相关操作
|
||||||
suspend fun addBookmark(bookmark: BookmarkEntity): Long = bookmarkDao.insert(bookmark)
|
suspend fun addBookmark(bookmark: BookmarkEntity): Long = bookmarkDao.insert(bookmark)
|
||||||
suspend fun updateBookmark(bookmark: BookmarkEntity): Int = bookmarkDao.update(bookmark)
|
suspend fun updateBookmark(bookmark: BookmarkEntity): Int = bookmarkDao.update(bookmark)
|
||||||
|
suspend fun updateBookmarkFilePath(oldPath: String, newPath: String) =
|
||||||
|
bookmarkDao.updateFilePath(oldPath, newPath)
|
||||||
|
|
||||||
suspend fun deleteBookmark(bookmark: BookmarkEntity): Int = bookmarkDao.delete(bookmark)
|
suspend fun deleteBookmark(bookmark: BookmarkEntity): Int = bookmarkDao.delete(bookmark)
|
||||||
suspend fun deleteAllBookmark(filePath: String): Int {
|
suspend fun deleteAllBookmark(filePath: String): Int {
|
||||||
return bookmarkDao.deleteAllByPdf(filePath)
|
return bookmarkDao.deleteAllByPdf(filePath)
|
||||||
|
|||||||
@ -73,7 +73,10 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
|||||||
viewModel.pdfDocument.observe(this) { document ->
|
viewModel.pdfDocument.observe(this) { document ->
|
||||||
document?.let {
|
document?.let {
|
||||||
pdfDocument = it
|
pdfDocument = it
|
||||||
|
if (lastLoadedFilePath != pdfDocument.filePath) {
|
||||||
|
lastLoadedFilePath = pdfDocument.filePath
|
||||||
loadPdf()
|
loadPdf()
|
||||||
|
}
|
||||||
initView()
|
initView()
|
||||||
} ?: run {
|
} ?: run {
|
||||||
showToast(getString(R.string.file_not))
|
showToast(getString(R.string.file_not))
|
||||||
@ -108,6 +111,31 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
|||||||
showToast(getString(R.string.bookmark_add_fail))
|
showToast(getString(R.string.bookmark_add_fail))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
viewModel.fileActionEvent.observeEvent<FileActionEvent.Rename>(this) { event ->
|
||||||
|
if (event.renameResult.success) {
|
||||||
|
showToast(getString(R.string.rename_successfully))
|
||||||
|
event.renameResult.newFilePath?.let {
|
||||||
|
viewModel.getPDFDocument(it)//修改成功后更新当前页面的数据
|
||||||
|
viewModel.getBookmarks(it)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
showToast(event.renameResult.errorMessage.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
viewModel.fileActionEvent.observeEvent<FileActionEvent.Duplicate>(this) { event ->
|
||||||
|
if (event.file != null) {
|
||||||
|
showToast(getString(R.string.duplicate_created_successfully))
|
||||||
|
} else {
|
||||||
|
showToast(getString(R.string.duplicate_created_failed))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
viewModel.fileActionEvent.observeEvent<FileActionEvent.Favorite>(this) { event ->
|
||||||
|
if (event.isFavorite) {
|
||||||
|
showToast(getString(R.string.added_to_favorites))
|
||||||
|
} else {
|
||||||
|
showToast(getString(R.string.removed_from_favorites))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initView() {
|
private fun initView() {
|
||||||
@ -138,7 +166,7 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
|||||||
AppUtils.shareFile(this, File(pdfDocument.filePath))
|
AppUtils.shareFile(this, File(pdfDocument.filePath))
|
||||||
}
|
}
|
||||||
binding.moreBtn.setOnClickListener {
|
binding.moreBtn.setOnClickListener {
|
||||||
// ListMoreDialogFragment(pdfDocument.filePath).show(supportFragmentManager, FRAG_TAG)
|
ListMoreDialogFragment(pdfDocument.filePath).show(supportFragmentManager, FRAG_TAG)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -67,7 +67,6 @@ class PdfViewModel : ViewModel() {
|
|||||||
val newFilePath = File(parentDir, finalName).absolutePath.toString()
|
val newFilePath = File(parentDir, finalName).absolutePath.toString()
|
||||||
Log.d("ocean", "renamePdf->newFilePath: $newFilePath, finalName=$finalName")
|
Log.d("ocean", "renamePdf->newFilePath: $newFilePath, finalName=$finalName")
|
||||||
pdfRepository.updateFilePathAndFileName(filePath, newFilePath, finalName)
|
pdfRepository.updateFilePathAndFileName(filePath, newFilePath, finalName)
|
||||||
|
|
||||||
val newResult = renameResult.copy(newFilePath = newFilePath)
|
val newResult = renameResult.copy(newFilePath = newFilePath)
|
||||||
_fileActionEvent.postValue(FileActionEvent.Rename(newResult))
|
_fileActionEvent.postValue(FileActionEvent.Rename(newResult))
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -68,7 +68,7 @@
|
|||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<LinearLayout
|
<RelativeLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
@ -80,7 +80,14 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:background="@color/grey" />
|
android:background="@color/grey" />
|
||||||
|
|
||||||
</LinearLayout>
|
<com.github.barteksc.pdfviewer.PDFView
|
||||||
|
android:id="@+id/pdfview2"
|
||||||
|
android:visibility="gone"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="@color/grey" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:id="@+id/navigationLayout"
|
android:id="@+id/navigationLayout"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user