瀏覽代碼

Add packages & PdfInfoHandler

cooperku_kdanmobile 5 年之前
父節點
當前提交
53becb6f7a

+ 12 - 3
reader/src/main/java/com/kdanmobile/reader/ReaderViewModel.kt

@@ -10,9 +10,10 @@ 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.reader.screen.view.ThumbnailHandler
+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(), ThumbnailHandler {
+class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMsg: String) : ViewModel(), PdfInfoHandler, ThumbnailHandler {
     enum class ViewDirection(val mode: Config.PDFViewMode) {
         VerticalSinglePageContinues(Config.PDFViewMode.VERTICAL_SINGLE_PAGE_CONTINUES),
         VerticalSinglePage(Config.PDFViewMode.VERTICAL_SINGLE_PAGE),
@@ -164,9 +165,13 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
     }
 
     //  ====================================================================
-    //  implement ThumbnailHandler
+    //  implement PdfInfoHandler
     //  ====================================================================
 
+    override fun getOpenPdfFilename(): String? {
+        return kmpdfFactory?.fileName
+    }
+
     override fun getPdfPageCount(): Int? {
         return kmpdfDocumentController?.getDocumentPageCount(false)
     }
@@ -179,6 +184,10 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
         return kmpdfDocumentController?.gotoPage(page)
     }
 
+    //  ====================================================================
+    //  implement ThumbnailHandler
+    //  ====================================================================
+
     override fun getPdfBitmap(position: Int, picWidth: Int, mode: Int, isDrawAnnot: Boolean): Bitmap? {
         return kmpdfDocumentController?.covertPDFToBitmap(position, picWidth, mode, isDrawAnnot)
     }

+ 11 - 11
reader/src/main/java/com/kdanmobile/reader/screen/view/ThumbnailAdapter.kt

@@ -1,4 +1,4 @@
-package com.kdanmobile.reader.screen.view
+package com.kdanmobile.reader.screen.adapter
 
 import android.graphics.Bitmap
 import android.support.v4.util.LruCache
@@ -10,15 +10,15 @@ import android.widget.FrameLayout
 import android.widget.ImageView
 import android.widget.TextView
 import com.kdanmobile.reader.R
-import com.kdanmobile.reader.ReaderViewModel
-import com.kdanmobile.reader.screen.view.ThumbnailAdapter.ThumbnailViewHolder
-import com.kdanmobile.reader.utils.DensityUtil
+import com.kdanmobile.reader.screen.handler.ThumbnailHandler
+import com.kdanmobile.reader.screen.adapter.ThumbnailAdapter.ThumbnailViewHolder
+import com.kdanmobile.reader.screen.handler.PdfInfoHandler
 import io.reactivex.Observable
 import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.disposables.Disposable
 import io.reactivex.schedulers.Schedulers
 
