Merge pull request #580 from naveensingh/minor_fixes_and_improvements

Crash fixes and improvements
This commit is contained in:
Tibor Kaputa 2023-07-28 09:38:49 +02:00 committed by GitHub
commit 91846910a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 253 additions and 232 deletions

View file

@ -22,6 +22,10 @@ import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.adapters.TracksAdapter
import com.simplemobiletools.musicplayer.adapters.TracksAdapter.Companion.TYPE_ALBUM
import com.simplemobiletools.musicplayer.adapters.TracksAdapter.Companion.TYPE_FOLDER
import com.simplemobiletools.musicplayer.adapters.TracksAdapter.Companion.TYPE_PLAYLIST
import com.simplemobiletools.musicplayer.adapters.TracksAdapter.Companion.TYPE_TRACKS
import com.simplemobiletools.musicplayer.adapters.TracksHeaderAdapter
import com.simplemobiletools.musicplayer.dialogs.ChangeSortingDialog
import com.simplemobiletools.musicplayer.dialogs.ExportPlaylistDialog
@ -40,11 +44,6 @@ import java.io.OutputStream
// this activity is used for displaying Playlist and Folder tracks, also Album tracks with a possible album header at the top
// Artists -> Albums -> Tracks
class TracksActivity : SimpleActivity() {
private val TYPE_PLAYLIST = 1
private val TYPE_FOLDER = 2
private val TYPE_ALBUM = 3
private val TYPE_GENRE = 4
private val PICK_EXPORT_FILE_INTENT = 2
private var isSearchOpen = false
@ -53,7 +52,7 @@ class TracksActivity : SimpleActivity() {
private var bus: EventBus? = null
private var playlist: Playlist? = null
private var folder: String? = null
private var tracksType = 0
private var sourceType = 0
private var lastFilePickerPath = ""
override fun onCreate(savedInstanceState: Bundle?) {
@ -118,11 +117,11 @@ class TracksActivity : SimpleActivity() {
private fun refreshMenuItems() {
tracks_toolbar.menu.apply {
findItem(R.id.search).isVisible = tracksType != TYPE_ALBUM
findItem(R.id.sort).isVisible = tracksType != TYPE_ALBUM
findItem(R.id.add_file_to_playlist).isVisible = tracksType == TYPE_PLAYLIST
findItem(R.id.add_folder_to_playlist).isVisible = tracksType == TYPE_PLAYLIST
findItem(R.id.export_playlist).isVisible = tracksType == TYPE_PLAYLIST && isOreoPlus()
findItem(R.id.search).isVisible = sourceType != TYPE_ALBUM
findItem(R.id.sort).isVisible = sourceType != TYPE_ALBUM
findItem(R.id.add_file_to_playlist).isVisible = sourceType == TYPE_PLAYLIST
findItem(R.id.add_folder_to_playlist).isVisible = sourceType == TYPE_PLAYLIST
findItem(R.id.export_playlist).isVisible = sourceType == TYPE_PLAYLIST && isOreoPlus()
}
}
@ -177,24 +176,24 @@ class TracksActivity : SimpleActivity() {
val playlistType = object : TypeToken<Playlist>() {}.type
playlist = Gson().fromJson<Playlist>(intent.getStringExtra(PLAYLIST), playlistType)
if (playlist != null) {
tracksType = TYPE_PLAYLIST
sourceType = TYPE_PLAYLIST
}
val albumType = object : TypeToken<Album>() {}.type
val album = Gson().fromJson<Album>(intent.getStringExtra(ALBUM), albumType)
if (album != null) {
tracksType = TYPE_ALBUM
sourceType = TYPE_ALBUM
}
val genreType = object : TypeToken<Genre>() {}.type
val genre = Gson().fromJson<Genre>(intent.getStringExtra(GENRE), genreType)
if (genre != null) {
tracksType = TYPE_GENRE
sourceType = TYPE_TRACKS
}
folder = intent.getStringExtra(FOLDER)
if (folder != null) {
tracksType = TYPE_FOLDER
sourceType = TYPE_FOLDER
tracks_placeholder_2.beGone()
}
@ -205,7 +204,7 @@ class TracksActivity : SimpleActivity() {
ensureBackgroundThread {
val tracks = ArrayList<Track>()
val listItems = ArrayList<ListItem>()
when (tracksType) {
when (sourceType) {
TYPE_PLAYLIST -> {
val playlistTracks = audioHelper.getPlaylistTracks(playlist!!.id)
runOnUiThread {
@ -224,7 +223,7 @@ class TracksActivity : SimpleActivity() {
listItems.add(header)
listItems.addAll(tracks)
}
TYPE_GENRE -> {
TYPE_TRACKS -> {
val genreTracks = audioHelper.getGenreTracks(genre.id)
tracks.addAll(genreTracks)
}
@ -240,7 +239,7 @@ class TracksActivity : SimpleActivity() {
}
runOnUiThread {
if (tracksType == TYPE_ALBUM) {
if (sourceType == TYPE_ALBUM) {
val currAdapter = tracks_list.adapter
if (currAdapter == null) {
TracksHeaderAdapter(this, listItems, tracks_list) {
@ -256,10 +255,16 @@ class TracksActivity : SimpleActivity() {
(currAdapter as TracksHeaderAdapter).updateItems(listItems)
}
} else {
val isPlaylistContent = tracksType == TYPE_PLAYLIST
val currAdapter = tracks_list.adapter
if (currAdapter == null) {
TracksAdapter(this, tracks, isPlaylistContent, tracks_list, playlist) {
TracksAdapter(
activity = this,
recyclerView = tracks_list,
sourceType = sourceType,
folder = folder,
playlist = playlist,
tracks = tracks
) {
itemClicked(it as Track)
}.apply {
tracks_list.adapter = this
@ -280,16 +285,16 @@ class TracksActivity : SimpleActivity() {
ChangeSortingDialog(this, ACTIVITY_PLAYLIST_FOLDER, playlist, folder) {
val adapter = tracks_list.adapter as? TracksAdapter ?: return@ChangeSortingDialog
val tracks = adapter.tracks
Track.sorting = when(tracksType) {
val sorting = when (sourceType) {
TYPE_PLAYLIST -> config.getProperPlaylistSorting(playlist?.id ?: -1)
TYPE_GENRE -> config.trackSorting
TYPE_TRACKS -> config.trackSorting
else -> config.getProperFolderSorting(folder ?: "")
}
tracks.sort()
tracks.sortSafely(sorting)
adapter.updateItems(tracks, forceUpdate = true)
if (tracksType == TYPE_GENRE) {
if (sourceType == TYPE_TRACKS) {
EventBus.getDefault().post(Events.RefreshTracks())
}
}
@ -383,7 +388,7 @@ class TracksActivity : SimpleActivity() {
}
private fun itemClicked(track: Track) {
val tracks = when (tracksType) {
val tracks = when (sourceType) {
TYPE_ALBUM -> (tracks_list.adapter as? TracksHeaderAdapter)?.items?.filterIsInstance<Track>()
else -> (tracks_list.adapter as? TracksAdapter)?.tracks
} ?: ArrayList()

View file

@ -170,5 +170,5 @@ class AlbumsAdapter(activity: BaseSimpleActivity, var albums: ArrayList<Album>,
}
}
override fun onChange(position: Int) = albums.getOrNull(position)?.getBubbleText() ?: ""
override fun onChange(position: Int) = albums.getOrNull(position)?.getBubbleText(activity.config.albumSorting) ?: ""
}

View file

@ -172,5 +172,5 @@ class ArtistsAdapter(activity: BaseSimpleActivity, var artists: ArrayList<Artist
}
}
override fun onChange(position: Int) = artists.getOrNull(position)?.getBubbleText() ?: ""
override fun onChange(position: Int) = artists.getOrNull(position)?.getBubbleText(activity.config.artistSorting) ?: ""
}

View file

@ -114,5 +114,5 @@ class FoldersAdapter(
}
}
override fun onChange(position: Int) = folders.getOrNull(position)?.getBubbleText() ?: ""
override fun onChange(position: Int) = folders.getOrNull(position)?.getBubbleText(activity.config.folderSorting) ?: ""
}

View file

@ -175,5 +175,5 @@ class GenresAdapter(activity: BaseSimpleActivity, var genres: ArrayList<Genre>,
}
}
override fun onChange(position: Int) = genres.getOrNull(position)?.getBubbleText() ?: ""
override fun onChange(position: Int) = genres.getOrNull(position)?.getBubbleText(activity.config.genreSorting) ?: ""
}

View file

@ -17,6 +17,7 @@ import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.dialogs.NewPlaylistDialog
import com.simplemobiletools.musicplayer.dialogs.RemovePlaylistDialog
import com.simplemobiletools.musicplayer.extensions.audioHelper
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.models.Events
import com.simplemobiletools.musicplayer.models.Playlist
import kotlinx.android.synthetic.main.item_playlist.view.playlist_frame
@ -161,5 +162,5 @@ class PlaylistsAdapter(
}
}
override fun onChange(position: Int) = playlists.getOrNull(position)?.getBubbleText() ?: ""
override fun onChange(position: Int) = playlists.getOrNull(position)?.getBubbleText(activity.config.playlistSorting) ?: ""
}

View file

@ -221,7 +221,7 @@ class QueueAdapter(activity: SimpleActivity, var items: ArrayList<Track>, recycl
override fun onRowSelected(myViewHolder: ViewHolder?) {}
override fun onChange(position: Int) = items.getOrNull(position)?.getBubbleText() ?: ""
override fun onChange(position: Int) = items.getOrNull(position)?.getBubbleText(activity.config.trackSorting) ?: ""
fun updateItems(newItems: ArrayList<Track>, highlightText: String = "", forceUpdate: Boolean = false) {
if (forceUpdate || newItems.hashCode() != items.hashCode()) {

View file

@ -38,10 +38,11 @@ import java.util.Collections
class TracksAdapter(
activity: BaseSimpleActivity,
var tracks: ArrayList<Track>,
val isPlaylistContent: Boolean,
recyclerView: MyRecyclerView,
val sourceType: Int,
val folder: String? = null,
val playlist: Playlist? = null,
var tracks: ArrayList<Track>,
itemClick: (Any) -> Unit
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), RecyclerViewFastScroller.OnPopupTextUpdate, ItemTouchHelperContract {
@ -81,7 +82,7 @@ class TracksAdapter(
override fun prepareActionMode(menu: Menu) {
menu.apply {
findItem(R.id.cab_remove_from_playlist).isVisible = isPlaylistContent
findItem(R.id.cab_remove_from_playlist).isVisible = isPlaylistContent()
findItem(R.id.cab_rename).isVisible =
isOneItemSelected() && getSelectedTracks().firstOrNull()?.let { !it.path.startsWith("content://") && tagHelper.isEditTagSupported(it) } == true
findItem(R.id.cab_play_next).isVisible =
@ -116,13 +117,13 @@ class TracksAdapter(
override fun getItemKeyPosition(key: Int) = tracks.indexOfFirst { it.hashCode() == key }
override fun onActionModeCreated() {
if (isPlaylistContent) {
if (isPlaylistContent()) {
notifyItemRangeChanged(0, itemCount)
}
}
override fun onActionModeDestroyed() {
if (isPlaylistContent) {
if (isPlaylistContent()) {
notifyItemRangeChanged(0, itemCount)
}
}
@ -210,7 +211,7 @@ class TracksAdapter(
finishActMode()
// finish activity if all tracks are deleted
if (tracks.isEmpty() && !isPlaylistContent) {
if (tracks.isEmpty() && !isPlaylistContent()) {
activity.finish()
}
}
@ -247,7 +248,7 @@ class TracksAdapter(
} else {
("${track.artist}${track.album}").highlightTextPart(textToHighlight, properPrimaryColor)
}
track_drag_handle.beVisibleIf(isPlaylistContent && selectedKeys.isNotEmpty())
track_drag_handle.beVisibleIf(isPlaylistContent() && selectedKeys.isNotEmpty())
track_drag_handle.applyColorFilter(textColor)
track_drag_handle.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
@ -266,7 +267,7 @@ class TracksAdapter(
.transform(CenterCrop(), RoundedCorners(cornerRadius))
context.getTrackCoverArt(track) { coverArt ->
if (!activity.isDestroyed || !activity.isFinishing) {
activity.ensureActivityNotDestroyed {
Glide.with(activity)
.load(coverArt)
.apply(options)
@ -279,7 +280,17 @@ class TracksAdapter(
}
}
override fun onChange(position: Int) = tracks.getOrNull(position)?.getBubbleText() ?: ""
override fun onChange(position: Int): String {
val sorting = if (isPlaylistContent() && playlist != null) {
activity.config.getProperPlaylistSorting(playlist.id)
} else if (sourceType == TYPE_FOLDER && folder != null) {
activity.config.getProperFolderSorting(folder)
} else {
activity.config.trackSorting
}
return tracks.getOrNull(position)?.getBubbleText(sorting) ?: ""
}
private fun displayEditDialog() {
getSelectedTracks().firstOrNull()?.let { selectedTrack ->
@ -319,4 +330,13 @@ class TracksAdapter(
}
}
}
private fun isPlaylistContent() = sourceType == TYPE_PLAYLIST
companion object {
const val TYPE_PLAYLIST = 1
const val TYPE_FOLDER = 2
const val TYPE_ALBUM = 3
const val TYPE_TRACKS = 4
}
}

View file

@ -248,7 +248,7 @@ class TracksHeaderAdapter(activity: SimpleActivity, var items: ArrayList<ListIte
override fun onChange(position: Int): CharSequence {
val listItem = items.getOrNull(position)
return when (listItem) {
is Track -> listItem.getBubbleText()
is Track -> listItem.getBubbleText(activity.config.trackSorting)
is AlbumHeader -> listItem.title
else -> ""
}

View file

@ -0,0 +1,8 @@
package com.simplemobiletools.musicplayer.extensions
fun <T> ArrayList<T>.sortSafely(comparator: Comparator<T>) {
try {
sortWith(comparator)
} catch (ignored: Exception) {
}
}

View file

@ -22,6 +22,7 @@ import com.simplemobiletools.musicplayer.extensions.mediaScanner
import com.simplemobiletools.musicplayer.helpers.ALBUM
import com.simplemobiletools.musicplayer.helpers.TAB_ALBUMS
import com.simplemobiletools.musicplayer.models.Album
import com.simplemobiletools.musicplayer.models.sortSafely
import kotlinx.android.synthetic.main.fragment_albums.view.albums_fastscroller
import kotlinx.android.synthetic.main.fragment_albums.view.albums_list
import kotlinx.android.synthetic.main.fragment_albums.view.albums_placeholder
@ -31,7 +32,6 @@ class AlbumsFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
private var albums = ArrayList<Album>()
override fun setupFragment(activity: BaseSimpleActivity) {
Album.sorting = context.config.albumSorting
ensureBackgroundThread {
val cachedAlbums = activity.audioHelper.getAllAlbums()
activity.runOnUiThread {
@ -94,8 +94,7 @@ class AlbumsFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
override fun onSortOpen(activity: SimpleActivity) {
ChangeSortingDialog(activity, TAB_ALBUMS) {
val adapter = albums_list.adapter as? AlbumsAdapter ?: return@ChangeSortingDialog
Album.sorting = activity.config.albumSorting
albums.sort()
albums.sortSafely(activity.config.albumSorting)
adapter.updateItems(albums, forceUpdate = true)
}
}

View file

@ -22,6 +22,7 @@ import com.simplemobiletools.musicplayer.extensions.mediaScanner
import com.simplemobiletools.musicplayer.helpers.ARTIST
import com.simplemobiletools.musicplayer.helpers.TAB_ARTISTS
import com.simplemobiletools.musicplayer.models.Artist
import com.simplemobiletools.musicplayer.models.sortSafely
import kotlinx.android.synthetic.main.fragment_artists.view.artists_fastscroller
import kotlinx.android.synthetic.main.fragment_artists.view.artists_list
import kotlinx.android.synthetic.main.fragment_artists.view.artists_placeholder
@ -31,7 +32,6 @@ class ArtistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
private var artists = ArrayList<Artist>()
override fun setupFragment(activity: BaseSimpleActivity) {
Artist.sorting = context.config.artistSorting
ensureBackgroundThread {
val cachedArtists = activity.audioHelper.getAllArtists()
activity.runOnUiThread {
@ -93,8 +93,7 @@ class ArtistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun onSortOpen(activity: SimpleActivity) {
ChangeSortingDialog(activity, TAB_ARTISTS) {
val adapter = artists_list.adapter as? ArtistsAdapter ?: return@ChangeSortingDialog
Artist.sorting = activity.config.artistSorting
artists.sort()
artists.sortSafely(activity.config.artistSorting)
adapter.updateItems(artists, forceUpdate = true)
}
}

View file

@ -19,6 +19,7 @@ import com.simplemobiletools.musicplayer.extensions.mediaScanner
import com.simplemobiletools.musicplayer.helpers.FOLDER
import com.simplemobiletools.musicplayer.helpers.TAB_FOLDERS
import com.simplemobiletools.musicplayer.models.Folder
import com.simplemobiletools.musicplayer.models.sortSafely
import kotlinx.android.synthetic.main.fragment_folders.view.folders_fastscroller
import kotlinx.android.synthetic.main.fragment_folders.view.folders_list
import kotlinx.android.synthetic.main.fragment_folders.view.folders_placeholder
@ -67,8 +68,7 @@ class FoldersFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
activity.startActivity(Intent(activity, ExcludedFoldersActivity::class.java))
}
Folder.sorting = activity.config.folderSorting
folders.sort()
folders.sortSafely(activity.config.folderSorting)
this.folders = folders
val adapter = folders_list.adapter
@ -111,8 +111,7 @@ class FoldersFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun onSortOpen(activity: SimpleActivity) {
ChangeSortingDialog(activity, TAB_FOLDERS) {
val adapter = folders_list.adapter as? FoldersAdapter ?: return@ChangeSortingDialog
Folder.sorting = activity.config.folderSorting
folders.sort()
folders.sortSafely(activity.config.folderSorting)
adapter.updateItems(folders, forceUpdate = true)
}
}

View file

@ -22,6 +22,7 @@ import com.simplemobiletools.musicplayer.extensions.mediaScanner
import com.simplemobiletools.musicplayer.helpers.GENRE
import com.simplemobiletools.musicplayer.helpers.TAB_GENRES
import com.simplemobiletools.musicplayer.models.Genre
import com.simplemobiletools.musicplayer.models.sortSafely
import kotlinx.android.synthetic.main.fragment_genres.view.genres_fastscroller
import kotlinx.android.synthetic.main.fragment_genres.view.genres_list
import kotlinx.android.synthetic.main.fragment_genres.view.genres_placeholder
@ -30,7 +31,6 @@ class GenresFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
private var genres = ArrayList<Genre>()
override fun setupFragment(activity: BaseSimpleActivity) {
Genre.sorting = context.config.genreSorting
ensureBackgroundThread {
val cachedGenres = activity.audioHelper.getAllGenres()
activity.runOnUiThread {
@ -92,8 +92,7 @@ class GenresFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
override fun onSortOpen(activity: SimpleActivity) {
ChangeSortingDialog(activity, TAB_GENRES) {
val adapter = genres_list.adapter as? GenresAdapter ?: return@ChangeSortingDialog
Genre.sorting = activity.config.genreSorting
genres.sort()
genres.sortSafely(activity.config.genreSorting)
adapter.updateItems(genres, forceUpdate = true)
}
}

View file

@ -21,6 +21,7 @@ import com.simplemobiletools.musicplayer.helpers.PLAYLIST
import com.simplemobiletools.musicplayer.helpers.TAB_PLAYLISTS
import com.simplemobiletools.musicplayer.models.Events
import com.simplemobiletools.musicplayer.models.Playlist
import com.simplemobiletools.musicplayer.models.sortSafely
import kotlinx.android.synthetic.main.fragment_playlists.view.playlists_fastscroller
import kotlinx.android.synthetic.main.fragment_playlists.view.playlists_list
import kotlinx.android.synthetic.main.fragment_playlists.view.playlists_placeholder
@ -44,8 +45,7 @@ class PlaylistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
it.trackCount = context.audioHelper.getPlaylistTrackCount(it.id)
}
Playlist.sorting = context.config.playlistSorting
playlists.sort()
playlists.sortSafely(context.config.playlistSorting)
this.playlists = playlists
activity.runOnUiThread {
@ -100,8 +100,7 @@ class PlaylistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
override fun onSortOpen(activity: SimpleActivity) {
ChangeSortingDialog(activity, TAB_PLAYLISTS) {
val adapter = playlists_list.adapter as? PlaylistsAdapter ?: return@ChangeSortingDialog
Playlist.sorting = activity.config.playlistSorting
playlists.sort()
playlists.sortSafely(activity.config.playlistSorting)
adapter.updateItems(playlists, forceUpdate = true)
}
}

View file

@ -23,6 +23,7 @@ import com.simplemobiletools.musicplayer.helpers.RESTART_PLAYER
import com.simplemobiletools.musicplayer.helpers.TAB_TRACKS
import com.simplemobiletools.musicplayer.helpers.TRACK
import com.simplemobiletools.musicplayer.models.Track
import com.simplemobiletools.musicplayer.models.sortSafely
import kotlinx.android.synthetic.main.fragment_tracks.view.tracks_fastscroller
import kotlinx.android.synthetic.main.fragment_tracks.view.tracks_list
import kotlinx.android.synthetic.main.fragment_tracks.view.tracks_placeholder
@ -33,7 +34,6 @@ class TracksFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
override fun setupFragment(activity: BaseSimpleActivity) {
ensureBackgroundThread {
Track.sorting = context.config.trackSorting
tracks = context.audioHelper.getAllTracks()
val excludedFolders = context.config.excludedFolders
@ -51,7 +51,7 @@ class TracksFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
tracks_placeholder.beVisibleIf(tracks.isEmpty())
val adapter = tracks_list.adapter
if (adapter == null) {
TracksAdapter(activity, tracks, false, tracks_list) {
TracksAdapter(activity = activity, recyclerView = tracks_list, sourceType = TracksAdapter.TYPE_TRACKS, tracks = tracks) {
activity.hideKeyboard()
activity.handleNotificationPermission { granted ->
if (granted) {
@ -102,8 +102,7 @@ class TracksFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
override fun onSortOpen(activity: SimpleActivity) {
ChangeSortingDialog(activity, TAB_TRACKS) {
val adapter = tracks_list.adapter as? TracksAdapter ?: return@ChangeSortingDialog
Track.sorting = activity.config.trackSorting
tracks.sort()
tracks.sortSafely(activity.config.trackSorting)
adapter.updateItems(tracks, forceUpdate = true)
}
}

View file

@ -21,7 +21,7 @@ class AudioHelper(private val context: Context) {
val tracks = context.tracksDAO.getAll()
.distinctBy { "${it.path}/${it.mediaStoreId}" } as ArrayList<Track>
tracks.sort()
tracks.sortSafely(config.trackSorting)
return tracks
}
@ -32,8 +32,7 @@ class AudioHelper(private val context: Context) {
it.title = it.getProperTitle(showFilename)
} as ArrayList<Track>
Track.sorting = config.getProperFolderSorting(folder)
tracks.sort()
tracks.sortSafely(config.getProperFolderSorting(folder))
return tracks
}
@ -61,7 +60,7 @@ class AudioHelper(private val context: Context) {
fun getAllArtists(): ArrayList<Artist> {
val artists = context.artistDAO.getAll() as ArrayList<Artist>
artists.sort()
artists.sortSafely(config.artistSorting)
return artists
}
@ -103,7 +102,7 @@ class AudioHelper(private val context: Context) {
fun getAllAlbums(): ArrayList<Album> {
val albums = context.albumsDAO.getAll() as ArrayList<Album>
albums.sort()
albums.sortSafely(config.albumSorting)
return albums
}
@ -143,7 +142,7 @@ class AudioHelper(private val context: Context) {
fun getAllGenres(): ArrayList<Genre> {
val genres = context.genresDAO.getAll() as ArrayList<Genre>
genres.sort()
genres.sortSafely(config.genreSorting)
return genres
}
@ -151,8 +150,7 @@ class AudioHelper(private val context: Context) {
val tracks = context.tracksDAO.getGenreTracks(genreId)
.distinctBy { "${it.path}/${it.mediaStoreId}" } as ArrayList<Track>
Track.sorting = config.trackSorting
tracks.sort()
tracks.sortSafely(config.trackSorting)
return tracks
}
@ -160,8 +158,7 @@ class AudioHelper(private val context: Context) {
val tracks = genres.flatMap { context.tracksDAO.getGenreTracks(it.id) }
.distinctBy { "${it.path}/${it.mediaStoreId}" } as ArrayList<Track>
Track.sorting = config.trackSorting
tracks.sort()
tracks.sortSafely(config.trackSorting)
return tracks
}
@ -186,8 +183,7 @@ class AudioHelper(private val context: Context) {
it.title = it.getProperTitle(showFilename)
} as ArrayList<Track>
Track.sorting = config.getProperPlaylistSorting(playlistId)
tracks.sort()
tracks.sortSafely(config.getProperPlaylistSorting(playlistId))
return tracks
}

View file

@ -7,6 +7,7 @@ import androidx.room.Index
import androidx.room.PrimaryKey
import com.simplemobiletools.commons.helpers.AlphanumericComparator
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.musicplayer.extensions.sortSafely
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_ARTIST_TITLE
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_DATE_ADDED
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_TITLE
@ -21,47 +22,43 @@ data class Album(
@ColumnInfo(name = "track_cnt") var trackCnt: Int,
@ColumnInfo(name = "artist_id") var artistId: Long,
@ColumnInfo(name = "date_added") var dateAdded: Int,
) : ListItem(), Comparable<Album> {
) : ListItem() {
companion object {
var sorting = 0
fun getComparator(sorting: Int) = Comparator<Album> { first, second ->
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> {
when {
first.title == MediaStore.UNKNOWN_STRING && second.title != MediaStore.UNKNOWN_STRING -> 1
first.title != MediaStore.UNKNOWN_STRING && second.title == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(first.title.lowercase(), second.title.lowercase())
}
}
sorting and PLAYER_SORT_BY_ARTIST_TITLE != 0 -> {
when {
first.artist == MediaStore.UNKNOWN_STRING && second.artist != MediaStore.UNKNOWN_STRING -> 1
first.artist != MediaStore.UNKNOWN_STRING && second.artist == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(first.artist.lowercase(), second.artist.lowercase())
}
}
sorting and PLAYER_SORT_BY_DATE_ADDED != 0 -> first.dateAdded.compareTo(second.dateAdded)
else -> first.year.compareTo(second.year)
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return@Comparator result
}
}
override fun compareTo(other: Album): Int {
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> {
when {
title == MediaStore.UNKNOWN_STRING && other.title != MediaStore.UNKNOWN_STRING -> 1
title != MediaStore.UNKNOWN_STRING && other.title == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(title.lowercase(), other.title.lowercase())
}
}
sorting and PLAYER_SORT_BY_ARTIST_TITLE != 0 -> {
when {
artist == MediaStore.UNKNOWN_STRING && other.artist != MediaStore.UNKNOWN_STRING -> 1
artist != MediaStore.UNKNOWN_STRING && other.artist == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(artist.lowercase(), other.artist.lowercase())
}
}
sorting and PLAYER_SORT_BY_DATE_ADDED != 0 -> {
when {
dateAdded == 0 && other.dateAdded != 0 -> -1
dateAdded != 0 && other.dateAdded == 0 -> 1
else -> dateAdded.compareTo(other.dateAdded)
}
}
else -> year.compareTo(other.year)
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return result
}
fun getBubbleText() = when {
fun getBubbleText(sorting: Int) = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> title
sorting and PLAYER_SORT_BY_ARTIST_TITLE != 0 -> artist
else -> year.toString()
}
}
fun ArrayList<Album>.sortSafely(sorting: Int) = sortSafely(Album.getComparator(sorting))

View file

@ -7,6 +7,7 @@ import androidx.room.Index
import androidx.room.PrimaryKey
import com.simplemobiletools.commons.helpers.AlphanumericComparator
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.musicplayer.extensions.sortSafely
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_ALBUM_COUNT
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_TITLE
@ -16,34 +17,36 @@ data class Artist(
@ColumnInfo(name = "title") val title: String,
@ColumnInfo(name = "album_cnt") var albumCnt: Int,
@ColumnInfo(name = "track_cnt") var trackCnt: Int,
@ColumnInfo(name = "album_art") var albumArt: String) : Comparable<Artist> {
@ColumnInfo(name = "album_art") var albumArt: String
) {
companion object {
var sorting = 0
}
override fun compareTo(other: Artist): Int {
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> {
when {
title == MediaStore.UNKNOWN_STRING && other.title != MediaStore.UNKNOWN_STRING -> 1
title != MediaStore.UNKNOWN_STRING && other.title == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(title.lowercase(), other.title.lowercase())
fun getComparator(sorting: Int) = Comparator<Artist> { first, second ->
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> {
when {
first.title == MediaStore.UNKNOWN_STRING && second.title != MediaStore.UNKNOWN_STRING -> 1
first.title != MediaStore.UNKNOWN_STRING && second.title == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(first.title.lowercase(), second.title.lowercase())
}
}
sorting and PLAYER_SORT_BY_ALBUM_COUNT != 0 -> first.albumCnt.compareTo(second.albumCnt)
else -> first.trackCnt.compareTo(second.trackCnt)
}
sorting and PLAYER_SORT_BY_ALBUM_COUNT != 0 -> albumCnt.compareTo(other.albumCnt)
else -> trackCnt.compareTo(other.trackCnt)
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return result
return@Comparator result
}
}
fun getBubbleText() = when {
fun getBubbleText(sorting: Int) = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> title
sorting and PLAYER_SORT_BY_ALBUM_COUNT != 0 -> albumCnt.toString()
else -> trackCnt.toString()
}
}
fun ArrayList<Artist>.sortSafely(sorting: Int) = sortSafely(Artist.getComparator(sorting))

View file

@ -2,28 +2,30 @@ package com.simplemobiletools.musicplayer.models
import com.simplemobiletools.commons.helpers.AlphanumericComparator
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.musicplayer.extensions.sortSafely
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_TITLE
data class Folder(val title: String, val trackCount: Int, val path: String) : Comparable<Folder> {
data class Folder(val title: String, val trackCount: Int, val path: String) {
companion object {
var sorting = 0
fun getComparator(sorting: Int) = Comparator<Folder> { first, second ->
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> AlphanumericComparator().compare(first.title.lowercase(), second.title.lowercase())
else -> first.trackCount.compareTo(second.trackCount)
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return@Comparator result
}
}
override fun compareTo(other: Folder): Int {
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> AlphanumericComparator().compare(title.lowercase(), other.title.lowercase())
else -> trackCount.compareTo(other.trackCount)
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return result
}
fun getBubbleText() = when {
fun getBubbleText(sorting: Int) = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> title
else -> trackCount.toString()
}
}
fun ArrayList<Folder>.sortSafely(sorting: Int) = sortSafely(Folder.getComparator(sorting))

View file

@ -6,6 +6,7 @@ import androidx.room.Index
import androidx.room.PrimaryKey
import com.simplemobiletools.commons.helpers.AlphanumericComparator
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.musicplayer.extensions.sortSafely
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_TITLE
@Entity("genres", indices = [(Index(value = ["id"], unique = true))])
@ -13,26 +14,26 @@ data class Genre(
@PrimaryKey(autoGenerate = true) var id: Long,
@ColumnInfo(name = "title") val title: String,
@ColumnInfo(name = "track_cnt") var trackCnt: Int,
) : Comparable<Genre> {
) {
companion object {
var sorting = 0
fun getComparator(sorting: Int) = Comparator<Genre> { first, second ->
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> AlphanumericComparator().compare(first.title.lowercase(), second.title.lowercase())
else -> first.trackCnt.compareTo(second.trackCnt)
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return@Comparator result
}
}
override fun compareTo(other: Genre): Int {
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> AlphanumericComparator().compare(title.lowercase(), other.title.lowercase())
else -> trackCnt.compareTo(other.trackCnt)
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return result
}
fun getBubbleText() = when {
fun getBubbleText(sorting: Int) = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> title
else -> trackCnt.toString()
}
}
fun ArrayList<Genre>.sortSafely(sorting: Int) = sortSafely(Genre.getComparator(sorting))

View file

@ -3,6 +3,7 @@ package com.simplemobiletools.musicplayer.models
import androidx.room.*
import com.simplemobiletools.commons.helpers.AlphanumericComparator
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.musicplayer.extensions.sortSafely
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_TITLE
@Entity(tableName = "playlists", indices = [(Index(value = ["id"], unique = true))])
@ -11,28 +12,28 @@ data class Playlist(
@ColumnInfo(name = "title") var title: String,
@Ignore var trackCount: Int = 0
) : Comparable<Playlist> {
) {
constructor() : this(0, "", 0)
companion object {
var sorting = 0
fun getComparator(sorting: Int) = Comparator<Playlist> { first, second ->
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> AlphanumericComparator().compare(first.title.lowercase(), second.title.lowercase())
else -> first.trackCount.compareTo(second.trackCount)
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return@Comparator result
}
}
override fun compareTo(other: Playlist): Int {
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> AlphanumericComparator().compare(title.lowercase(), other.title.lowercase())
else -> trackCount.compareTo(other.trackCount)
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return result
}
fun getBubbleText() = when {
fun getBubbleText(sorting: Int) = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> title
else -> trackCount.toString()
}
}
fun ArrayList<Playlist>.sortSafely(sorting: Int) = sortSafely(Playlist.getComparator(sorting))

View file

@ -11,6 +11,7 @@ import com.simplemobiletools.commons.extensions.getFilenameFromPath
import com.simplemobiletools.commons.extensions.getFormattedDuration
import com.simplemobiletools.commons.helpers.AlphanumericComparator
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.musicplayer.extensions.sortSafely
import com.simplemobiletools.musicplayer.helpers.*
import java.io.File
import java.io.Serializable
@ -36,57 +37,44 @@ data class Track(
@ColumnInfo(name = "date_added") var dateAdded: Int,
@ColumnInfo(name = "order_in_playlist") var orderInPlaylist: Int,
@ColumnInfo(name = "flags") var flags: Int = 0
) : Serializable, Comparable<Track>, ListItem() {
) : Serializable, ListItem() {
companion object {
private const val serialVersionUID = 6717978793256852245L
var sorting = 0
fun getComparator(sorting: Int) = Comparator<Track> { first, second ->
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> {
when {
first.title == MediaStore.UNKNOWN_STRING && second.title != MediaStore.UNKNOWN_STRING -> 1
first.title != MediaStore.UNKNOWN_STRING && second.title == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(first.title.lowercase(), second.title.lowercase())
}
}
sorting and PLAYER_SORT_BY_ARTIST_TITLE != 0 -> {
when {
first.artist == MediaStore.UNKNOWN_STRING && second.artist != MediaStore.UNKNOWN_STRING -> 1
first.artist != MediaStore.UNKNOWN_STRING && second.artist == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(first.artist.lowercase(), second.artist.lowercase())
}
}
sorting and PLAYER_SORT_BY_TRACK_ID != 0 -> first.trackId.compareTo(second.trackId)
sorting and PLAYER_SORT_BY_DATE_ADDED != 0 -> first.dateAdded.compareTo(second.dateAdded)
sorting and PLAYER_SORT_BY_CUSTOM != 0 -> first.orderInPlaylist.compareTo(second.orderInPlaylist)
else -> first.duration.compareTo(second.duration)
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return@Comparator result
}
}
override fun compareTo(other: Track): Int {
var res = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> {
when {
title == MediaStore.UNKNOWN_STRING && other.title != MediaStore.UNKNOWN_STRING -> 1
title != MediaStore.UNKNOWN_STRING && other.title == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(title.lowercase(), other.title.lowercase())
}
}
sorting and PLAYER_SORT_BY_ARTIST_TITLE != 0 -> {
when {
artist == MediaStore.UNKNOWN_STRING && artist != MediaStore.UNKNOWN_STRING -> 1
artist != MediaStore.UNKNOWN_STRING && artist == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(artist.lowercase(), other.artist.lowercase())
}
}
sorting and PLAYER_SORT_BY_TRACK_ID != 0 -> {
when {
trackId == -1 && other.trackId != -1 -> 1
trackId != -1 && other.trackId == -1 -> -1
else -> AlphanumericComparator().compare(trackId.toString(), other.trackId.toString())
}
}
sorting and PLAYER_SORT_BY_DATE_ADDED != 0 -> {
when {
dateAdded == 0 && other.dateAdded != 0 -> -1
dateAdded != 0 && other.dateAdded == 0 -> 1
else -> dateAdded.compareTo(other.dateAdded)
}
}
sorting and PLAYER_SORT_BY_CUSTOM != 0 -> {
orderInPlaylist.compareTo(other.orderInPlaylist)
}
else -> duration.compareTo(other.duration)
}
if (sorting and SORT_DESCENDING != 0) {
res *= -1
}
return res
}
fun getBubbleText() = when {
fun getBubbleText(sorting: Int) = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> title
sorting and PLAYER_SORT_BY_ARTIST_TITLE != 0 -> artist
else -> duration.getFormattedDuration()
@ -106,3 +94,5 @@ data class Track(
ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mediaStoreId)
}
}
fun ArrayList<Track>.sortSafely(sorting: Int) = sortSafely(Track.getComparator(sorting))

View file

@ -37,6 +37,7 @@ import com.simplemobiletools.musicplayer.inlines.indexOfFirstOrNull
import com.simplemobiletools.musicplayer.models.Events
import com.simplemobiletools.musicplayer.models.QueueItem
import com.simplemobiletools.musicplayer.models.Track
import com.simplemobiletools.musicplayer.models.sortSafely
import org.greenrobot.eventbus.EventBus
import java.io.IOException
import kotlin.math.roundToInt
@ -355,7 +356,7 @@ class MusicService : Service(), MultiPlayer.PlaybackCallbacks {
mCurrTrackCover = getAlbumImage().first
trackChanged()
val secs = getPosition()!! / 1000
val secs = (getPosition() ?: 0) / 1000
broadcastTrackProgress(secs)
}
trackStateChanged()
@ -406,8 +407,7 @@ class MusicService : Service(), MultiPlayer.PlaybackCallbacks {
if (queueItems.isEmpty()) {
val tracks = audioHelper.getPlaylistTracks(ALL_TRACKS_PLAYLIST_ID)
if (tracks.isNotEmpty()) {
Track.sorting = config.trackSorting
tracks.sort()
tracks.sortSafely(config.trackSorting)
addQueueItems(newTracks = tracks) {}
queueItems = queueDAO.getAll()
}

View file

@ -1,9 +1,8 @@
package com.simplemobiletools.musicplayer.views
import android.app.Activity
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.os.Handler
import android.os.Looper
import android.provider.MediaStore
import android.util.AttributeSet
import android.widget.RelativeLayout
@ -13,12 +12,14 @@ import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.extensions.ensureActivityNotDestroyed
import com.simplemobiletools.musicplayer.extensions.getTrackCoverArt
import com.simplemobiletools.musicplayer.extensions.sendIntent
import com.simplemobiletools.musicplayer.extensions.updatePlayPauseIcon
import com.simplemobiletools.musicplayer.helpers.PLAYPAUSE
import com.simplemobiletools.musicplayer.models.Track
import kotlinx.android.synthetic.main.view_current_track_bar.view.*
import kotlinx.android.synthetic.main.view_current_track_bar.view.current_track_label
import kotlinx.android.synthetic.main.view_current_track_bar.view.current_track_play_pause
class CurrentTrackBar(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) {
fun updateColors() {
@ -51,10 +52,12 @@ class CurrentTrackBar(context: Context, attributeSet: AttributeSet) : RelativeLa
.transform(CenterCrop(), RoundedCorners(cornerRadius))
context.getTrackCoverArt(track) { coverArt ->
Glide.with(this)
.load(coverArt)
.apply(options)
.into(findViewById(R.id.current_track_image))
(context as? Activity)?.ensureActivityNotDestroyed {
Glide.with(this)
.load(coverArt)
.apply(options)
.into(findViewById(R.id.current_track_image))
}
}
}