|
@@ -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 **/
|