Skip to content

Commit

Permalink
Twelve: Handle MediaStore unknown strings
Browse files Browse the repository at this point in the history
Change-Id: I47c702006063af45a654f9f80ed206875f96a863
  • Loading branch information
SebaUbuntu committed Nov 27, 2024
1 parent c9df309 commit 370814b
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class LocalDataSource(context: Context, private val database: TwelveDatabase) :

Album(
uri,
album,
album.takeIf { it != MediaStore.UNKNOWN_STRING },
artistUri,
artist,
artist.takeIf { it != MediaStore.UNKNOWN_STRING },
lastYear.takeIf { it != 0 },
thumbnail,
)
Expand All @@ -89,7 +89,7 @@ class LocalDataSource(context: Context, private val database: TwelveDatabase) :

Artist(
uri,
artist,
artist.takeIf { it != MediaStore.UNKNOWN_STRING },
thumbnail,
)
}
Expand Down Expand Up @@ -157,9 +157,9 @@ class LocalDataSource(context: Context, private val database: TwelveDatabase) :
audioType,
duration,
artistUri,
artist,
artist.takeIf { it != MediaStore.UNKNOWN_STRING },
albumUri,
album,
album.takeIf { it != MediaStore.UNKNOWN_STRING },
discNumber,
discTrack,
genreUri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class SubsonicDataSource(arguments: Bundle) : MediaDataSource {
uri = getAlbumUri(id),
title = name,
artistUri = artistId?.let { getArtistUri(it) } ?: Uri.EMPTY,
artistName = artist ?: "",
artistName = artist,
year = year,
thumbnail = Thumbnail(
uri = Uri.parse(subsonicClient.getCoverArt(id)),
Expand All @@ -383,9 +383,9 @@ class SubsonicDataSource(arguments: Bundle) : MediaDataSource {
type = type.toAudioType(),
durationMs = (duration?.toLong()?.let { it * 1000 }) ?: 0,
artistUri = artistId?.let { getArtistUri(it) } ?: Uri.EMPTY,
artistName = artist ?: "",
artistName = artist,
albumUri = albumId?.let { getAlbumUri(it) } ?: Uri.EMPTY,
albumTitle = album ?: "",
albumTitle = album,
discNumber = discNumber,
trackNumber = track,
genreUri = genre?.let { getGenreUri(it) },
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/lineageos/twelve/ext/MediaItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.media3.common.util.UnstableApi

@androidx.annotation.OptIn(UnstableApi::class)
fun buildMediaItem(
title: String,
title: String?,
mediaId: String,
isPlayable: Boolean,
isBrowsable: Boolean,
Expand Down
17 changes: 13 additions & 4 deletions app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
}

view.headlineText = item.audio.title
view.supportingText = item.audio.artistName
item.audio.artistName?.also {
view.supportingText = it
} ?: view.setSupportingText(R.string.artist_unknown)
view.trailingSupportingText = TimestampFormatter.formatTimestampMillis(
item.audio.durationMs
)
Expand Down Expand Up @@ -273,15 +275,22 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
is RequestStatus.Success -> {
val (album, audios) = it.data

toolbar.title = album.title
albumTitleTextView.text = album.title
album.title?.also { albumTitle ->
toolbar.title = albumTitle
albumTitleTextView.text = albumTitle
} ?: run {
toolbar.setTitle(R.string.album_unknown)
albumTitleTextView.setText(R.string.album_unknown)
}

thumbnailImageView.loadThumbnail(
album.thumbnail,
placeholder = R.drawable.ic_album
)

artistNameTextView.text = album.artistName
album.artistName?.also { artistName ->
artistNameTextView.text = artistName
} ?: artistNameTextView.setText(R.string.artist_unknown)
artistNameTextView.setOnClickListener {
findNavController().navigateSafe(
R.id.action_albumFragment_to_fragment_artist,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ class AlbumsFragment : Fragment(R.layout.fragment_albums) {
}

override fun ViewHolder.onBindView(item: Album) {
view.headlineText = item.title
view.supportingText = item.artistName
item.title?.also {
view.headlineText = it
} ?: view.setHeadlineText(R.string.unknown)

item.artistName?.also {
view.supportingText = it
} ?: view.setSupportingText(R.string.artist_unknown)
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions app/src/main/java/org/lineageos/twelve/fragments/ArtistFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ class ArtistFragment : Fragment(R.layout.fragment_artist) {
override fun ViewHolder.onBindView(item: Album) {
view.loadThumbnailImage(item.thumbnail, R.drawable.ic_album)

view.headlineText = item.title
item.title?.also {
view.headlineText = it
} ?: view.setHeadlineText(R.string.album_unknown)
view.headlineMaxLines = 2
view.supportingText = item.year?.toString()
}
Expand Down Expand Up @@ -212,8 +214,13 @@ class ArtistFragment : Fragment(R.layout.fragment_artist) {
is RequestStatus.Success -> {
val (artist, artistWorks) = it.data

toolbar.title = artist.name
artistNameTextView.text = artist.name
artist.name?.also { artistName ->
toolbar.title = artistName
artistNameTextView.text = artistName
} ?: run {
toolbar.setTitle(R.string.artist_unknown)
artistNameTextView.setText(R.string.artist_unknown)
}

thumbnailImageView.loadThumbnail(
artist.thumbnail,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class ArtistsFragment : Fragment(R.layout.fragment_artists) {
}

override fun ViewHolder.onBindView(item: Artist) {
view.headlineText = item.name
item.name?.also {
view.headlineText = it
} ?: view.setHeadlineText(R.string.unknown)
}
}
}
Expand Down
21 changes: 16 additions & 5 deletions app/src/main/java/org/lineageos/twelve/fragments/GenreFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ class GenreFragment : Fragment(R.layout.fragment_genre) {
override fun ViewHolder.onBindView(item: Album) {
view.loadThumbnailImage(item.thumbnail, R.drawable.ic_album)

view.headlineText = item.title
view.supportingText = item.artistName
item.title?.also {
view.headlineText = it
} ?: view.setHeadlineText(R.string.album_unknown)
item.artistName?.also {
view.supportingText = it
} ?: view.setSupportingText(R.string.artist_unknown)
view.tertiaryText = item.year?.toString()
}
}
Expand Down Expand Up @@ -155,8 +159,12 @@ class GenreFragment : Fragment(R.layout.fragment_genre) {

override fun ViewHolder.onBindView(item: Audio) {
view.headlineText = item.title
view.supportingText = item.artistName
view.tertiaryText = item.albumTitle
item.artistName?.also {
view.supportingText = it
} ?: view.setSupportingText(R.string.artist_unknown)
item.albumTitle?.also {
view.tertiaryText = it
} ?: view.setTertiaryText(R.string.album_unknown)
}
}
}
Expand Down Expand Up @@ -258,9 +266,12 @@ class GenreFragment : Fragment(R.layout.fragment_genre) {
is RequestStatus.Success -> {
val (genre, genreContent) = it.data

(genre.name ?: getString(R.string.genre_unknown)).let { genreName ->
genre.name?.also { genreName ->
toolbar.title = genreName
genreNameTextView.text = genreName
} ?: run {
toolbar.setTitle(R.string.genre_unknown)
genreNameTextView.setText(R.string.genre_unknown)
}

thumbnailImageView.loadThumbnail(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ class GenresFragment : Fragment(R.layout.fragment_genres) {
}

override fun ViewHolder.onBindView(item: Genre) {
item.name?.let {
item.name?.also {
view.headlineText = it
} ?: run {
view.headlineText = getString(R.string.genre_unknown)
}
} ?: view.setHeadlineText(R.string.unknown)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,31 +320,21 @@ class NowPlayingFragment : Fragment(R.layout.fragment_now_playing) {
viewModel.mediaMetadata.collectLatest { mediaMetadata ->
val audioTitle = mediaMetadata.displayTitle
?: mediaMetadata.title
audioTitle?.let { title ->
if (audioTitleTextView.text != title) {
audioTitleTextView.text = title
}
audioTitleTextView.isVisible = true
} ?: run {
audioTitleTextView.isVisible = false
?: getString(R.string.unknown)
if (audioTitleTextView.text != audioTitle) {
audioTitleTextView.text = audioTitle
}

mediaMetadata.artist?.let { artist ->
if (artistNameTextView.text != artist) {
artistNameTextView.text = artist
}
artistNameTextView.isVisible = true
} ?: run {
artistNameTextView.isVisible = false
val artistName = mediaMetadata.artist
?: getString(R.string.artist_unknown)
if (artistNameTextView.text != artistName) {
artistNameTextView.text = artistName
}

mediaMetadata.albumTitle?.let { albumTitle ->
if (albumTitleTextView.text != albumTitle) {
albumTitleTextView.text = albumTitle
}
albumTitleTextView.isVisible = true
} ?: run {
albumTitleTextView.isVisible = false
val albumTitle = mediaMetadata.albumTitle
?: getString(R.string.album_unknown)
if (albumTitleTextView.text != albumTitle) {
albumTitleTextView.text = albumTitle
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ class PlaylistFragment : Fragment(R.layout.fragment_playlist) {

override fun ViewHolder.onBindView(item: Audio) {
view.headlineText = item.title
view.supportingText = item.artistName
item.artistName?.also {
view.supportingText = it
} ?: view.setSupportingText(R.string.artist_unknown)
view.trailingSupportingText = TimestampFormatter.formatTimestampMillis(
item.durationMs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ class QueueFragment : Fragment(R.layout.fragment_queue) {
false -> R.drawable.ic_music_note
}
)
view.headlineText = mediaItem.mediaMetadata.title
view.supportingText = mediaItem.mediaMetadata.artist
mediaItem.mediaMetadata.title?.also {
view.headlineText = it
} ?: view.setHeadlineText(R.string.unknown)
mediaItem.mediaMetadata.artist?.also {
view.supportingText = it
} ?: view.setSupportingText(R.string.artist_unknown)
view.trailingSupportingText = mediaItem.mediaMetadata.durationMs?.let {
TimestampFormatter.formatTimestampMillis(it)
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/lineageos/twelve/models/Album.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import org.lineageos.twelve.ext.toByteArray
*/
data class Album(
override val uri: Uri,
val title: String,
val title: String?,
val artistUri: Uri,
val artistName: String,
val artistName: String?,
val year: Int?,
val thumbnail: Thumbnail?,
) : MediaItem<Album> {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/lineageos/twelve/models/Artist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.lineageos.twelve.ext.toByteArray
*/
data class Artist(
override val uri: Uri,
val name: String,
val name: String?,
val thumbnail: Thumbnail?,
) : MediaItem<Artist> {
override val mediaType = MediaType.ARTIST
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/lineageos/twelve/models/Audio.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ data class Audio(
val type: Type,
val durationMs: Long,
val artistUri: Uri,
val artistName: String,
val artistName: String?,
val albumUri: Uri,
val albumTitle: String,
val albumTitle: String?,
val discNumber: Int?,
val trackNumber: Int?,
val genreUri: Uri?,
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/lineageos/twelve/models/Genre.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ data class Genre(
) == 0

override fun toMedia3MediaItem() = buildMediaItem(
title = name ?: "Unknown",
title = name,
mediaId = "$GENRE_MEDIA_ITEM_ID_PREFIX${uri}",
isPlayable = false,
isBrowsable = true,
Expand Down
29 changes: 13 additions & 16 deletions app/src/main/java/org/lineageos/twelve/ui/views/NowPlayingBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
Expand Down Expand Up @@ -103,25 +102,23 @@ class NowPlayingBar @JvmOverloads constructor(
}

fun updateMediaMetadata(mediaMetadata: MediaMetadata) {
mediaMetadata.title?.also {
titleTextView.text = it
titleTextView.isVisible = true
} ?: run {
titleTextView.isVisible = false
val audioTitle = mediaMetadata.displayTitle
?: mediaMetadata.title
?: context.getString(R.string.unknown)
if (titleTextView.text != audioTitle) {
titleTextView.text = audioTitle
}

mediaMetadata.artist?.also {
artistNameTextView.text = it
artistNameTextView.isVisible = true
} ?: run {
artistNameTextView.isVisible = false
val artistName = mediaMetadata.artist
?: context.getString(R.string.artist_unknown)
if (artistNameTextView.text != artistName) {
artistNameTextView.text = artistName
}

mediaMetadata.albumTitle?.also {
albumTitleTextView.text = it
albumTitleTextView.isVisible = true
} ?: run {
albumTitleTextView.isVisible = false
val albumTitle = mediaMetadata.albumTitle
?: context.getString(R.string.album_unknown)
if (albumTitleTextView.text != albumTitle) {
albumTitleTextView.text = albumTitle
}
}

Expand Down
7 changes: 5 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@
<string name="no_results">No results</string>
<string name="shrug" translatable="false">¯\\_(ツ)_/¯</string>

<!-- Genres -->
<string name="genre_unknown">Unknown</string>
<!-- Unknown items -->
<string name="unknown">Unknown</string>
<string name="album_unknown">Unknown album</string>
<string name="artist_unknown">Unknown artist</string>
<string name="genre_unknown">Unknown genre</string>

<!-- Tracks information -->
<string name="year_format" translatable="false">%1$d</string>
Expand Down

0 comments on commit 370814b

Please sign in to comment.