Skip to content

Commit

Permalink
sort by date added implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vtkhatri committed Feb 5, 2021
1 parent 508279b commit fe8077b
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ object GoConstants {
const val TRACK_SORTING = 3
const val TRACK_SORTING_INVERTED = 4
const val SHUFFLE_SORTING = 5
const val DATE_ADDED_SORTING = 6

// error tags
const val TAG_NO_PERMISSION = "NO_PERMISSION"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class MusicViewModel(application: Application) : AndroidViewModel(application) {
MediaStore.Audio.AudioColumns.ALBUM, // 6
MediaStore.Audio.AudioColumns.ALBUM_ID, // 7
pathColumn, // 8
MediaStore.Audio.AudioColumns._ID // 9
MediaStore.Audio.AudioColumns._ID, // 9
MediaStore.MediaColumns.DATE_MODIFIED // 10
)

val selection = "${MediaStore.Audio.AudioColumns.IS_MUSIC} = 1"
Expand Down Expand Up @@ -130,6 +131,8 @@ class MusicViewModel(application: Application) : AndroidViewModel(application) {
cursor.getColumnIndexOrThrow(pathColumn)
val idIndex =
cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns._ID)
val dateAddedIndex =
cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_MODIFIED)

while (cursor.moveToNext()) {

Expand Down Expand Up @@ -159,6 +162,9 @@ class MusicViewModel(application: Application) : AndroidViewModel(application) {
)
}
}

val audioDateAdded = cursor.getString(dateAddedIndex)

