From daf395734afd96c44134b73cd20eed95e1842737 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 7 Oct 2020 23:31:50 +0200 Subject: [PATCH] handle reseting queue items on selecting some new songs --- .../musicplayer/activities/AlbumsActivity.kt | 9 ++++++--- .../musicplayer/activities/SongsActivity.kt | 9 ++++++--- .../musicplayer/extensions/Context.kt | 20 +++++++++++++++---- .../musicplayer/interfaces/QueueItemsDao.kt | 9 ++++++++- .../musicplayer/models/QueueItem.kt | 2 +- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/AlbumsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/AlbumsActivity.kt index 0084705d..d6f492ea 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/AlbumsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/AlbumsActivity.kt @@ -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,9 +54,11 @@ class AlbumsActivity : SimpleActivity() { startActivity(this) } } else { - Intent(this, TrackActivity::class.java).apply { - putExtra(TRACK, Gson().toJson(it)) - startActivity(this) + resetQueueItems(tracksToAdd) { + Intent(this, TrackActivity::class.java).apply { + putExtra(TRACK, Gson().toJson(it)) + startActivity(this) + } } } }.apply { diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SongsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SongsActivity.kt index 52c82da4..8420134a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SongsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SongsActivity.kt @@ -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,9 +37,11 @@ class SongsActivity : SimpleActivity() { runOnUiThread { SongsAdapter(this, items, songs_list) { - Intent(this, TrackActivity::class.java).apply { - putExtra(TRACK, Gson().toJson(it)) - startActivity(this) + resetQueueItems(songs) { + Intent(this, TrackActivity::class.java).apply { + putExtra(TRACK, Gson().toJson(it)) + startActivity(this) + } } }.apply { songs_list.adapter = this diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt index bdb21434..778327eb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt @@ -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 { return songs } + +fun Context.resetQueueItems(newTracks: ArrayList, callback: () -> Unit) { + ensureBackgroundThread { + queueDAO.deleteAllItems() + val itemsToInsert = ArrayList() + var order = 0 + newTracks.forEach { + val queueItem = QueueItem(0, it.id, order++, false, 0, false) + itemsToInsert.add(queueItem) + } + + queueDAO.insert(itemsToInsert) + callback() + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/interfaces/QueueItemsDao.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/interfaces/QueueItemsDao.kt index a9022945..5bdd7ada 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/interfaces/QueueItemsDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/interfaces/QueueItemsDao.kt @@ -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) + + @Query("SELECT * FROM queue_items") + fun getQueueItems(): List + + @Query("DELETE FROM queue_items") + fun deleteAllItems() } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/QueueItem.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/QueueItem.kt index 9daa04c4..de10e28c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/QueueItem.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/QueueItem.kt @@ -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,