Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,16 @@ open class BaseDecorAdapter<T : AdapterFragment.BaseInterface<*>>(
popupMenu.menu.findItem(it.key).isVisible = adapter.canChangeLayout
}
popupMenu.menu.findItem(R.id.display).isVisible = adapter.canChangeLayout
if (adapter.sortType.value != Sorter.Type.None) {
when (adapter.sortType.value) {
in buttonMap.values -> {
popupMenu.menu.findItem(
buttonMap.entries
.first { it.value == adapter.sortType.value }.key
).isChecked = true
}

else -> throw IllegalStateException("Invalid sortType ${adapter.sortType.value.name}")
}
val currentSort = adapter.sortType.value
val activeEntry = buttonMap.entries.find { it.value == currentSort || Sorter.Type.inverse(it.value) == currentSort }

if (activeEntry == null) {
throw IllegalStateException("Invalid sortType ${adapter.sortType.value.name}")
}

popupMenu.menu.findItem(activeEntry.key).isChecked = true

if (adapter.canChangeLayout) {
when (adapter.layoutType) {
in layoutMap.values -> {
Expand All @@ -131,16 +129,29 @@ open class BaseDecorAdapter<T : AdapterFragment.BaseInterface<*>>(
else -> throw IllegalStateException("Invalid layoutType ${adapter.layoutType?.name}")
}
}

val reverseItem = popupMenu.menu.findItem(R.id.reverse_order)
val inverse = Sorter.Type.inverse(adapter.sortType.value)
if (inverse == null) {
reverseItem.isVisible = false
} else {
reverseItem.isChecked = currentSort != activeEntry.value && currentSort != Sorter.Type.None
}

popupMenu.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
in buttonMap.keys -> {
if (!menuItem.isChecked) {
adapter.sort(buttonMap[menuItem.itemId]!!)
// always use default direction for this sort mode if sort mode is changed,
// and reset the reverseOrder checkbox
val targetType = buttonMap[menuItem.itemId]!!
reverseItem.isChecked = false
adapter.sort(targetType)
menuItem.isChecked = true
prefs.edit {
putString(
"S" + getAdapterType(adapter).toString(),
buttonMap[menuItem.itemId].toString()
targetType.toString()
)
}
}
Expand All @@ -161,6 +172,25 @@ open class BaseDecorAdapter<T : AdapterFragment.BaseInterface<*>>(
true
}

R.id.reverse_order -> {
menuItem.isChecked = !menuItem.isChecked
val activeId = buttonMap.entries.find {
it.value == adapter.sortType.value || Sorter.Type.inverse(it.value) == adapter.sortType.value
}?.key ?: -1
val baseType = buttonMap[activeId]
if (baseType != null) {
val targetType = if (menuItem.isChecked) Sorter.Type.inverse(baseType) ?: baseType else baseType
adapter.sort(targetType)
prefs.edit {
putString(
"S" + getAdapterType(adapter).toString(),
targetType.toString()
)
}
}
true
}

else -> onExtraMenuButtonPressed(menuItem)
}
}
Expand Down Expand Up @@ -321,4 +351,4 @@ open class BaseDecorAdapter<T : AdapterFragment.BaseInterface<*>>(
override fun getItemHeightFromZeroTo(to: Int): Int {
return if (to > 0) dpHeight else 0
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ class DetailedFolderAdapter(
Sorter.Type.None
}
override val sortTypes = setOf(
Sorter.Type.ByFilePathAscending, Sorter.Type.BySizeDescending,
Sorter.Type.ByAddDateDescending, Sorter.Type.ByModifiedDateDescending
Sorter.Type.ByFilePathAscending, Sorter.Type.ByFilePathDescending,
Sorter.Type.BySizeDescending, Sorter.Type.BySizeAscending,
Sorter.Type.ByAddDateDescending, Sorter.Type.ByAddDateAscending,
Sorter.Type.ByModifiedDateDescending, Sorter.Type.ByModifiedDateAscending
)
override val sortType = MutableStateFlow(
if (prefSortType != Sorter.Type.None && sortTypes.contains(prefSortType))
Expand Down Expand Up @@ -138,14 +140,32 @@ class DetailedFolderAdapter(
it.folderList.size + it.songList.size
}

Sorter.Type.BySizeAscending -> item.folderList.values.sortedBy {
it.folderList.size + it.songList.size
}

Sorter.Type.ByAddDateDescending -> item.folderList.values.sortedByDescending {
it.addDate ?: Long.MIN_VALUE
}

Sorter.Type.ByAddDateAscending -> item.folderList.values.sortedBy {
it.addDate ?: Long.MIN_VALUE
}

Sorter.Type.ByModifiedDateDescending -> item.folderList.values.sortedByDescending {
it.modifiedDate ?: Long.MIN_VALUE
}

Sorter.Type.ByModifiedDateAscending -> item.folderList.values.sortedBy {
it.modifiedDate ?: Long.MIN_VALUE
}

Sorter.Type.ByFilePathDescending -> item.folderList.values.sortedWith(
SupportComparator.createAlphanumericComparator(inverted = true, cnv = {
it.folderName
})
)

else -> item.folderList.values.sortedWith(
SupportComparator.createAlphanumericComparator(cnv = {
it.folderName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,4 @@ class SongAdapter(
return item.mediaMetadata.modifiedDate ?: -1
}
}
}
}
42 changes: 32 additions & 10 deletions app/src/main/java/org/akanework/gramophone/ui/adapters/Sorter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ class Sorter<T>(
ByAlbumYearDescending, ByAlbumYearAscending,
BySizeDescending, BySizeAscending,
ByAlbumSizeDescending, ByAlbumSizeAscending,
NaturalOrder, ByAddDateDescending, ByAddDateAscending,
NaturalOrder, NaturalOrderDescending, ByAddDateDescending, ByAddDateAscending,
ByReleaseDateDescending, ByReleaseDateAscending,
ByModifiedDateDescending, ByModifiedDateAscending,
ByFilePathDescending, ByFilePathAscending,
ByDiscAndTrack,
ByDiscAndTrack, ByDiscAndTrackDescending,
None;

companion object {
Expand All @@ -127,7 +127,8 @@ class Sorter<T>(
BySizeAscending -> BySizeDescending
ByAlbumSizeDescending -> ByAlbumSizeAscending
ByAlbumSizeAscending -> ByAlbumSizeDescending
NaturalOrder -> null
NaturalOrder -> NaturalOrderDescending
NaturalOrderDescending -> NaturalOrder
ByAddDateDescending -> ByAddDateAscending
ByAddDateAscending -> ByAddDateDescending
ByReleaseDateDescending -> ByReleaseDateAscending
Expand All @@ -136,17 +137,26 @@ class Sorter<T>(
ByModifiedDateAscending -> ByModifiedDateDescending
ByFilePathDescending -> ByFilePathAscending
ByFilePathAscending -> ByFilePathDescending
ByDiscAndTrack -> null
ByDiscAndTrack -> ByDiscAndTrackDescending
ByDiscAndTrackDescending -> ByDiscAndTrack
None -> null
}
}
}

fun getSupportedTypes(): Set<Type> {
return sortingHelper.typesSupported.let { types ->
var res = types
if (naturalOrderHelper != null || rawOrderExposed == Type.NaturalOrder)
types + Type.NaturalOrder
else types
res = res + Type.NaturalOrder

// Automatically add inverse for everything supported
val allWithInverses = mutableSetOf<Type>()
res.forEach {
allWithInverses.add(it)
Type.inverse(it)?.let { inv -> allWithInverses.add(inv) }
}
allWithInverses
}
}

Expand Down Expand Up @@ -343,12 +353,24 @@ class Sorter<T>(
compareBy { sortingHelper.getDiscAndTrack(it) }
}

Type.ByDiscAndTrackDescending -> {
SupportComparator.createInversionComparator(
compareBy { sortingHelper.getDiscAndTrack(it) }, true
)
}

Type.NaturalOrder -> {
SupportComparator.createInversionComparator(
compareBy { naturalOrderHelper!!.lookup(it) }, false
)
}

Type.NaturalOrderDescending -> {
SupportComparator.createInversionComparator(
compareBy { naturalOrderHelper!!.lookup(it) }, true
)
}

Type.None -> throw IllegalStateException()
}
)
Expand Down Expand Up @@ -396,7 +418,7 @@ class Sorter<T>(
sortingHelper.getAlbumSize(item).toString()
}

Type.ByDiscAndTrack -> {
Type.ByDiscAndTrack, Type.ByDiscAndTrackDescending -> {
sortingHelper.getDiscAndTrack(item).toString()
}

Expand All @@ -409,11 +431,11 @@ class Sorter<T>(
}

Type.ByModifiedDateDescending, Type.ByModifiedDateAscending -> {
CalculationUtils.convertUnixTimestampToMonthDay(sortingHelper.getAddDate(item))
CalculationUtils.convertUnixTimestampToMonthDay(sortingHelper.getModifiedDate(item))
}

Type.NaturalOrder -> {
(if (rawOrderExposed == sortType) {
Type.NaturalOrder, Type.NaturalOrderDescending -> {
(if (rawOrderExposed == sortType || (rawOrderExposed != null && Type.inverse(rawOrderExposed) == sortType)) {
pos
} else {
naturalOrderHelper!!.lookup(item)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/uk/akane/libphonograph/items/FileNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface FileNode {
if (it == Long.MAX_VALUE) null else it
}
val modifiedDate: Long?
get() = min(songList.maxOfOrNull { it.mediaMetadata.modifiedDate ?: Long.MIN_VALUE }
get() = maxOf(songList.maxOfOrNull { it.mediaMetadata.modifiedDate ?: Long.MIN_VALUE }
?: Long.MIN_VALUE,
folderList.maxOfOrNull { it.value.modifiedDate ?: Long.MIN_VALUE }
?: Long.MIN_VALUE).let {
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/res/menu/sort_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/reverse_order"
android:checkable="true"
android:title="@string/reverse_order"
app:showAsAction="never" />
<group android:checkableBehavior="single">
<item
android:id="@+id/album_artist_checkbox"
Expand Down Expand Up @@ -108,4 +113,4 @@
</group>
</menu>
</item>
</menu>
</menu>
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 @@ -153,6 +153,8 @@
<string name="folders">Folders</string>
<!-- Color theme in appearance settings: light/dark/follow system -->
<string name="settings_app_theme">App theme</string>
<!-- Switch that reverses the order of the selected sort mode -->
<string name="reverse_order">Reverse order</string>
<!-- The sort mode if something has inherent proper order (playlist order for playlist, CD order
for album, etc) -->
<string name="natural_order">Natural order</string>
Expand Down