home: rework fab adaptiveness

Rework how the fab's adaptive functionality works.

Handle fab adaptiveness by using a style instead of a fully custom
view. This helps eliminate a usage of private resources.
This commit is contained in:
OxygenCobalt 2022-02-23 16:13:59 -07:00
parent 87035805d2
commit 280b582efa
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
8 changed files with 44 additions and 58 deletions

View file

@ -1,40 +0,0 @@
package org.oxycblt.auxio.home
import android.content.Context
import android.util.AttributeSet
import com.google.android.material.floatingactionbutton.FloatingActionButton
import org.oxycblt.auxio.util.getDimenSizeSafe
import org.oxycblt.auxio.util.logD
import com.google.android.material.R as MaterialR
/**
* A FloatingActionButton that automatically switches to a normal or large FAB depending on the
* screen size.
*/
@Suppress("PrivateResource")
class AdaptiveFloatingActionButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = MaterialR.style.Widget_Material3_FloatingActionButton_Primary
) : FloatingActionButton(context, attrs, defStyleAttr) {
init {
size = SIZE_NORMAL
// Use a large FAB on large screens, as it makes it easier to touch.
if (resources.configuration.smallestScreenWidthDp >= 640) {
logD("Using large FAB configuration")
val largeFabSize = context.getDimenSizeSafe(
MaterialR.dimen.m3_large_fab_size
)
val largeImageSize = context.getDimenSizeSafe(
MaterialR.dimen.m3_large_fab_max_image_size
)
customSize = largeFabSize
setMaxImageSize(largeImageSize)
}
}
}

View file

@ -30,7 +30,7 @@ import org.oxycblt.auxio.util.systemBarInsetsCompat
* A container for a FloatingActionButton that enables edge-to-edge support.
* @author OxygenCobalt
*/
class FloatingActionButtonContainer @JvmOverloads constructor(
class EdgeFabContainer @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = 0
@ -45,7 +45,6 @@ class FloatingActionButtonContainer @JvmOverloads constructor(
override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
updatePadding(bottom = insets.systemBarInsetsCompat.bottom)
return insets
}
}

View file

@ -159,11 +159,9 @@ class HomeFragment : Fragment() {
).attach()
}
binding.homeFab.setOnClickListener {
playbackModel.shuffleAll()
}
binding.homeShuffleFab.setup(playbackModel)
// --- VIEWMODEL SETUP ---
// --- VIEWMODEL SETUP ---
// There is no way a fast scrolling event can continue across a re-create. Reset it.
homeModel.updateFastScrolling(false)
@ -171,12 +169,12 @@ class HomeFragment : Fragment() {
musicModel.loaderResponse.observe(viewLifecycleOwner) { response ->
// Handle the loader response.
when (response) {
is MusicStore.Response.Ok -> binding.homeFab.show()
is MusicStore.Response.Ok -> binding.homeShuffleFab.show()
// While loading or during an error, make sure we keep the shuffle fab hidden so
// that any kind of playback is impossible. PlaybackStateManager also relies on this
// invariant, so please don't change it.
else -> binding.homeFab.hide()
else -> binding.homeShuffleFab.hide()
}
}
@ -188,9 +186,9 @@ class HomeFragment : Fragment() {
}
if (scrolling) {
binding.homeFab.hide()
binding.homeShuffleFab.hide()
} else {
binding.homeFab.show()
binding.homeShuffleFab.show()
}
}

View file

@ -40,24 +40,23 @@
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
tools:layout="@layout/fragment_home_list" />
<org.oxycblt.auxio.home.FloatingActionButtonContainer
<org.oxycblt.auxio.home.EdgeFabContainer
android:id="@+id/home_fab_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/home_pager"
app:layout_anchorGravity="bottom|end">
<org.oxycblt.auxio.home.AdaptiveFloatingActionButton
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/home_fab"
style="@style/Widget.Auxio.FloatingActionButton.Adaptive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.Material3.FloatingActionButton.Primary"
android:layout_margin="@dimen/spacing_medium"
android:contentDescription="@string/desc_shuffle_all"
android:src="@drawable/ic_shuffle"
app:tint="?attr/colorOnPrimaryContainer" />
android:src="@drawable/ic_shuffle" />
</org.oxycblt.auxio.home.FloatingActionButtonContainer>
</org.oxycblt.auxio.home.EdgeFabContainer>
</org.oxycblt.auxio.ui.EdgeCoordinatorLayout>
</layout>

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<layout>
<merge 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"
tools:parentTag="android.widget.FrameLayout">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/shuffle_fab"
style="@style/Widget.Auxio.FloatingActionButton.Adaptive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_medium"
android:contentDescription="@string/desc_shuffle_all"
android:src="@drawable/ic_shuffle"
app:icon="@drawable/ic_shuffle" />
</merge>
</layout>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Widget.Auxio.FloatingActionButton.Adaptive" parent="Widget.Material3.FloatingActionButton.Large.Primary">
<item name="fabSize">normal</item>
</style>
</resources>

View file

@ -167,9 +167,14 @@
compensate.
2. For some reason elevation behaves strangely in the playback panel, so we disable it.
-->
<item name="maxImageSize">@dimen/size_playback_icon</item>
<item name="fabSize">normal</item>
<item name="fabCustomSize">@dimen/size_btn_large</item>
<item name="maxImageSize">@dimen/size_playback_icon</item>
<item name="android:elevation">0dp</item>
<item name="elevation">0dp</item>
</style>
<style name="Widget.Auxio.FloatingActionButton.Adaptive" parent="Widget.Material3.FloatingActionButton.Primary">
<item name="fabSize">normal</item>
</style>
</resources>

View file

@ -9,7 +9,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.1'
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_version"