PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/fragments/base/AbsRecyclerViewCustomGridSi...

126 lines
3.6 KiB
Kotlin
Raw Normal View History

2019-04-20 05:29:45 +00:00
package code.name.monkey.retromusic.fragments.base
2018-11-30 01:06:16 +00:00
import android.os.Bundle
import android.view.View
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.util.RetroUtil
abstract class AbsRecyclerViewCustomGridSizeFragment<A : RecyclerView.Adapter<*>, LM : RecyclerView.LayoutManager> :
AbsRecyclerViewFragment<A, LM>() {
2018-11-30 01:06:16 +00:00
private var gridSize: Int = 0
private var sortOrder: String? = null
private var currentLayoutRes: Int = 0
2020-01-17 17:19:06 +00:00
private val isLandscape: Boolean
get() = RetroUtil.isLandscape()
2018-11-30 01:06:16 +00:00
val maxGridSize: Int
get() = if (isLandscape) {
resources.getInteger(R.integer.max_columns_land)
} else {
resources.getInteger(R.integer.max_columns)
}
2020-01-17 17:19:06 +00:00
fun itemLayoutRes(): Int {
return if (getGridSize() > maxGridSizeForList) {
loadLayoutRes()
2018-11-30 01:06:16 +00:00
} else R.layout.item_list
2020-01-17 17:19:06 +00:00
}
2018-11-30 01:06:16 +00:00
2020-01-17 17:19:06 +00:00
fun setAndSaveLayoutRes(layoutRes: Int) {
saveLayoutRes(layoutRes)
2020-02-17 15:08:20 +00:00
invalidateAdapter()
2020-01-17 17:19:06 +00:00
}
private val maxGridSizeForList: Int
2018-11-30 01:06:16 +00:00
get() = if (isLandscape) {
2020-03-01 13:28:51 +00:00
resources.getInteger(R.integer.default_list_columns_land)
} else resources.getInteger(R.integer.default_list_columns)
2018-11-30 01:06:16 +00:00
fun getGridSize(): Int {
if (gridSize == 0) {
2018-12-08 03:33:02 +00:00
gridSize = if (isLandscape) {
loadGridSizeLand()
2018-11-30 01:06:16 +00:00
} else {
2018-12-08 03:33:02 +00:00
loadGridSize()
2018-11-30 01:06:16 +00:00
}
}
return gridSize
}
fun getSortOrder(): String? {
if (sortOrder == null) {
sortOrder = loadSortOrder()
}
return sortOrder
}
fun setAndSaveSortOrder(sortOrder: String) {
this.sortOrder = sortOrder
saveSortOrder(sortOrder)
setSortOrder(sortOrder)
}
fun setAndSaveGridSize(gridSize: Int) {
2020-01-17 17:19:06 +00:00
val oldLayoutRes = itemLayoutRes()
2018-11-30 01:06:16 +00:00
this.gridSize = gridSize
if (isLandscape) {
saveGridSizeLand(gridSize)
} else {
saveGridSize(gridSize)
}
invalidateLayoutManager()
2018-11-30 01:06:16 +00:00
// only recreate the adapter and layout manager if the layout currentLayoutRes has changed
2020-01-17 17:19:06 +00:00
if (oldLayoutRes != itemLayoutRes()) {
2018-11-30 01:06:16 +00:00
invalidateAdapter()
} else {
setGridSize(gridSize)
}
}
protected fun notifyLayoutResChanged(@LayoutRes res: Int) {
this.currentLayoutRes = res
val recyclerView = recyclerView()
applyRecyclerViewPaddingForLayoutRes(recyclerView, currentLayoutRes)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
applyRecyclerViewPaddingForLayoutRes(recyclerView(), currentLayoutRes)
}
2019-11-15 16:30:10 +00:00
private fun applyRecyclerViewPaddingForLayoutRes(recyclerView: RecyclerView, res: Int) {
val padding: Int = if (res == R.layout.item_grid) {
(resources.displayMetrics.density * 2).toInt()
2018-11-30 01:06:16 +00:00
} else {
2019-11-15 16:30:10 +00:00
0
2018-11-30 01:06:16 +00:00
}
recyclerView.setPadding(padding, padding, padding, padding)
}
protected abstract fun setGridSize(gridSize: Int)
protected abstract fun setSortOrder(sortOrder: String)
2018-11-30 01:06:16 +00:00
protected abstract fun loadSortOrder(): String
protected abstract fun saveSortOrder(sortOrder: String)
protected abstract fun loadGridSize(): Int
protected abstract fun saveGridSize(gridColumns: Int)
protected abstract fun loadGridSizeLand(): Int
protected abstract fun saveGridSizeLand(gridColumns: Int)
2020-01-17 17:19:06 +00:00
protected abstract fun loadLayoutRes(): Int
protected abstract fun saveLayoutRes(layoutRes: Int)
2018-11-30 01:06:16 +00:00
}