From f909efe0e535c5d1562cf10ad612c7543f13134f Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 2 Oct 2021 10:09:58 +0200 Subject: [PATCH] moving track deleting from context to activity extension --- .../musicplayer/extensions/Activity.kt | 20 ++++++++++++++++++ .../musicplayer/extensions/Context.kt | 21 ++----------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Activity.kt index cd3e38f4..28a03c7e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Activity.kt @@ -1,11 +1,14 @@ package com.simplemobiletools.musicplayer.extensions import android.app.Activity +import android.provider.MediaStore import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.musicplayer.dialogs.SelectPlaylistDialog import com.simplemobiletools.musicplayer.helpers.RoomHelper +import com.simplemobiletools.musicplayer.models.Events import com.simplemobiletools.musicplayer.models.Track import com.simplemobiletools.musicplayer.services.MusicService +import org.greenrobot.eventbus.EventBus fun Activity.addTracksToPlaylist(tracks: List, callback: () -> Unit) { SelectPlaylistDialog(this) { playlistId -> @@ -39,3 +42,20 @@ fun Activity.addTracksToQueue(tracks: List, callback: () -> Unit) { } } } + +fun Activity.deleteTracks(tracks: List, callback: () -> Unit) { + tracks.forEach { track -> + try { + val where = "${MediaStore.Audio.Media._ID} = ?" + val args = arrayOf(track.mediaStoreId.toString()) + val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI + contentResolver.delete(uri, where, args) + tracksDAO.removeTrack(track.mediaStoreId) + } catch (ignored: Exception) { + } + } + + removeQueueItems(tracks) {} + EventBus.getDefault().post(Events.TrackDeleted()) + callback() +} 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 6c341221..a06d504b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt @@ -15,7 +15,6 @@ import com.simplemobiletools.musicplayer.helpers.* import com.simplemobiletools.musicplayer.interfaces.* import com.simplemobiletools.musicplayer.models.* import com.simplemobiletools.musicplayer.services.MusicService -import org.greenrobot.eventbus.EventBus import java.io.File @SuppressLint("NewApi") @@ -134,7 +133,8 @@ fun Context.getAlbumsSync(artist: Artist): ArrayList { Audio.Albums._ID, Audio.Albums.ARTIST, Audio.Albums.FIRST_YEAR, - Audio.Albums.ALBUM) + Audio.Albums.ALBUM + ) var selection = "${Audio.Albums.ARTIST} = ?" var selectionArgs = arrayOf(artist.title) @@ -263,23 +263,6 @@ fun Context.removeQueueItems(tracks: List, callback: () -> Unit) { } } -fun Context.deleteTracks(tracks: List, callback: () -> Unit) { - tracks.forEach { track -> - try { - val where = "${Audio.Media._ID} = ?" - val args = arrayOf(track.mediaStoreId.toString()) - val uri = Audio.Media.EXTERNAL_CONTENT_URI - contentResolver.delete(uri, where, args) - tracksDAO.removeTrack(track.mediaStoreId) - } catch (ignored: Exception) { - } - } - - removeQueueItems(tracks) {} - EventBus.getDefault().post(Events.TrackDeleted()) - callback() -} - fun Context.getAllInitialTracks(): ArrayList { val allTracks = ArrayList() getArtistsSync().forEach { artist ->