use TracksAdapter at opening a playlist

This commit is contained in:
tibbi 2020-10-23 12:26:00 +02:00
parent f54f1325d5
commit 99a02e69e8
2 changed files with 31 additions and 29 deletions

View file

@ -8,6 +8,7 @@ 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.TracksAdapter
import com.simplemobiletools.musicplayer.adapters.TracksHeaderAdapter
import com.simplemobiletools.musicplayer.extensions.getAlbumTracksSync
import com.simplemobiletools.musicplayer.extensions.getPlaylistTracks
@ -52,27 +53,32 @@ class TracksActivity : SimpleActivity() {
albumTracks.sortWith(compareBy({ it.trackId }, { it.title.toLowerCase() }))
tracks.addAll(albumTracks)
val coverArt = ContentUris.withAppendedId(artworkUri, album.id.toLong()).toString()
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 {
val adapter = TracksHeaderAdapter(this, listItems, tracks_list, album == null, tracks_fastscroller) {
resetQueueItems(tracks) {
Intent(this, TrackActivity::class.java).apply {
putExtra(TRACK, Gson().toJson(it))
putExtra(RESTART_PLAYER, true)
startActivity(this)
}
val adapter = if (playlist != null) {
TracksAdapter(this, tracks, tracks_list, tracks_fastscroller) {
itemClicked(tracks, it as Track)
}
} else {
TracksHeaderAdapter(this, listItems, tracks_list, tracks_fastscroller) {
itemClicked(tracks, it as Track)
}
}.apply {
tracks_list.adapter = this
}
tracks_list.adapter = adapter
tracks_fastscroller.setViews(tracks_list) {
val listItem = adapter.items.getOrNull(it)
val listItem = when (adapter) {
is TracksAdapter -> adapter.tracks.getOrNull(it)
is TracksHeaderAdapter -> adapter.items.getOrNull(it)
else -> return@setViews
}
if (listItem is Track) {
tracks_fastscroller.updateBubbleText(listItem.title)
} else if (listItem is AlbumHeader) {
@ -110,6 +116,16 @@ class TracksActivity : SimpleActivity() {
current_track_bar.updateTrackState(MusicService.getIsPlaying())
}
private fun itemClicked(tracks: ArrayList<Track>, track: Track) {
resetQueueItems(tracks) {
Intent(this, TrackActivity::class.java).apply {
putExtra(TRACK, Gson().toJson(track))
putExtra(RESTART_PLAYER, true)
startActivity(this)
}
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun trackChangedEvent(event: Events.TrackChanged) {
current_track_bar.updateCurrentTrack(event.track)

View file

@ -25,7 +25,7 @@ import kotlinx.android.synthetic.main.item_album_header.view.*
import kotlinx.android.synthetic.main.item_track.view.*
import java.util.*
class TracksHeaderAdapter(activity: SimpleActivity, val items: ArrayList<ListItem>, recyclerView: MyRecyclerView, val showAlbumCover: Boolean, fastScroller: FastScroller,
class TracksHeaderAdapter(activity: SimpleActivity, val items: ArrayList<ListItem>, recyclerView: MyRecyclerView, fastScroller: FastScroller,
itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) {
private val ITEM_HEADER = 0
@ -119,23 +119,9 @@ class TracksHeaderAdapter(activity: SimpleActivity, val items: ArrayList<ListIte
}
track_duration.text = track.duration.getFormattedDuration()
if (showAlbumCover) {
track_image.beVisible()
track_id.beGone()
val options = RequestOptions()
.error(placeholder)
.transform(CenterCrop(), RoundedCorners(8))
Glide.with(activity)
.load(track.coverArt)
.apply(options)
.into(findViewById(R.id.track_image))
} else {
track_image.beGone()
track_id.beVisible()
track_id.text = track.trackId.toString()
}
track_id.text = track.trackId.toString()
track_image.beGone()
track_id.beVisible()
}
}