From 7c034f040b298e1bc10cdb30c17ae8cc72eb22e1 Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 23 May 2020 15:02:05 -0400 Subject: [PATCH] Fixed weird behavior with dragging categories --- .../ui/category/CategoryController.kt | 4 +-- .../ui/category/CategoryPresenter.kt | 23 +++++++++----- .../util/view/ControllerExtensions.kt | 31 +++++++++++++++++++ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryController.kt index f743cc2775..0c9b6b2474 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryController.kt @@ -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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt index cf56ffa9eb..e470fc85fe 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt @@ -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) { - categories.forEachIndexed { i, category -> - if (category.order != CREATE_CATEGORY_ORDER) - category.order = i - 1 + scope.launch { + categories.forEachIndexed { i, category -> + 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)) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt index 41cd90baff..5794f95089 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt @@ -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,