Browse Source

Add packages & PdfInfoHandler

cooperku_kdanmobile 5 years ago
parent
commit
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.KMPDFFactory
 import com.kdanmobile.kmpdfkit.manager.controller.KMPDFDocumentController
 import com.kdanmobile.kmpdfkit.manager.controller.KMPDFDocumentController
 import com.kdanmobile.kmpdfkit.pdfcommon.KMPDFReaderView
 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) {
     enum class ViewDirection(val mode: Config.PDFViewMode) {
         VerticalSinglePageContinues(Config.PDFViewMode.VERTICAL_SINGLE_PAGE_CONTINUES),
         VerticalSinglePageContinues(Config.PDFViewMode.VERTICAL_SINGLE_PAGE_CONTINUES),
         VerticalSinglePage(Config.PDFViewMode.VERTICAL_SINGLE_PAGE),
         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? {
     override fun getPdfPageCount(): Int? {
         return kmpdfDocumentController?.getDocumentPageCount(false)
         return kmpdfDocumentController?.getDocumentPageCount(false)
     }
     }
@@ -179,6 +184,10 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
         return kmpdfDocumentController?.gotoPage(page)
         return kmpdfDocumentController?.gotoPage(page)
     }
     }
 
 
+    //  ====================================================================
+    //  implement ThumbnailHandler
+    //  ====================================================================
+
     override fun getPdfBitmap(position: Int, picWidth: Int, mode: Int, isDrawAnnot: Boolean): Bitmap? {
     override fun getPdfBitmap(position: Int, picWidth: Int, mode: Int, isDrawAnnot: Boolean): Bitmap? {
         return kmpdfDocumentController?.covertPDFToBitmap(position, picWidth, mode, isDrawAnnot)
         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.graphics.Bitmap
 import android.support.v4.util.LruCache
 import android.support.v4.util.LruCache
@@ -10,15 +10,15 @@ import android.widget.FrameLayout
 import android.widget.ImageView
 import android.widget.ImageView
 import android.widget.TextView
 import android.widget.TextView
 import com.kdanmobile.reader.R
 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.Observable
 import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.disposables.Disposable
 import io.reactivex.disposables.Disposable
 import io.reactivex.schedulers.Schedulers
 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 {
     companion object {
         lateinit var mMemoryCache: LruCache<String, Bitmap>
         lateinit var mMemoryCache: LruCache<String, Bitmap>
@@ -27,7 +27,7 @@ class ThumbnailAdapter(private val handler: ThumbnailHandler, private val filena
     private var currentPage = 0
     private var currentPage = 0
 
 
     init {
     init {
-        this.currentPage = handler.getCurrentPage() ?: 0
+        this.currentPage = pdfInfoHandler.getCurrentPage() ?: 0
 
 
         mMemoryCache = object: LruCache<String, Bitmap>(maxMemory.toInt()) {
         mMemoryCache = object: LruCache<String, Bitmap>(maxMemory.toInt()) {
             override fun sizeOf(key: String, value: Bitmap): Int {
             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 {
     override fun getItemCount(): Int {
-        return handler.getPdfPageCount() ?: 0
+        return pdfInfoHandler.getPdfPageCount() ?: 0
     }
     }
 
 
     override fun onBindViewHolder(holder: ThumbnailViewHolder, position: Int) {
     override fun onBindViewHolder(holder: ThumbnailViewHolder, position: Int) {
         holder.page.text = (holder.adapterPosition + 1).toString()
         holder.page.text = (holder.adapterPosition + 1).toString()
         holder.onShowBorder(currentPage == holder.adapterPosition)
         holder.onShowBorder(currentPage == holder.adapterPosition)
-        holder.loadThumbSync(filename, handler)
+        holder.loadThumbSync()
     }
     }
 
 
     override fun onBindViewHolder(holder: ThumbnailViewHolder, position: Int, payloads: List<Any>) {
     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
             border.visibility = if (isShow) View.VISIBLE else View.GONE
         }
         }
 
 
-        internal fun loadThumbSync(filename: String, handler: ThumbnailHandler) {
+        internal fun loadThumbSync() {
             stopLoadThumbSync()
             stopLoadThumbSync()
 
 
             disposable = Observable.create<Bitmap> { emitter ->
             disposable = Observable.create<Bitmap> { emitter ->
@@ -110,13 +110,13 @@ class ThumbnailAdapter(private val handler: ThumbnailHandler, private val filena
                 if (null != mMemoryCache[pageNameTag]) {
                 if (null != mMemoryCache[pageNameTag]) {
                     bitmap = mMemoryCache[pageNameTag]!!
                     bitmap = mMemoryCache[pageNameTag]!!
                 } else {
                 } else {
-                    bitmap = handler.getPdfBitmap(adapterPosition, 180, ThumbnailHandler.MODE_DAY, true) !!
+                    bitmap = thumbnailHandler.getPdfBitmap(adapterPosition, 180, ThumbnailHandler.MODE_DAY, true) !!
                     mMemoryCache.put(pageNameTag, bitmap)
                     mMemoryCache.put(pageNameTag, bitmap)
                 }
                 }
                 emitter.onNext(bitmap)
                 emitter.onNext(bitmap)
                 emitter.onComplete()
                 emitter.onComplete()
             }
             }
-                    .subscribeOn(Schedulers.computation())
+                    .subscribeOn(Schedulers.io())
                     .observeOn(AndroidSchedulers.mainThread())
                     .observeOn(AndroidSchedulers.mainThread())
                     .subscribe({
                     .subscribe({
                         thumb.setImageBitmap(it)
                         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
 import android.graphics.Bitmap
 
 
@@ -13,11 +13,5 @@ interface ThumbnailHandler {
         val MODE_WHITE_LILAC = 5
         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?
     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 android.widget.RelativeLayout
 import com.kdanmobile.reader.R
 import com.kdanmobile.reader.R
 import com.kdanmobile.reader.ReaderViewModel
 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 com.kdanmobile.reader.widget.drag.OnRecyclerItemClickListener
 import kotlinx.android.synthetic.main.view_thumbnail.view.*
 import kotlinx.android.synthetic.main.view_thumbnail.view.*
 
 
@@ -15,23 +18,21 @@ class ThumbnailView: RelativeLayout {
     companion object {
     companion object {
         private var currentPage = 0
         private var currentPage = 0
     }
     }
-    private lateinit var handler: ThumbnailHandler
     private lateinit var adapter: ThumbnailAdapter
     private lateinit var adapter: ThumbnailAdapter
 
 
     constructor(context: Context, viewModel: ReaderViewModel) : super(context) {
     constructor(context: Context, viewModel: ReaderViewModel) : super(context) {
-        initView(viewModel)
+        initView(viewModel, viewModel)
     }
     }
 
 
     constructor(context: Context, attributeSet: AttributeSet, viewModel: ReaderViewModel) : super(context, attributeSet) {
     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) {
     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)
         LayoutInflater.from(context).inflate(R.layout.view_thumbnail, this)
         val layoutManager = LinearLayoutManager(context)
         val layoutManager = LinearLayoutManager(context)
         layoutManager.orientation = LinearLayoutManager.VERTICAL
         layoutManager.orientation = LinearLayoutManager.VERTICAL
@@ -42,7 +43,7 @@ class ThumbnailView: RelativeLayout {
         id_km_thumb_recycler.addOnItemTouchListener(object : OnRecyclerItemClickListener(id_km_thumb_recycler) {
         id_km_thumb_recycler.addOnItemTouchListener(object : OnRecyclerItemClickListener(id_km_thumb_recycler) {
             override fun onItemClick(viewHolder: RecyclerView.ViewHolder) {
             override fun onItemClick(viewHolder: RecyclerView.ViewHolder) {
                 onScrollToPosition(viewHolder.adapterPosition, true)
                 onScrollToPosition(viewHolder.adapterPosition, true)
-                handler.setCurrentPage(viewHolder.adapterPosition)
+                pdfInfoHandler.setCurrentPage(viewHolder.adapterPosition)
             }
             }
 
 
             override fun onItemLongClick(viewHolder: RecyclerView.ViewHolder) {
             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
         id_km_thumb_recycler.adapter = adapter
         onScrollToPosition(currentPage, true)
         onScrollToPosition(currentPage, true)
     }
     }