Skip to content

Commit f448b98

Browse files
committed
file path, album artist sort
1 parent 8b743f5 commit f448b98

File tree

8 files changed

+65
-31
lines changed

8 files changed

+65
-31
lines changed

app/src/main/kotlin/org/akanework/gramophone/logic/comparators/SupportComparator.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ class SupportComparator<T, U>(
4141
cmp: Comparator<T>,
4242
invert: Boolean = false,
4343
fallback: Comparator<T>? = null
44-
):
45-
Comparator<T> {
46-
if (!invert) return cmp
47-
return SupportComparator(cmp, fallback, true) { it }
44+
): Comparator<T> {
45+
if (!invert && fallback == null) return cmp
46+
return SupportComparator(cmp, fallback, invert) { it }
4847
}
4948

5049
fun <T> createAlphanumericComparator(

app/src/main/kotlin/org/akanework/gramophone/ui/adapters/AlbumAdapter.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package org.akanework.gramophone.ui.adapters
2020
import android.net.Uri
2121
import androidx.appcompat.widget.PopupMenu
2222
import androidx.fragment.app.Fragment
23+
import java.util.GregorianCalendar
2324
import kotlinx.coroutines.flow.Flow
2425
import org.akanework.gramophone.R
2526
import org.akanework.gramophone.ui.MainActivity
@@ -105,7 +106,8 @@ class AlbumAdapter(
105106
setOf(
106107
Sorter.Type.ByTitleDescending, Sorter.Type.ByTitleAscending,
107108
Sorter.Type.ByArtistDescending, Sorter.Type.ByArtistAscending,
108-
Sorter.Type.BySizeDescending, Sorter.Type.BySizeAscending
109+
Sorter.Type.BySizeDescending, Sorter.Type.BySizeAscending,
110+
Sorter.Type.ByReleaseDateAscending, Sorter.Type.ByReleaseDateDescending
109111
)
110112
) {
111113
override fun getArtist(item: Album): String? {
@@ -115,5 +117,9 @@ class AlbumAdapter(
115117
override fun getCover(item: Album): Uri? {
116118
return item.cover ?: super.getCover(item)
117119
}
120+
121+
override fun getReleaseDate(item: Album): Long {
122+
return GregorianCalendar(item.albumYear ?: 0, 0, 0, 0, 0, 0).timeInMillis
123+
}
118124
}
119125
}

app/src/main/kotlin/org/akanework/gramophone/ui/adapters/BaseDecorAdapter.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ open class BaseDecorAdapter<T : BaseAdapter<*>>(
7676
),
7777
Pair(R.id.artist, Sorter.Type.ByArtistAscending),
7878
Pair(R.id.album, Sorter.Type.ByAlbumTitleAscending),
79+
Pair(R.id.album_artist, Sorter.Type.ByAlbumTitleAscending),
7980
Pair(R.id.size, Sorter.Type.BySizeDescending),
8081
Pair(R.id.add_date, Sorter.Type.ByAddDateDescending),
8182
Pair(R.id.release_date, Sorter.Type.ByReleaseDateDescending),
82-
Pair(R.id.mod_date, Sorter.Type.ByModifiedDateDescending)
83+
Pair(R.id.mod_date, Sorter.Type.ByModifiedDateDescending),
84+
Pair(R.id.file_path, Sorter.Type.ByFilePathAscending)
8385
)
8486
val layoutMap = mapOf(
8587
Pair(R.id.list, BaseAdapter.LayoutType.LIST),

app/src/main/kotlin/org/akanework/gramophone/ui/adapters/SongAdapter.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,15 @@ class SongAdapter(
301301
}
302302
} else {
303303
super.onBindViewHolder(holder, position, payloads)
304-
if (currentMediaItem == null || getSongList()[position].mediaId != currentMediaItem)
304+
if (currentMediaItem == null || getSongList().getOrNull(position)?.mediaId != currentMediaItem)
305305
return
306306
}
307307
holder.nowPlaying.setImageDrawable(NowPlayingDrawable()
308308
.also { it.level = if (currentIsPlaying == true) 1 else 0 })
309309
holder.nowPlaying.visibility = View.VISIBLE
310310
}
311311