// Add the current music to the list
mDeviceMusicList.add(
Music(
Expand All @@ -173,7 +179,8 @@ class MusicViewModel(application: Application) : AndroidViewModel(application) {
audioFolderName,
audioId,
GoConstants.ARTIST_VIEW,
0
0,
audioDateAdded
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ fun Music.toSavedMusic(playerPosition: Int, savedLaunchedBy: String) =
relativePath,
id,
savedLaunchedBy,
playerPosition
playerPosition,
dateAdded
)

fun List<Music>.savedSongIsAvailable(first: Music?) : Music? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class AllMusicFragment : Fragment(R.layout.fragment_all_music), SearchView.OnQue

mAllMusicFragmentBinding.searchToolbar.run {

inflateMenu(R.menu.menu_search)
inflateMenu(R.menu.menu_music_search)

overflowIcon = ContextCompat.getDrawable(requireActivity(), R.drawable.ic_sort)

Expand All @@ -161,7 +161,7 @@ class AllMusicFragment : Fragment(R.layout.fragment_all_music), SearchView.OnQue

menu.run {

mSortMenuItem = ListsHelper.getSelectedSorting(mSorting, this).apply {
mSortMenuItem = ListsHelper.getSelectedSorting(mSorting, this, true).apply {
setTitleColor(ThemeHelper.resolveThemeAccent(requireActivity()))
}

Expand Down Expand Up @@ -264,7 +264,10 @@ class AllMusicFragment : Fragment(R.layout.fragment_all_music), SearchView.OnQue

mAllMusicFragmentBinding.searchToolbar.setOnMenuItemClickListener {

if (it.itemId == R.id.default_sorting || it.itemId == R.id.ascending_sorting || it.itemId == R.id.descending_sorting) {
if (it.itemId == R.id.default_sorting
|| it.itemId == R.id.ascending_sorting
|| it.itemId == R.id.descending_sorting
|| it.itemId == R.id.date_added_sorting) {

mSorting = it.order

Expand All @@ -284,7 +287,7 @@ class AllMusicFragment : Fragment(R.layout.fragment_all_music), SearchView.OnQue
)
)

mSortMenuItem = ListsHelper.getSelectedSorting(mSorting, menu).apply {
mSortMenuItem = ListsHelper.getSelectedSorting(mSorting, menu, true).apply {
setTitleColor(ThemeHelper.resolveThemeAccent(requireActivity()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class MusicContainersListFragment : Fragment(R.layout.fragment_music_container_l

menu.run {

mSortMenuItem = ListsHelper.getSelectedSorting(mSorting, this).apply {
mSortMenuItem = ListsHelper.getSelectedSorting(mSorting, this, false).apply {
setTitleColor(ThemeHelper.resolveThemeAccent(requireActivity()))
}

Expand Down Expand Up @@ -351,7 +351,7 @@ class MusicContainersListFragment : Fragment(R.layout.fragment_music_container_l
)
)

mSortMenuItem = ListsHelper.getSelectedSorting(mSorting, menu).apply {
mSortMenuItem = ListsHelper.getSelectedSorting(mSorting, menu, false).apply {
setTitleColor(ThemeHelper.resolveThemeAccent(requireActivity()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,23 @@ object ListsHelper {
return toTrans
}

fun getSelectedSorting(sorting: Int, menu: Menu): MenuItem = when (sorting) {
GoConstants.DEFAULT_SORTING -> menu.findItem(R.id.default_sorting)
GoConstants.ASCENDING_SORTING -> menu.findItem(R.id.ascending_sorting)
else -> menu.findItem(R.id.descending_sorting)
fun getSelectedSorting(sorting: Int, menu: Menu, isAllMusic: Boolean): MenuItem {
if (isAllMusic) {
return when (sorting) {
GoConstants.DEFAULT_SORTING -> menu.findItem(R.id.default_sorting)
GoConstants.ASCENDING_SORTING -> menu.findItem(R.id.ascending_sorting)
GoConstants.DESCENDING_SORTING -> menu.findItem(R.id.descending_sorting)
GoConstants.DATE_ADDED_SORTING -> menu.findItem(R.id.date_added_sorting)
else -> menu.findItem(R.id.default_sorting)
}
} else {
return when (sorting) {
GoConstants.DEFAULT_SORTING -> menu.findItem(R.id.default_sorting)
GoConstants.ASCENDING_SORTING -> menu.findItem(R.id.ascending_sorting)
GoConstants.DESCENDING_SORTING -> menu.findItem(R.id.descending_sorting)
else -> menu.findItem(R.id.default_sorting)
}
}
}

@JvmStatic
Expand Down Expand Up @@ -137,6 +150,11 @@ object ListsHelper {
list?.sortBy { it.track }
list?.asReversed()
}

GoConstants.DATE_ADDED_SORTING -> {
list?.sortBy { it.dateAdded }
list?.asReversed()
}
else -> list
}
}
Expand All @@ -146,6 +164,7 @@ object ListsHelper {
GoConstants.TRACK_SORTING -> GoConstants.TRACK_SORTING_INVERTED
GoConstants.TRACK_SORTING_INVERTED -> GoConstants.ASCENDING_SORTING
GoConstants.ASCENDING_SORTING -> GoConstants.DESCENDING_SORTING
GoConstants.DESCENDING_SORTING -> GoConstants.DATE_ADDED_SORTING
else -> GoConstants.TRACK_SORTING
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ data class Music(
val id: Long?,
val launchedBy: String,
val startFrom: Int,
val dateAdded: String?,
)
40 changes: 40 additions & 0 deletions project/app/src/main/res/menu/menu_music_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<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/action_search"
android:icon="@drawable/ic_search"
android:title=""
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|always"
tools:ignore="AlwaysShowAction" />

<group android:id="@+id/sorting">

<item
android:id="@+id/default_sorting"
android:orderInCategory="0"
android:title="@string/sorting_pref_default"
app:showAsAction="never" />

<item
android:id="@+id/ascending_sorting"
android:orderInCategory="1"
android:title="@string/sorting_pref_ascending"
app:showAsAction="never" />

<item
android:id="@+id/descending_sorting"
android:orderInCategory="2"
android:title="@string/sorting_pref_descending"
app:showAsAction="never" />

<item
android:id="@+id/date_added_sorting"
android:orderInCategory="6"
android:title="@string/sorting_pref_date_added"
app:showAsAction="never" />
</group>
</menu>
1 change: 1 addition & 0 deletions project/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="sorting_pref_default">Default</string>
<string name="sorting_pref_track">By track no. &#8595;</string>
<string name="sorting_pref_track_inv">By track no. &#8593;</string>
<string name="sorting_pref_date_added">By Date Added</string>
<string name="shuffle_am">Shuffle all songs</string>
<string name="shuffle_sa">Shuffle selected album</string>
<string name="queue_add">Add to queue</string>
Expand Down

0 comments on commit fe8077b

Please sign in to comment.