Skip to content

Commit

Permalink
Refactor in preparation of FlowReader
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Nov 16, 2024
1 parent a65b93d commit 91a4c68
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ import kotlinx.coroutines.launch
import org.akanework.gramophone.BuildConfig
import org.akanework.gramophone.logic.use
import org.akanework.gramophone.logic.utils.exoplayer.EndedWorkaroundPlayer
import uk.akane.libphonograph.items.EXTRA_ADD_DATE
import uk.akane.libphonograph.items.EXTRA_ALBUM_ID
import uk.akane.libphonograph.items.EXTRA_ARTIST_ID
import uk.akane.libphonograph.items.EXTRA_AUTHOR
import uk.akane.libphonograph.items.EXTRA_CD_TRACK_NUMBER
import uk.akane.libphonograph.items.EXTRA_GENRE_ID
import uk.akane.libphonograph.items.EXTRA_MODIFIED_DATE
import uk.akane.libphonograph.items.addDate
import uk.akane.libphonograph.items.albumId
import uk.akane.libphonograph.items.artistId
import uk.akane.libphonograph.items.author
import uk.akane.libphonograph.items.cdTrackNumber
import uk.akane.libphonograph.items.genreId
import uk.akane.libphonograph.items.modifiedDate

@OptIn(UnstableApi::class)
class LastPlayedManager(
Expand Down Expand Up @@ -108,21 +122,22 @@ class LastPlayedManager(
b.writeInt(it.mediaMetadata.releaseYear)
b.writeBool(it.mediaMetadata.isBrowsable)
b.writeBool(it.mediaMetadata.isPlayable)
b.writeLong(it.mediaMetadata.extras?.getLong("AddDate"))
b.writeLong(it.mediaMetadata.addDate)
b.writeStringSafe(it.mediaMetadata.writer)
b.writeStringSafe(it.mediaMetadata.compilation)
b.writeStringSafe(it.mediaMetadata.composer)
b.writeStringSafe(it.mediaMetadata.genre)
b.writeInt(it.mediaMetadata.recordingDay)
b.writeInt(it.mediaMetadata.recordingMonth)
b.writeLong(it.mediaMetadata.extras?.getLong("ArtistId"))
b.writeLong(it.mediaMetadata.extras?.getLong("AlbumId"))
b.writeLong(it.mediaMetadata.extras?.getLong("GenreId"))
b.writeStringSafe(it.mediaMetadata.extras?.getString("Author"))
b.writeInt(it.mediaMetadata.extras?.getInt("CdTrackNumber"))
b.writeLong(it.mediaMetadata.artistId)
b.writeLong(it.mediaMetadata.albumId)
b.writeLong(it.mediaMetadata.genreId)
b.writeStringSafe(it.mediaMetadata.author)
b.skip() // used to be CdTrackNumber
b.writeLong(it.mediaMetadata.durationMs)
b.writeStringUnsafe(it.mediaMetadata.extras?.getString("Path"))
b.writeLong(it.mediaMetadata.extras?.getLong("ModifiedDate"))
b.skip() // used to be Path
b.writeLong(it.mediaMetadata.modifiedDate)
b.writeStringSafe(it.mediaMetadata.cdTrackNumber)
b.toString()
})
prefs.edit {
Expand Down Expand Up @@ -177,6 +192,7 @@ class LastPlayedManager(
PrefsListUtils.parse(lastPlayedLst, lastPlayedGrp)
.map {
val b = SafeDelimitedStringDecat(":", it)
// add new entries at the bottom and remember they are null for upgrade path
val mediaId = b.readStringUnsafe()
val uri = b.readUri()
val mimeType = b.readStringSafe()
Expand All @@ -202,10 +218,11 @@ class LastPlayedManager(
val albumId = b.readLong()
val genreId = b.readLong()
val author = b.readStringSafe()
val cdTrackNumber = b.readInt()
b.skip() // used to be CdTrackNumber
val duration = b.readLong()
val path = b.readStringUnsafe()
b.skip() // used to be Path
val modifiedDate = b.readLong()
val cdTrackNumber = b.readStringSafe()
MediaItem.Builder()
.setUri(uri)
.setMediaId(mediaId!!)
Expand Down Expand Up @@ -233,24 +250,21 @@ class LastPlayedManager(
.setIsPlayable(isPlayable)
.setExtras(Bundle().apply {
if (addDate != null) {
putLong("AddDate", addDate)
putLong(EXTRA_ADD_DATE, addDate)
}
if (artistId != null) {
putLong("ArtistId", artistId)
putLong(EXTRA_ARTIST_ID, artistId)
}
if (albumId != null) {
putLong("AlbumId", albumId)
putLong(EXTRA_ALBUM_ID, albumId)
}
if (genreId != null) {
putLong("GenreId", genreId)
putLong(EXTRA_GENRE_ID, genreId)
}
if (cdTrackNumber != null) {
putInt("CdTrackNumber", cdTrackNumber)
}
putString("Author", author)
putString("Path", path)
putString(EXTRA_CD_TRACK_NUMBER, cdTrackNumber)
putString(EXTRA_AUTHOR, author)
if (modifiedDate != null) {
putLong("ModifiedDate", modifiedDate)
putLong(EXTRA_MODIFIED_DATE, modifiedDate)
}
})
.build()
Expand Down Expand Up @@ -324,11 +338,11 @@ private class SafeDelimitedStringConcat(private val delimiter: String) {
fun writeBase64(b: ByteArray?) = append(b?.let { Base64.encodeToString(it, Base64.DEFAULT) })
fun writeStringSafe(s: CharSequence?) =
writeBase64(s?.toString()?.toByteArray(StandardCharsets.UTF_8))

fun writeInt(i: Int?) = append(i?.toString())
fun writeLong(i: Long?) = append(i?.toString())
fun writeBool(b: Boolean?) = append(b?.toString())
fun writeUri(u: Uri?) = writeStringSafe(u?.toString())
fun skip() = append(null)
}

private class SafeDelimitedStringDecat(delimiter: String, str: String) {
Expand All @@ -347,6 +361,9 @@ private class SafeDelimitedStringDecat(delimiter: String, str: String) {
fun readLong(): Long? = read()?.toLong()
fun readBool(): Boolean? = read()?.toBooleanStrict()
fun readUri(): Uri? = Uri.parse(readStringSafe())
fun skip() {
read()
}
}

private object PrefsListUtils {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import org.akanework.gramophone.R
import org.akanework.gramophone.ui.LibraryViewModel
import uk.akane.libphonograph.reader.Reader
import uk.akane.libphonograph.reader.ReaderResult
import uk.akane.libphonograph.reader.SimpleReader
import uk.akane.libphonograph.reader.SimpleReaderResult

/**
* [MediaStoreUtils] contains all the methods for reading
Expand All @@ -55,73 +57,18 @@ object MediaStoreUtils {
* @return
*/
@OptIn(UnstableApi::class)
private fun getAllSongs(context: Context): ReaderResult<MediaItem> {
private fun getAllSongs(context: Context): SimpleReaderResult {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val limitValue = if (BuildConfig.DISABLE_MEDIA_STORE_FILTER) 0 else prefs.getInt(
"mediastore_filter",
context.resources.getInteger(R.integer.filter_default_sec)
)
val folderFilter = prefs.getStringSet("folderFilter", setOf()) ?: setOf()
return Reader.readFromMediaStore(
return SimpleReader.readFromMediaStore(
context,
{ uri, mediaId, mimeType, title, writer, compilation, composer, artist,
albumTitle, albumArtist, artworkUri, cdTrackNumber, trackNumber,
discNumber, genre, recordingDay, recordingMonth, recordingYear,
releaseYear, artistId, albumId, genreId, author, addDate, duration,
modifiedDate ->
return@readFromMediaStore MediaItem
.Builder()
.setUri(uri)
.setMediaId(mediaId.toString())
.setMimeType(mimeType)
.setMediaMetadata(
MediaMetadata
.Builder()
.setIsBrowsable(false)
.setIsPlayable(true)
.setDurationMs(duration)
.setTitle(title)
.setWriter(writer)
.setCompilation(compilation)
.setComposer(composer)
.setArtist(artist)
.setAlbumTitle(albumTitle)
.setAlbumArtist(albumArtist)
.setArtworkUri(artworkUri)
.setTrackNumber(trackNumber)
.setDiscNumber(discNumber)
.setGenre(genre)
.setRecordingDay(recordingDay)
.setRecordingMonth(recordingMonth)
.setRecordingYear(recordingYear)
.setReleaseYear(releaseYear)
.setExtras(Bundle().apply {
if (artistId != null) {
putLong("ArtistId", artistId)
}
if (albumId != null) {
putLong("AlbumId", albumId)
}
if (genreId != null) {
putLong("GenreId", genreId)
}
putString("Author", author)
if (addDate != null) {
putLong("AddDate", addDate)
}
if (modifiedDate != null) {
putLong("ModifiedDate", modifiedDate)
}
cdTrackNumber?.toIntOrNull()
?.let { it1 -> putInt("CdTrackNumber", it1) }
})
.build(),
).build()
},
minSongLengthSeconds = limitValue.toLong(),
blackListSet = folderFilter,
shouldUseEnhancedCoverReading = null,
shouldLoadPlaylists = true
shouldUseEnhancedCoverReading = null
)
}

Expand All @@ -133,14 +80,14 @@ object MediaStoreUtils {
val pairObject = getAllSongs(context)
CoroutineScope(Dispatchers.Main).launch {
libraryViewModel.mediaItemList.value = pairObject.songList
libraryViewModel.albumItemList.value = pairObject.albumList!!
libraryViewModel.artistItemList.value = pairObject.artistList!!
libraryViewModel.albumArtistItemList.value = pairObject.albumArtistList!!
libraryViewModel.genreItemList.value = pairObject.genreList!!
libraryViewModel.dateItemList.value = pairObject.dateList!!
libraryViewModel.playlistList.value = pairObject.playlistList!!
libraryViewModel.folderStructure.value = pairObject.folderStructure!!
libraryViewModel.shallowFolderStructure.value = pairObject.shallowFolder!!
libraryViewModel.albumItemList.value = pairObject.albumList
libraryViewModel.artistItemList.value = pairObject.artistList
libraryViewModel.albumArtistItemList.value = pairObject.albumArtistList
libraryViewModel.genreItemList.value = pairObject.genreList
libraryViewModel.dateItemList.value = pairObject.dateList
libraryViewModel.playlistList.value = pairObject.playlistList
libraryViewModel.folderStructure.value = pairObject.folderStructure
libraryViewModel.shallowFolderStructure.value = pairObject.shallowFolder
libraryViewModel.allFolderSet.value = pairObject.folders
then?.let { it() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ import uk.akane.libphonograph.items.Playlist
*/
class LibraryViewModel : ViewModel() {
val mediaItemList: MutableLiveData<List<MediaItem>> = MutableLiveData()
val albumItemList: MutableLiveData<List<Album<MediaItem>>> = MutableLiveData()
val albumArtistItemList: MutableLiveData<List<Artist<MediaItem>>> = MutableLiveData()
val artistItemList: MutableLiveData<List<Artist<MediaItem>>> = MutableLiveData()
val genreItemList: MutableLiveData<List<Genre<MediaItem>>> = MutableLiveData()
val dateItemList: MutableLiveData<List<Date<MediaItem>>> = MutableLiveData()
val playlistList: MutableLiveData<List<Playlist<MediaItem>>> = MutableLiveData()
val folderStructure: MutableLiveData<FileNode<MediaItem>> = MutableLiveData()
val shallowFolderStructure: MutableLiveData<FileNode<MediaItem>> = MutableLiveData()
val albumItemList: MutableLiveData<List<Album>> = MutableLiveData()
val albumArtistItemList: MutableLiveData<List<Artist>> = MutableLiveData()
val artistItemList: MutableLiveData<List<Artist>> = MutableLiveData()
val genreItemList: MutableLiveData<List<Genre>> = MutableLiveData()
val dateItemList: MutableLiveData<List<Date>> = MutableLiveData()
val playlistList: MutableLiveData<List<Playlist>> = MutableLiveData()
val folderStructure: MutableLiveData<FileNode> = MutableLiveData()
val shallowFolderStructure: MutableLiveData<FileNode> = MutableLiveData()
val allFolderSet: MutableLiveData<Set<String>> = MutableLiveData()
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ import androidx.activity.viewModels
import androidx.appcompat.widget.PopupMenu
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.media3.common.MediaItem
import org.akanework.gramophone.R
import org.akanework.gramophone.ui.LibraryViewModel
import org.akanework.gramophone.ui.fragments.GeneralSubFragment
import uk.akane.libphonograph.items.Album

class AlbumAdapter(
fragment: Fragment,
albumList: MutableLiveData<List<Album<MediaItem>>>?,
albumList: MutableLiveData<List<Album>>?,
ownsView: Boolean = true,
isSubFragment: Boolean = false,
fallbackSpans: Int = 1
) : BaseAdapter<Album<MediaItem>>
) : BaseAdapter<Album>
(
fragment,
liveData = albumList,
Expand All @@ -52,7 +51,7 @@ class AlbumAdapter(

constructor(
fragment: Fragment,
albumList: List<Album<MediaItem>>,
albumList: List<Album>,
isSubFragment: Boolean = false,
fallbackSpans: Int = 1
) : this(
Expand All @@ -65,7 +64,7 @@ class AlbumAdapter(
updateList(albumList, now = true, false)
}

override fun virtualTitleOf(item: Album<MediaItem>): String {
override fun virtualTitleOf(item: Album): String {
return context.getString(R.string.unknown_album)
}

Expand All @@ -82,7 +81,7 @@ class AlbumAdapter(
}
}

override fun onClick(item: Album<MediaItem>) {
override fun onClick(item: Album) {
mainActivity.startFragment(GeneralSubFragment()) {
putInt("Position", item.let {
if (ownsView) toRawPos(it) else {
Expand All @@ -93,7 +92,7 @@ class AlbumAdapter(
}
}

override fun onMenu(item: Album<MediaItem>, popupMenu: PopupMenu) {
override fun onMenu(item: Album, popupMenu: PopupMenu) {
popupMenu.inflate(R.menu.more_menu_less)

popupMenu.setOnMenuItemClickListener { it1 ->
Expand All @@ -120,18 +119,18 @@ class AlbumAdapter(
}
}

class StoreAlbumHelper : StoreItemHelper<Album<MediaItem>>(
class StoreAlbumHelper : StoreItemHelper<Album>(
setOf(
Sorter.Type.ByTitleDescending, Sorter.Type.ByTitleAscending,
Sorter.Type.ByArtistDescending, Sorter.Type.ByArtistAscending,
Sorter.Type.BySizeDescending, Sorter.Type.BySizeAscending
)
) {
override fun getArtist(item: Album<MediaItem>): String? {
override fun getArtist(item: Album): String? {
return item.albumArtist
}

override fun getCover(item: Album<MediaItem>): Uri? {
override fun getCover(item: Album): Uri? {
return item.cover ?: super.getCover(item)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import uk.akane.libphonograph.items.Artist
*/
class ArtistAdapter(
fragment: Fragment,
private val artistList: MutableLiveData<List<Artist<MediaItem>>>,
private val albumArtists: MutableLiveData<List<Artist<MediaItem>>>,
) : BaseAdapter<Artist<MediaItem>>
private val artistList: MutableLiveData<List<Artist>>,
private val albumArtists: MutableLiveData<List<Artist>>,
) : BaseAdapter<Artist>
(
fragment,
liveData = null,
Expand All @@ -48,7 +48,7 @@ class ArtistAdapter(
defaultLayoutType = LayoutType.LIST
) {

override fun virtualTitleOf(item: Artist<MediaItem>): String {
override fun virtualTitleOf(item: Artist): String {
return context.getString(R.string.unknown_artist)
}

Expand All @@ -61,7 +61,7 @@ class ArtistAdapter(
liveData = if (isAlbumArtist) albumArtists else artistList
}

override fun onClick(item: Artist<MediaItem>) {
override fun onClick(item: Artist) {
mainActivity.startFragment(ArtistSubFragment()) {
putInt("Position", toRawPos(item))
putInt(
Expand All @@ -74,7 +74,7 @@ class ArtistAdapter(
}
}

override fun onMenu(item: Artist<MediaItem>, popupMenu: PopupMenu) {
override fun onMenu(item: Artist, popupMenu: PopupMenu) {
popupMenu.inflate(R.menu.more_menu_less)

popupMenu.setOnMenuItemClickListener { it1 ->
Expand Down Expand Up @@ -102,7 +102,7 @@ class ArtistAdapter(
}
}

override fun createDecorAdapter(): BaseDecorAdapter<out BaseAdapter<Artist<MediaItem>>> {
override fun createDecorAdapter(): BaseDecorAdapter<out BaseAdapter<Artist>> {
return ArtistDecorAdapter(this)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ abstract class BaseAdapter<T>(
NONE, LIST, COMPACT_LIST, GRID
}

open class StoreItemHelper<T : Item<MediaItem>>(
open class StoreItemHelper<T : Item>(
typesSupported: Set<Sorter.Type> = setOf(
Sorter.Type.ByTitleDescending, Sorter.Type.ByTitleAscending,
Sorter.Type.BySizeDescending, Sorter.Type.BySizeAscending
Expand Down
Loading

0 comments on commit 91a4c68

Please sign in to comment.