Browse Source

Create extension function for applying system insets

arkon 4 years ago
parent
commit
1d7c692e89

+ 3 - 8
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<MainActivityBinding>() {
 
         // 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<ViewGroup.MarginLayoutParams> {
                     bottomMargin = initialFabBottomMargin + systemInsets.bottom
                 }
-                insets
             }
         }
 

+ 0 - 5
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)
 

+ 0 - 4
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)

+ 11 - 0
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
+    }
+}