312+
// TODO support album year sort
312313
class MediaItemHelper(
313314
types: Set<Sorter.Type> = setOf(
314315
Sorter.Type.ByTitleDescending, Sorter.Type.ByTitleAscending,
@@ -359,14 +360,14 @@ class SongAdapter(
359360
&& item.mediaMetadata.releaseDay == null
360361
) {
361362
return GregorianCalendar(
362-
(item.mediaMetadata.recordingYear ?: 0) + 1900,
363+
item.mediaMetadata.recordingYear ?: 0,
363364
(item.mediaMetadata.recordingMonth ?: 1) - 1,
364365
item.mediaMetadata.recordingDay ?: 0, 0, 0, 0
365366
)
366367
.timeInMillis
367368
}
368369
return GregorianCalendar(
369-
(item.mediaMetadata.releaseYear ?: 0) + 1900,
370+
item.mediaMetadata.releaseYear ?: 0,
370371
(item.mediaMetadata.releaseMonth ?: 1) - 1,
371372
item.mediaMetadata.releaseDay ?: 0, 0, 0, 0
372373
)

app/src/main/kotlin/org/akanework/gramophone/ui/adapters/Sorter.kt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.akanework.gramophone.ui.adapters
1919

2020
import android.net.Uri
21+
import java.io.File
2122
import org.akanework.gramophone.logic.comparators.SupportComparator
2223
import org.akanework.gramophone.logic.utils.CalculationUtils
2324

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

4142
open fun getArtist(item: T): String? = throw UnsupportedOperationException()
43+
open fun getFile(item: T): File = throw UnsupportedOperationException()
4244
open fun getAlbumTitle(item: T): String? = throw UnsupportedOperationException()
4345
open fun getAlbumArtist(item: T): String? = throw UnsupportedOperationException()
4446
open fun getSize(item: T): Int = throw UnsupportedOperationException()
@@ -66,6 +68,9 @@ class Sorter<T>(
6668
fun canGetAddDate(): Boolean = typesSupported.contains(Type.ByAddDateAscending)
6769
|| typesSupported.contains(Type.ByAddDateDescending)
6870

71+
fun canGetFile(): Boolean = typesSupported.contains(Type.ByFilePathAscending)
72+
|| typesSupported.contains(Type.ByFilePathDescending)
73+
6974
fun canGetReleaseDate(): Boolean = typesSupported.contains(Type.ByReleaseDateAscending)
7075
|| typesSupported.contains(Type.ByReleaseDateDescending)
7176

@@ -86,6 +91,7 @@ class Sorter<T>(
8691
NaturalOrder, ByAddDateDescending, ByAddDateAscending,
8792
ByReleaseDateDescending, ByReleaseDateAscending,
8893
ByModifiedDateDescending, ByModifiedDateAscending,
94+
ByFilePathDescending, ByFilePathAscending,
8995
ByDiscAndTrack,
9096

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

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

194201
Type.ByReleaseDateDescending -> {
195202
SupportComparator.createInversionComparator(
196-
compareBy { sortingHelper.getReleaseDate(it) }, true
203+
compareBy { sortingHelper.getReleaseDate(it) }, true,
204+
if (sortingHelper.canGetDiskAndTrack()) getComparator(Type.ByDiscAndTrack) else null
197205
)
198206
}
199207

200208
Type.ByReleaseDateAscending -> {
201209
SupportComparator.createInversionComparator(
202-
compareBy { sortingHelper.getReleaseDate(it) }, false
210+
compareBy { sortingHelper.getReleaseDate(it) }, false,
211+
if (sortingHelper.canGetDiskAndTrack()) getComparator(Type.ByDiscAndTrack) else null
203212
)
204213
}
205214

@@ -215,6 +224,18 @@ class Sorter<T>(
215224
)
216225
}
217226

227+
Type.ByFilePathDescending -> {
228+
SupportComparator.createAlphanumericComparator(true, {
229+
sortingHelper.getFile(it).path
230+
}, null)
231+
}
232+
233+
Type.ByFilePathAscending -> {
234+
SupportComparator.createAlphanumericComparator(false, {
235+
sortingHelper.getFile(it).path
236+
}, null)
237+
}
238+
218239
Type.ByDiscAndTrack -> {
219240
compareBy { sortingHelper.getDiscAndTrack(it) }
220241
}
@@ -235,19 +256,23 @@ class Sorter<T>(
235256
fun getFastScrollHintFor(item: T, sortType: Type): String? {
236257
return when (sortType) {
237258
Type.ByTitleDescending, Type.ByTitleAscending, Type.NativeOrder, Type.NativeOrderDescending -> {
238-
(sortingHelper.getTitle(item) ?: "-").firstOrNull()?.toString()
259+
sortingHelper.getTitle(item)?.firstOrNull()?.toString()
239260
}
240261

241262
Type.ByArtistDescending, Type.ByArtistAscending -> {
242-
(sortingHelper.getArtist(item) ?: "-").firstOrNull()?.toString()
263+
sortingHelper.getArtist(item)?.firstOrNull()?.toString()
243264
}
244265

245266
Type.ByAlbumTitleDescending, Type.ByAlbumTitleAscending -> {
246-
(sortingHelper.getAlbumTitle(item) ?: "-").firstOrNull()?.toString()
267+
sortingHelper.getAlbumTitle(item)?.firstOrNull()?.toString()
247268
}
248269

249270
Type.ByAlbumArtistDescending, Type.ByAlbumArtistAscending -> {
250-
(sortingHelper.getAlbumArtist(item) ?: "-").firstOrNull()?.toString()
271+
sortingHelper.getAlbumArtist(item)?.firstOrNull()?.toString()
272+
}
273+
274+
Type.ByFilePathDescending, Type.ByFilePathAscending -> {
275+
null // can probably not do anything better
251276
}
252277

253278
Type.BySizeDescending, Type.BySizeAscending -> {

app/src/main/res/menu/sort_menu.xml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools">
55
<group android:checkableBehavior="single">
6-
<item
7-
android:id="@+id/album_artist"
8-
android:checkable="true"
9-
android:orderInCategory="-1"
10-
android:title="@string/album_artist"
11-
android:visible="false"
12-
app:showAsAction="never" />
136
<item
147
android:id="@+id/natural"
158
android:orderInCategory="0"
@@ -32,29 +25,35 @@
3225
android:title="@string/sort_by_album"
3326
app:showAsAction="never" />
3427
<item
35-
android:id="@+id/size"
36-
android:checked="false"
28+
android:id="@+id/album_artist"
3729
android:orderInCategory="4"
30+
android:title="@string/album_artist"
31+
app:showAsAction="never" />
32+
<item
33+
android:id="@+id/size"
34+
android:orderInCategory="5"
3835
android:title="@string/sort_by_size"
3936
app:showAsAction="never" />
4037
<item
4138
android:id="@+id/add_date"
42-
android:checked="false"
43-
android:orderInCategory="5"
39+
android:orderInCategory="6"
4440
android:title="@string/sort_by_add_date"
4541
app:showAsAction="never" />
4642
<item
4743
android:id="@+id/release_date"
48-
android:checked="false"
49-
android:orderInCategory="6"
44+
android:orderInCategory="7"
5045
android:title="@string/sort_by_release_date"
5146
app:showAsAction="never" />
5247
<item
5348
android:id="@+id/mod_date"
54-
android:checked="false"
55-
android:orderInCategory="7"
49+
android:orderInCategory="8"
5650
android:title="@string/sort_by_modified_date"
5751
app:showAsAction="never" />
52+
<item
53+
android:id="@+id/file_path"
54+
android:orderInCategory="9"
55+
android:title="@string/sort_by_file_path"
56+
app:showAsAction="never" />
5857
</group>
5958

6059
<item

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<string name="sort_by_name">Name</string>
3636
<string name="sort_by_artist">Artist</string>
3737
<string name="sort_by_album">Album</string>
38+
<string name="sort_by_album_artist">Album artist</string>
3839
<string name="dismiss">Dismiss</string>
3940
<string name="unknown_genre">Unknown genre</string>
4041
<string name="unknown_year">Unknown year</string>
@@ -204,4 +205,5 @@
204205
<string name="delete">Delete</string>
205206
<string name="yes">Yes</string>
206207
<string name="no">No</string>
208+
<string name="sort_by_file_path">File path</string>
207209
</resources>

libphonograph

0 commit comments

Comments
 (0)