添加自定义toast
This commit is contained in:
parent
f0fb627774
commit
e15837b557
@ -2,10 +2,11 @@ package com.all.pdfreader.pro.app.model
|
||||
|
||||
data class RenameResult(
|
||||
val success: Boolean,
|
||||
val errorMessage: String? = null
|
||||
val errorMessage: String? = null,
|
||||
val newFilePath: String? = null
|
||||
) {
|
||||
companion object {
|
||||
fun success() = RenameResult(true)
|
||||
fun success(newFilePath: String) = RenameResult(true, null, newFilePath)
|
||||
fun failure(message: String) = RenameResult(false, message)
|
||||
}
|
||||
}
|
||||
@ -10,6 +10,7 @@ import androidx.appcompat.app.AppCompatActivity
|
||||
import com.all.pdfreader.pro.app.room.repository.PdfRepository
|
||||
import com.all.pdfreader.pro.app.sp.AppStore
|
||||
import com.all.pdfreader.pro.app.util.StoragePermissionHelper
|
||||
import com.all.pdfreader.pro.app.util.ToastUtils
|
||||
import com.tom_roush.pdfbox.contentstream.operator.text.ShowText
|
||||
|
||||
abstract class BaseActivity : AppCompatActivity() {
|
||||
@ -63,7 +64,7 @@ abstract class BaseActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
protected fun showToast(message: String) {
|
||||
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
|
||||
ToastUtils.show(this, message)
|
||||
}
|
||||
|
||||
//获取数据库实例
|
||||
|
||||
@ -12,10 +12,12 @@ import com.all.pdfreader.pro.app.databinding.ActivityPdfViewBinding
|
||||
import com.all.pdfreader.pro.app.model.FileActionEvent
|
||||
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||
import com.all.pdfreader.pro.app.ui.dialog.BookmarksDialogFragment
|
||||
import com.all.pdfreader.pro.app.ui.dialog.ListMoreDialogFragment
|
||||
import com.all.pdfreader.pro.app.ui.dialog.PdfPasswordProtectionDialogFragment
|
||||
import com.all.pdfreader.pro.app.ui.dialog.ViewModelDialogFragment
|
||||
import com.all.pdfreader.pro.app.ui.view.CustomScrollHandle
|
||||
import com.all.pdfreader.pro.app.util.AppUtils
|
||||
import com.all.pdfreader.pro.app.util.FileUtils
|
||||
import com.all.pdfreader.pro.app.viewmodel.PdfViewModel
|
||||
import com.all.pdfreader.pro.app.viewmodel.observeEvent
|
||||
import com.github.barteksc.pdfviewer.listener.OnErrorListener
|
||||
@ -32,6 +34,7 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
||||
override val TAG: String = "PdfViewActivity"
|
||||
|
||||
companion object {
|
||||
const val FRAG_TAG = "PdfViewActivity"
|
||||
private const val EXTRA_PDF_HASH = "extra_pdf_hash"
|
||||
|
||||
// 创建启动Intent的便捷方法
|
||||
@ -47,6 +50,7 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
||||
private val viewModel by lazy { ViewModelProvider(this)[PdfViewModel::class.java] }
|
||||
private val repository = getRepository()
|
||||
private var isFullScreen = false
|
||||
private var lastLoadedFilePath: String? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@ -61,6 +65,7 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
||||
viewModel.getPDFDocument(filePath)
|
||||
//加载书签数据
|
||||
viewModel.getBookmarks(filePath)
|
||||
setupOnClick()
|
||||
}
|
||||
|
||||
private fun initObserve() {
|
||||
@ -68,9 +73,13 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
||||
viewModel.pdfDocument.observe(this) { document ->
|
||||
document?.let {
|
||||
pdfDocument = it
|
||||
// 只有文件路径变化才重新加载 PDF
|
||||
// if (lastLoadedFilePath != pdfDocument.filePath) {
|
||||
// lastLoadedFilePath = pdfDocument.filePath
|
||||
// loadPdf()
|
||||
// }
|
||||
loadPdf()
|
||||
setupOnClick()
|
||||
toggleEyeCareMode(appStore.isEyeCareMode)
|
||||
initView()
|
||||
} ?: run {
|
||||
showToast(getString(R.string.file_not))
|
||||
finish()
|
||||
@ -104,6 +113,37 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
||||
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 {
|
||||
// //修改名称需要重新load界面,因为路径都更改了
|
||||
// viewModel.getPDFDocument(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() {
|
||||
binding.title.text = FileUtils.removeFileExtension(pdfDocument.fileName)
|
||||
toggleEyeCareMode(appStore.isEyeCareMode)
|
||||
}
|
||||
|
||||
private fun setupOnClick() {
|
||||
@ -128,6 +168,9 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
||||
binding.shareBtn.setOnClickListener {
|
||||
AppUtils.shareFile(this, File(pdfDocument.filePath))
|
||||
}
|
||||
binding.moreBtn.setOnClickListener {
|
||||
// ListMoreDialogFragment(pdfDocument.filePath).show(supportFragmentManager, FRAG_TAG)
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadPdf() {
|
||||
|
||||
@ -154,6 +154,6 @@ class BookmarksDialogFragment(
|
||||
}
|
||||
|
||||
private fun showToast(message: String) {
|
||||
Toast.makeText(requireActivity(), message, Toast.LENGTH_SHORT).show()
|
||||
ToastUtils.show(requireActivity(), message)
|
||||
}
|
||||
}
|
||||
@ -53,6 +53,6 @@ class FileDetailsDialogFragment() : BottomSheetDialogFragment() {
|
||||
}
|
||||
|
||||
private fun showToast(message: String) {
|
||||
Toast.makeText(requireActivity(), message, Toast.LENGTH_SHORT).show()
|
||||
ToastUtils.show(requireActivity(), message)
|
||||
}
|
||||
}
|
||||
@ -13,6 +13,7 @@ import com.all.pdfreader.pro.app.databinding.DialogListMoreBinding
|
||||
import com.all.pdfreader.pro.app.model.PrintResult
|
||||
import com.all.pdfreader.pro.app.model.RenameType
|
||||
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||
import com.all.pdfreader.pro.app.ui.act.PdfViewActivity
|
||||
import com.all.pdfreader.pro.app.ui.act.SplitPdfActivity
|
||||
import com.all.pdfreader.pro.app.ui.fragment.FavoriteFrag
|
||||
import com.all.pdfreader.pro.app.ui.fragment.HomeFrag
|
||||
@ -79,14 +80,30 @@ class ListMoreDialogFragment(val filePath: String) : BottomSheetDialogFragment()
|
||||
when (tag) {
|
||||
HomeFrag.FRAG_TAG -> {
|
||||
binding.removeRecentBtn.visibility = View.GONE
|
||||
binding.setPasswordBtn.visibility = View.VISIBLE
|
||||
binding.deleteFileBtn.visibility = View.VISIBLE
|
||||
binding.shareBtn.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
RecentlyFrag.FRAG_TAG -> {
|
||||
binding.removeRecentBtn.visibility = View.VISIBLE
|
||||
binding.setPasswordBtn.visibility = View.VISIBLE
|
||||
binding.deleteFileBtn.visibility = View.VISIBLE
|
||||
binding.shareBtn.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
FavoriteFrag.FRAG_TAG -> {
|
||||
binding.removeRecentBtn.visibility = View.GONE
|
||||
binding.setPasswordBtn.visibility = View.VISIBLE
|
||||
binding.deleteFileBtn.visibility = View.VISIBLE
|
||||
binding.shareBtn.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
PdfViewActivity.FRAG_TAG -> {
|
||||
binding.removeRecentBtn.visibility = View.GONE
|
||||
binding.setPasswordBtn.visibility = View.GONE
|
||||
binding.deleteFileBtn.visibility = View.GONE
|
||||
binding.shareBtn.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
binding.tvFileName.text = pdfDocument.fileName
|
||||
@ -215,6 +232,6 @@ class ListMoreDialogFragment(val filePath: String) : BottomSheetDialogFragment()
|
||||
}
|
||||
|
||||
private fun showToast(message: String) {
|
||||
Toast.makeText(requireActivity(), message, Toast.LENGTH_SHORT).show()
|
||||
ToastUtils.show(requireActivity(), message)
|
||||
}
|
||||
}
|
||||
@ -2,26 +2,20 @@ package com.all.pdfreader.pro.app.ui.dialog
|
||||
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.core.graphics.drawable.toDrawable
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import com.all.pdfreader.pro.app.R
|
||||
import com.all.pdfreader.pro.app.databinding.DialogPdfRemovePasswordBinding
|
||||
import com.all.pdfreader.pro.app.databinding.DialogPdfSetPasswordBinding
|
||||
import com.all.pdfreader.pro.app.room.entity.PdfDocumentEntity
|
||||
import com.all.pdfreader.pro.app.util.AppUtils.showKeyboard
|
||||
import com.all.pdfreader.pro.app.util.FileUtils.isPdfPasswordCorrect
|
||||
import com.all.pdfreader.pro.app.util.PdfSecurityUtils
|
||||
import com.all.pdfreader.pro.app.util.ToastUtils
|
||||
import com.all.pdfreader.pro.app.viewmodel.PdfViewModel
|
||||
import java.io.File
|
||||
import kotlin.getValue
|
||||
|
||||
class PdfRemovePasswordDialog() : DialogFragment(
|
||||
|
||||
@ -79,6 +73,6 @@ class PdfRemovePasswordDialog() : DialogFragment(
|
||||
}
|
||||
|
||||
private fun showToast(message: String) {
|
||||
Toast.makeText(requireActivity(), message, Toast.LENGTH_SHORT).show()
|
||||
ToastUtils.show(requireActivity(), message)
|
||||
}
|
||||
}
|
||||
@ -129,6 +129,6 @@ class PdfSetPasswordDialog() : DialogFragment(
|
||||
}
|
||||
|
||||
private fun showToast(message: String) {
|
||||
Toast.makeText(requireActivity(), message, Toast.LENGTH_SHORT).show()
|
||||
ToastUtils.show(requireActivity(), message)
|
||||
}
|
||||
}
|
||||
@ -65,6 +65,6 @@ class PromptDialogFragment(
|
||||
}
|
||||
|
||||
private fun showToast(message: String) {
|
||||
Toast.makeText(requireActivity(), message, Toast.LENGTH_SHORT).show()
|
||||
ToastUtils.show(requireActivity(), message)
|
||||
}
|
||||
}
|
||||
@ -203,6 +203,6 @@ class RenameDialogFragment(
|
||||
}
|
||||
|
||||
private fun showToast(message: String) {
|
||||
Toast.makeText(requireActivity(), message, Toast.LENGTH_SHORT).show()
|
||||
ToastUtils.show(requireActivity(), message)
|
||||
}
|
||||
}
|
||||
@ -160,6 +160,6 @@ class ViewModelDialogFragment() : BottomSheetDialogFragment() {
|
||||
|
||||
|
||||
private fun showToast(message: String) {
|
||||
Toast.makeText(requireActivity(), message, Toast.LENGTH_SHORT).show()
|
||||
ToastUtils.show(requireActivity(), message)
|
||||
}
|
||||
}
|
||||
@ -429,7 +429,7 @@ object FileUtils {
|
||||
return try {
|
||||
if (file.renameTo(newFile)) {
|
||||
Log.d("ocean", "✅ File renamed successfully: ${file.name} -> $validatedName")
|
||||
RenameResult.success()
|
||||
RenameResult.success(newName)
|
||||
} else {
|
||||
Log.e("ocean", "❌ File rename failed: ${file.path}")
|
||||
RenameResult.failure(PRApp.getStringRes(R.string.error_file_rename_failed))
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
package com.all.pdfreader.pro.app.util
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import com.all.pdfreader.pro.app.R
|
||||
|
||||
object ToastUtils {
|
||||
private var toast: Toast? = null
|
||||
|
||||
fun show(context: Context, message: String) {
|
||||
val inflater = LayoutInflater.from(context.applicationContext)
|
||||
val view = inflater.inflate(R.layout.layout_custom_toast, null)
|
||||
view.findViewById<TextView>(R.id.toastText).text = message
|
||||
|
||||
toast?.cancel() // 避免多个 Toast 叠加
|
||||
toast = Toast(context.applicationContext).apply {
|
||||
duration = Toast.LENGTH_SHORT
|
||||
this.view = view
|
||||
show()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -67,11 +67,14 @@ class PdfViewModel : ViewModel() {
|
||||
val newFilePath = File(parentDir, finalName).absolutePath.toString()
|
||||
Log.d("ocean", "renamePdf->newFilePath: $newFilePath, finalName=$finalName")
|
||||
pdfRepository.updateFilePathAndFileName(filePath, newFilePath, finalName)
|
||||
}
|
||||
|
||||
val newResult = renameResult.copy(newFilePath = newFilePath)
|
||||
_fileActionEvent.postValue(FileActionEvent.Rename(newResult))
|
||||
} else {
|
||||
_fileActionEvent.postValue(FileActionEvent.Rename(renameResult))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteFile(filePath: String) {
|
||||
viewModelScope.launch {
|
||||
|
||||
5
app/src/main/res/drawable/bg_custom_toast.xml
Normal file
5
app/src/main/res/drawable/bg_custom_toast.xml
Normal file
@ -0,0 +1,5 @@
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="#CC333333"/>
|
||||
<corners android:radius="8dp"/>
|
||||
</shape>
|
||||
@ -38,6 +38,34 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title"
|
||||
style="@style/TextViewFont_PopMedium"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:ellipsize="end"
|
||||
android:gravity="center_vertical"
|
||||
android:maxLines="1"
|
||||
android:text="@string/app_name"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/moreBtn"
|
||||
android:layout_width="40dp"
|
||||
android:layout_height="40dp"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:gravity="center">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:src="@drawable/more" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
|
||||
16
app/src/main/res/layout/layout_custom_toast.xml
Normal file
16
app/src/main/res/layout/layout_custom_toast.xml
Normal file
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="horizontal"
|
||||
android:padding="12dp"
|
||||
android:background="@drawable/bg_custom_toast"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/toastText"
|
||||
android:textColor="@android:color/white"
|
||||
android:textSize="14sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
</LinearLayout>
|
||||
Loading…
Reference in New Issue
Block a user