Browse Source

Copy and basic annotation(cannot set attributes) ok

Wayne 6 years ago
parent
commit
cbc4226c5f

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

@@ -9,7 +9,9 @@ import android.support.v7.app.AppCompatActivity
 import android.os.Bundle
 import android.os.Bundle
 import android.support.constraint.ConstraintSet
 import android.support.constraint.ConstraintSet
 import android.view.Menu
 import android.view.Menu
+import android.view.MotionEvent
 import android.view.View
 import android.view.View
+import android.widget.Toast
 import com.kdanmobile.kmpdfkit.pdfcommon.KMPDFReaderView
 import com.kdanmobile.kmpdfkit.pdfcommon.KMPDFReaderView
 import com.kdanmobile.reader.Utils.applyConstraintSet
 import com.kdanmobile.reader.Utils.applyConstraintSet
 import kotlinx.android.synthetic.main.activity_reader.*
 import kotlinx.android.synthetic.main.activity_reader.*
@@ -39,12 +41,15 @@ open class ReaderActivity : AppCompatActivity() {
         viewModel = ViewModelProviders.of(this, factory).get(ReaderViewModel::class.java)
         viewModel = ViewModelProviders.of(this, factory).get(ReaderViewModel::class.java)
         viewModel.isOpenedFileLiveData.observe(this, Observer(this::onIsOpenedFileUpdate))
         viewModel.isOpenedFileLiveData.observe(this, Observer(this::onIsOpenedFileUpdate))
         viewModel.fileNameLiveData.observe(this, Observer { tv_readerActivity_title.text = it })
         viewModel.fileNameLiveData.observe(this, Observer { tv_readerActivity_title.text = it })
+        viewModel.annotationModeLiveData.observe(this, Observer(this::onAnnotationModeUpdate))
+        viewModel.isCopyModeLiveData.observe(this, Observer (this::onIsCopyModeUpdate))
         viewModel.isOpenedFileLiveData.value?.also { isOpened ->
         viewModel.isOpenedFileLiveData.value?.also { isOpened ->
             if (isOpened) return@also
             if (isOpened) return@also
             val filePath = intent.getStringExtra(KEY_FILE_ABSOLUTE)
             val filePath = intent.getStringExtra(KEY_FILE_ABSOLUTE)
             val uri = Uri.parse(filePath)
             val uri = Uri.parse(filePath)
             viewModel.openPdfFile(this, uri, intent.type)
             viewModel.openPdfFile(this, uri, intent.type)
         }
         }
+        setupRightToolbar()
     }
     }
 
 
     override fun onCreateOptionsMenu(menu: Menu?): Boolean {
     override fun onCreateOptionsMenu(menu: Menu?): Boolean {
@@ -86,6 +91,43 @@ open class ReaderActivity : AppCompatActivity() {
         }
         }
     }
     }
 
 
