Skip to content

Commit

Permalink
fix: adjust layouts for Android 15 window inset changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy-Geenexus committed Jul 21, 2024
1 parent e001f4b commit fe2752c
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

package com.none.tom.exiferaser

import android.os.Build
import android.os.Bundle
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -29,7 +31,6 @@ import androidx.navigation.fragment.findNavController
import androidx.viewbinding.ViewBinding
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.elevation.SurfaceColors
import curtains.phoneWindow

abstract class BaseBottomSheetDialogFragment<B : ViewBinding> : BottomSheetDialogFragment() {
Expand All @@ -46,9 +47,14 @@ abstract class BaseBottomSheetDialogFragment<B : ViewBinding> : BottomSheetDialo
return binding.root
}

@Suppress("DEPRECATION")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.phoneWindow?.navigationBarColor = SurfaceColors.SURFACE_1.getColor(requireActivity())
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
val tv = TypedValue()
requireActivity().theme.resolveAttribute(R.attr.colorSurfaceContainerLow, tv, true)
view.phoneWindow?.navigationBarColor = tv.data
}
with(requireDialog()) {
setOnShowListener {
val v = findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/kotlin/com/none/tom/exiferaser/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ package com.none.tom.exiferaser

import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.navigation.NavDirections
import androidx.navigation.fragment.findNavController
Expand Down Expand Up @@ -66,6 +69,14 @@ abstract class BaseFragment<B : ViewBinding>(
}
}

protected fun setupLayoutMarginForNavigationWindowInsets() {
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { root, windowInsetsCompat ->
val insets = windowInsetsCompat.getInsets(WindowInsetsCompat.Type.navigationBars())
(root.layoutParams as FrameLayout.LayoutParams).bottomMargin = insets.bottom
WindowInsetsCompat.CONSUMED
}
}

protected fun navigate(navDirections: NavDirections) {
val navController = findNavController()
val action = navController.currentDestination?.getAction(navDirections.actionId)
Expand Down
46 changes: 31 additions & 15 deletions app/src/main/kotlin/com/none/tom/exiferaser/ExifEraserActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ package com.none.tom.exiferaser
import android.content.Intent
import android.content.res.Configuration
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.widget.FrameLayout
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.fragment.NavHostFragment
import androidx.window.core.layout.WindowSizeClass
import androidx.window.core.layout.WindowWidthSizeClass
import androidx.window.layout.WindowMetricsCalculator
import com.google.android.material.elevation.SurfaceColors
import com.none.tom.exiferaser.databinding.ActivityExifEraserBinding
import dagger.hilt.android.AndroidEntryPoint

Expand All @@ -58,30 +62,42 @@ class ExifEraserActivity : AppCompatActivity() {
}
}

@Suppress("DEPRECATION")
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
enableEdgeToEdge()
super.onCreate(savedInstanceState)
val binding = ActivityExifEraserBinding.inflate(layoutInflater)
setContentView(binding.root)
computeWindowSizeClasses()
(supportFragmentManager.findFragmentById(R.id.nav_controller) as NavHostFragment?)
?.navController
?.addOnDestinationChangedListener { _, destination, _ ->
if (windowSizeClass.windowWidthSizeClass != WindowWidthSizeClass.EXPANDED) {
if (destination.id == R.id.fragment_main) {
window.navigationBarColor = SurfaceColors.SURFACE_2.getColor(this)
} else {
window.navigationBarColor = SurfaceColors.SURFACE_0.getColor(this)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
(supportFragmentManager.findFragmentById(R.id.nav_controller) as NavHostFragment)
.navController
.addOnDestinationChangedListener { _, destination, _ ->
if (windowSizeClass.windowWidthSizeClass != WindowWidthSizeClass.EXPANDED) {
val tv = TypedValue()
if (destination.id == R.id.fragment_main) {
theme.resolveAttribute(R.attr.colorSurfaceContainer, tv, true)
} else {
theme.resolveAttribute(R.attr.colorSurface, tv, true)
}
window.navigationBarColor = tv.data
}
}
}
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsetsCompat ->
val insets = windowInsetsCompat.getInsets(WindowInsetsCompat.Type.statusBars())
(view.layoutParams as FrameLayout.LayoutParams).topMargin = insets.top
windowInsetsCompat
}
binding.layout.addView(
object : View(this) {
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
computeWindowSizeClasses()
}
}
binding.layout.addView(object : View(this) {
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
computeWindowSizeClasses()
}
})
)
handleSupportedIntent()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import com.none.tom.exiferaser.main.data.supportedImageFormats
import com.none.tom.exiferaser.suspendRunCatching
import dagger.hilt.android.qualifiers.ApplicationContext
import io.github.tommygeenexus.exifinterfaceextended.ExifInterfaceExtended
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.buffer
Expand All @@ -45,9 +48,6 @@ import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.withContext
import org.apache.commons.io.FilenameUtils
import timber.log.Timber
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ImageProcessingRepository @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class ImageProcessingFragment : BaseFragment<FragmentImageProcessingBinding>(
toolbar = binding.toolbarInclude.toolbar,
titleRes = R.string.summary
)
setupLayoutMarginForNavigationWindowInsets()
setupResponsiveLayout()
lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,19 @@ class HelpViewHolder(
private fun getBuildVersion(): String {
val context = itemView.context
return try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.packageManager.getPackageInfo(
context.packageName,
PackageManager.PackageInfoFlags.of(0)
).versionName
} else {
context.packageManager.getPackageInfo(context.packageName, 0).versionName
}
checkNotNull(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context
.packageManager
.getPackageInfo(
context.packageName,
PackageManager.PackageInfoFlags.of(0)
)
.versionName
} else {
context.packageManager.getPackageInfo(context.packageName, 0).versionName
}
)
} catch (e: Exception) {
Timber.e(e)
""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class SettingsFragment :
toolbar = binding.toolbarInclude.toolbar,
titleRes = R.string.settings
)
setupLayoutMarginForNavigationWindowInsets()
binding.preferences.apply {
layoutManager = LinearLayoutManager(context)
adapter = SettingsAdapter(listener = this@SettingsFragment)
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/layout-sw600dp/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:transitionGroup="true">

Expand Down Expand Up @@ -59,8 +58,7 @@
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:paddingBottomSystemWindowInsets="false" />
android:layout_height="wrap_content" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/image_sources_reorder"
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/fragment_help.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:transitionGroup="true">

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/fragment_image_processing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
android:layout_height="match_parent">

<include
android:id="@+id/toolbar_include"
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:transitionGroup="true">

Expand Down Expand Up @@ -59,8 +58,7 @@
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:paddingBottomSystemWindowInsets="false" />
android:layout_height="wrap_content" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/image_sources_reorder"
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:transitionGroup="true">

Expand Down

0 comments on commit fe2752c

Please sign in to comment.