handle folder adding in Room

This commit is contained in:
tibbi 2018-05-21 20:40:30 +02:00
parent a630e2cf5e
commit a977991d78
4 changed files with 9 additions and 35 deletions

View file

@ -371,11 +371,13 @@ class MainActivity : SimpleActivity(), SongListListener {
RadioGroupDialog(this, items, config.currentPlaylist) {
if (it == -1) {
NewPlaylistDialog(this) {
wasInitialPlaylistSet = false
MusicService.mCurrSong = null
playlistChanged(it, false)
invalidateOptionsMenu()
}
} else {
wasInitialPlaylistSet = false
playlistChanged(it as Int)
invalidateOptionsMenu()
}
@ -387,7 +389,7 @@ class MainActivity : SimpleActivity(), SongListListener {
toast(R.string.fetching_songs)
Thread {
val folderSongs = getFolderSongs(File(it))
dbHelper.addSongsToPlaylist(folderSongs)
RoomHelper(applicationContext).addSongsToPlaylist(folderSongs)
sendIntent(REFRESH_LIST)
}.start()
}
@ -451,7 +453,7 @@ class MainActivity : SimpleActivity(), SongListListener {
val playlist = Playlist(0, playlistName)
val newPlaylistId = playlistDAO.insert(playlist).toInt()
//dbHelper.addSongsToPlaylist(folderSongs, newPlaylistId)
RoomHelper(applicationContext).addSongsToPlaylist(folderSongs, newPlaylistId)
playlistChanged(newPlaylistId)
}

View file

@ -5,7 +5,10 @@ import android.content.Intent
import android.util.TypedValue
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.databases.SongsDatabase
import com.simplemobiletools.musicplayer.helpers.*
import com.simplemobiletools.musicplayer.helpers.Config
import com.simplemobiletools.musicplayer.helpers.PAUSE
import com.simplemobiletools.musicplayer.helpers.REFRESH_LIST
import com.simplemobiletools.musicplayer.helpers.SETUP
import com.simplemobiletools.musicplayer.interfaces.PlaylistsDao
import com.simplemobiletools.musicplayer.interfaces.SongsDao
import com.simplemobiletools.musicplayer.models.Playlist
@ -25,8 +28,6 @@ fun Context.sendIntent(action: String) {
val Context.config: Config get() = Config.newInstance(applicationContext)
val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext)
val Context.songsDB: SongsDatabase get() = SongsDatabase.getInstance(applicationContext)
val Context.playlistDAO: PlaylistsDao get() = songsDB.PlaylistsDao()

View file

@ -1,11 +1,8 @@
package com.simplemobiletools.musicplayer.helpers
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.text.TextUtils
import com.simplemobiletools.musicplayer.extensions.config
class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
private val TABLE_NAME_PLAYLISTS = "playlists"
@ -16,8 +13,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private val COL_PATH = "path"
private val COL_PLAYLIST_ID = "playlist_id"
private val mDb = writableDatabase
companion object {
private const val DB_VERSION = 1
const val DB_NAME = "playlists.db"
@ -37,27 +32,4 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
db.execSQL("CREATE TABLE $TABLE_NAME_SONGS ($COL_ID INTEGER PRIMARY KEY, $COL_PATH TEXT, $COL_PLAYLIST_ID INTEGER, " +
"UNIQUE($COL_PATH, $COL_PLAYLIST_ID) ON CONFLICT IGNORE)")
}
fun removePlaylists(ids: ArrayList<Int>) {
val args = TextUtils.join(", ", ids.filter { it != ALL_SONGS_PLAYLIST_ID })
val songSelection = "$COL_PLAYLIST_ID IN ($args)"
mDb.delete(TABLE_NAME_SONGS, songSelection, null)
}
fun addSongsToPlaylist(paths: ArrayList<String>, playlistId: Int = context.config.currentPlaylist) {
try {
mDb.beginTransaction()
val values = ContentValues()
for (path in paths) {
values.apply {
put(COL_PATH, path)
put(COL_PLAYLIST_ID, playlistId)
mDb.insert(TABLE_NAME_SONGS, null, this)
}
}
mDb.setTransactionSuccessful()
} finally {
mDb.endTransaction()
}
}
}

View file

@ -25,7 +25,6 @@ import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.activities.MainActivity
import com.simplemobiletools.musicplayer.databases.SongsDatabase
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.dbHelper
import com.simplemobiletools.musicplayer.extensions.getPlaylistSongs
import com.simplemobiletools.musicplayer.helpers.*
import com.simplemobiletools.musicplayer.models.Events
@ -290,7 +289,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
cursor?.close()
}
dbHelper.addSongsToPlaylist(paths)
RoomHelper(this).addSongsToPlaylist(paths)
}
private fun getSortedSongs() {