+    private fun onIsCopyModeUpdate(isCopyMode: Boolean?) {
+        if (isCopyMode == null) return
+        when (isCopyMode) {
+            true -> {
+                val color = resources.getColor(R.color.reader_right_toolbar_selected_bg)
+                iv_readerActivity_copy.setBackgroundColor(color)
+                hideTopLeftBottomToolbars()
+            }
+            false -> {
+                iv_readerActivity_copy.setBackgroundDrawable(null)
+                showAllToolbars()
+            }
+        }
+    }
+
+    private fun onAnnotationModeUpdate(annotationMode: ReaderViewModel.AnnotationMode?) {
+        if (annotationMode == null) return
+        val map = HashMap<ReaderViewModel.AnnotationMode, View>().apply {
+            put(ReaderViewModel.AnnotationMode.Highlight, iv_readerActivity_highLight)
+            put(ReaderViewModel.AnnotationMode.Strike, iv_readerActivity_strike)
+            put(ReaderViewModel.AnnotationMode.Underline, iv_readerActivity_underline)
+            put(ReaderViewModel.AnnotationMode.Ink, iv_readerActivity_ink)
+        }
+        map.forEach {
+            if (it.key == annotationMode) {
+                val color = resources.getColor(R.color.reader_right_toolbar_selected_bg)
+                it.value.setBackgroundColor(color)
+            } else {
+                it.value.setBackgroundDrawable(null)
+            }
+        }
+        when (annotationMode == ReaderViewModel.AnnotationMode.None) {
+            true -> showAllToolbars()
+            false -> hideTopLeftBottomToolbars()
+        }
+    }
+
     private fun onIsOpenedFileUpdate(isOpened: Boolean?) {
     private fun onIsOpenedFileUpdate(isOpened: Boolean?) {
         if (isOpened == null) return
         if (isOpened == null) return
         val container = viewGroup_readerActivity_container
         val container = viewGroup_readerActivity_container
@@ -93,6 +135,18 @@ open class ReaderActivity : AppCompatActivity() {
         if (!isOpened) return
         if (!isOpened) return
         val context = this
         val context = this
         val readerView = object : KMPDFReaderView(context) {
         val readerView = object : KMPDFReaderView(context) {
+            override fun onTouchEvent(motionEvent: MotionEvent): Boolean {
+                if (motionEvent.action == MotionEvent.ACTION_UP) {
+                    if (viewModel.isCopyModeLiveData.value == true) {
+                        if (viewModel.copySelection()) {
+                            val context = this@ReaderActivity
+                            Toast.makeText(context, R.string.reader_copy_text_success, Toast.LENGTH_SHORT).show()
+                        }
+                    }
+                }
+                return super.onTouchEvent(motionEvent)
+            }
+
             override fun onTapMainDocArea() {
             override fun onTapMainDocArea() {
                 super.onTapMainDocArea()
                 super.onTapMainDocArea()
                 when (isHideToolbar) {
                 when (isHideToolbar) {
@@ -141,4 +195,12 @@ open class ReaderActivity : AppCompatActivity() {
         Utils.setTintDrawableList(context, ib_readerActivity_bottomToolbarViewAll, R.drawable.ic_view_all, normalColor, pressColor)
         Utils.setTintDrawableList(context, ib_readerActivity_bottomToolbarViewAll, R.drawable.ic_view_all, normalColor, pressColor)
         Utils.setTintDrawableList(context, ib_readerActivity_bottomToolbarKdanCloud, R.drawable.ic_kdan_cloud, normalColor, pressColor)
         Utils.setTintDrawableList(context, ib_readerActivity_bottomToolbarKdanCloud, R.drawable.ic_kdan_cloud, normalColor, pressColor)
     }
     }
+
+    private fun setupRightToolbar() {
+        iv_readerActivity_copy.setOnClickListener { viewModel.onClickCopyBtn() }
+        iv_readerActivity_highLight.setOnClickListener { viewModel.onClickHighlightBtn() }
+        iv_readerActivity_strike.setOnClickListener { viewModel.onClickStrikeBtn() }
+        iv_readerActivity_underline.setOnClickListener { viewModel.onClickUnderlineBtn() }
+        iv_readerActivity_ink.setOnClickListener { viewModel.onClickInkBtn() }
+    }
 }
 }

+ 69 - 11
reader/src/main/java/com/kdanmobile/reader/ReaderViewModel.kt

@@ -17,6 +17,14 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
         HorizontalSinglePage(Config.PDFViewMode.HORIZONTAL_SINGLE_PAGE)
         HorizontalSinglePage(Config.PDFViewMode.HORIZONTAL_SINGLE_PAGE)
     }
     }
 
 
+    enum class AnnotationMode {
+        None,
+        Highlight,
+        Strike,
+        Underline,
+        Ink,
+    }
+
     var viewDirection = ViewDirection.VerticalSinglePageContinues
     var viewDirection = ViewDirection.VerticalSinglePageContinues
         set(value) {
         set(value) {
             field = value
             field = value
@@ -27,8 +35,6 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
             field = value
             field = value
             updateCrop()
             updateCrop()
         }
         }
-    var isCopyMode = false
-        private set
 
 
     private var isVerified = false
     private var isVerified = false
 
 
@@ -36,6 +42,8 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
     private var kmpdfDocumentController: KMPDFDocumentController? = null
     private var kmpdfDocumentController: KMPDFDocumentController? = null
     val isOpenedFileLiveData = MutableLiveData<Boolean>().apply { value = false }
     val isOpenedFileLiveData = MutableLiveData<Boolean>().apply { value = false }
     val fileNameLiveData = MutableLiveData<String>()
     val fileNameLiveData = MutableLiveData<String>()
+    val annotationModeLiveData = MutableLiveData<AnnotationMode>().apply { value = AnnotationMode.None }
+    var isCopyModeLiveData = MutableLiveData<Boolean>().apply { value = false }
 
 
     @JvmOverloads
     @JvmOverloads
     fun openPdfFile(context: Context, uri: Uri, type: String? = null): Boolean {
     fun openPdfFile(context: Context, uri: Uri, type: String? = null): Boolean {
@@ -73,14 +81,59 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
         return kmpdfDocumentController?.copySelection() ?: false
         return kmpdfDocumentController?.copySelection() ?: false
     }
     }
 
 
-    fun startCopyTextMode() {
+    fun onClickCopyBtn() {
+        if (annotationModeLiveData.value != AnnotationMode.None) stopAnnotationMode()
+        if (isCopyModeLiveData.value == true) {
+            stopCopyTextMode()
+        } else {
+            startCopyTextMode()
+        }
+    }
+
+    fun onClickHighlightBtn() {
+        if (isCopyModeLiveData.value == true) stopCopyTextMode()
+        if (annotationModeLiveData.value == AnnotationMode.Highlight) {
+            stopAnnotationMode()
+        } else {
+            startHighLightEditMode()
+        }
+    }
+
+    fun onClickStrikeBtn() {
+        if (isCopyModeLiveData.value == true) stopCopyTextMode()
+        if (annotationModeLiveData.value == AnnotationMode.Strike) {
+            stopAnnotationMode()
+        } else {
+            startStrikeOutEditMode()
+        }
+    }
+
+    fun onClickUnderlineBtn() {
+        if (isCopyModeLiveData.value == true) stopCopyTextMode()
+        if (annotationModeLiveData.value == AnnotationMode.Underline) {
+            stopAnnotationMode()
+        } else {
+            startUnderLineEditMode()
+        }
+    }
+
+    fun onClickInkBtn() {
+        if (isCopyModeLiveData.value == true) stopCopyTextMode()
+        if (annotationModeLiveData.value == AnnotationMode.Ink) {
+            stopAnnotationMode()
+        } else {
+            startInkEditMode()
+        }
+    }
+
+    private fun startCopyTextMode() {
         kmpdfDocumentController?.startCopyText()
         kmpdfDocumentController?.startCopyText()
-        isCopyMode = true
+        isCopyModeLiveData.postValue(true)
     }
     }
 
 
-    fun stopCopyTextMode() {
+    private fun stopCopyTextMode() {
         kmpdfDocumentController?.stopCopyText()
         kmpdfDocumentController?.stopCopyText()
-        isCopyMode = false
+        isCopyModeLiveData.postValue(false)
     }
     }
 
 
     fun setHighLightAttributes(color: Int, alpha: Int) {
     fun setHighLightAttributes(color: Int, alpha: Int) {
@@ -91,8 +144,9 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
         }
         }
     }
     }
 
 
-    fun startHighLightEditMode() {
+    private fun startHighLightEditMode() {
         kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.HIGH_LIGHT)
         kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.HIGH_LIGHT)
+        annotationModeLiveData.postValue(AnnotationMode.Highlight)
     }
     }
 
 
     fun setStrikeOutAttributes(color: Int, alpha: Int) {
     fun setStrikeOutAttributes(color: Int, alpha: Int) {
@@ -103,8 +157,9 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
         }
         }
     }
     }
 
 
