From 89b6bbf6fd4b4ff40c196be89a40720686850290 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 19 Dec 2024 18:27:16 +0200 Subject: [PATCH] Fix edge-to-edge ui --- .../workinspector/WorkInspectorActivity.kt | 14 -------- .../ui/details/WorkDetailsFragment.kt | 32 +++++++++++++++---- .../ui/workers/WorkersListFragment.kt | 32 +++++++++++++++---- .../res/layout/wi_activity_work_inspector.xml | 3 +- 4 files changed, 54 insertions(+), 27 deletions(-) diff --git a/workinspector/src/main/java/org/koitharu/workinspector/WorkInspectorActivity.kt b/workinspector/src/main/java/org/koitharu/workinspector/WorkInspectorActivity.kt index c9f3c5e..9f72ef9 100644 --- a/workinspector/src/main/java/org/koitharu/workinspector/WorkInspectorActivity.kt +++ b/workinspector/src/main/java/org/koitharu/workinspector/WorkInspectorActivity.kt @@ -2,12 +2,8 @@ package org.koitharu.workinspector import android.os.Bundle import android.view.MenuItem -import android.view.ViewGroup.MarginLayoutParams import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity -import androidx.core.view.ViewCompat -import androidx.core.view.WindowInsetsCompat -import androidx.core.view.updateLayoutParams import org.koitharu.workinspector.databinding.WiActivityWorkInspectorBinding public class WorkInspectorActivity : AppCompatActivity() { @@ -18,16 +14,6 @@ public class WorkInspectorActivity : AppCompatActivity() { super.onCreate(savedInstanceState) binding = WiActivityWorkInspectorBinding.inflate(layoutInflater) setContentView(binding.root) - ViewCompat.setOnApplyWindowInsetsListener(binding.appbar) { v, windowInsets -> - val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) - v.updateLayoutParams { - leftMargin = insets.left - bottomMargin = insets.bottom - rightMargin = insets.right - topMargin = insets.top - } - WindowInsetsCompat.CONSUMED - } setSupportActionBar(binding.toolbar) supportActionBar?.setDisplayHomeAsUpEnabled(true) } diff --git a/workinspector/src/main/java/org/koitharu/workinspector/ui/details/WorkDetailsFragment.kt b/workinspector/src/main/java/org/koitharu/workinspector/ui/details/WorkDetailsFragment.kt index 771ab2d..3dd68a4 100644 --- a/workinspector/src/main/java/org/koitharu/workinspector/ui/details/WorkDetailsFragment.kt +++ b/workinspector/src/main/java/org/koitharu/workinspector/ui/details/WorkDetailsFragment.kt @@ -2,7 +2,11 @@ package org.koitharu.workinspector.ui.details import android.os.Bundle import android.view.View +import androidx.core.view.OnApplyWindowInsetsListener +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat import androidx.core.view.isVisible +import androidx.core.view.updatePadding import androidx.fragment.app.Fragment import org.koin.androidx.viewmodel.ext.android.viewModel import org.koitharu.workinspector.IsolatedKoinComponent @@ -12,7 +16,8 @@ import org.koitharu.workinspector.ui.util.collectInLifecycle import org.koitharu.workinspector.ui.util.showErrorDialog internal class WorkDetailsFragment : Fragment(R.layout.wi_fragment_work_list), - IsolatedKoinComponent { + IsolatedKoinComponent, + OnApplyWindowInsetsListener { private val viewModel by viewModel() private var binding: WiFragmentWorkListBinding? = null @@ -23,11 +28,11 @@ internal class WorkDetailsFragment : Fragment(R.layout.wi_fragment_work_list), ) { super.onViewCreated(view, savedInstanceState) val listAdapter = WorkDetailsAdapter() - binding = - WiFragmentWorkListBinding.bind(view).apply { - recyclerView.setHasFixedSize(true) - recyclerView.adapter = listAdapter - } + binding = WiFragmentWorkListBinding.bind(view).apply { + ViewCompat.setOnApplyWindowInsetsListener(root, this@WorkDetailsFragment) + recyclerView.setHasFixedSize(true) + recyclerView.adapter = listAdapter + } viewModel.workers.collectInLifecycle(viewLifecycleOwner) { listAdapter.submitList(it) binding?.textViewHolder?.isVisible = it.isEmpty() @@ -37,6 +42,21 @@ internal class WorkDetailsFragment : Fragment(R.layout.wi_fragment_work_list), } } + override fun onApplyWindowInsets( + v: View, + insets: WindowInsetsCompat, + ): WindowInsetsCompat { + binding?.recyclerView?.run { + val systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()) + updatePadding( + left = systemBarsInsets.left, + right = systemBarsInsets.right, + bottom = systemBarsInsets.bottom + paddingTop, + ) + } + return WindowInsetsCompat.CONSUMED + } + override fun onDestroyView() { binding = null super.onDestroyView() diff --git a/workinspector/src/main/java/org/koitharu/workinspector/ui/workers/WorkersListFragment.kt b/workinspector/src/main/java/org/koitharu/workinspector/ui/workers/WorkersListFragment.kt index e7b397d..04c6f5d 100644 --- a/workinspector/src/main/java/org/koitharu/workinspector/ui/workers/WorkersListFragment.kt +++ b/workinspector/src/main/java/org/koitharu/workinspector/ui/workers/WorkersListFragment.kt @@ -2,7 +2,11 @@ package org.koitharu.workinspector.ui.workers import android.os.Bundle import android.view.View +import androidx.core.view.OnApplyWindowInsetsListener +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat import androidx.core.view.isVisible +import androidx.core.view.updatePadding import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentTransaction import androidx.fragment.app.commit @@ -16,7 +20,8 @@ import org.koitharu.workinspector.ui.util.showErrorDialog internal class WorkersListFragment : Fragment(R.layout.wi_fragment_work_list), IsolatedKoinComponent, - OnWorkerClickListener { + OnWorkerClickListener, + OnApplyWindowInsetsListener { private val viewModel by viewModel() private var binding: WiFragmentWorkListBinding? = null @@ -27,11 +32,11 @@ internal class WorkersListFragment : Fragment(R.layout.wi_fragment_work_list), ) { super.onViewCreated(view, savedInstanceState) val listAdapter = WorkersAdapter(this) - binding = - WiFragmentWorkListBinding.bind(view).apply { - recyclerView.setHasFixedSize(true) - recyclerView.adapter = listAdapter - } + binding = WiFragmentWorkListBinding.bind(view).apply { + ViewCompat.setOnApplyWindowInsetsListener(root, this@WorkersListFragment) + recyclerView.setHasFixedSize(true) + recyclerView.adapter = listAdapter + } viewModel.workers.collectInLifecycle(viewLifecycleOwner) { listAdapter.submitList(it) binding?.textViewHolder?.isVisible = it.isEmpty() @@ -41,6 +46,21 @@ internal class WorkersListFragment : Fragment(R.layout.wi_fragment_work_list), } } + override fun onApplyWindowInsets( + v: View, + insets: WindowInsetsCompat, + ): WindowInsetsCompat { + binding?.recyclerView?.run { + val systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()) + updatePadding( + left = systemBarsInsets.left, + right = systemBarsInsets.right, + bottom = systemBarsInsets.bottom + paddingTop, + ) + } + return WindowInsetsCompat.CONSUMED + } + override fun onDestroyView() { binding = null super.onDestroyView() diff --git a/workinspector/src/main/res/layout/wi_activity_work_inspector.xml b/workinspector/src/main/res/layout/wi_activity_work_inspector.xml index 3ad41ee..e45f587 100644 --- a/workinspector/src/main/res/layout/wi_activity_work_inspector.xml +++ b/workinspector/src/main/res/layout/wi_activity_work_inspector.xml @@ -7,7 +7,8 @@ + android:layout_height="wrap_content" + android:fitsSystemWindows="true">