Skip to content

Commit

Permalink
Fix edge-to-edge ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Koitharu committed Dec 19, 2024
1 parent c2040e7 commit 89b6bbf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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<MarginLayoutParams> {
leftMargin = insets.left
bottomMargin = insets.bottom
rightMargin = insets.right
topMargin = insets.top
}
WindowInsetsCompat.CONSUMED
}
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<WorkDetailsViewModel>()
private var binding: WiFragmentWorkListBinding? = null
Expand All @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<WorkersListViewModel>()
private var binding: WiFragmentWorkListBinding? = null
Expand All @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
Expand Down

0 comments on commit 89b6bbf

Please sign in to comment.