-    fun startStrikeOutEditMode() {
+    private fun startStrikeOutEditMode() {
         kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.STRIKE_OUT)
         kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.STRIKE_OUT)
+        annotationModeLiveData.postValue(AnnotationMode.Strike)
     }
     }
 
 
     fun setUnderLineAttributes(color: Int, alpha: Int) {
     fun setUnderLineAttributes(color: Int, alpha: Int) {
@@ -115,8 +170,9 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
         }
         }
     }
     }
 
 
-    fun startUnderLineEditMode() {
+    private fun startUnderLineEditMode() {
         kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.UNDER_LINE)
         kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.UNDER_LINE)
+        annotationModeLiveData.postValue(AnnotationMode.Underline)
     }
     }
 
 
     fun setInkAttributes(color: Int, alpha: Int, width: Float) {
     fun setInkAttributes(color: Int, alpha: Int, width: Float) {
@@ -130,12 +186,14 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
         }
         }
     }
     }
 
 
-    fun startInkEditMode() {
+    private fun startInkEditMode() {
         kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.INK)
         kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.INK)
+        annotationModeLiveData.postValue(AnnotationMode.Ink)
     }
     }
 
 
-    fun stopEditMode() {
+    private fun stopAnnotationMode() {
         kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.NULL)
         kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.NULL)
