handle reseting queue items on selecting some new songs
This commit is contained in:
parent
f5abbedd70
commit
daf395734a
5 changed files with 37 additions and 12 deletions
|
@ -10,6 +10,7 @@ import com.simplemobiletools.musicplayer.R
|
|||
import com.simplemobiletools.musicplayer.adapters.AlbumsAdapter
|
||||
import com.simplemobiletools.musicplayer.extensions.getAlbums
|
||||
import com.simplemobiletools.musicplayer.extensions.getSongsSync
|
||||
import com.simplemobiletools.musicplayer.extensions.resetQueueItems
|
||||
import com.simplemobiletools.musicplayer.helpers.ALBUM
|
||||
import com.simplemobiletools.musicplayer.helpers.ARTIST
|
||||
import com.simplemobiletools.musicplayer.helpers.TRACK
|
||||
|
@ -53,11 +54,13 @@ class AlbumsActivity : SimpleActivity() {
|
|||
startActivity(this)
|
||||
}
|
||||
} else {
|
||||
resetQueueItems(tracksToAdd) {
|
||||
Intent(this, TrackActivity::class.java).apply {
|
||||
putExtra(TRACK, Gson().toJson(it))
|
||||
startActivity(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
}.apply {
|
||||
albums_list.adapter = this
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import com.google.gson.reflect.TypeToken
|
|||
import com.simplemobiletools.musicplayer.R
|
||||
import com.simplemobiletools.musicplayer.adapters.SongsAdapter
|
||||
import com.simplemobiletools.musicplayer.extensions.getSongs
|
||||
import com.simplemobiletools.musicplayer.extensions.resetQueueItems
|
||||
import com.simplemobiletools.musicplayer.helpers.ALBUM
|
||||
import com.simplemobiletools.musicplayer.helpers.TRACK
|
||||
import com.simplemobiletools.musicplayer.helpers.artworkUri
|
||||
|
@ -36,10 +37,12 @@ class SongsActivity : SimpleActivity() {
|
|||
|
||||
runOnUiThread {
|
||||
SongsAdapter(this, items, songs_list) {
|
||||
resetQueueItems(songs) {
|
||||
Intent(this, TrackActivity::class.java).apply {
|
||||
putExtra(TRACK, Gson().toJson(it))
|
||||
startActivity(this)
|
||||
}
|
||||
}
|
||||
}.apply {
|
||||
songs_list.adapter = this
|
||||
}
|
||||
|
|
|
@ -19,10 +19,7 @@ import com.simplemobiletools.musicplayer.helpers.*
|
|||
import com.simplemobiletools.musicplayer.interfaces.PlaylistsDao
|
||||
import com.simplemobiletools.musicplayer.interfaces.QueueItemsDao
|
||||
import com.simplemobiletools.musicplayer.interfaces.SongsDao
|
||||
import com.simplemobiletools.musicplayer.models.Album
|
||||
import com.simplemobiletools.musicplayer.models.Artist
|
||||
import com.simplemobiletools.musicplayer.models.Playlist
|
||||
import com.simplemobiletools.musicplayer.models.Song
|
||||
import com.simplemobiletools.musicplayer.models.*
|
||||
import com.simplemobiletools.musicplayer.services.MusicService
|
||||
import java.io.File
|
||||
|
||||
|
@ -214,3 +211,18 @@ fun Context.getSongsSync(albumId: Int): ArrayList<Song> {
|
|||
|
||||
return songs
|
||||
}
|
||||
|
||||
fun Context.resetQueueItems(newTracks: ArrayList<Song>, callback: () -> Unit) {
|
||||
ensureBackgroundThread {
|
||||
queueDAO.deleteAllItems()
|
||||
val itemsToInsert = ArrayList<QueueItem>()
|
||||
var order = 0
|
||||
newTracks.forEach {
|
||||
val queueItem = QueueItem(0, it.id, order++, false, 0, false)
|
||||
itemsToInsert.add(queueItem)
|
||||
}
|
||||
|
||||
queueDAO.insert(itemsToInsert)
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,10 +3,17 @@ package com.simplemobiletools.musicplayer.interfaces
|
|||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.simplemobiletools.musicplayer.models.QueueItem
|
||||
|
||||
@Dao
|
||||
interface QueueItemsDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(queueItem: QueueItem): Long
|
||||
fun insert(queueItems: List<QueueItem>)
|
||||
|
||||
@Query("SELECT * FROM queue_items")
|
||||
fun getQueueItems(): List<QueueItem>
|
||||
|
||||
@Query("DELETE FROM queue_items")
|
||||
fun deleteAllItems()
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import androidx.room.PrimaryKey
|
|||
@Entity(tableName = "queue_items", indices = [(Index(value = ["id"], unique = true))])
|
||||
data class QueueItem(
|
||||
@PrimaryKey(autoGenerate = true) var id: Int,
|
||||
@ColumnInfo(name = "track_id") var trackId: Int,
|
||||
@ColumnInfo(name = "track_id") var trackId: Long,
|
||||
@ColumnInfo(name = "track_order") var trackOrder: Int,
|
||||
@ColumnInfo(name = "is_playing") var isPlaying: Boolean,
|
||||
@ColumnInfo(name = "last_position") var lastPosition: Int,
|
||||
|
|
Loading…
Reference in a new issue