Kaynağa Gözat

Refactor & remove unused code

cooperku_kdanmobile 5 yıl önce
ebeveyn
işleme
11fd6ed0b7
19 değiştirilmiş dosya ile 264 ekleme ve 285 silme
  1. 25 14
      reader/src/main/java/com/kdanmobile/reader/ReaderActivity.kt
  2. 43 61
      reader/src/main/java/com/kdanmobile/reader/ReaderViewModel.kt
  3. 14 16
      reader/src/main/java/com/kdanmobile/reader/screen/adapter/BookmarkAdapter.kt
  4. 10 11
      reader/src/main/java/com/kdanmobile/reader/screen/adapter/OutlineAdapter.kt
  5. 22 27
      reader/src/main/java/com/kdanmobile/reader/screen/adapter/SearchAdapter.kt
  6. 15 15
      reader/src/main/java/com/kdanmobile/reader/screen/adapter/ThumbnailAdapter.kt
  7. 1 1
      reader/src/main/java/com/kdanmobile/reader/screen/handler/BookmarkHandler.kt
  8. 1 1
      reader/src/main/java/com/kdanmobile/reader/screen/handler/OutlineHandler.kt
  9. 5 5
      reader/src/main/java/com/kdanmobile/reader/screen/handler/PdfInfoHandler.kt
  10. 4 6
      reader/src/main/java/com/kdanmobile/reader/screen/handler/SearchHandler.kt
  11. 1 1
      reader/src/main/java/com/kdanmobile/reader/screen/handler/ThumbnailHandler.kt
  12. 6 29
      reader/src/main/java/com/kdanmobile/reader/screen/model/SearchResultInfo.kt
  13. 16 19
      reader/src/main/java/com/kdanmobile/reader/screen/view/BookmarkView.kt
  14. 29 15
      reader/src/main/java/com/kdanmobile/reader/screen/view/OutlineView.kt
  15. 50 42
      reader/src/main/java/com/kdanmobile/reader/screen/view/SearchView.kt
  16. 17 14
      reader/src/main/java/com/kdanmobile/reader/screen/view/ThumbnailView.kt
  17. 4 4
      reader/src/main/java/com/kdanmobile/reader/widget/drag/OnRecyclerItemClickListener.kt
  18. 1 2
      reader/src/main/res/layout/item_bookmark.xml
  19. 0 2
      reader/src/main/res/layout/view_bookmark.xml

+ 25 - 14
reader/src/main/java/com/kdanmobile/reader/ReaderActivity.kt

@@ -45,9 +45,9 @@ open class ReaderActivity : AppCompatActivity() {
     private var isLeftToolbarOpen = false
 
     enum class LeftToolbarType {
-        INIT, THUMBNAIL, BOOKMARK, OUTLINE, SEARCH
+        NONE, THUMBNAIL, BOOKMARK, OUTLINE, SEARCH
     }
-    private var leftToolbarType = LeftToolbarType.INIT
+    private var leftToolbarType = LeftToolbarType.NONE
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -171,6 +171,7 @@ open class ReaderActivity : AppCompatActivity() {
             AnimationUtil.showViewFromBottomToTop(viewGroup_readerActivity_bottomToolbar, UI_ANIMATION_DURATION)
             AnimationUtil.showViewFromLeftToRight(viewGroup_readerActivity_leftToolbar, UI_ANIMATION_DURATION)
             AnimationUtil.showViewFromRightToLeft(viewGroup_readerActivity_rightToolbar, UI_ANIMATION_DURATION)
+            updateThumbnailViewSelectedPage()
         } else {
             constrainLayout_readerActivity_root.applyConstraintSet(originConstraintSet, UI_ANIMATION_DURATION)
         }
@@ -199,12 +200,18 @@ open class ReaderActivity : AppCompatActivity() {
         }
     }
 
