2020-10-06 08:46:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Hemanth Savarla.
|
|
|
|
*
|
|
|
|
* Licensed under the GNU General Public License v3
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
|
|
2020-07-30 14:11:19 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fun setAndSaveLayoutRes(layoutRes: Int) {
|
2020-02-04 08:09:10 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-03-02 17:26:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-03-02 17:26:03 +00:00
|
|
|
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
|
|
|
}
|