Parcourir la source

Merge branch 'refactorLeftToolbar'

cooperku_kdanmobile il y a 5 ans
Parent
commit
8278006590

+ 72 - 97
reader/src/main/java/com/kdanmobile/reader/ReaderActivity.kt

@@ -70,12 +70,7 @@ abstract class ReaderActivity : AppCompatActivity() {
     private var w_240: Int = 0
     private var w_left: Int = 0
 
-    private var isLeftToolbarOpen = false
-
-    enum class LeftToolbarType {
-        NONE, THUMBNAIL, BOOKMARK, OUTLINE, SEARCH
-    }
-    private var leftToolbarType = LeftToolbarType.NONE
+    private var oldLeftToolbarType = ReaderViewModel.LeftToolbarType.NONE
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -116,6 +111,8 @@ abstract class ReaderActivity : AppCompatActivity() {
         setupRightToolbar()
         viewModel.pageIndexLiveData.observe(this, Observer(this::onPageIndexChanged))
         viewModel.isPageInBookmarksLiveData.observe(this, Observer(this::onIsPageInBookmarksChanged))
+        viewModel.isLeftToolbarOpenLiveData.observe(this, Observer(this::onLeftToolbarOpenStateChanged))
+        viewModel.leftToolbarTypeLiveData.observe(this, Observer(this::onLeftToolbarTypeChanged))
     }
 
     override fun onBackPressed() {
@@ -485,7 +482,16 @@ abstract class ReaderActivity : AppCompatActivity() {
         viewGroup_readerActivity_leftToolbar.smoothScrollTo(w_240, 0, 0)
         val child = layoutInflater.inflate(R.layout.view_thumbnail, null)
         linearLayout_readerActivity_leftToolbarContainer.addView(child)
-        cloneOriginConstraintSet()
+
+        fun onClickLeftToolbarItem(leftToolbarType: ReaderViewModel.LeftToolbarType) {
+            viewModel.setLeftToolbarType(leftToolbarType)
+            updateLeftToolbarOpenState(leftToolbarType)
+        }
+
+        iv_readerActivity_thumbnail.setOnClickListener { onClickLeftToolbarItem(ReaderViewModel.LeftToolbarType.THUMBNAIL) }
+        iv_readerActivity_bookmark.setOnClickListener { onClickLeftToolbarItem(ReaderViewModel.LeftToolbarType.BOOKMARK) }
+        iv_readerActivity_outline.setOnClickListener { onClickLeftToolbarItem(ReaderViewModel.LeftToolbarType.OUTLINE) }
+        iv_readerActivity_search.setOnClickListener { onClickLeftToolbarItem(ReaderViewModel.LeftToolbarType.SEARCH) }
     }
 
     private fun setupRightSideToolbar() {
@@ -642,7 +648,6 @@ abstract class ReaderActivity : AppCompatActivity() {
         }
     }
 
-
     private fun setLeftToolbarWidth(width: Int) {
         var params = viewGroup_readerActivity_leftToolbar.layoutParams as ConstraintLayout.LayoutParams
         params.width = width + w_left
@@ -654,23 +659,9 @@ abstract class ReaderActivity : AppCompatActivity() {
         linearLayout_readerActivity_leftToolbarContainer.layoutParams = params
     }
 
-    private fun changeLeftToolbarType(type: LeftToolbarType) {
-        iv_readerActivity_thumbnail.setImageResource(R.drawable.ic_left_toolbar_thumbnails)
-        iv_readerActivity_bookmark.setImageResource(R.drawable.ic_left_toolbar_bookmarks)
-        iv_readerActivity_outline.setImageResource(R.drawable.ic_left_toolbar_outline)
-        iv_readerActivity_search.setImageResource(R.drawable.ic_left_toolbar_search)
-
-        val width = when (type) {
-            LeftToolbarType.THUMBNAIL -> w_240
-            else -> w_318
-        }
-        setLeftToolbarWidth(width)
-
-        isLeftToolbarOpen = (!isLeftToolbarOpen || leftToolbarType != type)
-        if (isLeftToolbarOpen)
-            viewGroup_readerActivity_leftToolbar.smoothScrollTo(0, 0, UI_ANIMATION_DURATION.toInt())
-        else
-            viewGroup_readerActivity_leftToolbar.smoothScrollTo(width, 0, UI_ANIMATION_DURATION.toInt())
+    private fun updateLeftToolbarOpenState(type: ReaderViewModel.LeftToolbarType) {
+        val isLeftToolbarOpen = viewModel.isLeftToolbarOpenLiveData.value ?: false
+        viewModel.setIsLeftToolbarOpen(!isLeftToolbarOpen || oldLeftToolbarType != type)
         cloneOriginConstraintSet()
     }
 
@@ -683,91 +674,75 @@ abstract class ReaderActivity : AppCompatActivity() {
         viewModel.clearSelection()
     }
 
-    private fun onThumbnailClick() {
-        val type = LeftToolbarType.THUMBNAIL
-        changeLeftToolbarType(type)
-        if (isLeftToolbarOpen) {
-            if (leftToolbarType != type) {
-                linearLayout_readerActivity_leftToolbarContainer.removeAllViews()
-                val thumbnailView = ThumbnailView(this)
-                thumbnailView.setHandlers(viewModel.pdfInfoHandler, viewModel.thumbnailHandler)
-                linearLayout_readerActivity_leftToolbarContainer.addView(thumbnailView)
+    private fun setLeftToolbarContentView(leftToolbarType: ReaderViewModel.LeftToolbarType) {
+        if (oldLeftToolbarType != leftToolbarType) {
+            linearLayout_readerActivity_leftToolbarContainer.removeAllViews()
+            val view = when (leftToolbarType) {
+                ReaderViewModel.LeftToolbarType.NONE,
+                ReaderViewModel.LeftToolbarType.THUMBNAIL -> {
+                    val thumbnailView = ThumbnailView(this)
+                    thumbnailView.setHandlers(viewModel.pdfInfoHandler, viewModel.thumbnailHandler)
+                    thumbnailView
+                }
+                ReaderViewModel.LeftToolbarType.BOOKMARK -> {
+                    val bookmarkView = BookmarkView(this)
+                    bookmarkView.setHandlers(viewModel.pdfInfoHandler, viewModel.thumbnailHandler, viewModel.bookmarkHandler)
+                    bookmarkView
+                }
+                ReaderViewModel.LeftToolbarType.OUTLINE -> {
+                    val outlineView = OutlineView(this)
+                    outlineView.setHandlers(viewModel.pdfInfoHandler, viewModel.outlineHandler)
+                    outlineView
+                }
+                ReaderViewModel.LeftToolbarType.SEARCH -> {
+                    val searchView = SearchView(this)
+                    searchView.setHandlers(viewModel.pdfInfoHandler, viewModel.searchHandler)
+                    searchView
+                }
             }
-            iv_readerActivity_thumbnail.setImageResource(R.drawable.ic_left_toolbar_thumbnails_selected)
-        } else {
-            iv_readerActivity_thumbnail.setImageResource(R.drawable.ic_left_toolbar_thumbnails)
+            linearLayout_readerActivity_leftToolbarContainer.addView(view)
+            oldLeftToolbarType = leftToolbarType
         }
-        leftToolbarType = type
     }
 
-    private fun onBookmarkClick() {
-        val type = LeftToolbarType.BOOKMARK
-        changeLeftToolbarType(type)
-        if (isLeftToolbarOpen) {
-            if (leftToolbarType != type) {
-                linearLayout_readerActivity_leftToolbarContainer.removeAllViews()
-                val bookmarkView = BookmarkView(this)
-                bookmarkView.setHandlers(viewModel.pdfInfoHandler, viewModel.thumbnailHandler, viewModel.bookmarkHandler)
-                linearLayout_readerActivity_leftToolbarContainer.addView(bookmarkView)
-            }
-            iv_readerActivity_bookmark.setImageResource(R.drawable.ic_left_toolbar_bookmarks_selected)
-        } else {
-            iv_readerActivity_bookmark.setImageResource(R.drawable.ic_left_toolbar_bookmarks)
+    private fun getLeftToolbarContentWidth(): Int {
+        return when (viewModel.leftToolbarTypeLiveData.value == ReaderViewModel.LeftToolbarType.THUMBNAIL) {
+            true -> w_240
+            false -> w_318
         }
-        leftToolbarType = type
     }
 
-    private fun onOutlineClick() {
-        val type = LeftToolbarType.OUTLINE
-        changeLeftToolbarType(type)
-        if (isLeftToolbarOpen) {
-            if (leftToolbarType != type) {
-                linearLayout_readerActivity_leftToolbarContainer.removeAllViews()
-                val outlineView = OutlineView(this)
-                outlineView.setHandlers(viewModel.pdfInfoHandler, viewModel.outlineHandler)
-                linearLayout_readerActivity_leftToolbarContainer.addView(outlineView)
-            }
-            iv_readerActivity_outline.setImageResource(R.drawable.ic_left_toolbar_outline_selected)
-        } else {
-            iv_readerActivity_outline.setImageResource(R.drawable.ic_left_toolbar_outline)
+    private fun onLeftToolbarTypeChanged(leftToolbarType: ReaderViewModel.LeftToolbarType?) {
+        updateLeftToolbarIcons()
+        setLeftToolbarWidth(getLeftToolbarContentWidth())
+        cloneOriginConstraintSet()
+
+        if (leftToolbarType != null) {
+            setLeftToolbarContentView(leftToolbarType)
         }
-        leftToolbarType = type
     }
 
-    private fun onSearchClick() {
-        val type = LeftToolbarType.SEARCH
-        changeLeftToolbarType(type)
-        if (isLeftToolbarOpen) {
-            if (leftToolbarType != type) {
-                linearLayout_readerActivity_leftToolbarContainer.removeAllViews()
-                val searchView = SearchView(this)
-                searchView.setHandlers(viewModel.pdfInfoHandler, viewModel.searchHandler)
-                linearLayout_readerActivity_leftToolbarContainer.addView(searchView)
-            }
-            iv_readerActivity_search.setImageResource(R.drawable.ic_left_toolbar_search_selected)
-        } else {
-            iv_readerActivity_search.setImageResource(R.drawable.ic_left_toolbar_search)
-            hideSearchViewSoftKeyboard()
+    private fun onLeftToolbarOpenStateChanged(open: Boolean?) {
+        updateLeftToolbarIcons()
+
+        when (open == true) {
+            true -> viewGroup_readerActivity_leftToolbar.smoothScrollTo(0, 0, UI_ANIMATION_DURATION.toInt())
+            false -> viewGroup_readerActivity_leftToolbar.smoothScrollTo(getLeftToolbarContentWidth(), 0, UI_ANIMATION_DURATION.toInt())
         }
-        leftToolbarType = type
     }
 
-    fun onClick(view: View) {
-        when (view.id) {
-            R.id.iv_readerActivity_thumbnail -> {
-                onThumbnailClick()
-            }
-            R.id.iv_readerActivity_bookmark -> {
-                onBookmarkClick()
-            }
-            R.id.iv_readerActivity_outline -> {
-                onOutlineClick()
-            }
-            R.id.iv_readerActivity_search -> {
-                onSearchClick()
-            }
-            else -> {
-
+    private fun updateLeftToolbarIcons() {
+        iv_readerActivity_thumbnail.setImageResource(R.drawable.ic_left_toolbar_thumbnails)
+        iv_readerActivity_bookmark.setImageResource(R.drawable.ic_left_toolbar_bookmarks)
+        iv_readerActivity_outline.setImageResource(R.drawable.ic_left_toolbar_outline)
+        iv_readerActivity_search.setImageResource(R.drawable.ic_left_toolbar_search)
+        if (viewModel.isLeftToolbarOpenLiveData.value == true) {
+            when (viewModel.leftToolbarTypeLiveData.value) {
+                ReaderViewModel.LeftToolbarType.NONE -> {}
+                ReaderViewModel.LeftToolbarType.THUMBNAIL -> iv_readerActivity_thumbnail.setImageResource(R.drawable.ic_left_toolbar_thumbnails_selected)
+                ReaderViewModel.LeftToolbarType.BOOKMARK -> iv_readerActivity_bookmark.setImageResource(R.drawable.ic_left_toolbar_bookmarks_selected)
+                ReaderViewModel.LeftToolbarType.OUTLINE -> iv_readerActivity_outline.setImageResource(R.drawable.ic_left_toolbar_outline_selected)
+                ReaderViewModel.LeftToolbarType.SEARCH -> iv_readerActivity_search.setImageResource(R.drawable.ic_left_toolbar_search_selected)
             }
         }
     }

+ 20 - 0
reader/src/main/java/com/kdanmobile/reader/ReaderViewModel.kt

@@ -88,6 +88,26 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
     private val mPageIndexLiveData = MutableLiveData<Int>().apply { value = 0 }
     private val mIsPageInBookmarksLiveData = MutableLiveData<Boolean>().apply { value = false }
 
+    enum class LeftToolbarType {
+        NONE, THUMBNAIL, BOOKMARK, OUTLINE, SEARCH
+    }
+
+    val leftToolbarTypeLiveData: LiveData<LeftToolbarType>
+        get() = mLeftToolbarTypeLiveData
+    private val mLeftToolbarTypeLiveData = MutableLiveData<LeftToolbarType>().apply { value = LeftToolbarType.NONE }
+
+    val isLeftToolbarOpenLiveData: LiveData<Boolean>
+        get() = mIsLeftToolbarOpenLiveData
+    private val mIsLeftToolbarOpenLiveData = MutableLiveData<Boolean>().apply { value = false }
+
+    fun setLeftToolbarType(leftToolbarType: LeftToolbarType) {
+        mLeftToolbarTypeLiveData.postValue(leftToolbarType)
+    }
+
+    fun setIsLeftToolbarOpen(open: Boolean) {
+        mIsLeftToolbarOpenLiveData.postValue(open)
+    }
+
     @JvmOverloads
     fun openPdfFile(context: Context, uri: Uri, type: String? = null): Boolean {
         if (!isVerified) {

+ 0 - 4
reader/src/main/res/layout/activity_reader.xml

@@ -74,22 +74,18 @@
                 android:layout_height="wrap_content">
                 <ImageView
                     android:id="@+id/iv_readerActivity_thumbnail"
-                    android:onClick="onClick"
                     style="@style/ReaderLeftRightToolbarIImageButton"
                     android:src="@drawable/ic_left_toolbar_thumbnails" />
                 <ImageView
                     android:id="@+id/iv_readerActivity_bookmark"
-                    android:onClick="onClick"
                     style="@style/ReaderLeftRightToolbarIImageButton"
                     android:src="@drawable/ic_left_toolbar_bookmarks" />
                 <ImageView
                     android:id="@+id/iv_readerActivity_outline"
-                    android:onClick="onClick"
                     style="@style/ReaderLeftRightToolbarIImageButton"
                     android:src="@drawable/ic_left_toolbar_outline" />
                 <ImageView
                     android:id="@+id/iv_readerActivity_search"
-                    android:onClick="onClick"
                     style="@style/ReaderLeftRightToolbarIImageButton"
                     android:src="@drawable/ic_left_toolbar_search" />
             </LinearLayout>