Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
Updater: Drop fitsSystemWindows, fix fab issue (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsummer233 authored May 1, 2024
1 parent dc17db7 commit cfbb9db
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 36 deletions.
52 changes: 24 additions & 28 deletions app/src/main/kotlin/top/yukonga/update/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.text.InputType
import android.text.TextWatcher
import android.text.method.LinkMovementMethod
import android.view.View.OnFocusChangeListener
import android.view.WindowInsets
import android.view.WindowManager
import android.widget.ArrayAdapter
import android.widget.LinearLayout
Expand Down Expand Up @@ -44,9 +43,10 @@ import top.yukonga.update.logic.utils.AnimUtils.fadInAnimation
import top.yukonga.update.logic.utils.AnimUtils.fadOutAnimation
import top.yukonga.update.logic.utils.AnimUtils.setTextAnimation
import top.yukonga.update.logic.utils.AppUtils
import top.yukonga.update.logic.utils.AppUtils.addInsetsByMargin
import top.yukonga.update.logic.utils.AppUtils.addInsetsByPadding
import top.yukonga.update.logic.utils.AppUtils.dp
import top.yukonga.update.logic.utils.AppUtils.hideKeyBoard
import top.yukonga.update.logic.utils.AppUtils.isLandscape
import top.yukonga.update.logic.utils.AppUtils.json
import top.yukonga.update.logic.utils.AppUtils.setCopyClickListener
import top.yukonga.update.logic.utils.AppUtils.setDownloadClickListener
Expand Down Expand Up @@ -173,8 +173,8 @@ class MainActivity : AppCompatActivity() {
} else {
showStringToast(this@MainActivity, getString(R.string.login_expired_dialog), 0)
activityMainBinding.apply {
topAppBar.menu.findItem(R.id.login).isVisible = true
topAppBar.menu.findItem(R.id.logout).isVisible = false
toolbar.menu.findItem(R.id.login).isVisible = true
toolbar.menu.findItem(R.id.logout).isVisible = false
}
}
}
Expand Down Expand Up @@ -301,11 +301,11 @@ class MainActivity : AppCompatActivity() {
MaterialAlertDialogBuilder(this@MainActivity).apply {
setView(view)
setNegativeButton(getString(R.string.cancel)) { dialog, _ ->
hapticReject(activityMainBinding.topAppBar)
hapticReject(activityMainBinding.toolbar)
dialog.dismiss()
}
setPositiveButton(getString(R.string.login)) { _, _ ->
hapticConfirm(activityMainBinding.topAppBar)
hapticConfirm(activityMainBinding.toolbar)
val global = prefs.getString("global", "") ?: "0"
val savePassword = prefs.getString("save_password", "") ?: "0"
val mInputAccount = inputAccount.text.toString()
Expand All @@ -320,8 +320,8 @@ class MainActivity : AppCompatActivity() {
loginDesc.text = getString(R.string.using_v2)
}
activityMainBinding.apply {
topAppBar.menu.findItem(R.id.login).isVisible = false
topAppBar.menu.findItem(R.id.logout).isVisible = true
toolbar.menu.findItem(R.id.login).isVisible = false
toolbar.menu.findItem(R.id.logout).isVisible = true
}
}
}
Expand All @@ -334,11 +334,11 @@ class MainActivity : AppCompatActivity() {
MaterialAlertDialogBuilder(this@MainActivity).apply {
setTitle(getString(R.string.logout))
setMessage(getString(R.string.logout_desc)).setNegativeButton(getString(R.string.cancel)) { dialog, _ ->
hapticReject(activityMainBinding.topAppBar)
hapticReject(activityMainBinding.toolbar)
dialog.dismiss()
}
setPositiveButton(getString(R.string.confirm)) { _, _ ->
hapticConfirm(activityMainBinding.topAppBar)
hapticConfirm(activityMainBinding.toolbar)
CoroutineScope(Dispatchers.Default).launch {
LoginUtils().logout(this@MainActivity)
withContext(Dispatchers.Main) {
Expand All @@ -348,8 +348,8 @@ class MainActivity : AppCompatActivity() {
loginDesc.text = getString(R.string.login_desc)
}
activityMainBinding.apply {
topAppBar.menu.findItem(R.id.login).isVisible = true
topAppBar.menu.findItem(R.id.logout).isVisible = false
toolbar.menu.findItem(R.id.login).isVisible = true
toolbar.menu.findItem(R.id.logout).isVisible = false
}
}
}
Expand All @@ -368,21 +368,17 @@ class MainActivity : AppCompatActivity() {
}

private fun setupEdgeToEdge() {

// Enable edge to edge
enableEdgeToEdge()
if (AppUtils.atLeast(Build.VERSION_CODES.Q)) window.isNavigationBarContrastEnforced = false
if (AppUtils.atLeast(Build.VERSION_CODES.R)) {
mainContentBinding.downloadInfo.setOnApplyWindowInsetsListener { _, insets ->
mainContentBinding.downloadInfo.layoutParams.apply {
(this as LinearLayout.LayoutParams).bottomMargin =
if (isLandscape()) 32.dp else 24.dp + insets.getInsets(WindowInsets.Type.systemGestures()).bottom
}
insets
}
} else {
mainContentBinding.downloadInfo.layoutParams.apply {
(this as LinearLayout.LayoutParams).bottomMargin = if (isLandscape()) 32.dp else 24.dp + 46.dp
}
}

// Add insets
activityMainBinding.root.addInsetsByPadding(top = true)
activityMainBinding.appBarLayout.addInsetsByPadding(left = true, right = true)
activityMainBinding.implement.addInsetsByMargin(bottom = true, left = true, right = true)
mainContentBinding.scrollView.addInsetsByPadding(left = true, right = true, bottom = true)

}

private fun setupCutoutMode() {
Expand Down Expand Up @@ -531,7 +527,7 @@ class MainActivity : AppCompatActivity() {
}

private fun setupTopAppBar() {
activityMainBinding.topAppBar.apply {
activityMainBinding.toolbar.apply {
setNavigationOnClickListener {
hapticConfirm(this)
showAboutDialog()
Expand Down Expand Up @@ -566,8 +562,8 @@ class MainActivity : AppCompatActivity() {
loginDesc.text = getString(R.string.using_v2)
}
activityMainBinding.apply {
topAppBar.menu.findItem(R.id.login).isVisible = false
topAppBar.menu.findItem(R.id.logout).isVisible = true
toolbar.menu.findItem(R.id.login).isVisible = false
toolbar.menu.findItem(R.id.logout).isVisible = true
}
}
}
Expand Down
95 changes: 95 additions & 0 deletions app/src/main/kotlin/top/yukonga/update/logic/utils/AppUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import android.net.Uri
import android.os.Build
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.view.WindowInsets
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity.INPUT_METHOD_SERVICE
import androidx.core.graphics.Insets
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -86,4 +91,94 @@ object AppUtils {
}
}

fun View.addInsetsByPadding(
top: Boolean = false,
bottom: Boolean = false,
left: Boolean = false,
right: Boolean = false
) {
ViewCompat.setOnApplyWindowInsetsListener(this) { view, insets ->
val inset = Insets.max(
insets.getInsets(WindowInsetsCompat.Type.systemBars()),
insets.getInsets(WindowInsetsCompat.Type.displayCutout())
)
if (top) {
val lastTopPadding = view.getTag(R.id.view_add_insets_padding_top_tag) as? Int ?: 0
val newTopPadding = inset.top
view.setTag(R.id.view_add_insets_padding_top_tag, newTopPadding)
view.updatePadding(top = view.paddingTop - lastTopPadding + newTopPadding)
}
if (bottom) {
val lastBottomPadding =
view.getTag(R.id.view_add_insets_padding_bottom_tag) as? Int ?: 0
val newBottomPadding = inset.bottom
view.setTag(R.id.view_add_insets_padding_bottom_tag, newBottomPadding)
view.updatePadding(bottom = view.paddingBottom - lastBottomPadding + newBottomPadding)
}
if (left) {
val lastLeftPadding = view.getTag(R.id.view_add_insets_padding_left_tag) as? Int ?: 0
val newLeftPadding = inset.left
view.setTag(R.id.view_add_insets_padding_left_tag, newLeftPadding)
view.updatePadding(left = view.paddingLeft - lastLeftPadding + newLeftPadding)
}
if (right) {
val lastRightPadding = view.getTag(R.id.view_add_insets_padding_right_tag) as? Int ?: 0
val newRightPadding = inset.right
view.setTag(R.id.view_add_insets_padding_right_tag, newRightPadding)
view.updatePadding(right = view.paddingRight - lastRightPadding + newRightPadding)
}
return@setOnApplyWindowInsetsListener insets
}
}

fun View.addInsetsByMargin(
top: Boolean = false,
bottom: Boolean = false,
left: Boolean = false,
right: Boolean = false
) {
ViewCompat.setOnApplyWindowInsetsListener(this) { view, insets ->
val inset = Insets.max(
insets.getInsets(WindowInsetsCompat.Type.systemBars()),
insets.getInsets(WindowInsetsCompat.Type.displayCutout())
)
if (top) {
val lastTopMargin = view.getTag(R.id.view_add_insets_margin_top_tag) as? Int ?: 0
val newTopMargin = inset.top
view.setTag(R.id.view_add_insets_margin_top_tag, newTopMargin)
(view.layoutParams as? ViewGroup.MarginLayoutParams)?.let { layoutParams ->
layoutParams.topMargin = layoutParams.topMargin - lastTopMargin + newTopMargin
view.layoutParams = layoutParams
}
}
if (bottom) {
val lastBottomMargin = view.getTag(R.id.view_add_insets_margin_bottom_tag) as? Int ?: 0
val newBottomMargin = inset.bottom
view.setTag(R.id.view_add_insets_margin_bottom_tag, newBottomMargin)
(view.layoutParams as? ViewGroup.MarginLayoutParams)?.let { layoutParams ->
layoutParams.bottomMargin = layoutParams.bottomMargin - lastBottomMargin + newBottomMargin
view.layoutParams = layoutParams
}
}
if (left) {
val lastLeftMargin = view.getTag(R.id.view_add_insets_margin_left_tag) as? Int ?: 0
val newLeftMargin = inset.left
view.setTag(R.id.view_add_insets_margin_left_tag, newLeftMargin)
(view.layoutParams as? ViewGroup.MarginLayoutParams)?.let { layoutParams ->
layoutParams.leftMargin = layoutParams.leftMargin - lastLeftMargin + newLeftMargin
view.layoutParams = layoutParams
}
}
if (right) {
val lastRightMargin = view.getTag(R.id.view_add_insets_margin_right_tag) as? Int ?: 0
val newRightMargin = inset.right
view.setTag(R.id.view_add_insets_margin_right_tag, newRightMargin)
(view.layoutParams as? ViewGroup.MarginLayoutParams)?.let { layoutParams ->
layoutParams.rightMargin = layoutParams.rightMargin - lastRightMargin + newRightMargin
view.layoutParams = layoutParams
}
}
return@setOnApplyWindowInsetsListener insets
}
}
}
6 changes: 3 additions & 3 deletions app/src/main/res/layout-land/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorSurface"
app:liftOnScrollColor="?attr/colorSurface"
app:statusBarScrim="?attr/colorSurface">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="20dp"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout-land/main_content.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<androidx.constraintlayout.widget.ConstraintLayout
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorSurface"
app:liftOnScrollColor="?attr/colorSurface"
app:statusBarScrim="?attr/colorSurface">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="12dp"
Expand Down Expand Up @@ -45,6 +44,6 @@
app:backgroundTint="?attr/colorSecondaryContainer"
app:icon="@drawable/ic_check"
app:iconTint="?attr/colorOnSecondaryContainer"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/layout/main_content.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<item name="view_add_insets_padding_top_tag" type="id" />
<item name="view_add_insets_padding_bottom_tag" type="id" />
<item name="view_add_insets_padding_left_tag" type="id" />
<item name="view_add_insets_padding_right_tag" type="id" />

<item name="view_add_insets_margin_top_tag" type="id" />
<item name="view_add_insets_margin_bottom_tag" type="id" />
<item name="view_add_insets_margin_left_tag" type="id" />
<item name="view_add_insets_margin_right_tag" type="id" />

</resources>

0 comments on commit cfbb9db

Please sign in to comment.