Skip to content

Commit

Permalink
Twelve: How about we just let Coil understand Thumbnail
Browse files Browse the repository at this point in the history
smh my head

Change-Id: I806acbd5163ea24317d3152831da74a825056b70
  • Loading branch information
SebaUbuntu committed Nov 23, 2024
1 parent 743540a commit 5839598
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 76 deletions.
12 changes: 11 additions & 1 deletion app/src/main/java/org/lineageos/twelve/TwelveApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ package org.lineageos.twelve
import android.app.Application
import androidx.media3.common.util.UnstableApi
import androidx.media3.common.util.Util
import coil3.ImageLoader
import coil3.PlatformContext
import coil3.SingletonImageLoader
import com.google.android.material.color.DynamicColors
import kotlinx.coroutines.MainScope
import org.lineageos.twelve.database.TwelveDatabase
import org.lineageos.twelve.repositories.MediaRepository
import org.lineageos.twelve.repositories.ResumptionPlaylistRepository
import org.lineageos.twelve.ui.coil.ThumbnailMapper

@androidx.annotation.OptIn(UnstableApi::class)
class TwelveApplication : Application() {
class TwelveApplication : Application(), SingletonImageLoader.Factory {
private val database by lazy { TwelveDatabase.getInstance(applicationContext) }
val mediaRepository by lazy { MediaRepository(applicationContext, MainScope(), database) }
val resumptionPlaylistRepository by lazy { ResumptionPlaylistRepository(database) }
Expand All @@ -27,4 +31,10 @@ class TwelveApplication : Application() {
// Observe dynamic colors changes
DynamicColors.applyToActivitiesIfAvailable(this)
}

override fun newImageLoader(context: PlatformContext) = ImageLoader.Builder(this)
.components {
add(ThumbnailMapper)
}
.build()
}
13 changes: 6 additions & 7 deletions app/src/main/java/org/lineageos/twelve/ViewActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.media3.common.Player
import coil3.load
import com.google.android.material.button.MaterialButton
import com.google.android.material.slider.Slider
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.lineageos.twelve.ext.loadThumbnail
import org.lineageos.twelve.models.MediaType
import org.lineageos.twelve.models.RepeatMode
import org.lineageos.twelve.models.RequestStatus
Expand Down Expand Up @@ -176,12 +176,11 @@ class ViewActivity : AppCompatActivity(R.layout.activity_view) {
}

is RequestStatus.Success -> {
it.data?.bitmap?.let { bitmap ->
thumbnailImageView.load(bitmap)
thumbnailImageView.isVisible = true
dummyThumbnailImageView.isVisible = false
} ?: it.data?.uri?.let { uri ->
thumbnailImageView.load(uri)
it.data?.let { thumbnail ->
thumbnailImageView.loadThumbnail(
thumbnail,
placeholder = R.drawable.ic_music_note,
)
thumbnailImageView.isVisible = true
dummyThumbnailImageView.isVisible = false
} ?: run {
Expand Down
15 changes: 4 additions & 11 deletions app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,10 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
toolbar.title = album.title
albumTitleTextView.text = album.title

album.thumbnail?.uri?.also { uri ->
thumbnailImageView.loadThumbnail(
uri,
placeholder = R.drawable.ic_album
)
} ?: album.thumbnail?.bitmap?.also { bitmap ->
thumbnailImageView.loadThumbnail(
bitmap,
placeholder = R.drawable.ic_album
)
} ?: thumbnailImageView.setImageResource(R.drawable.ic_album)
thumbnailImageView.loadThumbnail(
album.thumbnail,
placeholder = R.drawable.ic_album
)

artistNameTextView.text = album.artistName
artistNameTextView.setOnClickListener {
Expand Down
21 changes: 5 additions & 16 deletions app/src/main/java/org/lineageos/twelve/fragments/ArtistFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ class ArtistFragment : Fragment(R.layout.fragment_artist) {
}

override fun ViewHolder.onBindView(item: Album) {
item.thumbnail?.uri?.also { uri ->
view.loadThumbnailImage(uri, R.drawable.ic_album)
} ?: item.thumbnail?.bitmap?.also { bitmap ->
view.loadThumbnailImage(bitmap)
} ?: view.setThumbnailImage(R.drawable.ic_album)
view.loadThumbnailImage(item.thumbnail, R.drawable.ic_album)

view.headlineText = item.title
view.headlineMaxLines = 2
Expand Down Expand Up @@ -219,17 +215,10 @@ class ArtistFragment : Fragment(R.layout.fragment_artist) {
toolbar.title = artist.name
artistNameTextView.text = artist.name

artist.thumbnail?.uri?.also { uri ->
thumbnailImageView.loadThumbnail(
uri,
placeholder = R.drawable.ic_person
)
} ?: artist.thumbnail?.bitmap?.also { bitmap ->
thumbnailImageView.loadThumbnail(
bitmap,
placeholder = R.drawable.ic_person
)
} ?: thumbnailImageView.setImageResource(R.drawable.ic_person)
thumbnailImageView.loadThumbnail(
artist.thumbnail,
placeholder = R.drawable.ic_person
)

albumsAdapter.submitList(artistWorks.albums)
appearsInAlbumAdapter.submitList(artistWorks.appearsInAlbum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ class GenreFragment : Fragment(R.layout.fragment_genre) {
}

override fun ViewHolder.onBindView(item: Album) {
item.thumbnail?.uri?.also { uri ->
view.loadThumbnailImage(uri, R.drawable.ic_album)
} ?: item.thumbnail?.bitmap?.also { bitmap ->
view.loadThumbnailImage(bitmap)
} ?: view.setThumbnailImage(R.drawable.ic_album)
view.loadThumbnailImage(item.thumbnail, R.drawable.ic_album)

view.headlineText = item.title
view.supportingText = item.artistName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package org.lineageos.twelve.fragments

import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.ImageDecoder
import android.graphics.PixelFormat
import android.icu.text.DecimalFormat
import android.icu.text.DecimalFormatSymbols
Expand Down Expand Up @@ -46,6 +45,7 @@ import me.bogerchan.niervisualizer.NierVisualizerManager
import org.lineageos.twelve.R
import org.lineageos.twelve.TwelveApplication
import org.lineageos.twelve.ext.getViewProperty
import org.lineageos.twelve.ext.loadThumbnail
import org.lineageos.twelve.ext.navigateSafe
import org.lineageos.twelve.ext.updatePadding
import org.lineageos.twelve.models.PlaybackState
Expand Down Expand Up @@ -357,18 +357,10 @@ class NowPlayingFragment : Fragment(R.layout.fragment_now_playing) {
}

is RequestStatus.Success -> {
it.data?.bitmap?.also { bitmap ->
albumArtImageView.setImageBitmap(bitmap)
} ?: it.data?.uri?.also { artworkUri ->
ImageDecoder.createSource(
requireContext().contentResolver,
artworkUri
).let { source ->
ImageDecoder.decodeBitmap(source)
}.also { bitmap ->
albumArtImageView.setImageBitmap(bitmap)
}
} ?: albumArtImageView.setImageResource(R.drawable.ic_music_note)
albumArtImageView.loadThumbnail(
it.data,
placeholder = R.drawable.ic_music_note,
)
}

is RequestStatus.Error -> throw Exception(
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/org/lineageos/twelve/ui/coil/ThumbnailMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/

package org.lineageos.twelve.ui.coil

import coil3.map.Mapper
import coil3.request.Options
import org.lineageos.twelve.models.Thumbnail

object ThumbnailMapper : Mapper<Thumbnail, Any> {
override fun map(data: Thumbnail, options: Options) = data.bitmap ?: data.uri
}
12 changes: 2 additions & 10 deletions app/src/main/java/org/lineageos/twelve/ui/views/ActivityTabItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,15 @@ class ActivityTabItem @JvmOverloads constructor(
subheadText = item.artistName
supportingText = item.year?.toString()

item.thumbnail?.uri?.let {
loadThumbnailImage(it, R.drawable.ic_album)
} ?: item.thumbnail?.bitmap?.let {
loadThumbnailImage(it, R.drawable.ic_album)
} ?: setPlaceholderImage(R.drawable.ic_album)
loadThumbnailImage(item.thumbnail, R.drawable.ic_album)
}

is Artist -> {
headlineText = item.name
subheadText = null
supportingText = null

item.thumbnail?.uri?.let {
loadThumbnailImage(it)
} ?: item.thumbnail?.bitmap?.let {
loadThumbnailImage(it)
} ?: setPlaceholderImage(R.drawable.ic_person)
loadThumbnailImage(item.thumbnail, R.drawable.ic_person)
}

is Audio -> {
Expand Down
15 changes: 2 additions & 13 deletions app/src/main/java/org/lineageos/twelve/ui/views/NowPlayingBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.lineageos.twelve.ui.views

import android.content.Context
import android.graphics.ImageDecoder
import android.util.AttributeSet
import android.widget.FrameLayout
import android.widget.ImageButton
Expand All @@ -21,6 +20,7 @@ import androidx.media3.common.MediaMetadata
import com.google.android.material.card.MaterialCardView
import com.google.android.material.progressindicator.CircularProgressIndicator
import org.lineageos.twelve.R
import org.lineageos.twelve.ext.loadThumbnail
import org.lineageos.twelve.ext.slideDown
import org.lineageos.twelve.ext.slideUp
import org.lineageos.twelve.models.Thumbnail
Expand Down Expand Up @@ -126,18 +126,7 @@ class NowPlayingBar @JvmOverloads constructor(
}

fun updateMediaArtwork(artwork: Thumbnail?) {
artwork?.bitmap?.also { bitmap ->
thumbnailImageView.setImageBitmap(bitmap)
} ?: artwork?.uri?.also { artworkUri ->
ImageDecoder.createSource(
context.contentResolver,
artworkUri
).let { source ->
ImageDecoder.decodeBitmap(source)
}.also { bitmap ->
thumbnailImageView.setImageBitmap(bitmap)
}
} ?: thumbnailImageView.setImageResource(R.drawable.ic_music_note)
thumbnailImageView.loadThumbnail(artwork, placeholder = R.drawable.ic_music_note)
}

fun updateDurationCurrentPositionMs(durationMs: Long?, currentPositionMs: Long?) {
Expand Down

0 comments on commit 5839598

Please sign in to comment.