Explorar el Código

Feature: PDF outline in left toolbar

cooperku_kdanmobile hace 5 años
padre
commit
5569bc4e87

+ 3 - 2
reader/src/main/java/com/kdanmobile/reader/ReaderActivity.kt

@@ -15,6 +15,7 @@ import android.view.MotionEvent
 import android.view.View
 import com.kdanmobile.kmpdfkit.pdfcommon.KMPDFReaderView
 import com.kdanmobile.reader.Utils.applyConstraintSet
+import com.kdanmobile.reader.screen.view.OutlineView
 import com.kdanmobile.reader.screen.view.ThumbnailView
 import com.kdanmobile.reader.utils.AnimationUtil
 import com.kdanmobile.reader.utils.DensityUtil
@@ -286,8 +287,8 @@ open class ReaderActivity : AppCompatActivity() {
         if (isLeftToolbarOpen) {
             if (leftToolbarType != type) {
                 linearLayout_readerActivity_leftToolbarContainer.removeAllViews()
-                val child = layoutInflater.inflate(R.layout.view_thumbnail, null)
-                linearLayout_readerActivity_leftToolbarContainer.addView(child)
+                val outlineView = OutlineView(this, viewModel)
+                linearLayout_readerActivity_leftToolbarContainer.addView(outlineView)
             }
             iv_readerActivity_outline.setImageResource(R.drawable.ic_pagelist_outline_h)
         } else {

+ 9 - 1
reader/src/main/java/com/kdanmobile/reader/ReaderViewModel.kt

@@ -10,10 +10,12 @@ import com.kdanmobile.kmpdfkit.globaldata.Config
 import com.kdanmobile.kmpdfkit.manager.KMPDFFactory
 import com.kdanmobile.kmpdfkit.manager.controller.KMPDFDocumentController
 import com.kdanmobile.kmpdfkit.pdfcommon.KMPDFReaderView
+import com.kdanmobile.kmpdfkit.pdfcommon.OutlineItem
+import com.kdanmobile.reader.screen.handler.OutlineHandler
 import com.kdanmobile.reader.screen.handler.PdfInfoHandler
 import com.kdanmobile.reader.screen.handler.ThumbnailHandler
 
-class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMsg: String) : ViewModel(), PdfInfoHandler, ThumbnailHandler {
+class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMsg: String) : ViewModel(), PdfInfoHandler, ThumbnailHandler, OutlineHandler {
     enum class ViewDirection(val mode: Config.PDFViewMode) {
         VerticalSinglePageContinues(Config.PDFViewMode.VERTICAL_SINGLE_PAGE_CONTINUES),
         VerticalSinglePage(Config.PDFViewMode.VERTICAL_SINGLE_PAGE),
@@ -193,4 +195,10 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
     }
 
     //  ====================================================================
+    //  implement OutlineHandler
+    //  ====================================================================
+
+    override fun getOutline(): Array<OutlineItem>? {
+        return kmpdfDocumentController?.outline
+    }
 }

+ 60 - 0
reader/src/main/java/com/kdanmobile/reader/screen/adapter/OutlineAdapter.kt

@@ -0,0 +1,60 @@
+package com.kdanmobile.reader.screen.adapter
+
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import android.widget.BaseAdapter
+import android.widget.TextView
+import com.kdanmobile.kmpdfkit.pdfcommon.OutlineItem
+import com.kdanmobile.reader.R
+
+class OutlineAdapter(private val outlineListItemList: List<OutlineItem>?): BaseAdapter() {
+
+    override fun getCount(): Int {
+        return outlineListItemList?.size ?: 0
+    }
+
+    override fun getItem(position: Int): OutlineItem? {
+        val isNull = null == outlineListItemList || outlineListItemList.size < position
+        return if (isNull) null else outlineListItemList?.get(position)
+    }
+
+    override fun getItemId(position: Int): Long {
+        return position.toLong()
+    }
+
+    override fun getView(position: Int, view: View?, parent: ViewGroup): View {
+        var view = view
+        val holderView: HolderView
+        if (view == null) {
+            view = LayoutInflater.from(parent.context).inflate(R.layout.outline_entry, null)
+            holderView = HolderView(view!!)
+            view!!.tag = holderView
+        } else {
+            holderView = view.tag as HolderView
+        }
+
+        val item = getItem(position)
+        /****** 大纲层次缩进  */
+        var space = ""
+        val null_str = parent.context.getString(R.string.null_str)
+        for (i in 1 .. item!!.level) {
+            space += null_str + null_str
+        }
+
+        holderView.tv_name.text = space + item.title
+        holderView.tv_name.textSize = (15 - item.level * 1.2f)
+        holderView.tv_page.text = (item.page + 1).toString()
+        return view
+    }
+
+    private class HolderView(view: View) {
+        var tv_name: TextView
+        var tv_page: TextView
+
+        init {
+            tv_name = view.findViewById<View>(R.id.tv_outlineEntry_title) as TextView
+            tv_page = view.findViewById<View>(R.id.tv_outlineEntry_page) as TextView
+        }
+    }
+}

+ 8 - 0
reader/src/main/java/com/kdanmobile/reader/screen/handler/OutlineHandler.kt

@@ -0,0 +1,8 @@
+package com.kdanmobile.reader.screen.handler
+
+import com.kdanmobile.kmpdfkit.pdfcommon.OutlineItem
+
+interface OutlineHandler {
+
+    fun getOutline(): Array<OutlineItem>?
+}

+ 57 - 0
reader/src/main/java/com/kdanmobile/reader/screen/view/OutlineView.kt

@@ -0,0 +1,57 @@
+package com.kdanmobile.reader.screen.view
+
+import android.content.Context
+import android.util.AttributeSet
+import android.view.LayoutInflater
+import android.widget.RelativeLayout
+import com.kdanmobile.kmpdfkit.pdfcommon.OutlineItem
+import com.kdanmobile.reader.R
+import com.kdanmobile.reader.ReaderViewModel
+import com.kdanmobile.reader.screen.adapter.OutlineAdapter
+import com.kdanmobile.reader.screen.handler.OutlineHandler
+import com.kdanmobile.reader.screen.handler.PdfInfoHandler
+import io.reactivex.Observable
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.schedulers.Schedulers
+import kotlinx.android.synthetic.main.view_outline.view.*
+
+class OutlineView: RelativeLayout {
+    private lateinit var adapter: OutlineAdapter
+
+    constructor(context: Context, viewModel: ReaderViewModel) : super(context) {
+        initView(viewModel, viewModel)
+    }
+
+    constructor(context: Context, attributeSet: AttributeSet, viewModel: ReaderViewModel) : super(context, attributeSet) {
+        initView(viewModel, viewModel)
+    }
+
+    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int, viewModel: ReaderViewModel) : super(context, attributeSet, defStyleAttr) {
+        initView(viewModel, viewModel)
+    }
+
+    private fun initView(pdfInfoHandler: PdfInfoHandler, outlineHandler: OutlineHandler) {
+        LayoutInflater.from(context).inflate(R.layout.view_outline, this)
+
+        lv_viewPdfReaderOutline_.setOnItemClickListener { parent, view, position, id ->
+            val item = adapter.getItem(position) as OutlineItem
+            pdfInfoHandler.setCurrentPage(item.page)
+        }
+
+        val disposable = Observable.create<OutlineAdapter> {
+            val outlineItems = outlineHandler.getOutline()!!
+            val copyList = outlineItems.toList()
+            it.onNext(OutlineAdapter(copyList))
+            it.onComplete()
+        }
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe({
+                    adapter = it
+                    lv_viewPdfReaderOutline_.adapter = adapter
+                }, {
+                    it.printStackTrace()
+                })
+
+    }
+}