Create extension function for applying system insets

This commit is contained in:
arkon 2020-11-22 22:30:25 -05:00
parent 38bc8ec6b4
commit 1d7c692e89
4 changed files with 14 additions and 17 deletions

View file

@ -9,8 +9,6 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.core.view.marginBottom import androidx.core.view.marginBottom
import androidx.core.view.updateLayoutParams 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.ui.recent.updates.UpdatesController
import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.launchUI 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.appbar
import kotlinx.android.synthetic.main.main_activity.tabs import kotlinx.android.synthetic.main.main_activity.tabs
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
@ -92,19 +91,15 @@ class MainActivity : BaseActivity<MainActivityBinding>() {
// Inset paddings when drawing edge-to-edge in Android 9+ // Inset paddings when drawing edge-to-edge in Android 9+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ViewCompat.setOnApplyWindowInsetsListener(binding.bottomNav) { view, insets -> binding.bottomNav.applyInsets { view, systemInsets ->
val systemInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
view.updatePadding(bottom = systemInsets.bottom) view.updatePadding(bottom = systemInsets.bottom)
insets
} }
val initialFabBottomMargin = binding.rootFab.marginBottom val initialFabBottomMargin = binding.rootFab.marginBottom
ViewCompat.setOnApplyWindowInsetsListener(binding.rootFab) { view, insets -> binding.rootFab.applyInsets { view, systemInsets ->
val systemInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
view.updateLayoutParams<ViewGroup.MarginLayoutParams> { view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = initialFabBottomMargin + systemInsets.bottom bottomMargin = initialFabBottomMargin + systemInsets.bottom
} }
insets
} }
} }

View file

@ -71,11 +71,6 @@ class HistoryController :
return binding.root return binding.root
} }
/**
* Called when view is created
*
* @param view created view
*/
override fun onViewCreated(view: View) { override fun onViewCreated(view: View) {
super.onViewCreated(view) super.onViewCreated(view)

View file

@ -77,10 +77,6 @@ class UpdatesController :
return binding.root return binding.root
} }
/**
* Called when view is created
* @param view created view
*/
override fun onViewCreated(view: View) { override fun onViewCreated(view: View) {
super.onViewCreated(view) super.onViewCreated(view)
view.context.notificationManager.cancel(Notifications.ID_NEW_CHAPTERS) view.context.notificationManager.cancel(Notifications.ID_NEW_CHAPTERS)

View file

@ -11,6 +11,9 @@ import androidx.annotation.MenuRes
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.TooltipCompat 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 androidx.recyclerview.widget.RecyclerView
import com.google.android.material.chip.Chip import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup import com.google.android.material.chip.ChipGroup
@ -117,3 +120,11 @@ inline fun ChipGroup.setChips(
addView(chip) 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
}
}