-class ThumbnailAdapter(private val handler: ThumbnailHandler, private val filename: String): RecyclerView.Adapter<ThumbnailViewHolder>() {
+class ThumbnailAdapter(private val pdfInfoHandler: PdfInfoHandler, private val thumbnailHandler: ThumbnailHandler, private val filename: String = pdfInfoHandler.getOpenPdfFilename()!!): RecyclerView.Adapter<ThumbnailViewHolder>() {
 
     companion object {
         lateinit var mMemoryCache: LruCache<String, Bitmap>
@@ -27,7 +27,7 @@ class ThumbnailAdapter(private val handler: ThumbnailHandler, private val filena
     private var currentPage = 0
 
     init {
-        this.currentPage = handler.getCurrentPage() ?: 0
+        this.currentPage = pdfInfoHandler.getCurrentPage() ?: 0
 
         mMemoryCache = object: LruCache<String, Bitmap>(maxMemory.toInt()) {
             override fun sizeOf(key: String, value: Bitmap): Int {
@@ -49,13 +49,13 @@ class ThumbnailAdapter(private val handler: ThumbnailHandler, private val filena
     }
 
     override fun getItemCount(): Int {
-        return handler.getPdfPageCount() ?: 0
+        return pdfInfoHandler.getPdfPageCount() ?: 0
     }
 
     override fun onBindViewHolder(holder: ThumbnailViewHolder, position: Int) {
         holder.page.text = (holder.adapterPosition + 1).toString()
         holder.onShowBorder(currentPage == holder.adapterPosition)
-        holder.loadThumbSync(filename, handler)
+        holder.loadThumbSync()
     }
 
     override fun onBindViewHolder(holder: ThumbnailViewHolder, position: Int, payloads: List<Any>) {
@@ -101,7 +101,7 @@ class ThumbnailAdapter(private val handler: ThumbnailHandler, private val filena
             border.visibility = if (isShow) View.VISIBLE else View.GONE
         }
 
-        internal fun loadThumbSync(filename: String, handler: ThumbnailHandler) {
+        internal fun loadThumbSync() {
             stopLoadThumbSync()
 
             disposable = Observable.create<Bitmap> { emitter ->
@@ -110,13 +110,13 @@ class ThumbnailAdapter(private val handler: ThumbnailHandler, private val filena
                 if (null != mMemoryCache[pageNameTag]) {
                     bitmap = mMemoryCache[pageNameTag]!!
                 } else {
-                    bitmap = handler.getPdfBitmap(adapterPosition, 180, ThumbnailHandler.MODE_DAY, true) !!
+                    bitmap = thumbnailHandler.getPdfBitmap(adapterPosition, 180, ThumbnailHandler.MODE_DAY, true) !!
                     mMemoryCache.put(pageNameTag, bitmap)
                 }
                 emitter.onNext(bitmap)
                 emitter.onComplete()
             }
-                    .subscribeOn(Schedulers.computation())
+                    .subscribeOn(Schedulers.io())
                     .observeOn(AndroidSchedulers.mainThread())
                     .subscribe({
                         thumb.setImageBitmap(it)

+ 12 - 0
reader/src/main/java/com/kdanmobile/reader/screen/handler/PdfInfoHandler.kt

@@ -0,0 +1,12 @@
+package com.kdanmobile.reader.screen.handler
+
+interface PdfInfoHandler {
+
+    fun getOpenPdfFilename(): String?
+
+    fun getPdfPageCount(): Int?
+
+    fun getCurrentPage(): Int?
+
+    fun setCurrentPage(page: Int): Boolean?
+}

+ 1 - 7
reader/src/main/java/com/kdanmobile/reader/screen/view/ThumbnailHandler.kt

@@ -1,4 +1,4 @@
-package com.kdanmobile.reader.screen.view
+package com.kdanmobile.reader.screen.handler
 
 import android.graphics.Bitmap
 
@@ -13,11 +13,5 @@ interface ThumbnailHandler {
         val MODE_WHITE_LILAC = 5
     }
 
-    fun getPdfPageCount(): Int?
-
-    fun getCurrentPage(): Int?
-
-    fun setCurrentPage(page: Int): Boolean?
-
     fun getPdfBitmap(position: Int, picWidth: Int, mode: Int = MODE_DAY, isDrawAnnot: Boolean = true): Bitmap?
 }

+ 9 - 8
reader/src/main/java/com/kdanmobile/reader/screen/view/ThumbnailView.kt

@@ -8,6 +8,9 @@ import android.view.LayoutInflater
 import android.widget.RelativeLayout
 import com.kdanmobile.reader.R
 import com.kdanmobile.reader.ReaderViewModel
+import com.kdanmobile.reader.screen.adapter.ThumbnailAdapter
+import com.kdanmobile.reader.screen.handler.PdfInfoHandler
+import com.kdanmobile.reader.screen.handler.ThumbnailHandler
 import com.kdanmobile.reader.widget.drag.OnRecyclerItemClickListener
 import kotlinx.android.synthetic.main.view_thumbnail.view.*
 
@@ -15,23 +18,21 @@ class ThumbnailView: RelativeLayout {
     companion object {
         private var currentPage = 0
     }
-    private lateinit var handler: ThumbnailHandler
     private lateinit var adapter: ThumbnailAdapter
 
     constructor(context: Context, viewModel: ReaderViewModel) : super(context) {
-        initView(viewModel)
+        initView(viewModel, viewModel)
     }
 
     constructor(context: Context, attributeSet: AttributeSet, viewModel: ReaderViewModel) : super(context, attributeSet) {
-        initView(viewModel)
+        initView(viewModel, viewModel)
     }
 
     constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int, viewModel: ReaderViewModel) : super(context, attributeSet, defStyleAttr) {
-        initView(viewModel)
+        initView(viewModel, viewModel)
     }
 
-    private fun initView(handler: ThumbnailHandler) {
-        this.handler = handler
+    private fun initView(pdfInfoHandler: PdfInfoHandler, thumbnailHandler: ThumbnailHandler) {
         LayoutInflater.from(context).inflate(R.layout.view_thumbnail, this)
         val layoutManager = LinearLayoutManager(context)
         layoutManager.orientation = LinearLayoutManager.VERTICAL
@@ -42,7 +43,7 @@ class ThumbnailView: RelativeLayout {
         id_km_thumb_recycler.addOnItemTouchListener(object : OnRecyclerItemClickListener(id_km_thumb_recycler) {
             override fun onItemClick(viewHolder: RecyclerView.ViewHolder) {
                 onScrollToPosition(viewHolder.adapterPosition, true)
-                handler.setCurrentPage(viewHolder.adapterPosition)
+                pdfInfoHandler.setCurrentPage(viewHolder.adapterPosition)
             }
 
             override fun onItemLongClick(viewHolder: RecyclerView.ViewHolder) {
@@ -50,7 +51,7 @@ class ThumbnailView: RelativeLayout {
             }
         })
 
-        adapter = ThumbnailAdapter(handler, "FILENAME")
+        adapter = ThumbnailAdapter(pdfInfoHandler, thumbnailHandler)
         id_km_thumb_recycler.adapter = adapter
         onScrollToPosition(currentPage, true)
     }