From 1d7c692e89120856d9e65d3e39c0248e3a04c9f6 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 22 Nov 2020 22:30:25 -0500 Subject: [PATCH] Create extension function for applying system insets --- .../java/eu/kanade/tachiyomi/ui/main/MainActivity.kt | 11 +++-------- .../tachiyomi/ui/recent/history/HistoryController.kt | 5 ----- .../tachiyomi/ui/recent/updates/UpdatesController.kt | 4 ---- .../eu/kanade/tachiyomi/util/view/ViewExtensions.kt | 11 +++++++++++ 4 files changed, 14 insertions(+), 17 deletions(-) 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 d3174d6495..6947da2c8a 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 @@ -9,8 +9,6 @@ import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.coordinatorlayout.widget.CoordinatorLayout -import androidx.core.view.ViewCompat -import androidx.core.view.WindowInsetsCompat import androidx.core.view.isVisible import androidx.core.view.marginBottom import androidx.core.view.updateLayoutParams @@ -49,6 +47,7 @@ import eu.kanade.tachiyomi.ui.recent.history.HistoryController import eu.kanade.tachiyomi.ui.recent.updates.UpdatesController import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchUI +import eu.kanade.tachiyomi.util.view.applyInsets import kotlinx.android.synthetic.main.main_activity.appbar import kotlinx.android.synthetic.main.main_activity.tabs import kotlinx.coroutines.delay @@ -92,19 +91,15 @@ class MainActivity : BaseActivity() { // Inset paddings when drawing edge-to-edge in Android 9+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - ViewCompat.setOnApplyWindowInsetsListener(binding.bottomNav) { view, insets -> - val systemInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()) + binding.bottomNav.applyInsets { view, systemInsets -> view.updatePadding(bottom = systemInsets.bottom) - insets } val initialFabBottomMargin = binding.rootFab.marginBottom - ViewCompat.setOnApplyWindowInsetsListener(binding.rootFab) { view, insets -> - val systemInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()) + binding.rootFab.applyInsets { view, systemInsets -> view.updateLayoutParams { bottomMargin = initialFabBottomMargin + systemInsets.bottom } - insets } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/history/HistoryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/history/HistoryController.kt index e6378176d5..57dd831591 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/history/HistoryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/history/HistoryController.kt @@ -71,11 +71,6 @@ class HistoryController : return binding.root } - /** - * Called when view is created - * - * @param view created view - */ override fun onViewCreated(view: View) { super.onViewCreated(view) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt index 4e69be6287..c38af60639 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt @@ -77,10 +77,6 @@ class UpdatesController : return binding.root } - /** - * Called when view is created - * @param view created view - */ override fun onViewCreated(view: View) { super.onViewCreated(view) view.context.notificationManager.cancel(Notifications.ID_NEW_CHAPTERS) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt index c50f111bf7..8efd8e5369 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt @@ -11,6 +11,9 @@ import androidx.annotation.MenuRes import androidx.annotation.StringRes import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.TooltipCompat +import androidx.core.graphics.Insets +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat import androidx.recyclerview.widget.RecyclerView import com.google.android.material.chip.Chip import com.google.android.material.chip.ChipGroup @@ -117,3 +120,11 @@ inline fun ChipGroup.setChips( addView(chip) } } + +inline fun View.applyInsets(noinline block: (view: View, systemInsets: Insets) -> Unit) { + ViewCompat.setOnApplyWindowInsetsListener(this) { view, insets -> + val systemInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()) + block(view, systemInsets) + insets + } +}