adding track sorting

This commit is contained in:
tibbi 2020-10-24 21:52:20 +02:00
parent cf8a34ba68
commit a7b67ddf71
5 changed files with 34 additions and 33 deletions

View file

@ -57,7 +57,8 @@ class ChangeSortingDialog(val activity: Activity, val tabIndex: Int, val callbac
}
TAB_TRACKS -> {
radioItems.add(RadioItem(0, activity.getString(R.string.title), PLAYER_SORT_BY_TITLE))
radioItems.add(RadioItem(1, activity.getString(R.string.duration), PLAYER_SORT_BY_DURATION))
radioItems.add(RadioItem(1, activity.getString(R.string.artist), PLAYER_SORT_BY_ARTIST_TITLE))
radioItems.add(RadioItem(2, activity.getString(R.string.duration), PLAYER_SORT_BY_DURATION))
}
}

View file

@ -7,13 +7,14 @@ import com.google.gson.Gson
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.beGoneIf
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.helpers.AlphanumericComparator
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.musicplayer.activities.SimpleActivity
import com.simplemobiletools.musicplayer.activities.TrackActivity
import com.simplemobiletools.musicplayer.adapters.TracksAdapter
import com.simplemobiletools.musicplayer.dialogs.ChangeSortingDialog
import com.simplemobiletools.musicplayer.extensions.*
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.Album
import com.simplemobiletools.musicplayer.models.Track
@ -35,7 +36,9 @@ class TracksFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
albums.forEach {
tracks.addAll(activity.getAlbumTracksSync(it.id))
}
tracks.sortWith { o1, o2 -> AlphanumericComparator().compare(o1.title.toLowerCase(), o2.title.toLowerCase()) }
Track.sorting = activity.config.trackSorting
tracks.sort()
activity.runOnUiThread {
tracks_placeholder.beVisibleIf(tracks.isEmpty())
@ -89,5 +92,12 @@ 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
val tracks = adapter.tracks
Track.sorting = activity.config.trackSorting
tracks.sort()
adapter.updateItems(tracks, forceUpdate = true)
}
}
}

View file

@ -15,15 +15,15 @@ data class Album(val id: Long, val artist: String, val title: String, val coverA
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> {
when {
title == MediaStore.UNKNOWN_STRING -> 1
other.title == MediaStore.UNKNOWN_STRING -> -1
title == MediaStore.UNKNOWN_STRING && other.title != MediaStore.UNKNOWN_STRING -> 1
title != MediaStore.UNKNOWN_STRING && other.title == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(title.toLowerCase(), other.title.toLowerCase())
}
}
sorting and PLAYER_SORT_BY_ARTIST_TITLE != 0 -> {
when {
artist == MediaStore.UNKNOWN_STRING -> 1
other.artist == MediaStore.UNKNOWN_STRING -> -1
artist == MediaStore.UNKNOWN_STRING && other.artist != MediaStore.UNKNOWN_STRING -> 1
artist != MediaStore.UNKNOWN_STRING && other.artist == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(artist.toLowerCase(), other.artist.toLowerCase())
}
}

View file

@ -15,8 +15,8 @@ data class Artist(val id: Long, val title: String, var albumCnt: Int, var trackC
var result = when {
sorting and PLAYER_SORT_BY_TITLE != 0 -> {
when {
title == MediaStore.UNKNOWN_STRING -> 1
other.title == MediaStore.UNKNOWN_STRING -> -1
title == MediaStore.UNKNOWN_STRING && other.title != MediaStore.UNKNOWN_STRING -> 1
title != MediaStore.UNKNOWN_STRING && other.title == MediaStore.UNKNOWN_STRING -> -1
else -> AlphanumericComparator().compare(title.toLowerCase(), other.title.toLowerCase())
}
}

View file

@ -5,10 +5,9 @@ import androidx.room.ColumnInfo
import androidx.room.Entity
import com.simplemobiletools.commons.extensions.getFilenameFromPath
import com.simplemobiletools.commons.extensions.getFormattedDuration
import com.simplemobiletools.commons.helpers.SORT_BY_ARTIST
import com.simplemobiletools.commons.helpers.SORT_BY_PATH
import com.simplemobiletools.commons.helpers.SORT_BY_TITLE
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_ARTIST_TITLE
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_TITLE
import com.simplemobiletools.musicplayer.helpers.SHOW_FILENAME_IF_UNAVAILABLE
import com.simplemobiletools.musicplayer.helpers.SHOW_FILENAME_NEVER
import java.io.Serializable
@ -33,30 +32,21 @@ data class Track(
override fun compareTo(other: Track): Int {
var res = when {
sorting and SORT_BY_TITLE != 0 -> {
if (title == MediaStore.UNKNOWN_STRING && other.title != MediaStore.UNKNOWN_STRING) {
1
} else if (title != MediaStore.UNKNOWN_STRING && other.title == MediaStore.UNKNOWN_STRING) {
-1
} else {
title.toLowerCase().compareTo(other.title.toLowerCase())
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.toLowerCase(), other.title.toLowerCase())
}
}
sorting and SORT_BY_ARTIST != 0 -> {
if (artist == MediaStore.UNKNOWN_STRING && other.artist != MediaStore.UNKNOWN_STRING) {
1
} else if (artist != MediaStore.UNKNOWN_STRING && other.artist == MediaStore.UNKNOWN_STRING) {
-1
} else {
artist.toLowerCase().compareTo(other.artist.toLowerCase())
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.toLowerCase(), other.artist.toLowerCase())
}
}
sorting and SORT_BY_PATH != 0 -> path.toLowerCase().compareTo(other.path.toLowerCase())
else -> when {
duration == other.duration -> 0
duration > other.duration -> 1
else -> -1
}
else -> duration.compareTo(other.duration)
}
if (sorting and SORT_DESCENDING != 0) {