From d719cae942afd5dd650b6cecd85642890cb097a9 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 30 Oct 2019 01:19:30 -0700 Subject: [PATCH] More snack --- .../kanade/tachiyomi/data/cache/CoverCache.kt | 12 +++---- .../browse/BrowseCatalogueController.kt | 2 +- .../ui/library/LibraryCategoryView.kt | 2 +- .../tachiyomi/ui/library/LibraryController.kt | 7 ---- .../kanade/tachiyomi/ui/main/MainActivity.kt | 7 +++- .../ui/manga/chapter/ChaptersController.kt | 35 +++++++++++++++++-- .../ui/manga/info/MangaInfoController.kt | 2 +- .../RecentChaptersController.kt | 5 +-- .../kanade/tachiyomi/util/ViewExtensions.kt | 30 ++++++++++++---- .../tachiyomi/widget/FABAnimationUpDown.kt | 33 ++++++++++++++++- .../main/res/layout/chapters_controller.xml | 6 ++-- 11 files changed, 110 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt index 15d0f9cc04..520ce8b2b0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt @@ -79,14 +79,12 @@ class CoverCache(private val context: Context) { // Check if url is empty. if (thumbnailUrl.isNullOrEmpty()) return launchUI { - if (delayBy > 0) { - delay(delayBy) - if (manga.favorite) cancel() + delay(delayBy) + if (!manga.favorite) { + // Remove file. + val file = getCoverFile(thumbnailUrl) + if (file.exists()) file.delete() } - // Remove file. - val file = getCoverFile(thumbnailUrl) - if (file.exists()) - file.delete() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt index 81e4538d18..1c7f81b800 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt @@ -516,7 +516,7 @@ open class BrowseCatalogueController(bundle: Bundle) : } else { addManga(manga, position) snack = - catalogue_view?.snack(activity.getString(R.string.manga_added_library), Snackbar.LENGTH_SHORT) + catalogue_view?.snack(activity.getString(R.string.manga_added_library)) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt index d451da811f..5356f40e6a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt @@ -95,7 +95,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att swipe_refresh.setOnRefreshListener { if (!LibraryUpdateService.isRunning(context)) { LibraryUpdateService.start(context, category) - context.toast(R.string.updating_category) + swipe_refresh.snack(R.string.updating_category) } // It can be a very long operation, so we disable swipe refresh and show a toast. swipe_refresh.isRefreshing = false diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index e56b8bd484..80cc8ead0f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -157,13 +157,6 @@ class LibraryController( if (selectedMangas.isNotEmpty()) { createActionModeIfNeeded() } - view.doOnApplyWindowInsets { view, insets, psdding -> - val contextView = activity?.window?.decorView?.findViewById(R.id.action_mode_bar) - if (Build.VERSION.SDK_INT >= 23) contextView?.updateLayoutParams { - leftMargin = view.rootWindowInsets.systemWindowInsetLeft - rightMargin = view.rootWindowInsets.systemWindowInsetRight - } - } } override fun onChangeStarted(handler: ControllerChangeHandler, type: ControllerChangeType) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 6437074f7c..1586ab399d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -44,6 +44,7 @@ import eu.kanade.tachiyomi.util.doOnApplyWindowInsets import eu.kanade.tachiyomi.util.marginBottom import eu.kanade.tachiyomi.util.marginTop import eu.kanade.tachiyomi.util.openInBrowser +import eu.kanade.tachiyomi.util.updateLayoutParams import eu.kanade.tachiyomi.util.updatePadding import eu.kanade.tachiyomi.util.updatePaddingRelative import kotlinx.android.synthetic.main.chapters_controller.view.* @@ -153,7 +154,11 @@ class MainActivity : BaseActivity() { val drawerContainer: FrameLayout = findViewById(R.id.drawer_container) drawerContainer.setOnApplyWindowInsetsListener { v, insets -> - + val contextView = window?.decorView?.findViewById(R.id.action_mode_bar) + contextView?.updateLayoutParams { + leftMargin = insets.systemWindowInsetLeft + rightMargin = insets.systemWindowInsetRight + } // Consume any horizontal insets and pad all content in. There's not much we can do // with horizontal insets v.updatePadding( diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt index 0ed33b903a..e397296ba0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt @@ -5,12 +5,15 @@ import android.animation.AnimatorListenerAdapter import android.annotation.SuppressLint import android.app.Activity import android.content.Intent +import android.os.Build import com.google.android.material.snackbar.Snackbar import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.ActionMode import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import android.view.* +import androidx.coordinatorlayout.widget.CoordinatorLayout +import com.google.android.material.snackbar.BaseTransientBottomBar import com.jakewharton.rxbinding.support.v4.widget.refreshes import com.jakewharton.rxbinding.view.clicks import eu.davidea.flexibleadapter.FlexibleAdapter @@ -26,6 +29,11 @@ import eu.kanade.tachiyomi.ui.reader.ReaderActivity import eu.kanade.tachiyomi.util.* import kotlinx.android.synthetic.main.chapters_controller.* import timber.log.Timber +import com.google.android.material.floatingactionbutton.FloatingActionButton +import android.content.Context +import android.util.AttributeSet +import androidx.core.view.ViewCompat +import kotlin.math.* class ChaptersController : NucleusController(), ActionMode.Callback, @@ -48,6 +56,7 @@ class ChaptersController : NucleusController(), */ private var actionMode: ActionMode? = null + private var snack:Snackbar? = null /** * Selected items. Used to restore selections after a rotation. */ @@ -83,6 +92,11 @@ class ChaptersController : NucleusController(), val fabBaseMarginBottom = fab?.marginBottom ?: 0 recycler.doOnApplyWindowInsets { v, insets, padding -> v.updatePaddingRelative(bottom = padding.bottom + insets.systemWindowInsetBottom) + //if (snack == null) { + //fab?.translationY = -insets.systemWindowInsetBottom.toFloat() + /*} + else + fab?.translationY = 0f*/ fab?.updateLayoutParams { bottomMargin = fabBaseMarginBottom + insets.systemWindowInsetBottom } @@ -108,7 +122,14 @@ class ChaptersController : NucleusController(), openChapter(item.chapter) } } else { - view.context.toast(R.string.no_next_chapter) + snack = view.snack(R.string.no_next_chapter, Snackbar.LENGTH_LONG) { + addCallback(object : BaseTransientBottomBar.BaseCallback() { + override fun onDismissed(transientBottomBar: Snackbar?, event: Int) { + super.onDismissed(transientBottomBar, event) + if (snack == transientBottomBar) snack = null + } + }) + } } } } @@ -373,11 +394,17 @@ class ChaptersController : NucleusController(), destroyActionModeIfNeeded() presenter.downloadChapters(chapters) if (view != null && !presenter.manga.favorite) { - val snack = view.snack(view.context.getString(R.string.snack_add_to_library), Snackbar + snack = view.snack(view.context.getString(R.string.snack_add_to_library), Snackbar .LENGTH_INDEFINITE) { setAction(R.string.action_add) { presenter.addToLibrary() } + addCallback(object : BaseTransientBottomBar.BaseCallback() { + override fun onDismissed(transientBottomBar: Snackbar?, event: Int) { + super.onDismissed(transientBottomBar, event) + if (snack == transientBottomBar) snack = null + } + }) } } } @@ -493,3 +520,7 @@ class ChaptersController : NucleusController(), } } } + +class ShrinkBehavior(context: Context, attrs: AttributeSet) : + CoordinatorLayout.Behavior(context, attrs) { +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt index efc78efea8..7ebb33c770 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt @@ -421,7 +421,7 @@ class MangaInfoController : NucleusController(), private fun showAddedSnack() { val view = container snack?.dismiss() - snack = view?.snack(view.context.getString(R.string.manga_added_library), Snackbar.LENGTH_SHORT) + snack = view?.snack(view.context.getString(R.string.manga_added_library)) } private fun showRemovedSnack() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt index 4cf2cea26b..03c25a5563 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt @@ -20,6 +20,7 @@ import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.ui.reader.ReaderActivity import eu.kanade.tachiyomi.util.RecyclerWindowInsetsListener +import eu.kanade.tachiyomi.util.snack import eu.kanade.tachiyomi.util.toast import kotlinx.android.synthetic.main.recent_chapters_controller.* import timber.log.Timber @@ -86,9 +87,9 @@ class RecentChaptersController : NucleusController(), swipe_refresh.refreshes().subscribeUntilDestroy { if (!LibraryUpdateService.isRunning(view.context)) { LibraryUpdateService.start(view.context) - view.context.toast(R.string.action_update_library) + view.snack(R.string.action_update_library) } - // It can be a very long operation, so we disable swipe refresh and show a toast. + // It can be a very long operation, so we disable swipe refresh and show a snackbar. swipe_refresh.isRefreshing = false } recycler.setOnApplyWindowInsetsListener(RecyclerWindowInsetsListener) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ViewExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/ViewExtensions.kt index 7f5def81db..16934c14a0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ViewExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ViewExtensions.kt @@ -17,6 +17,7 @@ import android.view.ViewGroup import android.view.Window import android.view.WindowInsets import android.widget.TextView +import androidx.coordinatorlayout.widget.CoordinatorLayout import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator import eu.kanade.tachiyomi.R @@ -36,28 +37,45 @@ fun View.getCoordinates() = Point((left + right) / 2, (top + bottom) / 2) * @param length the duration of the snack. * @param f a function to execute in the snack, allowing for example to define a custom action. */ -fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG, f: (Snackbar.() -> +fun View.snack(message: String, length: Int = Snackbar.LENGTH_SHORT, f: (Snackbar.() -> Unit)? = null): Snackbar { val snack = Snackbar.make(this, message, length) val textView: TextView = snack.view.findViewById(com.google.android.material.R.id.snackbar_text) textView.setTextColor(context.getResourceColor(android.R.attr.textColorPrimaryInverse)) when { - Build.VERSION.SDK_INT >= 23 -> snack.config(context, rootWindowInsets.systemWindowInsetBottom) + Build.VERSION.SDK_INT >= 23 -> { + val leftM = if (this is CoordinatorLayout) 0 else rootWindowInsets.systemWindowInsetLeft + val rightM = if (this is CoordinatorLayout) 0 + else rootWindowInsets.systemWindowInsetRight + snack.config(context, rootWindowInsets + .systemWindowInsetBottom, rightM, leftM) + } else -> snack.config(context) } if (f != null) { snack.f() } - snack.view.doOnApplyWindowInsets { v, _, padding -> - v.setPadding(padding.left,0,padding.right,0) + snack.view.doOnApplyWindowInsets { v, insets, padding -> + //v.setPadding(padding.left, 0, padding.right, 0) + v.updateLayoutParams { + if (Build.VERSION.SDK_INT < 23) { + bottomMargin = 12 + insets.systemWindowInsetBottom + } + } } snack.show() return snack } -fun Snackbar.config(context: Context, bottomMargin: Int = 0) { +fun View.snack(resource: Int, length: Int = Snackbar.LENGTH_SHORT, f: (Snackbar.() -> +Unit)? = null): Snackbar { + return snack(context.getString(resource), length, f) +} + +fun Snackbar.config(context: Context, bottomMargin: Int = 0, rightMargin: Int = 0, leftMargin: +Int = 0) { val params = this.view.layoutParams as ViewGroup.MarginLayoutParams - params.setMargins(12, 12, 12, 12 + bottomMargin) + params.setMargins(12 + leftMargin, 12, 12 + rightMargin, 12 + bottomMargin) this.view.layoutParams = params this.view.background = context.getDrawable(R.drawable.bg_snackbar) diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/FABAnimationUpDown.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/FABAnimationUpDown.kt index e48c19e72a..c9e3d58a71 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/FABAnimationUpDown.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/FABAnimationUpDown.kt @@ -7,10 +7,14 @@ import android.util.AttributeSet import android.view.View import android.view.animation.Animation import android.view.animation.AnimationUtils +import androidx.coordinatorlayout.widget.CoordinatorLayout +import com.google.android.material.snackbar.Snackbar import eu.kanade.tachiyomi.R +import kotlin.math.min @Suppress("unused", "UNUSED_PARAMETER") -class FABAnimationUpDown @JvmOverloads constructor(ctx: Context, attrs: AttributeSet? = null) : FABAnimationBase() { +class FABAnimationUpDown @JvmOverloads constructor(ctx: Context, attrs: AttributeSet? = null) : + FABAnimationBase() { private val INTERPOLATOR = FastOutSlowInInterpolator() @@ -48,4 +52,31 @@ class FABAnimationUpDown @JvmOverloads constructor(ctx: Context, attrs: Attribut button.visibility = View.VISIBLE button.startAnimation(inAnimation) } + + + override fun layoutDependsOn(parent: CoordinatorLayout, child: FloatingActionButton, dependency: View): Boolean { + return dependency is Snackbar.SnackbarLayout + } + + override fun onDependentViewChanged(parent: CoordinatorLayout, child: FloatingActionButton, dependency: View): Boolean { + val translationY = getFabTranslationYForSnackbar(parent, child) + val percentComplete = -translationY / dependency.height + val scaleFactor = 1 - percentComplete + + child.translationY = -translationY + return false + } + + private fun getFabTranslationYForSnackbar(parent: CoordinatorLayout, fab: + FloatingActionButton): Float { + var minOffset = 0f + val dependencies = parent.getDependencies(fab) + for (i in 0 until dependencies.size) { + val view = dependencies[i] + if (view is Snackbar.SnackbarLayout) { + minOffset = min(minOffset, view.translationY - view.height) + } + } + return minOffset + } } \ No newline at end of file diff --git a/app/src/main/res/layout/chapters_controller.xml b/app/src/main/res/layout/chapters_controller.xml index a9aabd652d..e625e2fbb4 100644 --- a/app/src/main/res/layout/chapters_controller.xml +++ b/app/src/main/res/layout/chapters_controller.xml @@ -5,6 +5,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" + android:clipToPadding="false" android:orientation="vertical"> @@ -20,6 +22,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/toolbar_bottom" + android:clipToPadding="false" android:orientation="vertical"> \ No newline at end of file