From e7165d993f9bc9b234acada275f0aa991bc90083 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Mon, 20 May 2024 09:32:07 -0500 Subject: [PATCH] Make compatible with edge-to-edge (API 35) --- uhabits-android/build.gradle.kts | 2 ++ .../uhabits/activities/about/AboutView.kt | 2 ++ .../habits/edit/EditHabitActivity.kt | 9 ++++---- .../habits/list/ListHabitsActivity.kt | 2 ++ .../habits/show/ShowHabitActivity.kt | 2 ++ .../activities/settings/SettingsActivity.kt | 2 ++ .../uhabits/automation/EditSettingActivity.kt | 2 ++ .../isoron/uhabits/utils/ViewExtensions.kt | 22 +++++++++++++++++-- .../src/main/res/values/styles.xml | 2 +- 9 files changed, 38 insertions(+), 7 deletions(-) diff --git a/uhabits-android/build.gradle.kts b/uhabits-android/build.gradle.kts index 33bac5f0b..894c0b4ec 100644 --- a/uhabits-android/build.gradle.kts +++ b/uhabits-android/build.gradle.kts @@ -44,12 +44,14 @@ android { namespace = "org.isoron.uhabits" compileSdk = 34 + // compileSdkPreview = "VanillaIceCream" defaultConfig { versionCode = 20200 versionName = "2.2.0" minSdk = 28 targetSdk = 34 + // targetSdkPreview = "VanillaIceCream" applicationId = "org.isoron.uhabits" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/about/AboutView.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/about/AboutView.kt index bc52f6fc0..27a01225a 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/about/AboutView.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/about/AboutView.kt @@ -26,6 +26,7 @@ import org.isoron.uhabits.BuildConfig import org.isoron.uhabits.R import org.isoron.uhabits.core.models.PaletteColor import org.isoron.uhabits.databinding.AboutBinding +import org.isoron.uhabits.utils.applyRootViewInsets import org.isoron.uhabits.utils.currentTheme import org.isoron.uhabits.utils.setupToolbar @@ -54,5 +55,6 @@ class AboutView( binding.tvTranslate.setOnClickListener { screen.showTranslationWebsite() } binding.tvVersion.setOnClickListener { screen.onPressDeveloperCountdown() } binding.tvVersion.text = String.format(version, BuildConfig.VERSION_NAME) + applyRootViewInsets() } } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/EditHabitActivity.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/EditHabitActivity.kt index a2f329d71..5479ab78c 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/EditHabitActivity.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/edit/EditHabitActivity.kt @@ -22,7 +22,6 @@ package org.isoron.uhabits.activities.habits.edit import android.annotation.SuppressLint import android.content.res.ColorStateList import android.content.res.Resources -import android.graphics.Color import android.os.Bundle import android.text.Html import android.text.Spanned @@ -53,7 +52,8 @@ import org.isoron.uhabits.core.models.PaletteColor import org.isoron.uhabits.core.models.Reminder import org.isoron.uhabits.core.models.WeekdayList import org.isoron.uhabits.databinding.ActivityEditHabitBinding -import org.isoron.uhabits.utils.ColorUtils +import org.isoron.uhabits.utils.applyRootViewInsets +import org.isoron.uhabits.utils.applyToolbarInsets import org.isoron.uhabits.utils.dismissCurrentAndShow import org.isoron.uhabits.utils.formatTime import org.isoron.uhabits.utils.toFormattedString @@ -94,6 +94,8 @@ class EditHabitActivity : AppCompatActivity() { themeSwitcher.apply() binding = ActivityEditHabitBinding.inflate(layoutInflater) + binding.root.applyRootViewInsets() + binding.toolbar.applyToolbarInsets() setContentView(binding.root) if (intent.hasExtra("habitId")) { @@ -352,8 +354,7 @@ class EditHabitActivity : AppCompatActivity() { androidColor = themeSwitcher.currentTheme.color(color).toInt() binding.colorButton.backgroundTintList = ColorStateList.valueOf(androidColor) if (!themeSwitcher.isNightMode) { - val darkerAndroidColor = ColorUtils.mixColors(Color.BLACK, androidColor, 0.15f) - window.statusBarColor = darkerAndroidColor + window.statusBarColor = androidColor binding.toolbar.setBackgroundColor(androidColor) } } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsActivity.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsActivity.kt index 21936d4ea..d16f7d702 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsActivity.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsActivity.kt @@ -45,6 +45,7 @@ import org.isoron.uhabits.inject.ActivityContextModule import org.isoron.uhabits.inject.DaggerHabitsActivityComponent import org.isoron.uhabits.inject.HabitsActivityComponent import org.isoron.uhabits.inject.HabitsApplicationComponent +import org.isoron.uhabits.utils.applyRootViewInsets import org.isoron.uhabits.utils.dismissCurrentDialog import org.isoron.uhabits.utils.restartWithFade @@ -100,6 +101,7 @@ class ListHabitsActivity : AppCompatActivity(), Preferences.Listener { menu = component.listHabitsMenu Thread.setDefaultUncaughtExceptionHandler(BaseExceptionHandler(this)) component.listHabitsBehavior.onStartup() + rootView.applyRootViewInsets() setContentView(rootView) } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.kt index 14baee148..a7d07db53 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.kt @@ -49,6 +49,7 @@ import org.isoron.uhabits.core.ui.screens.habits.show.ShowHabitMenuPresenter import org.isoron.uhabits.core.ui.screens.habits.show.ShowHabitPresenter import org.isoron.uhabits.core.ui.views.OnDateClickedListener import org.isoron.uhabits.intents.IntentFactory +import org.isoron.uhabits.utils.applyRootViewInsets import org.isoron.uhabits.utils.currentTheme import org.isoron.uhabits.utils.dismissCurrentAndShow import org.isoron.uhabits.utils.dismissCurrentDialog @@ -109,6 +110,7 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener { ) view.setListener(presenter) + view.applyRootViewInsets() setContentView(view) } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/settings/SettingsActivity.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/settings/SettingsActivity.kt index 0d5e25224..684310cb6 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/settings/SettingsActivity.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/settings/SettingsActivity.kt @@ -26,6 +26,7 @@ import org.isoron.uhabits.R import org.isoron.uhabits.activities.AndroidThemeSwitcher import org.isoron.uhabits.core.models.PaletteColor import org.isoron.uhabits.databinding.SettingsActivityBinding +import org.isoron.uhabits.utils.applyRootViewInsets import org.isoron.uhabits.utils.setupToolbar class SettingsActivity : AppCompatActivity() { @@ -42,6 +43,7 @@ class SettingsActivity : AppCompatActivity() { color = PaletteColor(11), theme = themeSwitcher.currentTheme ) + binding.root.applyRootViewInsets() setContentView(binding.root) } } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.kt b/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.kt index 55468cfda..aea60a099 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.kt @@ -24,6 +24,7 @@ import androidx.appcompat.app.AppCompatActivity import org.isoron.uhabits.HabitsApplication import org.isoron.uhabits.activities.AndroidThemeSwitcher import org.isoron.uhabits.core.models.HabitMatcher +import org.isoron.uhabits.utils.applyRootViewInsets class EditSettingActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -45,6 +46,7 @@ class EditSettingActivity : AppCompatActivity() { onSave = controller::onSave, args = args ) + view.applyRootViewInsets() setContentView(view) } } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/utils/ViewExtensions.kt b/uhabits-android/src/main/java/org/isoron/uhabits/utils/ViewExtensions.kt index 3c92e7e1b..4da816dbb 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/utils/ViewExtensions.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/utils/ViewExtensions.kt @@ -46,6 +46,8 @@ import androidx.annotation.StringRes import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar import androidx.core.content.FileProvider +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat import com.google.android.material.snackbar.Snackbar import org.isoron.platform.gui.toInt import org.isoron.uhabits.HabitsApplication @@ -180,10 +182,10 @@ fun View.setupToolbar( } else { theme.color(color).toInt() } - val darkerColor = ColorUtils.mixColors(toolbarColor, Color.BLACK, 0.75f) toolbar.background = ColorDrawable(toolbarColor) + toolbar.applyToolbarInsets() val activity = context as AppCompatActivity - activity.window.statusBarColor = darkerColor + activity.window.statusBarColor = toolbarColor activity.setSupportActionBar(toolbar) activity.supportActionBar?.setDisplayHomeAsUpEnabled(displayHomeAsUpEnabled) } @@ -245,3 +247,19 @@ fun View.getCenter(): PointF { viewLocation[1] -= this.height / 2 return PointF(viewLocation[0].toFloat(), viewLocation[1].toFloat()) } + +fun View.applyRootViewInsets() { + ViewCompat.setOnApplyWindowInsetsListener(this) { view, insets -> + val systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()) + view.setPadding(systemBarsInsets.left, 0, systemBarsInsets.right, systemBarsInsets.bottom) + insets + } +} + +fun View.applyToolbarInsets() { + ViewCompat.setOnApplyWindowInsetsListener(this) { view, insets -> + val systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()) + view.setPadding(0, systemBarsInsets.top, 0, 0) + insets + } +} diff --git a/uhabits-android/src/main/res/values/styles.xml b/uhabits-android/src/main/res/values/styles.xml index 87d053fe9..a06043963 100644 --- a/uhabits-android/src/main/res/values/styles.xml +++ b/uhabits-android/src/main/res/values/styles.xml @@ -243,7 +243,7 @@