add playlist songs into the queue on click
This commit is contained in:
parent
32854932a6
commit
e79313ce51
8 changed files with 44 additions and 30 deletions
|
@ -9,7 +9,7 @@ import com.simplemobiletools.commons.extensions.getFormattedDuration
|
|||
import com.simplemobiletools.musicplayer.R
|
||||
import com.simplemobiletools.musicplayer.adapters.AlbumsAdapter
|
||||
import com.simplemobiletools.musicplayer.extensions.getAlbums
|
||||
import com.simplemobiletools.musicplayer.extensions.getTracksSync
|
||||
import com.simplemobiletools.musicplayer.extensions.getAlbumTracksSync
|
||||
import com.simplemobiletools.musicplayer.extensions.resetQueueItems
|
||||
import com.simplemobiletools.musicplayer.helpers.ALBUM
|
||||
import com.simplemobiletools.musicplayer.helpers.ARTIST
|
||||
|
@ -47,7 +47,7 @@ class AlbumsActivity : SimpleActivity() {
|
|||
var trackFullDuration = 0
|
||||
val tracksToAdd = ArrayList<Track>()
|
||||
albums.forEach {
|
||||
val tracks = getTracksSync(it.id)
|
||||
val tracks = getAlbumTracksSync(it.id)
|
||||
trackFullDuration += tracks.sumBy { it.duration }
|
||||
tracksToAdd.addAll(tracks)
|
||||
}
|
||||
|
|
|
@ -6,18 +6,14 @@ import android.os.Bundle
|
|||
import android.view.Menu
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||
import com.simplemobiletools.musicplayer.R
|
||||
import com.simplemobiletools.musicplayer.adapters.SongsAdapter
|
||||
import com.simplemobiletools.musicplayer.extensions.getTracks
|
||||
import com.simplemobiletools.musicplayer.extensions.getAlbumTracksSync
|
||||
import com.simplemobiletools.musicplayer.extensions.getPlaylistSongs
|
||||
import com.simplemobiletools.musicplayer.extensions.resetQueueItems
|
||||
import com.simplemobiletools.musicplayer.helpers.ALBUM
|
||||
import com.simplemobiletools.musicplayer.helpers.RESTART_PLAYER
|
||||
import com.simplemobiletools.musicplayer.helpers.TRACK
|
||||
import com.simplemobiletools.musicplayer.helpers.artworkUri
|
||||
import com.simplemobiletools.musicplayer.models.Album
|
||||
import com.simplemobiletools.musicplayer.models.AlbumHeader
|
||||
import com.simplemobiletools.musicplayer.models.Events
|
||||
import com.simplemobiletools.musicplayer.models.ListItem
|
||||
import com.simplemobiletools.musicplayer.helpers.*
|
||||
import com.simplemobiletools.musicplayer.models.*
|
||||
import com.simplemobiletools.musicplayer.services.MusicService
|
||||
import kotlinx.android.synthetic.main.activity_tracks.*
|
||||
import kotlinx.android.synthetic.main.view_current_track_bar.*
|
||||
|
@ -36,19 +32,30 @@ class TracksActivity : SimpleActivity() {
|
|||
bus = EventBus.getDefault()
|
||||
bus!!.register(this)
|
||||
|
||||
val playlistType = object : TypeToken<Playlist>() {}.type
|
||||
val playlist = Gson().fromJson<Playlist>(intent.getStringExtra(PLAYLIST), playlistType)
|
||||
|
||||
val albumType = object : TypeToken<Album>() {}.type
|
||||
val album = Gson().fromJson<Album>(intent.getStringExtra(ALBUM), albumType)
|
||||
title = album.title
|
||||
|
||||
getTracks(album.id) { tracks ->
|
||||
val items = ArrayList<ListItem>()
|
||||
val coverArt = ContentUris.withAppendedId(artworkUri, album.id.toLong()).toString()
|
||||
val header = AlbumHeader(album.title, coverArt, album.year, tracks.size, tracks.sumBy { it.duration }, album.artist)
|
||||
items.add(header)
|
||||
items.addAll(tracks)
|
||||
title = playlist?.title ?: album.title
|
||||
|
||||
ensureBackgroundThread {
|
||||
val tracks = ArrayList<Track>()
|
||||
val listItems = ArrayList<ListItem>()
|
||||
if (playlist != null) {
|
||||
tracks.addAll(getPlaylistSongs(playlist.id))
|
||||
listItems.addAll(tracks)
|
||||
} else {
|
||||
tracks.addAll(getAlbumTracksSync(album.id))
|
||||
val coverArt = ContentUris.withAppendedId(artworkUri, album.id.toLong()).toString()
|
||||
val header = AlbumHeader(album.title, coverArt, album.year, tracks.size, tracks.sumBy { it.duration }, album.artist)
|
||||
listItems.add(header)
|
||||
listItems.addAll(tracks)
|
||||
}
|
||||
|
||||
runOnUiThread {
|
||||
SongsAdapter(this, items, tracks_list) {
|
||||
SongsAdapter(this, listItems, tracks_list) {
|
||||
resetQueueItems(tracks) {
|
||||
Intent(this, TrackActivity::class.java).apply {
|
||||
putExtra(TRACK, Gson().toJson(it))
|
||||
|
|
|
@ -17,7 +17,7 @@ import com.simplemobiletools.commons.views.MyRecyclerView
|
|||
import com.simplemobiletools.musicplayer.R
|
||||
import com.simplemobiletools.musicplayer.activities.SimpleActivity
|
||||
import com.simplemobiletools.musicplayer.extensions.addTracksToPlaylist
|
||||
import com.simplemobiletools.musicplayer.extensions.getTracksSync
|
||||
import com.simplemobiletools.musicplayer.extensions.getAlbumTracksSync
|
||||
import com.simplemobiletools.musicplayer.models.Album
|
||||
import com.simplemobiletools.musicplayer.models.AlbumSection
|
||||
import com.simplemobiletools.musicplayer.models.ListItem
|
||||
|
@ -105,7 +105,7 @@ class AlbumsAdapter(activity: SimpleActivity, val items: ArrayList<ListItem>, re
|
|||
ensureBackgroundThread {
|
||||
val tracks = getSelectedTracks()
|
||||
getSelectedAlbums().forEach {
|
||||
tracks.addAll(activity.getTracksSync(it.id))
|
||||
tracks.addAll(activity.getAlbumTracksSync(it.id))
|
||||
}
|
||||
|
||||
activity.runOnUiThread {
|
||||
|
|
|
@ -17,7 +17,7 @@ import com.simplemobiletools.musicplayer.R
|
|||
import com.simplemobiletools.musicplayer.activities.SimpleActivity
|
||||
import com.simplemobiletools.musicplayer.extensions.addTracksToPlaylist
|
||||
import com.simplemobiletools.musicplayer.extensions.getAlbumsSync
|
||||
import com.simplemobiletools.musicplayer.extensions.getTracksSync
|
||||
import com.simplemobiletools.musicplayer.extensions.getAlbumTracksSync
|
||||
import com.simplemobiletools.musicplayer.models.Artist
|
||||
import com.simplemobiletools.musicplayer.models.Track
|
||||
import kotlinx.android.synthetic.main.item_artist.view.*
|
||||
|
@ -75,7 +75,7 @@ class ArtistsAdapter(activity: SimpleActivity, val artists: ArrayList<Artist>, r
|
|||
getSelectedArtists().forEach { artist ->
|
||||
val albums = activity.getAlbumsSync(artist)
|
||||
albums.forEach {
|
||||
tracks.addAll(activity.getTracksSync(it.id))
|
||||
tracks.addAll(activity.getAlbumTracksSync(it.id))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -165,14 +165,14 @@ fun Context.getAlbumsSync(artist: Artist): ArrayList<Album> {
|
|||
return albums
|
||||
}
|
||||
|
||||
fun Context.getTracks(albumId: Int, callback: (tracks: ArrayList<Track>) -> Unit) {
|
||||
fun Context.getAlbumTracks(albumId: Int, callback: (tracks: ArrayList<Track>) -> Unit) {
|
||||
ensureBackgroundThread {
|
||||
val tracks = getTracksSync(albumId)
|
||||
val tracks = getAlbumTracksSync(albumId)
|
||||
callback(tracks)
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.getTracksSync(albumId: Int): ArrayList<Track> {
|
||||
fun Context.getAlbumTracksSync(albumId: Int): ArrayList<Track> {
|
||||
val tracks = ArrayList<Track>()
|
||||
val uri = Audio.Media.EXTERNAL_CONTENT_URI
|
||||
val projection = arrayOf(
|
||||
|
|
|
@ -16,7 +16,7 @@ import com.simplemobiletools.musicplayer.activities.AlbumsActivity
|
|||
import com.simplemobiletools.musicplayer.activities.SimpleActivity
|
||||
import com.simplemobiletools.musicplayer.adapters.ArtistsAdapter
|
||||
import com.simplemobiletools.musicplayer.extensions.getAlbumsSync
|
||||
import com.simplemobiletools.musicplayer.extensions.getTracksSync
|
||||
import com.simplemobiletools.musicplayer.extensions.getAlbumTracksSync
|
||||
import com.simplemobiletools.musicplayer.helpers.ARTIST
|
||||
import com.simplemobiletools.musicplayer.models.Artist
|
||||
import kotlinx.android.synthetic.main.fragment_artists.view.*
|
||||
|
@ -94,7 +94,7 @@ class ArtistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
|
|||
artist.albumArtId = albumId
|
||||
}
|
||||
|
||||
artist.trackCnt += activity.getTracksSync(albumId.toInt()).size
|
||||
artist.trackCnt += activity.getAlbumTracksSync(albumId.toInt()).size
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
package com.simplemobiletools.musicplayer.fragments
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.AttributeSet
|
||||
import com.google.gson.Gson
|
||||
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
|
||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||
import com.simplemobiletools.musicplayer.activities.SimpleActivity
|
||||
import com.simplemobiletools.musicplayer.activities.TracksActivity
|
||||
import com.simplemobiletools.musicplayer.adapters.PlaylistsAdapter
|
||||
import com.simplemobiletools.musicplayer.extensions.playlistChanged
|
||||
import com.simplemobiletools.musicplayer.extensions.playlistDAO
|
||||
import com.simplemobiletools.musicplayer.extensions.tracksDAO
|
||||
import com.simplemobiletools.musicplayer.helpers.PLAYLIST
|
||||
import com.simplemobiletools.musicplayer.models.Playlist
|
||||
import kotlinx.android.synthetic.main.fragment_playlists.view.*
|
||||
|
||||
|
@ -22,7 +25,10 @@ class PlaylistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
|
|||
|
||||
activity.runOnUiThread {
|
||||
PlaylistsAdapter(activity, playlists, null, playlists_list) {
|
||||
activity.playlistChanged((it as Playlist).id)
|
||||
Intent(activity, TracksActivity::class.java).apply {
|
||||
putExtra(PLAYLIST, Gson().toJson(it))
|
||||
activity.startActivity(this)
|
||||
}
|
||||
}.apply {
|
||||
playlists_list.adapter = this
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ const val RESTART_PLAYER = "RESTART_PLAYER"
|
|||
const val ARTIST = "artist"
|
||||
const val ALBUM = "album"
|
||||
const val TRACK = "track"
|
||||
const val PLAYLIST = "playlist"
|
||||
|
||||
private const val PATH = "com.simplemobiletools.musicplayer.action."
|
||||
val artworkUri = Uri.parse("content://media/external/audio/albumart")
|
||||
|
|
Loading…
Reference in a new issue