Fixed weird behavior with dragging categories

This commit is contained in:
Jay 2020-05-23 15:02:05 -04:00
parent 32aea55f42
commit 7c034f040b
3 changed files with 48 additions and 10 deletions

View File

@ -15,7 +15,7 @@ import eu.kanade.tachiyomi.ui.base.controller.BaseController
import eu.kanade.tachiyomi.ui.category.CategoryPresenter.Companion.CREATE_CATEGORY_ORDER
import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.util.view.scrollViewWith
import eu.kanade.tachiyomi.util.view.liftAppbarWith
import eu.kanade.tachiyomi.util.view.snack
import kotlinx.android.synthetic.main.categories_controller.*
@ -66,7 +66,7 @@ class CategoryController(bundle: Bundle? = null) : BaseController(bundle),
*/
override fun onViewCreated(view: View) {
super.onViewCreated(view)
scrollViewWith(recycler, padBottom = true)
liftAppbarWith(recycler)
adapter = CategoryAdapter(this@CategoryController)
recycler.layoutManager = LinearLayoutManager(view.context)

View File

@ -4,8 +4,10 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.system.executeOnIO
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import uy.kohesive.injekt.Injekt
@ -22,6 +24,8 @@ class CategoryPresenter(
private val context = preferences.context
private var scope = CoroutineScope(Job() + Dispatchers.Default)
/**
* List containing categories.
*/
@ -34,7 +38,7 @@ class CategoryPresenter(
if (categories.isNotEmpty()) {
controller.setCategories(categories.map(::CategoryItem))
}
GlobalScope.launch(Dispatchers.IO) {
scope.launch(Dispatchers.IO) {
categories.clear()
categories.add(newCategory())
categories.addAll(db.getCategories().executeAsBlocking())
@ -98,13 +102,16 @@ class CategoryPresenter(
* @param categories The list of categories to reorder.
*/
fun reorderCategories(categories: List<Category>) {
scope.launch {
categories.forEachIndexed { i, category ->
if (category.order != CREATE_CATEGORY_ORDER)
category.order = i - 1
if (category.order != CREATE_CATEGORY_ORDER) category.order = i - 1
}
db.insertCategories(categories.filter { it.order != CREATE_CATEGORY_ORDER }).executeOnIO()
this@CategoryPresenter.categories = categories.sortedBy { it.order }.toMutableList()
withContext(Dispatchers.Main) {
controller.setCategories(this@CategoryPresenter.categories.map(::CategoryItem))
}
}
db.insertCategories(categories.filter { it.order != CREATE_CATEGORY_ORDER }).executeAsBlocking()
this.categories = categories.sortedBy { it.order }.toMutableList()
controller.setCategories(categories.map(::CategoryItem))
}
/**

View File

@ -55,6 +55,37 @@ fun Controller.setOnQueryTextChangeListener(
})
}
fun Controller.liftAppbarWith(recycler: RecyclerView) {
view?.applyWindowInsetsForController()
recycler.setOnApplyWindowInsetsListener(RecyclerWindowInsetsListener)
var elevationAnim: ValueAnimator? = null
var elevate = false
val elevateFunc: (Boolean) -> Unit = { el ->
elevate = el
elevationAnim?.cancel()
elevationAnim = ValueAnimator.ofFloat(
activity?.appbar?.elevation ?: 0f, if (el) 15f else 0f
)
elevationAnim?.addUpdateListener { valueAnimator ->
activity?.appbar?.elevation = valueAnimator.animatedValue as Float
}
elevationAnim?.start()
}
elevateFunc(recycler.canScrollVertically(-1))
recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (router?.backstack?.lastOrNull()
?.controller() == this@liftAppbarWith && activity != null
) {
val notAtTop = recycler.canScrollVertically(-1)
if (notAtTop != elevate) elevateFunc(notAtTop)
}
}
})
}
fun Controller.scrollViewWith(
recycler: RecyclerView,
padBottom: Boolean = false,