ui: rework custom ui components

Refactor the package structure and implementation of custom UI
components.
This commit is contained in:
OxygenCobalt
2022-05-21 14:52:08 -06:00
parent 61839405ce
commit 2d7dbd19cd
65 changed files with 342 additions and 324 deletions
@@ -21,11 +21,11 @@ import android.app.Application
import coil.ImageLoader
import coil.ImageLoaderFactory
import coil.request.CachePolicy
import org.oxycblt.auxio.coil.AlbumArtFetcher
import org.oxycblt.auxio.coil.ArtistImageFetcher
import org.oxycblt.auxio.coil.CrossfadeFactory
import org.oxycblt.auxio.coil.GenreImageFetcher
import org.oxycblt.auxio.coil.MusicKeyer
import org.oxycblt.auxio.image.AlbumArtFetcher
import org.oxycblt.auxio.image.ArtistImageFetcher
import org.oxycblt.auxio.image.CrossfadeFactory
import org.oxycblt.auxio.image.GenreImageFetcher
import org.oxycblt.auxio.image.MusicKeyer
import org.oxycblt.auxio.settings.SettingsManager
@Suppress("UNUSED")
@@ -42,13 +42,11 @@ import org.oxycblt.auxio.util.systemBarInsetsCompat
*
* TODO: Custom language support
*
* TODO: Rework menus [perhaps add multi-select]
*
* TODO: Rework some fragments to use listeners *even more*
*
* TODO: Fix how selection works in the RecyclerViews (doing it poorly right now)
*
* TODO: Rework padding ethos
*
* @author OxygenCobalt
*/
class MainActivity : AppCompatActivity() {
private val playbackModel: PlaybackViewModel by viewModels()
@@ -1,153 +0,0 @@
/*
* Copyright (c) 2022 Auxio Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.coil
import android.content.Context
import android.graphics.Matrix
import android.graphics.RectF
import android.util.AttributeSet
import android.widget.ImageView
import androidx.annotation.AttrRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.widget.AppCompatImageView
import coil.dispose
import coil.load
import com.google.android.material.shape.MaterialShapeDrawable
import kotlin.math.min
import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.Album
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
import org.oxycblt.auxio.util.getColorStateListSafe
/**
* An [AppCompatImageView] that applies many of the stylistic choices that Auxio uses regarding
* images.
*/
class StyledImageView
@JvmOverloads
constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr: Int = 0) :
AppCompatImageView(context, attrs, defStyleAttr) {
private val centerMatrix = Matrix()
private val matrixSrc = RectF()
private val matrixDst = RectF()
private var cornerRadius = 0f
init {
val styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.StyledImageView)
cornerRadius = styledAttrs.getDimension(R.styleable.StyledImageView_cornerRadius, 0f)
styledAttrs.recycle()
clipToOutline = true
background =
MaterialShapeDrawable().apply {
fillColor = context.getColorStateListSafe(R.color.sel_cover_bg)
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
// Use clipToOutline and a background drawable to crop images. While Coil's transformation
// could theoretically be used to round corners, the corner radius is dependent on the
// dimensions of the image, which will result in inconsistent corners across different
// album covers unless we resize all covers to be the same size. clipToOutline is both
// cheaper and more elegant.
if (!isInEditMode) {
val settingsManager = SettingsManager.getInstance()
if (settingsManager.roundCovers) {
(background as MaterialShapeDrawable).setCornerSize(cornerRadius)
}
}
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
// Scale the image down to half-size
imageMatrix =
centerMatrix.apply {
reset()
drawable?.let { drawable ->
// Android is too good to allow us to set a fixed image size, so we instead need
// to define a matrix to scale an image directly.
// First scale the icon up to the desired size.
val iconSize = min(measuredWidth, measuredHeight) / 2f
matrixSrc.set(
0f,
0f,
drawable.intrinsicWidth.toFloat(),
drawable.intrinsicHeight.toFloat())
matrixDst.set(0f, 0f, iconSize, iconSize)
centerMatrix.setRectToRect(matrixSrc, matrixDst, Matrix.ScaleToFit.CENTER)
// Then actually center it into the icon, which the previous call does not
// actually do.
centerMatrix.postTranslate(
(measuredWidth - iconSize) / 2f, (measuredHeight - iconSize) / 2f)
}
}
}
}
// TODO: Borg the extension methods into the view
/** Bind the album cover for a [song]. */
fun StyledImageView.bindAlbumCover(song: Song) =
load(song, R.drawable.ic_song, R.string.desc_album_cover)
/** Bind the album cover for an [album]. */
fun StyledImageView.bindAlbumCover(album: Album) =
load(album, R.drawable.ic_album, R.string.desc_album_cover)
/** Bind the image for an [artist] */
fun StyledImageView.bindArtistImage(artist: Artist) =
load(artist, R.drawable.ic_artist, R.string.desc_artist_image)
/** Bind the image for a [genre] */
fun StyledImageView.bindGenreImage(genre: Genre) =
load(genre, R.drawable.ic_genre, R.string.desc_genre_image)
private fun <T : Music> StyledImageView.load(
music: T,
@DrawableRes error: Int,
@StringRes desc: Int
) {
contentDescription = context.getString(desc, music.resolveName(context))
dispose()
load(music) {
error(error)
transformations(SquareFrameTransform.INSTANCE)
listener(
onSuccess = { _, _ ->
// Using the matrix scale type will shrink the cover images, so set it back to
// the default scale type.
scaleType = ImageView.ScaleType.FIT_CENTER
},
onError = { _, _ ->
// Error icons need to be scaled correctly, so set it to the custom matrix
// that the ImageView applies
scaleType = ImageView.ScaleType.MATRIX
})
}
}
@@ -22,7 +22,6 @@ import androidx.core.view.isInvisible
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.R
import org.oxycblt.auxio.coil.bindAlbumCover
import org.oxycblt.auxio.databinding.ItemAlbumSongBinding
import org.oxycblt.auxio.databinding.ItemDetailBinding
import org.oxycblt.auxio.databinding.ItemDiscHeaderBinding
@@ -123,7 +122,7 @@ private class AlbumDetailViewHolder private constructor(private val binding: Ite
BindingViewHolder<Album, AlbumDetailAdapter.Listener>(binding.root) {
override fun bind(item: Album, listener: AlbumDetailAdapter.Listener) {
binding.detailCover.bindAlbumCover(item)
binding.detailCover.bind(item)
binding.detailName.textSafe = item.resolveName(binding.context)
binding.detailSubhead.apply {
@@ -21,8 +21,6 @@ import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.R
import org.oxycblt.auxio.coil.bindAlbumCover
import org.oxycblt.auxio.coil.bindArtistImage
import org.oxycblt.auxio.databinding.ItemDetailBinding
import org.oxycblt.auxio.databinding.ItemParentBinding
import org.oxycblt.auxio.databinding.ItemSongBinding
@@ -139,7 +137,7 @@ private class ArtistDetailViewHolder private constructor(private val binding: It
BindingViewHolder<Artist, DetailAdapter.Listener>(binding.root) {
override fun bind(item: Artist, listener: DetailAdapter.Listener) {
binding.detailCover.bindArtistImage(item)
binding.detailCover.bind(item)
binding.detailName.textSafe = item.resolveName(binding.context)
// Get the genre that corresponds to the most songs in this artist, which would be
@@ -181,7 +179,7 @@ private constructor(
private val binding: ItemParentBinding,
) : BindingViewHolder<Album, MenuItemListener>(binding.root), Highlightable {
override fun bind(item: Album, listener: MenuItemListener) {
binding.parentImage.bindAlbumCover(item)
binding.parentImage.bind(item)
binding.parentName.textSafe = item.resolveName(binding.context)
binding.parentInfo.textSafe =
if (item.year != null) {
@@ -226,7 +224,7 @@ private constructor(
private val binding: ItemSongBinding,
) : BindingViewHolder<Song, MenuItemListener>(binding.root), Highlightable {
override fun bind(item: Song, listener: MenuItemListener) {
binding.songAlbumCover.bindAlbumCover(item)
binding.songAlbumCover.bind(item)
binding.songName.textSafe = item.resolveName(binding.context)
binding.songInfo.textSafe = item.album.resolveName(binding.context)
@@ -21,8 +21,6 @@ import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.R
import org.oxycblt.auxio.coil.bindAlbumCover
import org.oxycblt.auxio.coil.bindGenreImage
import org.oxycblt.auxio.databinding.ItemDetailBinding
import org.oxycblt.auxio.databinding.ItemSongBinding
import org.oxycblt.auxio.music.Genre
@@ -114,7 +112,7 @@ class GenreDetailAdapter(listener: Listener) :
private class GenreDetailViewHolder private constructor(private val binding: ItemDetailBinding) :
BindingViewHolder<Genre, DetailAdapter.Listener>(binding.root) {
override fun bind(item: Genre, listener: DetailAdapter.Listener) {
binding.detailCover.bindGenreImage(item)
binding.detailCover.bind(item)
binding.detailName.textSafe = item.resolveName(binding.context)
binding.detailSubhead.textSafe =
binding.context.getPluralSafe(R.plurals.fmt_song_count, item.songs.size)
@@ -146,7 +144,7 @@ private class GenreDetailViewHolder private constructor(private val binding: Ite
class GenreSongViewHolder private constructor(private val binding: ItemSongBinding) :
BindingViewHolder<Song, MenuItemListener>(binding.root), Highlightable {
override fun bind(item: Song, listener: MenuItemListener) {
binding.songAlbumCover.bindAlbumCover(item)
binding.songAlbumCover.bind(item)
binding.songName.textSafe = item.resolveName(binding.context)
binding.songInfo.textSafe = item.resolveIndividualArtistName(binding.context)
binding.root.apply {
@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.coil
package org.oxycblt.auxio.image
import android.content.Context
import android.graphics.Bitmap
@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.coil
package org.oxycblt.auxio.image
import android.content.Context
import android.graphics.Bitmap
@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.coil
package org.oxycblt.auxio.image
import android.content.Context
import coil.ImageLoader
@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.coil
package org.oxycblt.auxio.image
import coil.decode.DataSource
import coil.drawable.CrossfadeDrawable
@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.coil
package org.oxycblt.auxio.image
import android.graphics.Bitmap
import coil.size.Size
@@ -25,7 +25,6 @@ import androidx.core.view.updatePadding
import androidx.fragment.app.activityViewModels
import com.google.android.material.color.MaterialColors
import org.oxycblt.auxio.R
import org.oxycblt.auxio.coil.bindAlbumCover
import org.oxycblt.auxio.databinding.FragmentPlaybackBarBinding
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.ui.MainNavigationAction
@@ -104,7 +103,7 @@ class PlaybackBarFragment : ViewBindingFragment<FragmentPlaybackBarBinding>() {
if (song != null) {
val context = requireContext()
val binding = requireBinding()
binding.playbackCover.bindAlbumCover(song)
binding.playbackCover.bind(song)
binding.playbackSong.textSafe = song.resolveName(context)
binding.playbackInfo.textSafe = song.resolveIndividualArtistName(context)
binding.playbackProgressBar.max = song.durationSecs.toInt()
@@ -28,7 +28,6 @@ import com.google.android.material.color.MaterialColors
import com.google.android.material.slider.Slider
import kotlin.math.max
import org.oxycblt.auxio.R
import org.oxycblt.auxio.coil.bindAlbumCover
import org.oxycblt.auxio.databinding.FragmentPlaybackPanelBinding
import org.oxycblt.auxio.music.MusicParent
import org.oxycblt.auxio.music.Song
@@ -176,7 +175,7 @@ class PlaybackPanelFragment :
val binding = requireBinding()
val context = requireContext()
binding.playbackCover.bindAlbumCover(song)
binding.playbackCover.bind(song)
binding.playbackSong.textSafe = song.resolveName(context)
binding.playbackArtist.textSafe = song.resolveIndividualArtistName(context)
binding.playbackAlbum.textSafe = song.album.resolveName(context)
@@ -186,7 +185,7 @@ class PlaybackPanelFragment :
binding.playbackDuration.textSafe = seconds.formatDuration(false)
binding.playbackSeekBar.apply {
isEnabled = seconds > 0L
valueToSafe = max(seconds, 1L).toFloat()
valueTo = max(seconds, 1L).toFloat()
}
}
@@ -200,7 +199,7 @@ class PlaybackPanelFragment :
// around.
val binding = requireBinding()
if (!binding.playbackPosition.isActivated) {
binding.playbackSeekBar.valueSafe = position.toFloat()
binding.playbackSeekBar.value = position.toFloat()
binding.playbackPosition.textSafe = position.formatDuration(true)
}
}
@@ -219,26 +218,4 @@ class PlaybackPanelFragment :
private fun updateShuffled(isShuffled: Boolean) {
requireBinding().playbackShuffle.isActivated = isShuffled
}
private var Slider.valueSafe: Float
get() = value
set(v) {
value =
if (v > valueTo) {
valueTo
} else {
v
}
}
private var Slider.valueToSafe: Float
get() = valueTo
set(v) {
valueTo =
if (value > v) {
value
} else {
v
}
}
}
@@ -28,7 +28,6 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.shape.MaterialShapeDrawable
import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.coil.bindAlbumCover
import org.oxycblt.auxio.databinding.ItemQueueSongBinding
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.ui.BackingData
@@ -71,7 +70,7 @@ private constructor(
@SuppressLint("ClickableViewAccessibility")
override fun bind(item: Song, listener: QueueItemListener) {
binding.songAlbumCover.bindAlbumCover(item)
binding.songAlbumCover.bind(item)
binding.songName.textSafe = item.resolveName(binding.context)
binding.songInfo.textSafe = item.resolveIndividualArtistName(binding.context)
@@ -25,7 +25,7 @@ import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import com.google.android.exoplayer2.Player
import org.oxycblt.auxio.coil.BitmapProvider
import org.oxycblt.auxio.image.BitmapProvider
import org.oxycblt.auxio.music.MusicParent
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.playback.state.PlaybackStateManager
@@ -30,7 +30,7 @@ import androidx.media.app.NotificationCompat.MediaStyle
import org.oxycblt.auxio.BuildConfig
import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.R
import org.oxycblt.auxio.coil.BitmapProvider
import org.oxycblt.auxio.image.BitmapProvider
import org.oxycblt.auxio.music.MusicParent
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.playback.state.RepeatMode
@@ -19,7 +19,7 @@ package org.oxycblt.auxio.settings
import android.content.SharedPreferences
import androidx.core.content.edit
import org.oxycblt.auxio.accent.Accent
import org.oxycblt.auxio.ui.accent.Accent
// A couple of utils for migrating from old settings values to the new formats
@@ -30,12 +30,12 @@ import androidx.preference.children
import androidx.recyclerview.widget.RecyclerView
import coil.Coil
import org.oxycblt.auxio.R
import org.oxycblt.auxio.accent.AccentCustomizeDialog
import org.oxycblt.auxio.home.tabs.TabCustomizeDialog
import org.oxycblt.auxio.music.excluded.ExcludedDialog
import org.oxycblt.auxio.playback.PlaybackViewModel
import org.oxycblt.auxio.settings.pref.IntListPreference
import org.oxycblt.auxio.settings.pref.IntListPreferenceDialog
import org.oxycblt.auxio.ui.accent.AccentCustomizeDialog
import org.oxycblt.auxio.util.hardRestart
import org.oxycblt.auxio.util.isNight
import org.oxycblt.auxio.util.logD
@@ -22,12 +22,12 @@ import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import org.oxycblt.auxio.accent.Accent
import org.oxycblt.auxio.home.tabs.Tab
import org.oxycblt.auxio.playback.state.PlaybackMode
import org.oxycblt.auxio.playback.system.ReplayGainMode
import org.oxycblt.auxio.ui.DisplayMode
import org.oxycblt.auxio.ui.Sort
import org.oxycblt.auxio.ui.accent.Accent
import org.oxycblt.auxio.util.unlikelyToBeNull
/**
@@ -32,10 +32,11 @@ constructor(
defStyleAttr: Int = androidx.preference.R.attr.dialogPreferenceStyle,
defStyleRes: Int = 0
) : DialogPreference(context, attrs, defStyleAttr, defStyleRes) {
// Reflect into Preference to get the (normally inaccessible) default value.
val entries: Array<CharSequence>
val values: IntArray
private var currentValue: Int? = null
// Reflect into Preference to get the (normally inaccessible) default value.
private val defValue: Int
get() = defValueField.get(this) as Int
@@ -29,7 +29,7 @@ import org.oxycblt.auxio.util.getDrawableSafe
/**
* A [SwitchPreferenceCompat] that emulates the M3 switches until the design team actually bothers
* to add them to MDC.
* to add them to MDC TODO: Remove this once MaterialSwitch is stabilized.
*/
class M3SwitchPreference
@JvmOverloads
@@ -45,7 +45,6 @@ constructor(
// Lollipop cannot into ColorStateList, disable this feature on that version
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val switch = holder.findViewById(androidx.preference.R.id.switchWidget)
if (switch is SwitchCompat) {
switch.apply {
trackDrawable = context.getDrawableSafe(R.drawable.ui_m3_switch_track)
@@ -54,11 +54,9 @@ fun Fragment.newMenu(anchor: View, data: Item, flag: Int = ActionMenu.FLAG_NONE)
* @throws IllegalStateException When there is no menu for this specific datatype/flag
* @author OxygenCobalt
*
* TODO: Stop scrolling when a menu is open
*
* TODO: Prevent duplicate menus from showing up
*
* TODO: Maybe replace this with a bottom sheet?
* TODO: Add multi-select
*/
class ActionMenu(
activity: AppCompatActivity,
@@ -22,7 +22,10 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.oxycblt.auxio.music.Music
/** A ViewModel that handles complicated navigation situations. */
/**
* A ViewModel that handles complicated navigation situations.
* @author OxygenCobalt
*/
class NavigationViewModel : ViewModel() {
private val _mainNavigationAction = MutableLiveData<MainNavigationAction?>()
/** Flag for main fragment navigation. Intended for MainFragment use only. */
@@ -28,6 +28,7 @@ import androidx.recyclerview.widget.RecyclerView
/**
* An adapter for one viewholder tied to one type of data. All functionality is derived from the
* overridden values.
* @author OxygenCobalt
*/
abstract class MonoAdapter<T, L, VH : BindingViewHolder<T, L>>(private val listener: L) :
RecyclerView.Adapter<VH>() {
@@ -51,6 +52,7 @@ private typealias AnyCreator = BindingViewHolder.Creator<out RecyclerView.ViewHo
/**
* An adapter for many viewholders tied to many types of data. Deriving this is more complicated
* than [MonoAdapter], as less overrides can be provided "for free".
* @author OxygenCobalt
*/
abstract class MultiAdapter<L>(private val listener: L) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
@@ -99,6 +101,7 @@ abstract class MultiAdapter<L>(private val listener: L) :
* A variation of [RecyclerView.ViewHolder] that enables ViewBinding. This is be used to provide a
* universal surface for binding data to a ViewHolder, and can be used with [MonoAdapter] to get an
* entire adapter implementation for free.
* @author OxygenCobalt
*/
abstract class BindingViewHolder<T, L>(root: View) : RecyclerView.ViewHolder(root) {
abstract fun bind(item: T, listener: L)
@@ -159,6 +162,7 @@ abstract class BackingData<T> {
/**
* A list-backed [BackingData] that is modified using adapter primitives. Useful in cases where
* [AsyncBackingData] is not preferable due to bugs involving diffing.
* @author OxygenCobalt
*/
class PrimitiveBackingData<T>(private val adapter: RecyclerView.Adapter<*>) : BackingData<T>() {
private var _currentList = mutableListOf<T>()
@@ -184,6 +188,7 @@ class PrimitiveBackingData<T>(private val adapter: RecyclerView.Adapter<*>) : Ba
* A list-backed [BackingData] that is modified with [AsyncListDiffer]. This is useful in cases
* where data updates are rapid-fire and unpredictable, and where the benefits of asynchronously
* diffing the adapter outweigh the shortcomings.
* @author OxygenCobalt
*/
class AsyncBackingData<T>(
adapter: RecyclerView.Adapter<*>,
@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.playback
package org.oxycblt.auxio.ui
import android.content.Context
import android.graphics.Canvas
@@ -29,33 +29,33 @@ import org.oxycblt.auxio.util.getDimenSizeSafe
import org.oxycblt.auxio.util.getDrawableSafe
/**
* An [AppCompatImageButton] designed for the buttons used in the playback display.
* An [AppCompatImageButton] that applies many of the stylistic choices that Auxio uses regarding
* buttons.
*
* Auxio's playback buttons have never followed the typical 24dp icon size that all other UI
* elements do, mostly because those icons just look bad at that size with all the gobs of
* whitespace surrounding them. So, this view resizes the icons to a fixed 32dp in a way that
* doesn't require a whole new icon set.
*
* This view also enables use of an "indicator", which is a dot that can denote when a button is
* active. This is useful for the shuffle/repeat buttons, as at times highlighting them is not
* enough to differentiate them.
* More specifically, this class add two features:
* - Specification of the icon size. This is to accommodate the playback buttons, which tend to be
* larger as by default the playback icons look terrible with the gobs of whitespace everywhere.
* - Addition of an indicator, which is a dot that can denote when a button is active. This is
* also useful for the playback buttons, as at times highlighting them is not enough to
* differentiate them.
* @author OxygenCobalt
*/
class PlaybackButton
class StyledImageButton
@JvmOverloads
constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr: Int = 0) :
AppCompatImageButton(context, attrs, defStyleAttr) {
private val iconSize = context.getDimenSizeSafe(R.dimen.size_playback_icon)
private val centerMatrix = Matrix()
private val matrixSrc = RectF()
private val matrixDst = RectF()
private val indicatorDrawable = context.getDrawableSafe(R.drawable.ui_indicator)
private val iconSize: Int
private var hasIndicator = false
set(value) {
field = value
invalidate()
}
private val centerMatrix = Matrix()
private val matrixSrc = RectF()
private val matrixDst = RectF()
private val indicatorDrawable = context.getDrawableSafe(R.drawable.ui_indicator)
init {
val size = context.getDimenSizeSafe(R.dimen.size_btn_small)
minimumWidth = size
@@ -63,14 +63,22 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
scaleType = ScaleType.MATRIX
setBackgroundResource(R.drawable.ui_large_unbounded_ripple)
val styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.PlaybackButton)
hasIndicator = styledAttrs.getBoolean(R.styleable.PlaybackButton_hasIndicator, false)
val styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.StyledImageButton)
iconSize =
styledAttrs
.getDimension(
R.styleable.StyledImageButton_iconSize,
context.getDimenSizeSafe(R.dimen.size_icon_normal).toFloat())
.toInt()
hasIndicator = styledAttrs.getBoolean(R.styleable.StyledImageButton_hasIndicator, false)
styledAttrs.recycle()
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
// TODO: Scale this drawable based on available space after padding
imageMatrix =
centerMatrix.apply {
reset()
@@ -0,0 +1,140 @@
/*
* Copyright (c) 2022 Auxio Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.ui
import android.content.Context
import android.graphics.Canvas
import android.graphics.ColorFilter
import android.graphics.PixelFormat
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import androidx.annotation.AttrRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.widget.AppCompatImageView
import coil.dispose
import coil.load
import com.google.android.material.shape.MaterialShapeDrawable
import org.oxycblt.auxio.R
import org.oxycblt.auxio.image.SquareFrameTransform
import org.oxycblt.auxio.music.Album
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
import org.oxycblt.auxio.util.getColorStateListSafe
import org.oxycblt.auxio.util.getDrawableSafe
/**
* An [AppCompatImageView] that applies many of the stylistic choices that Auxio uses regarding
* images.
*
* Default behavior includes the addition of a tonal background, automatic sizing of icons to
* half of the view size, and corner radius application depending on user preference.
* @author OxygenCobalt
*/
class StyledImageView
@JvmOverloads
constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr: Int = 0) :
AppCompatImageView(context, attrs, defStyleAttr) {
private var cornerRadius = 0f
init {
val styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.StyledImageView)
cornerRadius = styledAttrs.getDimension(R.styleable.StyledImageView_cornerRadius, 0f)
styledAttrs.recycle()
clipToOutline = true
background =
MaterialShapeDrawable().apply {
fillColor = context.getColorStateListSafe(R.color.sel_cover_bg)
}
// If we have a pre-set drawable, ensure that it's half-size.
val drawable = drawable
if (drawable != null) {
setImageDrawable(HalfSizeDrawable(drawable))
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
// Use clipToOutline and a background drawable to crop images. While Coil's transformation
// could theoretically be used to round corners, the corner radius is dependent on the
// dimensions of the image, which will result in inconsistent corners across different
// album covers unless we resize all covers to be the same size. clipToOutline is both
// cheaper and more elegant.
if (!isInEditMode) {
val settingsManager = SettingsManager.getInstance()
if (settingsManager.roundCovers) {
(background as MaterialShapeDrawable).setCornerSize(cornerRadius)
}
}
}
/** Bind the album cover for a [song]. */
fun bind(song: Song) = load(song, R.drawable.ic_song, R.string.desc_album_cover)
/** Bind the album cover for an [album]. */
fun bind(album: Album) = load(album, R.drawable.ic_album, R.string.desc_album_cover)
/** Bind the image for an [artist] */
fun bind(artist: Artist) = load(artist, R.drawable.ic_artist, R.string.desc_artist_image)
/** Bind the image for a [genre] */
fun bind(genre: Genre) = load(genre, R.drawable.ic_genre, R.string.desc_genre_image)
private fun <T : Music> load(music: T, @DrawableRes error: Int, @StringRes desc: Int) {
dispose()
load(music) {
error(HalfSizeDrawable(context, error))
transformations(SquareFrameTransform.INSTANCE)
}
contentDescription = context.getString(desc, music.resolveName(context))
}
private class HalfSizeDrawable(private val src: Drawable) : Drawable() {
constructor(context: Context, @DrawableRes res: Int) : this(context.getDrawableSafe(res))
override fun draw(canvas: Canvas) {
src.bounds.set(canvas.clipBounds)
val adjustWidth = src.bounds.width() / 4
val adjustHeight = src.bounds.height() / 4
src.bounds.set(
adjustWidth,
adjustHeight,
src.bounds.width() - adjustWidth,
src.bounds.height() - adjustHeight)
src.draw(canvas)
}
override fun setAlpha(alpha: Int) {
src.alpha = alpha
}
override fun setColorFilter(colorFilter: ColorFilter?) {
src.colorFilter = colorFilter
}
override fun getOpacity(): Int = PixelFormat.TRANSLUCENT
}
}
@@ -28,6 +28,10 @@ import androidx.viewbinding.ViewBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.oxycblt.auxio.util.logD
/**
* A dialog fragment enabling ViewBinding inflation and usage across the dialog fragment lifecycle.
* @author OxygenCobalt
*/
abstract class ViewBindingDialogFragment<T : ViewBinding> : DialogFragment() {
private var _binding: T? = null
@@ -25,7 +25,10 @@ import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import org.oxycblt.auxio.util.logD
/** A fragment enabling ViewBinding inflation and usage across the fragment lifecycle. */
/**
* A fragment enabling ViewBinding inflation and usage across the fragment lifecycle.
* @author OxygenCobalt
*/
abstract class ViewBindingFragment<T : ViewBinding> : Fragment() {
private var _binding: T? = null
@@ -20,9 +20,6 @@ package org.oxycblt.auxio.ui
import android.content.Context
import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.R
import org.oxycblt.auxio.coil.bindAlbumCover
import org.oxycblt.auxio.coil.bindArtistImage
import org.oxycblt.auxio.coil.bindGenreImage
import org.oxycblt.auxio.databinding.ItemHeaderBinding
import org.oxycblt.auxio.databinding.ItemParentBinding
import org.oxycblt.auxio.databinding.ItemSongBinding
@@ -35,11 +32,14 @@ import org.oxycblt.auxio.util.getPluralSafe
import org.oxycblt.auxio.util.inflater
import org.oxycblt.auxio.util.textSafe
/** The shared ViewHolder for a [Song]. */
/**
* The shared ViewHolder for a [Song].
* @author OxygenCobalt
*/
class SongViewHolder private constructor(private val binding: ItemSongBinding) :
BindingViewHolder<Song, MenuItemListener>(binding.root) {
override fun bind(item: Song, listener: MenuItemListener) {
binding.songAlbumCover.bindAlbumCover(item)
binding.songAlbumCover.bind(item)
binding.songName.textSafe = item.resolveName(binding.context)
binding.songInfo.textSafe = item.resolveIndividualArtistName(binding.context)
binding.root.apply {
@@ -70,14 +70,17 @@ class SongViewHolder private constructor(private val binding: ItemSongBinding) :
}
}
/** The Shared ViewHolder for a [Album]. */
/**
* The Shared ViewHolder for a [Album].
* @author OxygenCobalt
*/
class AlbumViewHolder
private constructor(
private val binding: ItemParentBinding,
) : BindingViewHolder<Album, MenuItemListener>(binding.root) {
override fun bind(item: Album, listener: MenuItemListener) {
binding.parentImage.bindAlbumCover(item)
binding.parentImage.bind(item)
binding.parentName.textSafe = item.resolveName(binding.context)
binding.parentInfo.textSafe = item.artist.resolveName(binding.context)
binding.root.apply {
@@ -108,12 +111,15 @@ private constructor(
}
}
/** The Shared ViewHolder for a [Artist]. */
/**
* The Shared ViewHolder for a [Artist].
* @author OxygenCobalt
*/
class ArtistViewHolder private constructor(private val binding: ItemParentBinding) :
BindingViewHolder<Artist, MenuItemListener>(binding.root) {
override fun bind(item: Artist, listener: MenuItemListener) {
binding.parentImage.bindArtistImage(item)
binding.parentImage.bind(item)
binding.parentName.textSafe = item.resolveName(binding.context)
binding.parentInfo.textSafe =
binding.context.getString(
@@ -149,14 +155,17 @@ class ArtistViewHolder private constructor(private val binding: ItemParentBindin
}
}
/** The Shared ViewHolder for a [Genre]. */
/**
* The Shared ViewHolder for a [Genre].
* @author OxygenCobalt
*/
class GenreViewHolder
private constructor(
private val binding: ItemParentBinding,
) : BindingViewHolder<Genre, MenuItemListener>(binding.root) {
override fun bind(item: Genre, listener: MenuItemListener) {
binding.parentImage.bindGenreImage(item)
binding.parentImage.bind(item)
binding.parentName.textSafe = item.resolveName(binding.context)
binding.parentInfo.textSafe =
binding.context.getPluralSafe(R.plurals.fmt_song_count, item.songs.size)
@@ -187,7 +196,10 @@ private constructor(
}
}
/** The Shared ViewHolder for a [Header]. */
/**
* The Shared ViewHolder for a [Header].
* @author OxygenCobalt
*/
class NewHeaderViewHolder private constructor(private val binding: ItemHeaderBinding) :
BindingViewHolder<Header, Unit>(binding.root) {
@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.accent
package org.oxycblt.auxio.ui.accent
import org.oxycblt.auxio.R
@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.accent
package org.oxycblt.auxio.ui.accent
import android.content.Context
import androidx.appcompat.widget.TooltipCompat
@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.accent
package org.oxycblt.auxio.ui.accent
import android.os.Bundle
import android.view.LayoutInflater
@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.accent
package org.oxycblt.auxio.ui.accent
import android.content.Context
import android.util.AttributeSet
@@ -26,8 +26,6 @@ import org.oxycblt.auxio.playback.system.PlaybackService
import org.oxycblt.auxio.util.newBroadcastIntent
import org.oxycblt.auxio.util.newMainIntent
// TODO: Still need to change the default cover here
/**
* The default widget is displayed whenever there is no music playing. It just shows the message "No
* music playing".
@@ -109,7 +107,7 @@ private fun RemoteViews.applyCover(
R.id.widget_cover,
context.getString(R.string.desc_album_cover, state.song.album.resolveName(context)))
} else {
setImageViewResource(R.id.widget_cover, R.drawable.ic_widget_album)
setImageViewResource(R.id.widget_cover, R.drawable.ic_remote_default_cover)
setContentDescription(R.id.widget_cover, context.getString(R.string.desc_no_cover))
}
@@ -24,8 +24,8 @@ import coil.request.ImageRequest
import coil.size.Size
import coil.transform.RoundedCornersTransformation
import kotlin.math.min
import org.oxycblt.auxio.coil.BitmapProvider
import org.oxycblt.auxio.coil.SquareFrameTransform
import org.oxycblt.auxio.image.BitmapProvider
import org.oxycblt.auxio.image.SquareFrameTransform
import org.oxycblt.auxio.music.MusicParent
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.playback.state.PlaybackStateManager
@@ -87,7 +87,7 @@ class WidgetComponent(private val context: Context) :
val metrics = context.resources.displayMetrics
// Use RoundedCornersTransformation. This is because our hack to get a 1:1
// aspect ratio on widget ImageViews doesn't actually result in a square
// aspect ratio on widget ImageViews doesn't actually result in a square
// ImageView, so clipToOutline won't work.
builder
.transformations(