FastScroll.kt 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Nextcloud Android Library is available under MIT license
  3. *
  4. * @author Álvaro Brey Vilas
  5. * Copyright (C) 2022 Álvaro Brey Vilas
  6. * Copyright (C) 2022 Nextcloud GmbH
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  22. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  23. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. package com.nextcloud.utils.view
  28. import android.content.Context
  29. import android.graphics.drawable.Drawable
  30. import android.view.ViewGroup
  31. import androidx.annotation.ColorInt
  32. import androidx.core.content.res.ResourcesCompat
  33. import androidx.recyclerview.widget.RecyclerView
  34. import com.google.android.material.appbar.AppBarLayout
  35. import com.owncloud.android.utils.theme.ThemeColorUtils
  36. import com.owncloud.android.utils.theme.ThemeDrawableUtils
  37. import me.zhanghai.android.fastscroll.FastScroller
  38. import me.zhanghai.android.fastscroll.FastScrollerBuilder
  39. import me.zhanghai.android.fastscroll.PopupStyles
  40. object FastScroll {
  41. @JvmStatic
  42. @JvmOverloads
  43. fun applyFastScroll(
  44. context: Context,
  45. themeColorUtils: ThemeColorUtils,
  46. themeDrawableUtils: ThemeDrawableUtils,
  47. recyclerView: RecyclerView,
  48. viewHelper: FastScroller.ViewHelper? = null
  49. ) {
  50. val primaryColor = themeColorUtils.primaryColor(context)
  51. val builder = FastScrollerBuilder(recyclerView)
  52. .useMd2Style()
  53. .setThumbDrawable(getThumbDrawable(context, themeDrawableUtils, primaryColor))
  54. .setPopupStyle {
  55. PopupStyles.MD2.accept(it)
  56. it.background = FastScrollPopupBackground(context, primaryColor)
  57. }
  58. if (viewHelper != null) {
  59. builder.setViewHelper(viewHelper)
  60. }
  61. builder.build()
  62. }
  63. private fun getThumbDrawable(
  64. context: Context,
  65. themeDrawableUtils: ThemeDrawableUtils,
  66. @ColorInt color: Int
  67. ): Drawable {
  68. val thumbDrawable =
  69. ResourcesCompat.getDrawable(
  70. context.resources,
  71. me.zhanghai.android.fastscroll.R.drawable.afs_md2_thumb,
  72. null
  73. )
  74. themeDrawableUtils.tintDrawable(thumbDrawable, color)
  75. return thumbDrawable!!
  76. }
  77. @JvmStatic
  78. fun fixAppBarForFastScroll(appBarLayout: AppBarLayout, content: ViewGroup) {
  79. val contentLayoutInitialPaddingBottom = content.paddingBottom
  80. appBarLayout.addOnOffsetChangedListener(
  81. AppBarLayout.OnOffsetChangedListener { _, offset ->
  82. content.setPadding(
  83. content.paddingLeft,
  84. content.paddingTop,
  85. content.paddingRight,
  86. contentLayoutInitialPaddingBottom + appBarLayout.totalScrollRange + offset
  87. )
  88. }
  89. )
  90. }
  91. }