Jelajahi Sumber

New: integrate new pdf sdk add and delete page bookmark function

liweihao 5 tahun lalu
induk
melakukan
a71800fea4

+ 73 - 0
reader/src/main/java/com/kdanmobile/reader/ReaderActivity.kt

@@ -8,8 +8,13 @@ import android.os.Build
 import android.support.v7.app.AppCompatActivity
 import android.os.Bundle
 import android.support.constraint.ConstraintSet
+import android.support.v4.content.ContextCompat
+import android.support.v7.app.AlertDialog
 import android.view.Menu
+import android.view.MenuItem
 import android.view.View
+import android.widget.EditText
+import android.widget.LinearLayout
 import com.kdanmobile.kmpdfkit.pdfcommon.KMPDFReaderView
 import com.kdanmobile.reader.Utils.applyConstraintSet
 import kotlinx.android.synthetic.main.activity_reader.*
@@ -33,6 +38,8 @@ abstract class ReaderActivity : AppCompatActivity() {
     private var isHideToolbar = false
     private lateinit var originConstraintSet: ConstraintSet
 
+    private var menuItemBookmark: MenuItem? = null
+
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_reader)
@@ -52,10 +59,13 @@ abstract class ReaderActivity : AppCompatActivity() {
             val uri = Uri.parse(filePath)
             viewModel.openPdfFile(this, uri, intent.type)
         }
+        viewModel.pageIndexLiveData.observe(this, Observer(this::onPageIndexChanged))
+        viewModel.isPageInBookmarksLiveData.observe(this, Observer(this::onIsPageInBookmarksChanged))
     }
 
     override fun onCreateOptionsMenu(menu: Menu?): Boolean {
         menuInflater.inflate(R.menu.reader_more, menu)
+        menuItemBookmark = menu?.findItem(R.id.item_reader_bookmark)
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
             menu?.findItem(R.id.item_reader_more_print)?.isVisible = false
         }
@@ -108,11 +118,33 @@ abstract class ReaderActivity : AppCompatActivity() {
                 }
                 isHideToolbar = !isHideToolbar
             }
+
+            override fun onMoveToChild(p0: Int) {
+                super.onMoveToChild(p0)
+
+                viewModel.setPageIndex(p0)
+            }
         }
         viewModel.setReaderView(readerView)
         container.addView(readerView)
     }
 
