Skip to content

Commit

Permalink
file path, album artist sort
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Jan 3, 2025
1 parent 8b743f5 commit f448b98
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ class SupportComparator<T, U>(
cmp: Comparator<T>,
invert: Boolean = false,
fallback: Comparator<T>? = null
):
Comparator<T> {
if (!invert) return cmp
return SupportComparator(cmp, fallback, true) { it }
): Comparator<T> {
if (!invert && fallback == null) return cmp
return SupportComparator(cmp, fallback, invert) { it }
}

fun <T> createAlphanumericComparator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.akanework.gramophone.ui.adapters
import android.net.Uri
import androidx.appcompat.widget.PopupMenu
import androidx.fragment.app.Fragment
import java.util.GregorianCalendar
import kotlinx.coroutines.flow.Flow
import org.akanework.gramophone.R
import org.akanework.gramophone.ui.MainActivity
Expand Down Expand Up @@ -105,7 +106,8 @@ class AlbumAdapter(
setOf(
Sorter.Type.ByTitleDescending, Sorter.Type.ByTitleAscending,
Sorter.Type.ByArtistDescending, Sorter.Type.ByArtistAscending,
Sorter.Type.BySizeDescending, Sorter.Type.BySizeAscending
Sorter.Type.BySizeDescending, Sorter.Type.BySizeAscending,
Sorter.Type.ByReleaseDateAscending, Sorter.Type.ByReleaseDateDescending
)
) {
override fun getArtist(item: Album): String? {
Expand All @@ -115,5 +117,9 @@ class AlbumAdapter(
override fun getCover(item: Album): Uri? {
return item.cover ?: super.getCover(item)
}

override fun getReleaseDate(item: Album): Long {
return GregorianCalendar(item.albumYear ?: 0, 0, 0, 0, 0, 0).timeInMillis
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ open class BaseDecorAdapter<T : BaseAdapter<*>>(
),
Pair(R.id.artist, Sorter.Type.ByArtistAscending),
Pair(R.id.album, Sorter.Type.ByAlbumTitleAscending),
Pair(R.id.album_artist, Sorter.Type.ByAlbumTitleAscending),
Pair(R.id.size, Sorter.Type.BySizeDescending),
Pair(R.id.add_date, Sorter.Type.ByAddDateDescending),
Pair(R.id.release_date, Sorter.Type.ByReleaseDateDescending),
Pair(R.id.mod_date, Sorter.Type.ByModifiedDateDescending)
Pair(R.id.mod_date, Sorter.Type.ByModifiedDateDescending),
Pair(R.id.file_path, Sorter.Type.ByFilePathAscending)
)
val layoutMap = mapOf(
Pair(R.id.list, BaseAdapter.LayoutType.LIST),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,15 @@ class SongAdapter(
}
} else {
super.onBindViewHolder(holder, position, payloads)
if (currentMediaItem == null || getSongList()[position].mediaId != currentMediaItem)
if (currentMediaItem == null || getSongList().getOrNull(position)?.mediaId != currentMediaItem)
return
}
holder.nowPlaying.setImageDrawable(NowPlayingDrawable()
.also { it.level = if (currentIsPlaying == true) 1 else 0 })
holder.nowPlaying.visibility = View.VISIBLE
}

// TODO support album year sort
class MediaItemHelper(
types: Set<Sorter.Type> = setOf(
Sorter.Type.ByTitleDescending, Sorter.Type.ByTitleAscending,
Expand Down Expand Up @@ -359,14 +360,14 @@ class SongAdapter(
&& item.mediaMetadata.releaseDay == null
) {
return GregorianCalendar(
(item.mediaMetadata.recordingYear ?: 0) + 1900,
item.mediaMetadata.recordingYear ?: 0,
(item.mediaMetadata.recordingMonth ?: 1) - 1,
item.mediaMetadata.recordingDay ?: 0, 0, 0, 0
)
.timeInMillis
}
return GregorianCalendar(
(item.mediaMetadata.releaseYear ?: 0) + 1900,
item.mediaMetadata.releaseYear ?: 0,
(item.mediaMetadata.releaseMonth ?: 1) - 1,
item.mediaMetadata.releaseDay ?: 0, 0, 0, 0
)
Expand Down
37 changes: 31 additions & 6 deletions app/src/main/kotlin/org/akanework/gramophone/ui/adapters/Sorter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.akanework.gramophone.ui.adapters

import android.net.Uri
import java.io.File
import org.akanework.gramophone.logic.comparators.SupportComparator
import org.akanework.gramophone.logic.utils.CalculationUtils

Expand All @@ -39,6 +40,7 @@ class Sorter<T>(
abstract fun getCover(item: T): Uri?

open fun getArtist(item: T): String? = throw UnsupportedOperationException()
open fun getFile(item: T): File = throw UnsupportedOperationException()
open fun getAlbumTitle(item: T): String? = throw UnsupportedOperationException()
open fun getAlbumArtist(item: T): String? = throw UnsupportedOperationException()
open fun getSize(item: T): Int = throw UnsupportedOperationException()
Expand Down Expand Up @@ -66,6 +68,9 @@ class Sorter<T>(
fun canGetAddDate(): Boolean = typesSupported.contains(Type.ByAddDateAscending)
|| typesSupported.contains(Type.ByAddDateDescending)

fun canGetFile(): Boolean = typesSupported.contains(Type.ByFilePathAscending)
|| typesSupported.contains(Type.ByFilePathDescending)

fun canGetReleaseDate(): Boolean = typesSupported.contains(Type.ByReleaseDateAscending)
|| typesSupported.contains(Type.ByReleaseDateDescending)

Expand All @@ -86,6 +91,7 @@ class Sorter<T>(
NaturalOrder, ByAddDateDescending, ByAddDateAscending,
ByReleaseDateDescending, ByReleaseDateAscending,
ByModifiedDateDescending, ByModifiedDateAscending,
ByFilePathDescending, ByFilePathAscending,
ByDiscAndTrack,

/* do not use NativeOrder for something other than title or edit getSupportedTypes */
Expand Down Expand Up @@ -155,6 +161,7 @@ class Sorter<T>(
}, getComparator(Type.ByDiscAndTrack))
}

// TODO support choosing album artist > album year > disc/track
Type.ByAlbumArtistDescending -> {
SupportComparator.createAlphanumericComparator(true, {
sortingHelper.getAlbumArtist(it) ?: ""
Expand Down Expand Up @@ -193,13 +200,15 @@ class Sorter<T>(

Type.ByReleaseDateDescending -> {
SupportComparator.createInversionComparator(
compareBy { sortingHelper.getReleaseDate(it) }, true
compareBy { sortingHelper.getReleaseDate(it) }, true,
if (sortingHelper.canGetDiskAndTrack()) getComparator(Type.ByDiscAndTrack) else null
)
}

Type.ByReleaseDateAscending -> {
SupportComparator.createInversionComparator(
compareBy { sortingHelper.getReleaseDate(it) }, false
compareBy { sortingHelper.getReleaseDate(it) }, false,
if (sortingHelper.canGetDiskAndTrack()) getComparator(Type.ByDiscAndTrack) else null
)
}

Expand All @@ -215,6 +224,18 @@ class Sorter<T>(
)
}

Type.ByFilePathDescending -> {
SupportComparator.createAlphanumericComparator(true, {
sortingHelper.getFile(it).path
}, null)
}

Type.ByFilePathAscending -> {
SupportComparator.createAlphanumericComparator(false, {
sortingHelper.getFile(it).path
}, null)
}

Type.ByDiscAndTrack -> {
compareBy { sortingHelper.getDiscAndTrack(it) }
}
Expand All @@ -235,19 +256,23 @@ class Sorter<T>(
fun getFastScrollHintFor(item: T, sortType: Type): String? {
return when (sortType) {
Type.ByTitleDescending, Type.ByTitleAscending, Type.NativeOrder, Type.NativeOrderDescending -> {
(sortingHelper.getTitle(item) ?: "-").firstOrNull()?.toString()
sortingHelper.getTitle(item)?.firstOrNull()?.toString()
}

Type.ByArtistDescending, Type.ByArtistAscending -> {
(sortingHelper.getArtist(item) ?: "-").firstOrNull()?.toString()
sortingHelper.getArtist(item)?.firstOrNull()?.toString()
}

Type.ByAlbumTitleDescending, Type.ByAlbumTitleAscending -> {
(sortingHelper.getAlbumTitle(item) ?: "-").firstOrNull()?.toString()
sortingHelper.getAlbumTitle(item)?.firstOrNull()?.toString()
}

Type.ByAlbumArtistDescending, Type.ByAlbumArtistAscending -> {
(sortingHelper.getAlbumArtist(item) ?: "-").firstOrNull()?.toString()
sortingHelper.getAlbumArtist(item)?.firstOrNull()?.toString()
}

Type.ByFilePathDescending, Type.ByFilePathAscending -> {
null // can probably not do anything better
}

Type.BySizeDescending, Type.BySizeAscending -> {
Expand Down
29 changes: 14 additions & 15 deletions app/src/main/res/menu/sort_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<group android:checkableBehavior="single">
<item
android:id="@+id/album_artist"
android:checkable="true"
android:orderInCategory="-1"
android:title="@string/album_artist"
android:visible="false"
app:showAsAction="never" />
<item
android:id="@+id/natural"
android:orderInCategory="0"
Expand All @@ -32,29 +25,35 @@
android:title="@string/sort_by_album"
app:showAsAction="never" />
<item
android:id="@+id/size"
android:checked="false"
android:id="@+id/album_artist"
android:orderInCategory="4"
android:title="@string/album_artist"
app:showAsAction="never" />
<item
android:id="@+id/size"
android:orderInCategory="5"
android:title="@string/sort_by_size"
app:showAsAction="never" />
<item
android:id="@+id/add_date"
android:checked="false"
android:orderInCategory="5"
android:orderInCategory="6"
android:title="@string/sort_by_add_date"
app:showAsAction="never" />
<item
android:id="@+id/release_date"
android:checked="false"
android:orderInCategory="6"
android:orderInCategory="7"
android:title="@string/sort_by_release_date"
app:showAsAction="never" />
<item
android:id="@+id/mod_date"
android:checked="false"
android:orderInCategory="7"
android:orderInCategory="8"
android:title="@string/sort_by_modified_date"
app:showAsAction="never" />
<item
android:id="@+id/file_path"
android:orderInCategory="9"
android:title="@string/sort_by_file_path"
app:showAsAction="never" />
</group>

<item
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<string name="sort_by_name">Name</string>
<string name="sort_by_artist">Artist</string>
<string name="sort_by_album">Album</string>
<string name="sort_by_album_artist">Album artist</string>
<string name="dismiss">Dismiss</string>
<string name="unknown_genre">Unknown genre</string>
<string name="unknown_year">Unknown year</string>
Expand Down Expand Up @@ -204,4 +205,5 @@
<string name="delete">Delete</string>
<string name="yes">Yes</string>
<string name="no">No</string>
<string name="sort_by_file_path">File path</string>
</resources>

0 comments on commit f448b98

Please sign in to comment.