+    private fun updateThumbnailViewSelectedPage() {
+        val view = linearLayout_readerActivity_leftToolbarContainer.getChildAt(0)
+        if (view is ThumbnailView) {
+            val currentPage = viewModel.pdfInfoHandler.getCurrentPage()
+            view.onScrollToPosition(currentPage, true)
+        }
+    }
+
     private fun hideSearchViewSoftKeyboard() {
-        if (leftToolbarType == LeftToolbarType.SEARCH) {
-            val view = linearLayout_readerActivity_leftToolbarContainer.getChildAt(0)
-            if (view is SearchView) {
-                view.hideSoftKeyboard()
-            }
+        val view = linearLayout_readerActivity_leftToolbarContainer.getChildAt(0)
+        if (view is SearchView) {
+            view.hideSoftKeyboard()
         }
     }
 
@@ -232,12 +239,12 @@ open class ReaderActivity : AppCompatActivity() {
     //  =====================================================================
 
     private fun setLeftToolbarWidth(width: Int) {
-        var params = viewGroup_readerActivity_leftToolbar!!.layoutParams as ConstraintLayout.LayoutParams
+        var params = viewGroup_readerActivity_leftToolbar.layoutParams as ConstraintLayout.LayoutParams
         params.width = width + w_left
         params.height = 0
         viewGroup_readerActivity_leftToolbar.layoutParams = params
 
-        params = linearLayout_readerActivity_leftToolbarContainer!!.layoutParams as ConstraintLayout.LayoutParams
+        params = linearLayout_readerActivity_leftToolbarContainer.layoutParams as ConstraintLayout.LayoutParams
         params.width = width
         linearLayout_readerActivity_leftToolbarContainer.layoutParams = params
     }
@@ -268,7 +275,8 @@ open class ReaderActivity : AppCompatActivity() {
         if (isLeftToolbarOpen) {
             if (leftToolbarType != type) {
                 linearLayout_readerActivity_leftToolbarContainer.removeAllViews()
-                val thumbnailView = ThumbnailView(this, viewModel)
+                val thumbnailView = ThumbnailView(this)
+                thumbnailView.setHandlers(viewModel.pdfInfoHandler, viewModel.thumbnailHandler)
                 linearLayout_readerActivity_leftToolbarContainer.addView(thumbnailView)
             }
             iv_readerActivity_thumbnail.setImageResource(R.drawable.ic_pagelist_thumbnail_h)
@@ -284,7 +292,8 @@ open class ReaderActivity : AppCompatActivity() {
         if (isLeftToolbarOpen) {
             if (leftToolbarType != type) {
                 linearLayout_readerActivity_leftToolbarContainer.removeAllViews()
-                val bookmarkView = BookmarkView(this, viewModel)
+                val bookmarkView = BookmarkView(this)
+                bookmarkView.setHandlers(viewModel.pdfInfoHandler, viewModel.thumbnailHandler, viewModel.bookmarkHandler)
                 linearLayout_readerActivity_leftToolbarContainer.addView(bookmarkView)
             }
             iv_readerActivity_bookmark.setImageResource(R.drawable.ic_pagelist_bookmk_h)
@@ -300,7 +309,8 @@ open class ReaderActivity : AppCompatActivity() {
         if (isLeftToolbarOpen) {
             if (leftToolbarType != type) {
                 linearLayout_readerActivity_leftToolbarContainer.removeAllViews()
-                val outlineView = OutlineView(this, viewModel)
+                val outlineView = OutlineView(this)
+                outlineView.setHandlers(viewModel.pdfInfoHandler, viewModel.outlineHandler)
                 linearLayout_readerActivity_leftToolbarContainer.addView(outlineView)
             }
             iv_readerActivity_outline.setImageResource(R.drawable.ic_pagelist_outline_h)
@@ -316,8 +326,9 @@ open class ReaderActivity : AppCompatActivity() {
         if (isLeftToolbarOpen) {
             if (leftToolbarType != type) {
                 linearLayout_readerActivity_leftToolbarContainer.removeAllViews()
-                val outlineView = SearchView(this, viewModel)
-                linearLayout_readerActivity_leftToolbarContainer.addView(outlineView)
+                val searchView = SearchView(this)
+                searchView.setHandlers(viewModel.pdfInfoHandler, viewModel.searchHandler)
+                linearLayout_readerActivity_leftToolbarContainer.addView(searchView)
             }
             iv_readerActivity_search.setImageResource(R.drawable.ic_pagelist_search_h)
         } else {

+ 43 - 61
reader/src/main/java/com/kdanmobile/reader/ReaderViewModel.kt

@@ -10,13 +10,10 @@ import com.kdanmobile.kmpdfkit.annotation.bean.*
 import com.kdanmobile.kmpdfkit.globaldata.Config
 import com.kdanmobile.kmpdfkit.manager.KMPDFFactory
 import com.kdanmobile.kmpdfkit.manager.controller.KMPDFDocumentController
-import com.kdanmobile.kmpdfkit.pdfcommon.Bookmark
-import com.kdanmobile.kmpdfkit.pdfcommon.KMPDFReaderView
-import com.kdanmobile.kmpdfkit.pdfcommon.OutlineItem
-import com.kdanmobile.kmpdfkit.pdfcommon.TextWord
+import com.kdanmobile.kmpdfkit.pdfcommon.*
 import com.kdanmobile.reader.screen.handler.*
 
-class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMsg: String) : ViewModel(), PdfInfoHandler, ThumbnailHandler, BookmarkHandler, OutlineHandler, SearchHandler {
+class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMsg: String) : ViewModel() {
     enum class ViewDirection(val mode: Config.PDFViewMode) {
         VerticalSinglePageContinues(Config.PDFViewMode.VERTICAL_SINGLE_PAGE_CONTINUES),
         VerticalSinglePage(Config.PDFViewMode.VERTICAL_SINGLE_PAGE),
@@ -167,76 +164,61 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
         }
     }
 
-    //  ====================================================================
-    //  implement PdfInfoHandler
-    //  ====================================================================
-
-    override fun getOpenPdfFilename(): String? {
-        return kmpdfFactory?.fileName
-    }
-
-    override fun getPdfPageCount(): Int? {
-        return kmpdfDocumentController?.getDocumentPageCount(false)
-    }
-
-    override fun getCurrentPage(): Int? {
-        return kmpdfDocumentController?.currentPageNum
-    }
-
-    override fun setCurrentPage(page: Int): Boolean? {
-        return kmpdfDocumentController?.gotoPage(page)
-    }
-
-    override fun textLines(page: Int): Array<Array<TextWord>>? {
-        return kmpdfDocumentController?.textLines(page)
-    }
+    val pdfInfoHandler = object : PdfInfoHandler {
+        override fun getOpenPdfFilename(): String {
+            return kmpdfFactory?.fileName ?: ""
+        }
 
-    //  ====================================================================
-    //  implement ThumbnailHandler
-    //  ====================================================================
+        override fun getPdfPageCount(): Int {
+            return kmpdfDocumentController?.getDocumentPageCount(false) ?: 0
+        }
 
-    override fun getPdfBitmap(position: Int, picWidth: Int, mode: Int, isDrawAnnot: Boolean): Bitmap? {
-        return kmpdfDocumentController?.covertPDFToBitmap(position, picWidth, mode, isDrawAnnot)
-    }
+        override fun getCurrentPage(): Int {
+            return kmpdfDocumentController?.currentPageNum ?: 0
+        }
 
-    //  ====================================================================
-    //  implement BookmarkHandler
-    //  ====================================================================
+        override fun setCurrentPage(page: Int): Boolean {
+            return kmpdfDocumentController?.gotoPage(page) ?: false
+        }
 
-    override fun getBookmarks(): Array<Bookmark>? {
-        return kmpdfDocumentController?.bookmarks
+        override fun textLines(page: Int): Array<Array<TextWord>> {
+            return kmpdfDocumentController?.textLines(page) ?: arrayOf()
+        }
     }
 
-    //  ====================================================================
-    //  implement OutlineHandler
-    //  ====================================================================
-
-    override fun getOutline(): Array<OutlineItem>? {
-        return kmpdfDocumentController?.outline
+    val thumbnailHandler = object : ThumbnailHandler {
+        override fun getPdfBitmap(position: Int, picWidth: Int, mode: Int, isDrawAnnot: Boolean): Bitmap {
+            return kmpdfDocumentController?.covertPDFToBitmap(position, picWidth, mode, isDrawAnnot) ?: Bitmap.createBitmap(0, 0, Bitmap.Config.ARGB_4444)
+        }
     }
 
-    //  ====================================================================
-    //  implement SearchHandler
-    //  ====================================================================
-
-    override fun searchPage(page: Int, keyword: String): Array<RectF>? {
-        return kmpdfDocumentController?.searchPage(page, keyword)
+    val bookmarkHandler = object : BookmarkHandler {
+        override fun getBookmarks(): Array<Bookmark> {
+            return kmpdfDocumentController?.bookmarks ?: arrayOf()
+        }
     }
 
-    override fun cleanPreviousSearchResults(keyword: String): Boolean? {
-        return kmpdfDocumentController?.cleanPreviousSearchResults(keyword)
+    val outlineHandler = object : OutlineHandler {
+        override fun getOutline(): Array<OutlineItem> {
+            return kmpdfDocumentController?.outline ?: arrayOf()
+        }
     }
 
-    override fun cleanPreviousSearchResults(page: Int, keyword: String, rectArray: Array<RectF>): Boolean? {
-        return kmpdfDocumentController?.setSearchResult(keyword, page, rectArray)
-    }
+    val searchHandler = object : SearchHandler {
+        override fun searchPage(page: Int, keyword: String): Array<RectF> {
+            return kmpdfDocumentController?.searchPage(page, keyword) ?: arrayOf()
+        }
 
-    override fun stopSearchKeyWord(): Boolean? {
-        return kmpdfDocumentController?.stopSearchKeyWord()
-    }
+        override fun cleanPreviousSearchResults(keyword: String): Boolean {
+            return kmpdfDocumentController?.cleanPreviousSearchResults(keyword) ?: false
+        }
 
-    override fun updateSearchBoxes() {
-        val pageView = kmpdfFactory?.readerView?.displayedView
+        override fun cleanPreviousSearchResults(page: Int, keyword: String, rectArray: Array<RectF>): Boolean {
+            return kmpdfDocumentController?.setSearchResult(keyword, page, rectArray) ?: false
+        }
 
+        override fun stopSearchKeyWord(): Boolean {
+            return kmpdfDocumentController?.stopSearchKeyWord() ?: false
+        }
     }
 }

+ 14 - 16
reader/src/main/java/com/kdanmobile/reader/screen/adapter/BookmarkAdapter.kt

@@ -18,36 +18,33 @@ import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.disposables.Disposable
 import io.reactivex.schedulers.Schedulers
 
-class BookmarkAdapter(private val thumbnailHandler: ThumbnailHandler, private var bookmarkItems: Array<Bookmark>?): RecyclerView.Adapter<BookmarkAdapter.BookmarkViewHolder>() {
+class BookmarkAdapter(private val thumbnailHandler: ThumbnailHandler, private var bookmarkItems: Array<Bookmark>): RecyclerView.Adapter<BookmarkAdapter.BookmarkViewHolder>() {
 
     companion object {
-        lateinit var mMemoryCache: LruCache<String, Bitmap>
-        internal var maxMemory = Runtime.getRuntime().maxMemory() / 8
-    }
+        val maxMemory = Runtime.getRuntime().maxMemory() / 16
 
-    init {
-        mMemoryCache = object: LruCache<String, Bitmap>(maxMemory.toInt()) {
+        val mMemoryCache: LruCache<String, Bitmap> = object: LruCache<String, Bitmap>(maxMemory.toInt()) {
             override fun sizeOf(key: String, value: Bitmap): Int {
                 return value.byteCount
             }
         }
     }
 
-    fun setOutlineItems(outlineItems: Array<Bookmark>?) {
-        this.bookmarkItems = outlineItems
+    fun setOutlineItems(bookmarkItems: Array<Bookmark>) {
+        this.bookmarkItems = bookmarkItems
         notifyDataSetChanged()
     }
 
     override fun getItemCount(): Int {
-        return bookmarkItems?.size ?: 0
+        return bookmarkItems.size
     }
 
     fun getItem(position: Int): Bookmark? {
-        return bookmarkItems?.get(position)
+        return if (position >= 0 && position < bookmarkItems.size) bookmarkItems[position] else null
     }
 
     override fun onBindViewHolder(holder: BookmarkViewHolder, position: Int) {
-        val bookmark = bookmarkItems!![holder.adapterPosition]
+        val bookmark = bookmarkItems[holder.adapterPosition]
         holder.msg.text = bookmark.title
         holder.page.text = (bookmark.pageNum + 1).toString()
         holder.loadThumbSync(bookmark.title + bookmark.pageNum + bookmark.createTime, bookmark.pageNum)
@@ -70,7 +67,7 @@ class BookmarkAdapter(private val thumbnailHandler: ThumbnailHandler, private va
         private var disposable: Disposable? = null
 
         init {
-            var idReaderBookmarkFl = itemView.findViewById<View>(R.id.tv_fragmentPdfReaderBookmarkLvItem_rl) as RelativeLayout
+            val idReaderBookmarkFl = itemView.findViewById<View>(R.id.tv_fragmentPdfReaderBookmarkLvItem_rl) as RelativeLayout
             val params = LinearLayout.LayoutParams((parent.measuredWidth - 20) / 2, (parent.measuredWidth - 20) * 9 / 14)
             params.setMargins(10, 5, 10, 5)
             idReaderBookmarkFl.layoutParams = params
@@ -85,11 +82,12 @@ class BookmarkAdapter(private val thumbnailHandler: ThumbnailHandler, private va
 
             disposable = Observable.create<Bitmap> { emitter ->
                 val pageNameTag = filename + page
-                var bitmap: Bitmap
-                if (null != mMemoryCache[pageNameTag]) {
-                    bitmap = mMemoryCache[pageNameTag]!!
+                val bitmap: Bitmap
+                val value = mMemoryCache[pageNameTag]
+                if (null != value) {
+                    bitmap = value
                 } else {
-                    bitmap = thumbnailHandler.getPdfBitmap(page, 180, ThumbnailHandler.MODE_DAY, true) !!
+                    bitmap = thumbnailHandler.getPdfBitmap(page, 180, ThumbnailHandler.MODE_DAY, true)
                     mMemoryCache.put(pageNameTag, bitmap)
                 }
                 emitter.onNext(bitmap)

+ 10 - 11
reader/src/main/java/com/kdanmobile/reader/screen/adapter/OutlineAdapter.kt

@@ -8,15 +8,14 @@ import android.widget.TextView
 import com.kdanmobile.kmpdfkit.pdfcommon.OutlineItem
 import com.kdanmobile.reader.R
 
-class OutlineAdapter(private val outlineListItemList: List<OutlineItem>?): BaseAdapter() {
+class OutlineAdapter(private val outlineListItems: List<OutlineItem>): BaseAdapter() {
 
     override fun getCount(): Int {
-        return outlineListItemList?.size ?: 0
+        return outlineListItems.size
     }
 
     override fun getItem(position: Int): OutlineItem? {
-        val isNull = null == outlineListItemList || outlineListItemList.size < position
-        return if (isNull) null else outlineListItemList?.get(position)
+        return if (position >= 0 && position < outlineListItems.size) outlineListItems[position] else null
     }
 
     override fun getItemId(position: Int): Long {
@@ -24,14 +23,14 @@ class OutlineAdapter(private val outlineListItemList: List<OutlineItem>?): BaseA
     }
 
     override fun getView(position: Int, view: View?, parent: ViewGroup): View {
-        var view = view
+        var view1 = view
         val holderView: HolderView
-        if (view == null) {
-            view = LayoutInflater.from(parent.context).inflate(R.layout.outline_entry, null)
-            holderView = HolderView(view!!)
-            view!!.tag = holderView
+        if (view1 == null) {
+            view1 = LayoutInflater.from(parent.context).inflate(R.layout.outline_entry, null) as View
+            holderView = HolderView(view1)
+            view1.tag = holderView
         } else {
-            holderView = view.tag as HolderView
+            holderView = view1.tag as HolderView
         }
 
         val item = getItem(position)
@@ -45,7 +44,7 @@ class OutlineAdapter(private val outlineListItemList: List<OutlineItem>?): BaseA
         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
+        return view1
     }
 
     private class HolderView(view: View) {

+ 22 - 27
reader/src/main/java/com/kdanmobile/reader/screen/adapter/SearchAdapter.kt

@@ -13,14 +13,9 @@ import com.kdanmobile.reader.screen.model.SearchResultInfo
 import java.util.ArrayList
 
 class SearchAdapter(private var width: Int): BaseAdapter() {
-    private var list: MutableList<SearchResultInfo>
+    private var list: MutableList<SearchResultInfo> = ArrayList()
     private var selected = -1
 
-    init {
-        this.width = width
-        list = ArrayList()
-    }
-
     fun clear() {
         list.clear()
     }
@@ -47,42 +42,42 @@ class SearchAdapter(private var width: Int): BaseAdapter() {
     }
 
     override fun getView(position: Int, view: View?, parent: ViewGroup): View {
-        var view = view
+        var view1 = view
         val item = list[position]
-        val hv: HolderView
-        if (view == null) {
-            view = LayoutInflater.from(parent.context).inflate(R.layout.item_search, null)
-            hv = HolderView()
-            hv.tv_result = view!!.findViewById<View>(R.id.tv_fragmentPdfReaderSearchLvItem_result) as TextView
-            hv.tv_page = view.findViewById<View>(R.id.tv_fragmentPdfReaderSearchLvItem_page) as TextView
-            hv.view = view.findViewById(R.id.view_fragmentPdfReaderSearchLvItem_selected)
-            hv.rl = view .findViewById<View>(R.id.rl_fragmentPdfReaderSearchLvItem_) as RelativeLayout
+        val holderView: HolderView
+        if (view1 == null) {
+            view1 = LayoutInflater.from(parent.context).inflate(R.layout.item_search, null) as View
+            holderView = HolderView()
+            holderView.tv_result = view1.findViewById<View>(R.id.tv_fragmentPdfReaderSearchLvItem_result) as TextView
+            holderView.tv_page = view1.findViewById<View>(R.id.tv_fragmentPdfReaderSearchLvItem_page) as TextView
+            holderView.view = view1.findViewById(R.id.view_fragmentPdfReaderSearchLvItem_selected)
+            holderView.rl = view1 .findViewById<View>(R.id.rl_fragmentPdfReaderSearchLvItem_) as RelativeLayout
             val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
             params.leftMargin = 21 * width / 800
             params.rightMargin = 21 * width / 800
             params.topMargin = 8 * width / 800
-            hv.rl!!.layoutParams = params
-            view.tag = hv
+            holderView.rl.layoutParams = params
+            view1.tag = holderView
         } else {
-            hv = view.tag as HolderView
+            holderView = view1.tag as HolderView
         }
 
         if (selected == position) {
-            hv.view!!.setBackgroundResource(R.drawable.rectangle_6633b5e5)
+            holderView.view.setBackgroundResource(R.drawable.rectangle_6633b5e5)
         } else {
-            hv.view!!.setBackgroundResource(R.drawable.rectangle_0000)
+            holderView.view.setBackgroundResource(R.drawable.rectangle_0000)
         }
 
-        hv.tv_result!!.text = Html.fromHtml(item.result)
-        hv.tv_page!!.text = "" + (item.page + 1)
+        holderView.tv_result.text = Html.fromHtml(item.result)
+        holderView.tv_page.text = (item.page + 1).toString()
 
-        return view
+        return view1
     }
 
     private inner class HolderView {
-        var rl: RelativeLayout? = null
-        var tv_result: TextView? = null
-        var tv_page: TextView? = null
-        var view: View? = null
+        lateinit var rl: RelativeLayout
+        lateinit var tv_result: TextView
+        lateinit var tv_page: TextView
+        lateinit var view: View
     }
 }

+ 15 - 15
reader/src/main/java/com/kdanmobile/reader/screen/adapter/ThumbnailAdapter.kt

@@ -18,23 +18,22 @@ import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.disposables.Disposable
 import io.reactivex.schedulers.Schedulers
 
-class ThumbnailAdapter(private val pdfInfoHandler: PdfInfoHandler, private val thumbnailHandler: ThumbnailHandler, private val filename: String = pdfInfoHandler.getOpenPdfFilename()!!): 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>
-        internal var maxMemory = Runtime.getRuntime().maxMemory() / 8
-    }
-    private var currentPage = 0
-
-    init {
-        this.currentPage = pdfInfoHandler.getCurrentPage() ?: 0
+        val maxMemory = Runtime.getRuntime().maxMemory() / 16
 
-        mMemoryCache = object: LruCache<String, Bitmap>(maxMemory.toInt()) {
+        val mMemoryCache: LruCache<String, Bitmap> = object: LruCache<String, Bitmap>(maxMemory.toInt()) {
             override fun sizeOf(key: String, value: Bitmap): Int {
                 return value.byteCount
             }
         }
     }
+    private var currentPage = 0
+
+    init {
+        this.currentPage = pdfInfoHandler.getCurrentPage()
+    }
 
     fun setCurrentPage(currentPage: Int) {
         val oldCurrentPage = this.currentPage
@@ -49,7 +48,7 @@ class ThumbnailAdapter(private val pdfInfoHandler: PdfInfoHandler, private val t
     }
 
     override fun getItemCount(): Int {
-        return pdfInfoHandler.getPdfPageCount() ?: 0
+        return pdfInfoHandler.getPdfPageCount()
     }
 
     override fun onBindViewHolder(holder: ThumbnailViewHolder, position: Int) {
@@ -87,7 +86,7 @@ class ThumbnailAdapter(private val pdfInfoHandler: PdfInfoHandler, private val t
         private var disposable: Disposable? = null
 
         init {
-            var idReaderThumbFl = itemView.findViewById<View>(R.id.id_reader_thumb_fl) as FrameLayout
+            val idReaderThumbFl = itemView.findViewById<View>(R.id.id_reader_thumb_fl) as FrameLayout
             val params = RecyclerView.LayoutParams(parent.measuredWidth - 20, (parent.measuredWidth - 20) * 9 / 7)
             params.setMargins(10, 5, 10, 5)
             idReaderThumbFl.layoutParams = params
@@ -106,11 +105,12 @@ class ThumbnailAdapter(private val pdfInfoHandler: PdfInfoHandler, private val t
 
             disposable = Observable.create<Bitmap> { emitter ->
                 val pageNameTag = filename + adapterPosition
-                var bitmap: Bitmap
-                if (null != mMemoryCache[pageNameTag]) {
-                    bitmap = mMemoryCache[pageNameTag]!!
+                val bitmap: Bitmap
+                val value = mMemoryCache[pageNameTag]
+                if (null != value) {
+                    bitmap = value
                 } else {
-                    bitmap = thumbnailHandler.getPdfBitmap(adapterPosition, 180, ThumbnailHandler.MODE_DAY, true) !!
+                    bitmap = thumbnailHandler.getPdfBitmap(adapterPosition, 180, ThumbnailHandler.MODE_DAY, true)
                     mMemoryCache.put(pageNameTag, bitmap)
                 }
                 emitter.onNext(bitmap)

+ 1 - 1
reader/src/main/java/com/kdanmobile/reader/screen/handler/BookmarkHandler.kt

@@ -4,5 +4,5 @@ import com.kdanmobile.kmpdfkit.pdfcommon.Bookmark
 
 interface BookmarkHandler {
 
-    fun getBookmarks(): Array<Bookmark>?
+    fun getBookmarks(): Array<Bookmark>
 }

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

@@ -4,5 +4,5 @@ import com.kdanmobile.kmpdfkit.pdfcommon.OutlineItem
 
 interface OutlineHandler {
 
-    fun getOutline(): Array<OutlineItem>?
+    fun getOutline(): Array<OutlineItem>
 }

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

@@ -4,13 +4,13 @@ import com.kdanmobile.kmpdfkit.pdfcommon.TextWord
 
 interface PdfInfoHandler {
 
-    fun getOpenPdfFilename(): String?
+    fun getOpenPdfFilename(): String
 
-    fun getPdfPageCount(): Int?
+    fun getPdfPageCount(): Int
 
-    fun getCurrentPage(): Int?
+    fun getCurrentPage(): Int
 
-    fun setCurrentPage(page: Int): Boolean?
+    fun setCurrentPage(page: Int): Boolean
 
-    fun textLines(page: Int): Array<Array<TextWord>>?
+    fun textLines(page: Int): Array<Array<TextWord>>
 }

+ 4 - 6
reader/src/main/java/com/kdanmobile/reader/screen/handler/SearchHandler.kt

@@ -4,13 +4,11 @@ import android.graphics.RectF
 
 interface SearchHandler {
 
-    fun searchPage(page: Int, keyword: String): Array<RectF>?
+    fun searchPage(page: Int, keyword: String): Array<RectF>
 
-    fun cleanPreviousSearchResults(keyword: String): Boolean?
+    fun cleanPreviousSearchResults(keyword: String): Boolean
 
-    fun cleanPreviousSearchResults(page: Int, keyword: String, rectArray: Array<RectF>): Boolean?
+    fun cleanPreviousSearchResults(page: Int, keyword: String, rectArray: Array<RectF>): Boolean
 
-    fun stopSearchKeyWord(): Boolean?
-
-    fun updateSearchBoxes()
+    fun stopSearchKeyWord(): Boolean
 }

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

@@ -13,5 +13,5 @@ interface ThumbnailHandler {
         val MODE_WHITE_LILAC = 5
     }
 
-    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
 }

+ 6 - 29
reader/src/main/java/com/kdanmobile/reader/screen/model/SearchResultInfo.kt

@@ -3,37 +3,14 @@ package com.kdanmobile.reader.screen.model
 import android.graphics.RectF
 import com.kdanmobile.kmpdfkit.pdfcommon.TextWord
 
-class SearchResultInfo {
-    lateinit var result: String
-    var page: Int = 0
-    lateinit var rf: RectF
-    lateinit var search: String
-    lateinit var line: Array<TextWord>
+class SearchResultInfo(var page: Int, var search: String, var rf: RectF, lines: Array<TextWord>) {
+    var result: String = ""
 
-    constructor() {
-
-    }
-
-    constructor(result: String, page: Int) {
-        this.result = result
-        this.page = page
-    }
-
-    constructor(page: Int, search: String, rf: RectF, line: Array<TextWord>) {
-        this.page = page
-        this.search = search
-        this.rf = rf
-        this.line = line
-        result = ""
-        for (aLine in line) {
-            result = result + aLine.w + " "
+    init {
+        for (line in lines) {
+            result = result + line.w + " "
         }
 
-        this.result = replace(result, search)
-    }
-
-    private fun replace(result: String, search: String): String {
-        var result = result
         val sb = StringBuilder()
         var src = result.toLowerCase()
         val seh = search.toLowerCase()
@@ -51,6 +28,6 @@ class SearchResultInfo {
 
         sb.append(result)
 
-        return sb.toString()
+        result = sb.toString()
     }
 }

+ 16 - 19
reader/src/main/java/com/kdanmobile/reader/screen/view/BookmarkView.kt

@@ -8,7 +8,6 @@ import android.view.LayoutInflater
 import android.widget.RelativeLayout
 import com.kdanmobile.kmpdfkit.pdfcommon.Bookmark
 import com.kdanmobile.reader.R
-import com.kdanmobile.reader.ReaderViewModel
 import com.kdanmobile.reader.screen.adapter.BookmarkAdapter
 import com.kdanmobile.reader.screen.handler.BookmarkHandler
 import com.kdanmobile.reader.screen.handler.PdfInfoHandler
@@ -19,28 +18,25 @@ import java.util.*
 
 class BookmarkView: RelativeLayout {
     private lateinit var adapter: BookmarkAdapter
-    private lateinit var handler: ThumbnailHandler
-    private var bookmarkItems: Array<Bookmark>? = null
+    private var pdfInfoHandler: PdfInfoHandler? = null
 
-    constructor(context: Context, viewModel: ReaderViewModel) : super(context) {
-        initView(viewModel, viewModel, viewModel)
+    constructor(context: Context) : super(context) {
+        initView()
     }
 
-    constructor(context: Context, attributeSet: AttributeSet, viewModel: ReaderViewModel) : super(context, attributeSet) {
-        initView(viewModel, viewModel, viewModel)
+    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
+        initView()
     }
 
-    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int, viewModel: ReaderViewModel) : super(context, attributeSet, defStyleAttr) {
-        initView(viewModel, viewModel, viewModel)
+    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) {
+        initView()
     }
 
-    private fun initView(pdfInfoHandler: PdfInfoHandler, thumbnailHandler: ThumbnailHandler, bookmarkHandler: BookmarkHandler) {
+    private fun initView() {
         LayoutInflater.from(context).inflate(R.layout.view_bookmark, this)
         val layoutManager = LinearLayoutManager(context)
         layoutManager.orientation = LinearLayoutManager.VERTICAL
 
-        this.handler = thumbnailHandler
-
         id_km_bookmark_recycler.setHasFixedSize(true)
         id_km_bookmark_recycler.layoutManager = layoutManager
         id_km_bookmark_recycler.layoutManager = LinearLayoutManager(context)
@@ -48,7 +44,7 @@ class BookmarkView: RelativeLayout {
             override fun onItemClick(viewHolder: RecyclerView.ViewHolder) {
                 val bookmark = adapter.getItem(viewHolder.adapterPosition)
                 if (null != bookmark) {
-                    pdfInfoHandler.setCurrentPage(bookmark.pageNum)
+                    pdfInfoHandler?.setCurrentPage(bookmark.pageNum)
                 }
             }
 
@@ -56,23 +52,24 @@ class BookmarkView: RelativeLayout {
 
             }
         })
+    }
 
-        adapter = BookmarkAdapter(handler, bookmarkItems)
+    fun setHandlers(pdfInfoHandler: PdfInfoHandler, thumbnailHandler: ThumbnailHandler, bookmarkHandler: BookmarkHandler) {
+        this.pdfInfoHandler = pdfInfoHandler
+        adapter = BookmarkAdapter(thumbnailHandler, arrayOf())
         id_km_bookmark_recycler.adapter = adapter
-        onRefreshBookmarkDatas(bookmarkHandler.getBookmarks()!!)
+        onRefreshBookmarkData(bookmarkHandler.getBookmarks())
     }
 
 
-    private fun onRefreshBookmarkDatas(items: Array<Bookmark>) {
+    private fun onRefreshBookmarkData(bookmarkItems: Array<Bookmark>) {
         try {
-            this.bookmarkItems = items
-            if (bookmarkItems?.isNotEmpty() != null) {
+            if (bookmarkItems.isNotEmpty()) {
                 Arrays.sort(bookmarkItems) { lhs, rhs -> if (lhs.pageNum > rhs.pageNum) 1 else -1 }
             }
             adapter.setOutlineItems(bookmarkItems)
         } catch (e: Exception) {
             e.printStackTrace()
         }
-
     }
 }

+ 29 - 15
reader/src/main/java/com/kdanmobile/reader/screen/view/OutlineView.kt

@@ -4,42 +4,57 @@ 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.disposables.Disposable
 import io.reactivex.schedulers.Schedulers
 import kotlinx.android.synthetic.main.view_outline.view.*
 
 class OutlineView: RelativeLayout {
     private lateinit var adapter: OutlineAdapter
+    private var pdfInfoHandler: PdfInfoHandler? = null
+    private var disposable: Disposable? = null
 
-    constructor(context: Context, viewModel: ReaderViewModel) : super(context) {
-        initView(viewModel, viewModel)
+    constructor(context: Context) : super(context) {
+        initView()
     }
 
-    constructor(context: Context, attributeSet: AttributeSet, viewModel: ReaderViewModel) : super(context, attributeSet) {
-        initView(viewModel, viewModel)
+    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
+        initView()
     }
 
-    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int, viewModel: ReaderViewModel) : super(context, attributeSet, defStyleAttr) {
-        initView(viewModel, viewModel)
+    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) {
+        initView()
     }
 
-    private fun initView(pdfInfoHandler: PdfInfoHandler, outlineHandler: OutlineHandler) {
+    private fun initView() {
         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)
+        lv_viewPdfReaderOutline_.setOnItemClickListener { _, _, position, _ ->
+            val item = adapter.getItem(position)
+            if (null != item) {
+                pdfInfoHandler?.setCurrentPage(item.page)
+            }
         }
 
-        val disposable = Observable.create<OutlineAdapter> {
-            val outlineItems = outlineHandler.getOutline()!!
+    }
+
+    override fun onDetachedFromWindow() {
+        super.onDetachedFromWindow()
+        if (null != disposable && !disposable!!.isDisposed) {
+            disposable?.dispose()
+            disposable = null
+        }
+    }
+
+    fun setHandlers(pdfInfoHandler: PdfInfoHandler, outlineHandler: OutlineHandler) {
+        this.pdfInfoHandler = pdfInfoHandler
+        disposable = Observable.create<OutlineAdapter> {
+            val outlineItems = outlineHandler.getOutline()
             val copyList = outlineItems.toList()
             it.onNext(OutlineAdapter(copyList))
             it.onComplete()
@@ -52,6 +67,5 @@ class OutlineView: RelativeLayout {
                 }, {
                     it.printStackTrace()
                 })
-
     }
 }

+ 50 - 42
reader/src/main/java/com/kdanmobile/reader/screen/view/SearchView.kt

@@ -14,7 +14,6 @@ import android.view.inputmethod.InputMethodManager
 import android.widget.*
 import com.kdanmobile.kmpdfkit.pdfcommon.TextWord
 import com.kdanmobile.reader.R
-import com.kdanmobile.reader.ReaderViewModel
 import com.kdanmobile.reader.screen.adapter.SearchAdapter
 import com.kdanmobile.reader.screen.model.SearchResultInfo
 import com.kdanmobile.reader.screen.handler.PdfInfoHandler
@@ -31,8 +30,8 @@ import kotlinx.android.synthetic.main.view_search.view.*
 import java.lang.ref.WeakReference
 
 class SearchView: RelativeLayout, View.OnClickListener {
-    private lateinit var pdfInfoHandler: PdfInfoHandler
-    private lateinit var searchHandler: SearchHandler
+    private var pdfInfoHandler: PdfInfoHandler? = null
+    private var searchHandler: SearchHandler? = null
     private lateinit var linearLayout: LinearLayout
     private lateinit var tv_page: TextView
     private lateinit var tv_text:TextView
@@ -54,36 +53,38 @@ class SearchView: RelativeLayout, View.OnClickListener {
 
     private var disposable: Disposable? = null
 
-    constructor(context: Context, viewModel: ReaderViewModel) : super(context) {
-        initView(viewModel, viewModel)
+    constructor(context: Context) : super(context) {
+        initView()
     }
 
-    constructor(context: Context, attributeSet: AttributeSet, viewModel: ReaderViewModel) : super(context, attributeSet) {
-        initView(viewModel, viewModel)
+    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
+        initView()
     }
 
-    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int, viewModel: ReaderViewModel) : super(context, attributeSet, defStyleAttr) {
-        initView(viewModel, viewModel)
+    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) {
+        initView()
     }
 
-    private fun initView(pdfInfoHandler: PdfInfoHandler, searchHandler: SearchHandler) {
+    private fun initView() {
         LayoutInflater.from(context).inflate(R.layout.view_search, this)
 
-        this.pdfInfoHandler = pdfInfoHandler
-        this.searchHandler = searchHandler
-
         initHandler()
 
-        initView()
+        initLayout()
 
         setListener()
 
         postDelayed( { showSoftKeyboard() }, 200)
     }
 
+    fun setHandlers(pdfInfoHandler: PdfInfoHandler, searchHandler: SearchHandler) {
+        this.pdfInfoHandler = pdfInfoHandler
+        this.searchHandler = searchHandler
+    }
+
     private fun initHandler() {
 
-        class MyHandler(view: SearchView) : Handler() {
+        class WeakReferenceHandler(view: SearchView) : Handler() {
             private val mView: WeakReference<SearchView> = WeakReference(view)
 
             override fun handleMessage(msg: Message) {
@@ -105,16 +106,16 @@ class SearchView: RelativeLayout, View.OnClickListener {
                             val position = msg.arg1
                             val info = view.list[position]
                             SearchTaskResult.set(SearchTaskResult(info.search, info.page, arrayOf(info.rf)))
-                            view.pdfInfoHandler.setCurrentPage(view.list[position].page)
+                            view.pdfInfoHandler?.setCurrentPage(view.list[position].page)
                         }
                     }
                 }
             }
         }
-        msgHandler = MyHandler(this)
+        msgHandler = WeakReferenceHandler(this)
     }
 
-    private fun initView() {
+    private fun initLayout() {
 
         linearLayout = ll_fragmentPdfReaderSearch
         tv_page = tv_fragmentPdfReaderSearch_page
@@ -179,10 +180,7 @@ class SearchView: RelativeLayout, View.OnClickListener {
             msgHandler?.sendMessageDelayed(msgHandler?.obtainMessage(30, position, 0), 50)
         }
 
-        et_search.setOnEditorActionListener { v, actionId, event ->
-            // if (actionId == EditorInfo.IME_ACTION_DONE) {
-            // search();
-            // }
+        et_search.setOnEditorActionListener { _, actionId, _ ->
             if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                 if (!isPage && et_search.text.toString().length < 3) {
                     val la = IntArray(2)
@@ -212,12 +210,12 @@ class SearchView: RelativeLayout, View.OnClickListener {
                             et_search.setText(text)
                             et_search.setSelection(text.length)
                         }
-                        val searchSequence = et_search.text.toString()
-                        if (searchSequence.isNotEmpty()) {
-                            val page = Integer.parseInt(searchSequence)
-                            val pageCount = pdfInfoHandler.getPdfPageCount() ?: 0
+                        val searchText = et_search.text.toString()
+                        if (searchText.isNotEmpty()) {
+                            val page = Integer.parseInt(searchText)
+                            val pageCount = pdfInfoHandler?.getPdfPageCount() ?: 0
                             if (page > pageCount) {
-                                val text = searchSequence.subSequence(0, searchSequence.length - 1).toString()
+                                val text = searchText.subSequence(0, searchText.length - 1).toString()
                                 et_search.setText(text)
                                 et_search.setSelection(text.length)
                             }
@@ -275,14 +273,14 @@ class SearchView: RelativeLayout, View.OnClickListener {
     }
 
     private fun search() {
-        val search = et_search.text.toString()
-        if (search.isNotEmpty()) {
+        val searchText = et_search.text.toString()
+        if (searchText.isNotEmpty()) {
             if (isPage) {
-                val page = Integer.parseInt(search)
+                val page = Integer.parseInt(searchText)
                 if (page > 0) {
-                    pdfInfoHandler.setCurrentPage(page - 1)
+                    pdfInfoHandler?.setCurrentPage(page - 1)
                 }
-            } else if (search.length >= 3) {
+            } else if (searchText.length >= 3) {
                 if (list.size > 0) {
                     adapter.setSelected(-1)
                     adapter.clear()
@@ -292,16 +290,18 @@ class SearchView: RelativeLayout, View.OnClickListener {
 
                 isDone = false
                 disposable = Completable.create {
-                    val pageCount = pdfInfoHandler.getPdfPageCount()!!
-                    for (i in 0 until pageCount) {
-                        if (!isDone) {
-                            val results = searchHandler.searchPage(i, search)
-                            if (results?.isNotEmpty() != null) {
-                                val texts = pdfInfoHandler.textLines(i)
-                                val l = dealData(texts, results, search, i)
-                                msgHandler?.sendMessage(msgHandler?.obtainMessage(10, l))
-                                if (list.size + l.size >= MAX_SEARCH_RESULT) {
-                                    isDone = true
+                    if (null != pdfInfoHandler && null != searchHandler) {
+                        val pageCount = pdfInfoHandler!!.getPdfPageCount()
+                        for (i in 0 until pageCount) {
+                            if (!isDone) {
+                                val results = searchHandler!!.searchPage(i, searchText)
+                                if (results.isNotEmpty()) {
+                                    val texts = pdfInfoHandler!!.textLines(i)
+                                    val list = dealData(texts, results, searchText, i)
+                                    msgHandler?.sendMessage(msgHandler?.obtainMessage(10, list))
+                                    if (this.list.size + list.size >= MAX_SEARCH_RESULT) {
+                                        isDone = true
+                                    }
                                 }
                             }
                         }
@@ -322,6 +322,14 @@ class SearchView: RelativeLayout, View.OnClickListener {
         }
     }
 
+    override fun onDetachedFromWindow() {
+        super.onDetachedFromWindow()
+        if (null != disposable && !disposable!!.isDisposed) {
+            disposable?.dispose()
+            disposable = null
+        }
+    }
+
     private fun dealData(texts: Array<Array<TextWord>>?, results: Array<RectF>?, search: String, page: Int): List<SearchResultInfo> {
         val list = ArrayList<SearchResultInfo>()
         if (texts == null || results == null) {

+ 17 - 14
reader/src/main/java/com/kdanmobile/reader/screen/view/ThumbnailView.kt

@@ -7,7 +7,6 @@ import android.util.AttributeSet
 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
@@ -15,24 +14,23 @@ import com.kdanmobile.reader.widget.drag.OnRecyclerItemClickListener
 import kotlinx.android.synthetic.main.view_thumbnail.view.*
 
 class ThumbnailView: RelativeLayout {
-    companion object {
-        private var currentPage = 0
-    }
     private lateinit var adapter: ThumbnailAdapter
+    private var currentPage = 0
+    private var pdfInfoHandler: PdfInfoHandler? = null
 
-    constructor(context: Context, viewModel: ReaderViewModel) : super(context) {
-        initView(viewModel, viewModel)
+    constructor(context: Context) : super(context) {
+        initView()
     }
 
-    constructor(context: Context, attributeSet: AttributeSet, viewModel: ReaderViewModel) : super(context, attributeSet) {
-        initView(viewModel, viewModel)
+    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
+        initView()
     }
 
-    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int, viewModel: ReaderViewModel) : super(context, attributeSet, defStyleAttr) {
-        initView(viewModel, viewModel)
+    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) {
+        initView()
     }
 
-    private fun initView(pdfInfoHandler: PdfInfoHandler, thumbnailHandler: ThumbnailHandler) {
+    private fun initView() {
         LayoutInflater.from(context).inflate(R.layout.view_thumbnail, this)
         val layoutManager = LinearLayoutManager(context)
         layoutManager.orientation = LinearLayoutManager.VERTICAL
@@ -43,23 +41,28 @@ class ThumbnailView: RelativeLayout {
         id_km_thumb_recycler.addOnItemTouchListener(object : OnRecyclerItemClickListener(id_km_thumb_recycler) {
             override fun onItemClick(viewHolder: RecyclerView.ViewHolder) {
                 onScrollToPosition(viewHolder.adapterPosition, true)
-                pdfInfoHandler.setCurrentPage(viewHolder.adapterPosition)
+                pdfInfoHandler?.setCurrentPage(viewHolder.adapterPosition)
             }
 
             override fun onItemLongClick(viewHolder: RecyclerView.ViewHolder) {
 
             }
         })
+    }
+
+    fun setHandlers(pdfInfoHandler: PdfInfoHandler, thumbnailHandler: ThumbnailHandler) {
+        this.pdfInfoHandler = pdfInfoHandler
 
         adapter = ThumbnailAdapter(pdfInfoHandler, thumbnailHandler)
         id_km_thumb_recycler.adapter = adapter
+        currentPage = pdfInfoHandler?.getCurrentPage()
         onScrollToPosition(currentPage, true)
     }
 
-    private fun onScrollToPosition(position: Int, notify: Boolean) {
+    fun onScrollToPosition(position: Int, notify: Boolean) {
         currentPage = position
 
-        if (null != adapter && notify) {
+        if (notify) {
             adapter.setCurrentPage(position)
         }
 

+ 4 - 4
reader/src/main/java/com/kdanmobile/reader/widget/drag/OnRecyclerItemClickListener.kt

@@ -19,12 +19,12 @@ abstract class OnRecyclerItemClickListener(private val recyclerView: RecyclerVie
         mGestureDetector = GestureDetectorCompat(recyclerView.context, ItemTouchHelperGestureListener())
     }
 
-    override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
+    override fun onInterceptTouchEvent(recyclerView: RecyclerView, e: MotionEvent): Boolean {
         mGestureDetector.onTouchEvent(e)
         return false
     }
 
-    override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {
+    override fun onTouchEvent(recyclerView: RecyclerView, e: MotionEvent) {
         mGestureDetector.onTouchEvent(e)
     }
 
@@ -49,7 +49,7 @@ abstract class OnRecyclerItemClickListener(private val recyclerView: RecyclerVie
         }
     }
 
-    abstract fun onItemClick(vh: RecyclerView.ViewHolder)
+    abstract fun onItemClick(viewHolder: RecyclerView.ViewHolder)
 
-    abstract fun onItemLongClick(vh: RecyclerView.ViewHolder)
+    abstract fun onItemLongClick(viewHolder: RecyclerView.ViewHolder)
 }

+ 1 - 2
reader/src/main/res/layout/item_bookmark.xml

@@ -16,8 +16,7 @@
             android:layout_height="match_parent"
             android:background="@drawable/rectangle_stroke_1_d5d5d5_ffff"
             android:padding="1px"
-            android:scaleType="fitCenter"
-            android:src="@drawable/ic_format_pdf_48dp"/>
+            android:scaleType="fitCenter"/>
 
         <TextView
             android:id="@+id/tv_fragmentPdfReaderBookmarkLvItem_page"

+ 0 - 2
reader/src/main/res/layout/view_bookmark.xml

@@ -26,8 +26,6 @@
         android:id="@+id/id_km_bookmark_recycler"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
-        android:layout_below="@+id/id_km_thumb_divider"
-        android:layout_centerHorizontal="true"
         android:cacheColorHint="@null"
         android:fadeScrollbars="true"
         android:scrollbars="vertical"/>