+    private fun onPageIndexChanged(pageIndex: Int?) {
+        pageIndex?.let {
+            viewModel.setBookmarkDisplay(pageIndex)
+        }
+    }
+
+    private fun onIsPageInBookmarksChanged(isPageInBookmarks: Boolean?) {
+        isPageInBookmarks?.let {
+            if (it) {
+                menuItemBookmark?.icon = ContextCompat.getDrawable(this, R.drawable.ic_bookmark)
+            } else {
+                menuItemBookmark?.icon = ContextCompat.getDrawable(this, R.drawable.ic_bookmark_border)
+            }
+        }
+    }
+
     private fun showAllToolbars() {
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
             // TODO : Implement show toolbars on Android api level < 19 devices
@@ -140,6 +172,7 @@ abstract class ReaderActivity : AppCompatActivity() {
     private fun setupToolbar() {
         toolbar_readerActivity.setOnMenuItemClickListener {
             when (it.itemId) {
+                R.id.item_reader_bookmark -> onClickBookmark()
                 R.id.item_reader_more_share -> onClickShare()
                 R.id.item_reader_more_text_reflow -> onClickTextReflow()
                 R.id.item_reader_more_file_info -> onClickFileInfo()
@@ -150,6 +183,46 @@ abstract class ReaderActivity : AppCompatActivity() {
         }
     }
 
+    private fun onClickBookmark() {
+        viewModel.isPageInBookmarksLiveData.value?.let {
+            if (!it) {
+                showAddBookmarkDialog()
+            } else {
+                showDeleteBookmarkDialog()
+            }
+        }
+    }
+
+    private fun showAddBookmarkDialog() {
+        val alertDialogBuilder = AlertDialog.Builder(this)
+        val editText = EditText(this)
+        val layoutParams =
+                LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
+
+        editText.layoutParams = layoutParams
+
+        alertDialogBuilder.setTitle(getString(R.string.reader_alert_dialog_title_add_bookmark))
+        alertDialogBuilder.setView(editText)
+        alertDialogBuilder.setPositiveButton(getString(R.string.reader_alert_dialog_button_confirm)) { dialog, which ->
+            viewModel.addBookmark(editText.text.toString())
+        }
+        alertDialogBuilder.setNegativeButton(getString(R.string.reader_alert_dialog_button_cancel)) { dialog, which ->
+        }
+        alertDialogBuilder.create().show()
+    }
+
+    private fun showDeleteBookmarkDialog() {
+        val alertDialogBuilder = AlertDialog.Builder(this)
+        alertDialogBuilder.setTitle(getString(R.string.reader_alert_dialog_title_delete_bookmark))
+        alertDialogBuilder.setMessage(getString(R.string.reader_alert_dialog_message_delete_bookmark))
+        alertDialogBuilder.setPositiveButton(getString(R.string.reader_alert_dialog_button_confirm)) { dialog, which ->
+            viewModel.deleteBookmark()
+        }
+        alertDialogBuilder.setNegativeButton(getString(R.string.reader_alert_dialog_button_cancel)) { dialog, which ->
+        }
+        alertDialogBuilder.create().show()
+    }
+
     private fun setupBottomToolbar() {
         val context = this
         /** Set buttons color **/

+ 42 - 0
reader/src/main/java/com/kdanmobile/reader/ReaderViewModel.kt

@@ -1,5 +1,6 @@
 package com.kdanmobile.reader
 
+import android.arch.lifecycle.LiveData
 import android.arch.lifecycle.MutableLiveData
 import android.arch.lifecycle.ViewModel
 import android.content.Context
@@ -37,6 +38,14 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
     val isOpenedFileLiveData = MutableLiveData<Boolean>().apply { value = false }
     val fileNameLiveData = MutableLiveData<String>()
 
+    val pageIndexLiveData: LiveData<Int>
+        get() = mPageIndexLiveData
+    val isPageInBookmarksLiveData: LiveData<Boolean>
+        get() = mIsPageInBookmarksLiveData
+
+    private val mPageIndexLiveData = MutableLiveData<Int>().apply { value = 0 }
+    private val mIsPageInBookmarksLiveData = MutableLiveData<Boolean>().apply { value = false }
+
     @JvmOverloads
     fun openPdfFile(context: Context, uri: Uri, type: String? = null): Boolean {
         if (!isVerified) {
@@ -144,6 +153,39 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
         updateViewDirection()
     }
 
+    fun setPageIndex(pageIndex: Int){
+        mPageIndexLiveData.value = pageIndex
+    }
+
+    fun addBookmark(title: String) {
+        pageIndexLiveData.value?.let {
+            kmpdfDocumentController?.addBookmark(title, it)
+            setBookmarkDisplay(it)
+        }
+    }
+
+    fun deleteBookmark() {
+        pageIndexLiveData.value?.let {
+            kmpdfDocumentController?.deleteBookmarks(it)
+            setBookmarkDisplay(it)
+        }
+    }
+
+    fun setBookmarkDisplay(pageIndex: Int) {
+        kmpdfDocumentController?.let {
+            var isPageInBookmarks = false
+            it.bookmarks?.let {
+                for (bookmark in it) {
+                    if (bookmark.pageNum == pageIndex) {
+                        isPageInBookmarks = true
+                        break
+                    }
+                }
+            }
+            mIsPageInBookmarksLiveData.value = isPageInBookmarks
+        }
+    }
+
     private fun verifyLicense(context: Context): Boolean {
         val errorCode = KMPDFFactory.verifyLicense(context, pdfSdkLicense, pdfSdkRsaMsg)
         return errorCode == 0

+ 6 - 0
reader/src/main/res/values-de/strings.xml

@@ -5,4 +5,10 @@
     <string name="reader_more_menu_share">"Teilen  "</string>
     <string name="reader_more_menu_text_reflow">Text Rückfluss</string>
     <string name="reader_more_menu_print">Drucken</string>
+    <string name="reader_more_menu_bookmark">Lesezeichen</string>
+    <string name="reader_alert_dialog_title_add_bookmark">Lesezeichen hinzufügen</string>
+    <string name="reader_alert_dialog_button_confirm">Bestätigen</string>
+    <string name="reader_alert_dialog_button_cancel">Abbrechen</string>
+    <string name="reader_alert_dialog_title_delete_bookmark">Lesezeichen löschen</string>
+    <string name="reader_alert_dialog_message_delete_bookmark">Sind Sie sicher, dass Sie %s löschen wollen?</string>
 </resources>

+ 6 - 0
reader/src/main/res/values-es/strings.xml

@@ -5,4 +5,10 @@
     <string name="reader_more_menu_share">Compartir</string>
     <string name="reader_more_menu_text_reflow">Herramienta de marcado</string>
     <string name="reader_more_menu_print">Borrar firma</string>
+    <string name="reader_more_menu_bookmark">Marcador</string>
+    <string name="reader_alert_dialog_title_add_bookmark">Añadir marcador</string>
+    <string name="reader_alert_dialog_button_confirm">Confirmar</string>
+    <string name="reader_alert_dialog_button_cancel">Cancelar</string>
+    <string name="reader_alert_dialog_title_delete_bookmark">Eliminar marcador</string>
+    <string name="reader_alert_dialog_message_delete_bookmark">¿Seguro que deseas eliminar %s ?</string>
 </resources>

+ 6 - 0
reader/src/main/res/values-fr/strings.xml

@@ -5,4 +5,10 @@
     <string name="reader_more_menu_share">Partager</string>
     <string name="reader_more_menu_text_reflow">Text Reflow</string>
     <string name="reader_more_menu_print">Imprimer</string>
+    <string name="reader_more_menu_bookmark">Marquer la page</string>
+    <string name="reader_alert_dialog_title_add_bookmark">Ajouter un marque page</string>
+    <string name="reader_alert_dialog_button_confirm">Confirmer</string>
+    <string name="reader_alert_dialog_button_cancel">Annuler</string>
+    <string name="reader_alert_dialog_title_delete_bookmark">Supprimer un marque page</string>
+    <string name="reader_alert_dialog_message_delete_bookmark">Etes-vous sûre de vouloir supprimer %s ?</string>
 </resources>

+ 6 - 0
reader/src/main/res/values-it/strings.xml

@@ -5,4 +5,10 @@
     <string name="reader_more_menu_share">Condividere</string>
     <string name="reader_more_menu_text_reflow">Testo riflusso</string>
     <string name="reader_more_menu_print">Stampa</string>
+    <string name="reader_more_menu_bookmark">Segnalibro</string>
+    <string name="reader_alert_dialog_title_add_bookmark">Aggiungi segnalibro</string>
+    <string name="reader_alert_dialog_button_confirm">Conferma</string>
+    <string name="reader_alert_dialog_button_cancel">Cancella</string>
+    <string name="reader_alert_dialog_title_delete_bookmark">Elimina segnalibro</string>
+    <string name="reader_alert_dialog_message_delete_bookmark">Sei sicuro di voler cancellare %s ?</string>
 </resources>

+ 6 - 0
reader/src/main/res/values-ja/strings.xml

@@ -5,4 +5,10 @@
     <string name="reader_more_menu_print">印刷</string>
     <string name="reader_more_menu_file_info">ファイル内容</string>
     <string name="reader_more_menu_user_guide">ユーザーガイド</string>
+    <string name="reader_more_menu_bookmark">ブックマーク</string>
+    <string name="reader_alert_dialog_title_add_bookmark">ブックマークを作成する</string>
+    <string name="reader_alert_dialog_button_confirm">確定</string>
+    <string name="reader_alert_dialog_button_cancel">キャンセル</string>
+    <string name="reader_alert_dialog_title_delete_bookmark">ブックマークを削除する</string>
+    <string name="reader_alert_dialog_message_delete_bookmark">「%s」のブックマークを削除しますか?</string>
 </resources>

+ 6 - 0
reader/src/main/res/values-ko/strings.xml

@@ -5,4 +5,10 @@
     <string name="reader_more_menu_share">공유</string>
     <string name="reader_more_menu_text_reflow">텍스트 리플로우</string>
     <string name="reader_more_menu_print">인쇄</string>
+    <string name="reader_more_menu_bookmark">북마크</string>
+    <string name="reader_alert_dialog_title_add_bookmark">북마크 추가</string>
+    <string name="reader_alert_dialog_button_confirm">확인</string>
+    <string name="reader_alert_dialog_button_cancel">취소</string>
+    <string name="reader_alert_dialog_title_delete_bookmark">북마크 삭제</string>
+    <string name="reader_alert_dialog_message_delete_bookmark">%s 를 삭제하시겠습니까?</string>
 </resources>

+ 6 - 0
reader/src/main/res/values-pt/strings.xml

@@ -5,4 +5,10 @@
     <string name="reader_more_menu_share">Compartilhar</string>
     <string name="reader_more_menu_text_reflow">Refluxo de texto</string>
     <string name="reader_more_menu_print">Imprimir</string>
+    <string name="reader_more_menu_bookmark">Marcador</string>
+    <string name="reader_alert_dialog_title_add_bookmark">Adicionar marcador</string>
+    <string name="reader_alert_dialog_button_confirm">Confirmar</string>
+    <string name="reader_alert_dialog_button_cancel">Cancelar</string>
+    <string name="reader_alert_dialog_title_delete_bookmark">Apagar marcador</string>
+    <string name="reader_alert_dialog_message_delete_bookmark">Você tem certeza que quer apagar %s ?</string>
 </resources>

+ 6 - 0
reader/src/main/res/values-ru/strings.xml

@@ -5,4 +5,10 @@
     <string name="reader_more_menu_share">Поделиться</string>
     <string name="reader_more_menu_text_reflow">Перенаправить Текст</string>
     <string name="reader_more_menu_print">Печать</string>
+    <string name="reader_more_menu_bookmark">Закладка</string>
+    <string name="reader_alert_dialog_title_add_bookmark">Добавить закладку</string>
+    <string name="reader_alert_dialog_button_confirm">Подтвердить</string>
+    <string name="reader_alert_dialog_button_cancel">Отменить</string>
+    <string name="reader_alert_dialog_title_delete_bookmark">Удалить закладку</string>
+    <string name="reader_alert_dialog_message_delete_bookmark">Вы уверены что хотите удалить %s ?</string>
 </resources>

+ 5 - 0
reader/src/main/res/values-zh-rTW/strings.xml

@@ -6,4 +6,9 @@
     <string name="reader_more_menu_file_info">文件資訊</string>
     <string name="reader_more_menu_user_guide">使用指南</string>
     <string name="reader_more_menu_bookmark">書籤</string>
+    <string name="reader_alert_dialog_title_add_bookmark">新增書籤</string>
+    <string name="reader_alert_dialog_button_confirm">確定</string>
+    <string name="reader_alert_dialog_button_cancel">取消</string>
+    <string name="reader_alert_dialog_title_delete_bookmark">刪除書籤</string>
+    <string name="reader_alert_dialog_message_delete_bookmark">確定刪除書籤 %s ?</string>
 </resources>

+ 5 - 0
reader/src/main/res/values-zh/strings.xml

@@ -6,4 +6,9 @@
     <string name="reader_more_menu_file_info">文件信息</string>
     <string name="reader_more_menu_user_guide">使用导航</string>
     <string name="reader_more_menu_bookmark">书签</string>
+    <string name="reader_alert_dialog_title_add_bookmark">添加书签</string>
+    <string name="reader_alert_dialog_button_confirm">确定</string>
+    <string name="reader_alert_dialog_button_cancel">取消</string>
+    <string name="reader_alert_dialog_title_delete_bookmark">删除书签</string>
+    <string name="reader_alert_dialog_message_delete_bookmark">确定删除书签 %s ?</string>
 </resources>

+ 6 - 0
reader/src/main/res/values/strings.xml

@@ -7,4 +7,10 @@
     <string name="reader_more_menu_print">Print</string>
     <string name="reader_more_menu_file_info">File Info</string>
     <string name="reader_more_menu_user_guide">User Guide</string>
+
+    <string name="reader_alert_dialog_title_add_bookmark">Add bookmark</string>
+    <string name="reader_alert_dialog_title_delete_bookmark">Delete bookmark</string>
+    <string name="reader_alert_dialog_message_delete_bookmark">Are you sure you want to delete %s ?</string>
+    <string name="reader_alert_dialog_button_confirm">Confirm</string>
+    <string name="reader_alert_dialog_button_cancel">Cancel</string>
 </resources>