create an activity extension for adding tracks to playlists

This commit is contained in:
tibbi 2020-10-17 21:47:21 +02:00
parent c17b6be1c5
commit 99169a71ee
2 changed files with 29 additions and 18 deletions

View file

@ -10,12 +10,10 @@ import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
import com.simplemobiletools.commons.extensions.getFormattedDuration
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.activities.SimpleActivity
import com.simplemobiletools.musicplayer.dialogs.SelectPlaylistDialog
import com.simplemobiletools.musicplayer.helpers.RoomHelper
import com.simplemobiletools.musicplayer.extensions.addTracksToPlaylist
import com.simplemobiletools.musicplayer.models.AlbumHeader
import com.simplemobiletools.musicplayer.models.ListItem
import com.simplemobiletools.musicplayer.models.Track
@ -92,23 +90,11 @@ class SongsAdapter(activity: SimpleActivity, val items: ArrayList<ListItem>, rec
override fun onActionModeDestroyed() {}
private fun addToPlaylist() {
SelectPlaylistDialog(activity) { playlistId ->
val tracks = ArrayList<Track>()
getSelectedTracks().forEach {
it.playListId = playlistId
tracks.add(it)
}
ensureBackgroundThread {
RoomHelper(activity).insertTracksWithPlaylist(tracks)
activity.runOnUiThread {
activity.addTracksToPlaylist(getSelectedTracks()) {
finishActMode()
notifyDataSetChanged()
}
}
}
}
private fun getSelectedTracks(): List<Track> = items.filter { it is Track && selectedKeys.contains(it.hashCode()) }.toMutableList() as List<Track>

View file

@ -0,0 +1,25 @@
package com.simplemobiletools.musicplayer.extensions
import android.app.Activity
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.musicplayer.dialogs.SelectPlaylistDialog
import com.simplemobiletools.musicplayer.helpers.RoomHelper
import com.simplemobiletools.musicplayer.models.Track
fun Activity.addTracksToPlaylist(tracks: List<Track>, callback: () -> Unit) {
SelectPlaylistDialog(this) { playlistId ->
val tracksToAdd = ArrayList<Track>()
tracks.forEach {
it.playListId = playlistId
tracksToAdd.add(it)
}
ensureBackgroundThread {
RoomHelper(this).insertTracksWithPlaylist(tracksToAdd)
runOnUiThread {
callback()
}
}
}
}