use the TracksActivity for displaying folder tracks too

This commit is contained in:
tibbi 2022-01-02 22:42:18 +01:00
parent e4de0733f9
commit 4c9f9b216c
4 changed files with 57 additions and 32 deletions

View file

@ -25,29 +25,43 @@ import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
const val TYPE_PLAYLIST = 1
const val TYPE_FOLDER = 2
const val TYPE_ALBUM = 3
// this activity is used for displaying Playlist and Folder tracks, also Album tracks with a possible album header at the top
// Artists -> Albums -> Tracks
class TracksActivity : SimpleActivity() {
private var bus: EventBus? = null
private var playlist: Playlist? = null
private var tracksType = 0
private var lastFilePickerPath = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tracks)
bus = EventBus.getDefault()
bus!!.register(this)
val playlistType = object : TypeToken<Playlist>() {}.type
playlist = Gson().fromJson<Playlist>(intent.getStringExtra(PLAYLIST), playlistType)
if (playlist != null) {
tracksType = TYPE_PLAYLIST
invalidateOptionsMenu()
}
val albumType = object : TypeToken<Album>() {}.type
val album = Gson().fromJson<Album>(intent.getStringExtra(ALBUM), albumType)
if (album != null) {
tracksType = TYPE_ALBUM
}
title = playlist?.title ?: album.title
val folder = intent.getStringExtra(FOLDER)
if (folder != null) {
tracksType = TYPE_FOLDER
}
title = playlist?.title ?: album?.title ?: folder
val adjustedPrimaryColor = getAdjustedPrimaryColor()
tracks_fastscroller.updateColors(adjustedPrimaryColor)
@ -61,24 +75,30 @@ class TracksActivity : SimpleActivity() {
ensureBackgroundThread {
val tracks = ArrayList<Track>()
val listItems = ArrayList<ListItem>()
if (playlist != null) {
val playlistTracks = tracksDAO.getTracksFromPlaylist(playlist!!.id)
runOnUiThread {
tracks_placeholder.beVisibleIf(playlistTracks.isEmpty())
tracks_placeholder_2.beVisibleIf(playlistTracks.isEmpty())
when (tracksType) {
TYPE_PLAYLIST -> {
val playlistTracks = tracksDAO.getTracksFromPlaylist(playlist!!.id)
runOnUiThread {
tracks_placeholder.beVisibleIf(playlistTracks.isEmpty())
tracks_placeholder_2.beVisibleIf(playlistTracks.isEmpty())
}
tracks.addAll(playlistTracks)
listItems.addAll(tracks)
}
TYPE_ALBUM -> {
val albumTracks = getAlbumTracksSync(album.id)
albumTracks.sortWith(compareBy({ it.trackId }, { it.title.toLowerCase() }))
tracks.addAll(albumTracks)
tracks.addAll(playlistTracks)
listItems.addAll(tracks)
} else {
val albumTracks = getAlbumTracksSync(album.id)
albumTracks.sortWith(compareBy({ it.trackId }, { it.title.toLowerCase() }))
tracks.addAll(albumTracks)
val coverArt = ContentUris.withAppendedId(artworkUri, album.id).toString()
val header = AlbumHeader(album.title, coverArt, album.year, tracks.size, tracks.sumBy { it.duration }, album.artist)
listItems.add(header)
listItems.addAll(tracks)
}
else -> {
val coverArt = ContentUris.withAppendedId(artworkUri, album.id).toString()
val header = AlbumHeader(album.title, coverArt, album.year, tracks.size, tracks.sumBy { it.duration }, album.artist)
listItems.add(header)
listItems.addAll(tracks)
}
}
runOnUiThread {
@ -121,9 +141,9 @@ class TracksActivity : SimpleActivity() {
menuInflater.inflate(R.menu.menu_playlist, menu)
menu.apply {
findItem(R.id.sort).isVisible = playlist != null
findItem(R.id.add_file_to_playlist).isVisible = playlist != null
findItem(R.id.add_folder_to_playlist).isVisible = playlist != null
findItem(R.id.sort).isVisible = tracksType != TYPE_ALBUM
findItem(R.id.add_file_to_playlist).isVisible = tracksType == TYPE_PLAYLIST
findItem(R.id.add_folder_to_playlist).isVisible = tracksType == TYPE_PLAYLIST
}
updateMenuItemColors(menu)
@ -141,7 +161,7 @@ class TracksActivity : SimpleActivity() {
}
private fun showSortingDialog() {
ChangeSortingDialog(this, ACTIVITY_PLAYLIST) {
ChangeSortingDialog(this, ACTIVITY_PLAYLIST_FOLDER) {
val adapter = tracks_list.adapter as? TracksAdapter ?: return@ChangeSortingDialog
val tracks = adapter.tracks
Track.sorting = config.playlistTracksSorting
@ -209,7 +229,6 @@ class TracksActivity : SimpleActivity() {
val newTracks = tracksDAO.getTracksFromPlaylist(playlist!!.id).toMutableList() as ArrayList<Track>
runOnUiThread {
(tracks_list.adapter as? TracksAdapter)?.updateItems(newTracks)
tracks_placeholder.beVisibleIf(newTracks.isEmpty())
tracks_placeholder_2.beVisibleIf(newTracks.isEmpty())
}
@ -222,11 +241,10 @@ class TracksActivity : SimpleActivity() {
}
private fun itemClicked(track: Track) {
val tracks = if (playlist != null) {
(tracks_list.adapter as? TracksAdapter)?.tracks?.toMutableList() as? ArrayList<Track> ?: ArrayList()
} else {
(tracks_list.adapter as? TracksHeaderAdapter)?.items?.filterIsInstance<Track>()?.toMutableList() as? ArrayList<Track>
?: ArrayList()
val tracks = when (tracksType) {
TYPE_PLAYLIST -> (tracks_list.adapter as? TracksAdapter)?.tracks?.toMutableList() as? ArrayList<Track> ?: ArrayList()
TYPE_ALBUM -> (tracks_list.adapter as? TracksHeaderAdapter)?.items?.filterIsInstance<Track>()?.toMutableList() as? ArrayList<Track> ?: ArrayList()
else -> ArrayList()
}
resetQueueItems(tracks) {

View file

@ -25,7 +25,7 @@ class ChangeSortingDialog(val activity: Activity, val location: Int, val callbac
TAB_FOLDERS -> config.folderSorting
TAB_ARTISTS -> config.artistSorting
TAB_ALBUMS -> config.albumSorting
ACTIVITY_PLAYLIST -> config.playlistTracksSorting
ACTIVITY_PLAYLIST_FOLDER -> config.playlistTracksSorting
else -> config.trackSorting
}
@ -57,7 +57,7 @@ class ChangeSortingDialog(val activity: Activity, val location: Int, val callbac
radioItems.add(RadioItem(1, activity.getString(R.string.artist_name), PLAYER_SORT_BY_ARTIST_TITLE))
radioItems.add(RadioItem(2, activity.getString(R.string.year), PLAYER_SORT_BY_YEAR))
}
TAB_TRACKS, ACTIVITY_PLAYLIST -> {
TAB_TRACKS, ACTIVITY_PLAYLIST_FOLDER -> {
radioItems.add(RadioItem(0, activity.getString(R.string.title), PLAYER_SORT_BY_TITLE))
radioItems.add(RadioItem(1, activity.getString(R.string.artist), PLAYER_SORT_BY_ARTIST_TITLE))
radioItems.add(RadioItem(2, activity.getString(R.string.duration), PLAYER_SORT_BY_DURATION))
@ -103,7 +103,7 @@ class ChangeSortingDialog(val activity: Activity, val location: Int, val callbac
TAB_ARTISTS -> config.artistSorting = sorting
TAB_ALBUMS -> config.albumSorting = sorting
TAB_TRACKS -> config.trackSorting = sorting
ACTIVITY_PLAYLIST -> config.playlistTracksSorting = sorting
ACTIVITY_PLAYLIST_FOLDER -> config.playlistTracksSorting = sorting
}
callback()

View file

@ -1,6 +1,7 @@
package com.simplemobiletools.musicplayer.fragments
import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.areSystemAnimationsEnabled
@ -8,12 +9,14 @@ import com.simplemobiletools.commons.extensions.beGoneIf
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.activities.SimpleActivity
import com.simplemobiletools.musicplayer.activities.TracksActivity
import com.simplemobiletools.musicplayer.adapters.FoldersAdapter
import com.simplemobiletools.musicplayer.dialogs.ChangeSortingDialog
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.getAlbumTracksSync
import com.simplemobiletools.musicplayer.extensions.getAlbumsSync
import com.simplemobiletools.musicplayer.extensions.getArtistsSync
import com.simplemobiletools.musicplayer.helpers.FOLDER
import com.simplemobiletools.musicplayer.helpers.TAB_FOLDERS
import com.simplemobiletools.musicplayer.models.Album
import com.simplemobiletools.musicplayer.models.Folder
@ -54,7 +57,10 @@ class FoldersFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
val adapter = folders_list.adapter
if (adapter == null) {
FoldersAdapter(activity, folders, folders_list) {
Intent(activity, TracksActivity::class.java).apply {
putExtra(FOLDER, (it as Folder).title)
activity.startActivity(this)
}
}.apply {
folders_list.adapter = this
}

View file

@ -15,6 +15,7 @@ const val ARTIST = "artist"
const val ALBUM = "album"
const val TRACK = "track"
const val PLAYLIST = "playlist"
const val FOLDER = "folder"
private const val PATH = "com.simplemobiletools.musicplayer.action."
val artworkUri = Uri.parse("content://media/external/audio/albumart")
@ -67,7 +68,7 @@ const val TAB_FOLDERS = 1
const val TAB_ARTISTS = 2
const val TAB_ALBUMS = 3
const val TAB_TRACKS = 4
const val ACTIVITY_PLAYLIST = 5
const val ACTIVITY_PLAYLIST_FOLDER = 5
// use custom sorting constants, there are too many app specific ones
const val PLAYER_SORT_BY_TITLE = 1