coil: make rounded images more nuanced

Create a custom view for rounded images, making them more nuanced in
the process.

The previous method for applying rounded images in-app was generally
clunky and fragile. Introduce a new custom view that actually takes a
cornerRadius attribute from the ImageView itself that then applies it
whenever the user enables the setting. This also allows rounded images
to be more nuanced, as typical 8dp elevation can be used for small
views and a more fitting 16dp radius can be used for large views.
This commit is contained in:
OxygenCobalt
2022-02-23 17:42:34 -07:00
parent 280b582efa
commit 61dbfe3185
31 changed files with 83 additions and 53 deletions
@@ -79,10 +79,6 @@ class MainFragment : Fragment() {
// but for some insane reason google decided to cripple the window APIs one could use
// to limit it's size. So, we just have our own special layout that is shown whenever
// the screen is too small because of course we have to.
// Another fun fact: smallestScreenWidthDp is completely bugged and uses the total
// screen size, even when the window is smaller. This basically borks split screen
// even more than it already does. Fun!
if (requireActivity().isInMultiWindowMode) {
val config = resources.configuration
@@ -35,7 +35,6 @@ import org.oxycblt.auxio.music.Artist
import org.oxycblt.auxio.music.Genre
import org.oxycblt.auxio.music.Music
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.settings.SettingsManager
// --- BINDING ADAPTERS ---
@@ -66,21 +65,6 @@ fun ImageView.bindGenreImage(genre: Genre?) = load(genre, R.drawable.ic_genre)
fun <T : Music> ImageView.load(music: T?, @DrawableRes error: Int) {
dispose()
// We don't round album covers by default as it desecrates album artwork, but we do provide
// an option if one wants it.
// As for why we use clipToOutline instead of coils RoundedCornersTransformation, the radii
// of an image's corners is dependent on the actual dimensions of the image, which would force
// us to resize all images to a fixed size. clipToOutline is pretty much always cheaper as long
// as we have a perfectly-square image.
val settingsManager = SettingsManager.getInstance()
if (settingsManager.roundCovers && background == null) {
setBackgroundResource(R.drawable.ui_rounded_cutout)
clipToOutline = true
} else if (!settingsManager.roundCovers && background != null) {
background = null
clipToOutline = false
}
load(music) {
error(error)
transformations(SquareFrameTransform())
@@ -0,0 +1,41 @@
package org.oxycblt.auxio.coil
import android.content.Context
import android.util.AttributeSet
import androidx.annotation.AttrRes
import androidx.appcompat.widget.AppCompatImageView
import com.google.android.material.shape.MaterialShapeDrawable
import org.oxycblt.auxio.R
import org.oxycblt.auxio.settings.SettingsManager
import org.oxycblt.auxio.util.getColorSafe
import org.oxycblt.auxio.util.stateList
class RoundableImageView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr) {
init {
val styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.RoundableImageView)
val cornerRadius = styledAttrs.getDimension(R.styleable.RoundableImageView_cornerRadius, 0.0f)
styledAttrs.recycle()
background = MaterialShapeDrawable().apply {
setCornerSize(cornerRadius)
fillColor = context.getColorSafe(android.R.color.transparent).stateList
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
// We don't round album covers by default as it desecrates album artwork, but we do
// provide an option if one wants it.
// As for why we use clipToOutline instead of coils RoundedCornersTransformation, the radii
// of an image's corners is dependent on the actual dimensions of the image, which would
// force us to resize all images to a fixed size. clipToOutline is pretty much always
// cheaper as long as we have a perfectly-square image.
val settingsManager = SettingsManager.getInstance()
clipToOutline = settingsManager.roundCovers
}
}
@@ -159,7 +159,9 @@ class HomeFragment : Fragment() {
).attach()
}
binding.homeShuffleFab.setup(playbackModel)
binding.homeFab.setOnClickListener {
playbackModel.shuffleAll()
}
// --- VIEWMODEL SETUP ---
@@ -169,12 +171,12 @@ class HomeFragment : Fragment() {
musicModel.loaderResponse.observe(viewLifecycleOwner) { response ->
// Handle the loader response.
when (response) {
is MusicStore.Response.Ok -> binding.homeShuffleFab.show()
is MusicStore.Response.Ok -> binding.homeFab.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.homeShuffleFab.hide()
else -> binding.homeFab.hide()
}
}
@@ -186,9 +188,9 @@ class HomeFragment : Fragment() {
}
if (scrolling) {
binding.homeShuffleFab.hide()
binding.homeFab.hide()
} else {
binding.homeShuffleFab.show()
binding.homeFab.show()
}
}