+        annotationModeLiveData.postValue(AnnotationMode.None)
     }
     }
 
 
     fun setReaderView(readerView: KMPDFReaderView) {
     fun setReaderView(readerView: KMPDFReaderView) {

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

@@ -5,4 +5,5 @@
     <string name="reader_more_menu_print">列印</string>
     <string name="reader_more_menu_print">列印</string>
     <string name="reader_more_menu_file_info">文件資訊</string>
     <string name="reader_more_menu_file_info">文件資訊</string>
     <string name="reader_more_menu_user_guide">使用指南</string>
     <string name="reader_more_menu_user_guide">使用指南</string>
+    <string name="reader_copy_text_success">已複製到剪貼簿中!</string>
 </resources>
 </resources>

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

@@ -5,4 +5,5 @@
     <string name="reader_more_menu_print">打印</string>
     <string name="reader_more_menu_print">打印</string>
     <string name="reader_more_menu_file_info">文件信息</string>
     <string name="reader_more_menu_file_info">文件信息</string>
     <string name="reader_more_menu_user_guide">使用导航</string>
     <string name="reader_more_menu_user_guide">使用导航</string>
+    <string name="reader_copy_text_success">已复制到剪切板!</string>
 </resources>
 </resources>

+ 1 - 0
reader/src/main/res/values/colors.xml

@@ -9,6 +9,7 @@
     <color name="reader_toolbar_title_color">#E2000000</color>
     <color name="reader_toolbar_title_color">#E2000000</color>
     <color name="reader_bottom_toolbar_bottom_color_normal">#FFFFFF</color>
     <color name="reader_bottom_toolbar_bottom_color_normal">#FFFFFF</color>
     <color name="reader_bottom_toolbar_bottom_color_press">@color/picton_blue</color>
     <color name="reader_bottom_toolbar_bottom_color_press">@color/picton_blue</color>
+    <color name="reader_right_toolbar_selected_bg">#6633B5E5</color>
 
 
     <color name="highlight_default_color">#FFFF00</color>
     <color name="highlight_default_color">#FFFF00</color>
     <color name="strike_default_color">#FF0000</color>
     <color name="strike_default_color">#FF0000</color>

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

@@ -6,4 +6,5 @@
     <string name="reader_more_menu_print">Print</string>
     <string name="reader_more_menu_print">Print</string>
     <string name="reader_more_menu_file_info">File Info</string>
     <string name="reader_more_menu_file_info">File Info</string>
     <string name="reader_more_menu_user_guide">User Guide</string>
     <string name="reader_more_menu_user_guide">User Guide</string>
+    <string name="reader_copy_text_success">It has been copied to the clipboard!</string>
 </resources>
 </resources>