add helper extension variables for DAOs

This commit is contained in:
tibbi 2018-05-20 00:07:59 +02:00
parent 134e865c68
commit a43ec79c40
6 changed files with 41 additions and 34 deletions

View file

@ -324,7 +324,7 @@ class MainActivity : SimpleActivity(), SongListListener {
toast(R.string.all_songs_cannot_be_deleted)
} else {
Thread {
val playlist = songsDB.PlaylistsDao().getPlaylistWithId(config.currentPlaylist)
val playlist = playlistDAO.getPlaylistWithId(config.currentPlaylist)
runOnUiThread {
RemovePlaylistDialog(this, playlist) {
if (it) {
@ -334,7 +334,7 @@ class MainActivity : SimpleActivity(), SongListListener {
deleteFiles(files) { }
}
Thread {
songsDB.PlaylistsDao().deletePlaylistById(config.currentPlaylist)
playlistDAO.deletePlaylistById(config.currentPlaylist)
}.start()
}
}
@ -344,7 +344,7 @@ class MainActivity : SimpleActivity(), SongListListener {
private fun openPlaylist() {
Thread {
val playlists = songsDB.PlaylistsDao().getAll() as ArrayList<Playlist>
val playlists = playlistDAO.getAll() as ArrayList<Playlist>
runOnUiThread {
showPlaylists(playlists)
}
@ -436,7 +436,7 @@ class MainActivity : SimpleActivity(), SongListListener {
}
val playlist = Playlist(0, playlistName)
val newPlaylistId = songsDB.PlaylistsDao().insert(playlist).toInt()
val newPlaylistId = playlistDAO.insert(playlist).toInt()
//dbHelper.addSongsToPlaylist(songs, newPlaylistId)
playlistChanged(newPlaylistId)
}

View file

@ -7,7 +7,7 @@ import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.adapters.PlaylistsAdapter
import com.simplemobiletools.musicplayer.dialogs.NewPlaylistDialog
import com.simplemobiletools.musicplayer.extensions.playlistChanged
import com.simplemobiletools.musicplayer.extensions.songsDB
import com.simplemobiletools.musicplayer.extensions.playlistDAO
import com.simplemobiletools.musicplayer.interfaces.RefreshPlaylistsListener
import com.simplemobiletools.musicplayer.models.Playlist
import kotlinx.android.synthetic.main.activity_playlists.*
@ -21,7 +21,7 @@ class PlaylistsActivity : SimpleActivity(), RefreshPlaylistsListener {
private fun getPlaylists() {
Thread {
val playlists = songsDB.PlaylistsDao().getAll() as ArrayList<Playlist>
val playlists = playlistDAO.getAll() as ArrayList<Playlist>
runOnUiThread {
PlaylistsAdapter(this@PlaylistsActivity, playlists, this@PlaylistsActivity, playlists_list) {
getPlaylists()

View file

@ -13,7 +13,7 @@ import com.simplemobiletools.musicplayer.dialogs.NewPlaylistDialog
import com.simplemobiletools.musicplayer.dialogs.RemovePlaylistDialog
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.dbHelper
import com.simplemobiletools.musicplayer.extensions.songsDB
import com.simplemobiletools.musicplayer.extensions.playlistDAO
import com.simplemobiletools.musicplayer.helpers.ALL_SONGS_PLAYLIST_ID
import com.simplemobiletools.musicplayer.interfaces.RefreshPlaylistsListener
import com.simplemobiletools.musicplayer.models.Playlist
@ -72,7 +72,6 @@ class PlaylistsAdapter(activity: SimpleActivity, val playlists: ArrayList<Playli
} else {
removePlaylists(ids)
}
finishActMode()
}
}
@ -108,14 +107,15 @@ class PlaylistsAdapter(activity: SimpleActivity, val playlists: ArrayList<Playli
playlists.removeAll(playlistsToDelete)
Thread {
activity.songsDB.PlaylistsDao().deletePlaylists(playlistsToDelete)
activity.playlistDAO.deletePlaylists(playlistsToDelete)
activity.runOnUiThread {
if (isDeletingCurrentPlaylist) {
reloadList()
} else {
removeSelectedItems()
}
}
}.start()
if (isDeletingCurrentPlaylist) {
reloadList()
} else {
removeSelectedItems()
}
}
private fun showRenameDialog() {

View file

@ -6,6 +6,7 @@ import android.arch.persistence.room.Room
import android.arch.persistence.room.RoomDatabase
import android.content.Context
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.extensions.playlistDAO
import com.simplemobiletools.musicplayer.helpers.ALL_SONGS_PLAYLIST_ID
import com.simplemobiletools.musicplayer.interfaces.PlaylistsDao
import com.simplemobiletools.musicplayer.interfaces.SongsDao
@ -50,7 +51,7 @@ abstract class SongsDatabase : RoomDatabase() {
private fun addInitialPlaylist(context: Context) {
val allSongs = context.resources.getString(R.string.all_songs)
val playlist = Playlist(ALL_SONGS_PLAYLIST_ID, allSongs)
getInstance(context).PlaylistsDao().insert(playlist)
context.playlistDAO.insert(playlist)
}
}
}

View file

@ -9,7 +9,7 @@ import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.extensions.dbHelper
import com.simplemobiletools.musicplayer.extensions.getPlaylistIdWithTitle
import com.simplemobiletools.musicplayer.extensions.songsDB
import com.simplemobiletools.musicplayer.extensions.playlistDAO
import com.simplemobiletools.musicplayer.models.Playlist
import kotlinx.android.synthetic.main.dialog_new_playlist.view.*
@ -33,24 +33,24 @@ class NewPlaylistDialog(val activity: Activity, var playlist: Playlist? = null,
showKeyboard(view.new_playlist_title)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val title = view.new_playlist_title.value
val playlistIdWithTitle = activity.getPlaylistIdWithTitle(title)
var isPlaylistTitleTaken = isNewPlaylist && playlistIdWithTitle != -1
if (!isPlaylistTitleTaken)
isPlaylistTitleTaken = !isNewPlaylist && playlist!!.id != playlistIdWithTitle && playlistIdWithTitle != -1
if (title.isEmpty()) {
activity.toast(R.string.empty_name)
return@setOnClickListener
} else if (isPlaylistTitleTaken) {
activity.toast(R.string.playlist_name_exists)
return@setOnClickListener
}
playlist!!.title = title
Thread {
val playlistIdWithTitle = activity.getPlaylistIdWithTitle(title)
var isPlaylistTitleTaken = isNewPlaylist && playlistIdWithTitle != -1
if (!isPlaylistTitleTaken)
isPlaylistTitleTaken = !isNewPlaylist && playlist!!.id != playlistIdWithTitle && playlistIdWithTitle != -1
if (title.isEmpty()) {
activity.toast(R.string.empty_name)
return@Thread
} else if (isPlaylistTitleTaken) {
activity.toast(R.string.playlist_name_exists)
return@Thread
}
playlist!!.title = title
val eventTypeId = if (isNewPlaylist) {
activity.songsDB.PlaylistsDao().insert(playlist!!).toInt()
activity.playlistDAO.insert(playlist!!).toInt()
} else {
activity.dbHelper.updatePlaylist(playlist!!)
}

View file

@ -6,6 +6,8 @@ import android.util.TypedValue
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.databases.SongsDatabase
import com.simplemobiletools.musicplayer.helpers.*
import com.simplemobiletools.musicplayer.interfaces.PlaylistsDao
import com.simplemobiletools.musicplayer.interfaces.SongsDao
import com.simplemobiletools.musicplayer.services.MusicService
fun Context.sendIntent(action: String) {
@ -24,6 +26,10 @@ val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext)
val Context.songsDB: SongsDatabase get() = SongsDatabase.getInstance(applicationContext)
val Context.playlistDAO: PlaylistsDao get() = songsDB.PlaylistsDao()
val Context.songsDAO: SongsDao get() = songsDB.SongsDao()
fun Context.playlistChanged(newID: Int, callSetup: Boolean = true) {
config.currentPlaylist = newID
sendIntent(PAUSE)
@ -41,4 +47,4 @@ fun Context.getActionBarHeight(): Int {
return actionBarSize
}
fun Context.getPlaylistIdWithTitle(title: String) = songsDB.PlaylistsDao().getPlaylistWithTitle(title)?.id ?: -1
fun Context.getPlaylistIdWithTitle(title: String) = playlistDAO.getPlaylistWithTitle(title)?.id ?: -1