From f3e352e6b44a28da826aaa5ca4a847ac1ffb9eca Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 28 Mar 2017 21:10:45 +0200 Subject: [PATCH] add a helper function for getting current playlist song paths --- .../musicplayer/extensions/Context.kt | 3 +++ .../musicplayer/helpers/DBHelper.kt | 22 +++++++++++++++++++ .../musicplayer/services/MusicService.kt | 5 ++++- 3 files changed, 29 insertions(+), 1 deletion(-) 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 ce681bd2..bce3d3af 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.musicplayer.extensions import android.content.Context import android.content.Intent import com.simplemobiletools.musicplayer.helpers.Config +import com.simplemobiletools.musicplayer.helpers.DBHelper import com.simplemobiletools.musicplayer.services.MusicService fun Context.sendIntent(action: String) { @@ -13,3 +14,5 @@ fun Context.sendIntent(action: String) { } val Context.config: Config get() = Config.newInstance(this) + +val Context.dbHelper: DBHelper get() = DBHelper.newInstance(this) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/DBHelper.kt index ee474364..01107111 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/DBHelper.kt @@ -2,8 +2,10 @@ package com.simplemobiletools.musicplayer.helpers import android.content.ContentValues import android.content.Context +import android.database.Cursor import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper +import com.simplemobiletools.commons.extensions.getStringValue import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.extensions.config import com.simplemobiletools.musicplayer.models.Playlist @@ -69,4 +71,24 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } } } + + fun getCurrentPlaylistPaths(): ArrayList { + val paths = ArrayList() + val cols = arrayOf(COL_PATH) + val selection = "$COL_PLAYLIST_ID = ?" + val selectionArgs = arrayOf(context.config.currentPlaylist.toString()) + var cursor: Cursor? = null + try { + cursor = mDb.query(TABLE_NAME_SONGS, cols, selection, selectionArgs, null, null, null) + if (cursor?.moveToFirst() == true) { + do { + val path = cursor.getStringValue(COL_PATH) + paths.add(path) + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + return paths + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt index 05da680b..e0e1ac2a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt @@ -25,6 +25,7 @@ import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.activities.MainActivity import com.simplemobiletools.musicplayer.extensions.config +import com.simplemobiletools.musicplayer.extensions.dbHelper import com.simplemobiletools.musicplayer.helpers.* import com.simplemobiletools.musicplayer.models.Events import com.simplemobiletools.musicplayer.models.Song @@ -200,7 +201,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr cursor?.close() } - DBHelper.newInstance(this).addSongsToPlaylist(paths) + dbHelper.addSongsToPlaylist(paths) } private fun getSortedSongs() { @@ -209,6 +210,8 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr config.wasInitialPlaylistFilled = true } + val songPaths = dbHelper.getCurrentPlaylistPaths() + Song.sorting = mConfig!!.sorting mSongs?.sort() }