[Android] Fix the MenuItem sort order icon and improve the accessibility for it
Nicolas Pomepuy
git at videolan.org
Fri Jun 17 13:48:37 UTC 2022
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Jun 17 11:50:28 2022 +0200| [a3fc0bd10468f29a4a4c9207997e725f4de29669] | committer: Duncan McNamara
Fix the MenuItem sort order icon and improve the accessibility for it
> https://code.videolan.org/videolan/vlc-android/commit/a3fc0bd10468f29a4a4c9207997e725f4de29669
---
.../resources/src/main/res/values/strings.xml | 2 +
.../src/org/videolan/vlc/gui/helpers/UiTools.kt | 58 +++++++++++-----------
2 files changed, 32 insertions(+), 28 deletions(-)
diff --git a/application/resources/src/main/res/values/strings.xml b/application/resources/src/main/res/values/strings.xml
index ed118fc3f..3d5913363 100644
--- a/application/resources/src/main/res/values/strings.xml
+++ b/application/resources/src/main/res/values/strings.xml
@@ -978,6 +978,8 @@
<string name="open_vlc">Open VLC</string>
<string name="playing">Playing</string>
<string name="disable_track">Disable track</string>
+ <string name="ascending">Ascending</string>
+ <string name="descending">Descending</string>
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
index e6b01a1cf..0cb13e445 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
@@ -43,6 +43,7 @@ import android.view.animation.*
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.TextView
+import androidx.annotation.IdRes
import androidx.annotation.RequiresApi
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
@@ -578,20 +579,13 @@ object UiTools {
fun updateSortTitles(menu: Menu, provider: MedialibraryProvider<*>) {
val sort = provider.sort
val desc = provider.desc
- var item: MenuItem? = menu.findItem(R.id.ml_menu_sortby_name)
- item?.title = "${provider.context.getString(R.string.sortby_name)} ${if (sort == Medialibrary.SORT_ALPHA || sort == Medialibrary.SORT_DEFAULT) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_filename)
- item?.title = "${provider.context.getString(R.string.sortby_filename)} ${if (sort == Medialibrary.SORT_FILENAME) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_artist_name)
- item?.title = "${provider.context.getString(R.string.sortby_artist_name)} ${if (sort == Medialibrary.SORT_ARTIST) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_album_name)
- item?.title = "${provider.context.getString(R.string.sortby_album_name)} ${if (sort == Medialibrary.SORT_ALBUM) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_length)
- item?.title = "${provider.context.getString(R.string.sortby_length)} ${if (sort == Medialibrary.SORT_DURATION) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_date)
- item?.title = "${provider.context.getString(R.string.sortby_date)} ${if (sort == Medialibrary.SORT_RELEASEDATE) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_last_modified)
- item?.title = "${provider.context.getString(R.string.sortby_last_modified_date)} ${if (sort == Medialibrary.SORT_LASTMODIFICATIONDATE) if (!desc) "▼" else "▲" else ""}"
+ menu.appendSortOrder(provider.context, R.id.ml_menu_sortby_name, R.string.sortby_name, (sort == Medialibrary.SORT_ALPHA || sort == Medialibrary.SORT_DEFAULT), desc)
+ menu.appendSortOrder(provider.context, R.id.ml_menu_sortby_filename, R.string.sortby_filename, sort == Medialibrary.SORT_FILENAME, desc)
+ menu.appendSortOrder(provider.context, R.id.ml_menu_sortby_artist_name, R.string.sortby_artist_name, sort == Medialibrary.SORT_ARTIST, desc)
+ menu.appendSortOrder(provider.context, R.id.ml_menu_sortby_album_name, R.string.sortby_album_name, sort == Medialibrary.SORT_ALBUM, desc)
+ menu.appendSortOrder(provider.context, R.id.ml_menu_sortby_length, R.string.sortby_length, sort == Medialibrary.SORT_DURATION, desc)
+ menu.appendSortOrder(provider.context, R.id.ml_menu_sortby_date, R.string.sortby_date, sort == Medialibrary.SORT_RELEASEDATE, desc)
+ menu.appendSortOrder(provider.context,R.id.ml_menu_sortby_last_modified, R.string.sortby_last_modified_date, sort == Medialibrary.SORT_LASTMODIFICATIONDATE, desc)
// item = menu.findItem(R.id.ml_menu_sortby_number); TODO sort by track number
// if (item != null) item.setTitle(sort == Medialibrary.SORT_ && !desc ? R.string.sortby_number_desc : R.string.sortby_number);
@@ -602,25 +596,33 @@ object UiTools {
val model = sortable.viewModel
val sort = model.sort
val desc = model.desc
- var item: MenuItem? = menu.findItem(R.id.ml_menu_sortby_name)
- item?.title = "${sortable.requireContext().getString(R.string.sortby_name)} ${if (sort == Medialibrary.SORT_ALPHA) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_filename)
- item?.title = "${sortable.requireContext().getString(R.string.sortby_filename)} ${if (sort == Medialibrary.SORT_FILENAME || sort == Medialibrary.SORT_DEFAULT) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_artist_name)
- item?.title = "${sortable.requireContext().getString(R.string.sortby_artist_name)} ${if (sort == Medialibrary.SORT_ARTIST) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_album_name)
- item?.title = "${sortable.requireContext().getString(R.string.sortby_album_name)} ${if (sort == Medialibrary.SORT_ALBUM) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_length)
- item?.title = "${sortable.requireContext().getString(R.string.sortby_length)} ${if (sort == Medialibrary.SORT_DURATION) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_date)
- item?.title = "${sortable.requireContext().getString(R.string.sortby_date)} ${if (sort == Medialibrary.SORT_RELEASEDATE) if (!desc) "▼" else "▲" else ""}"
- item = menu.findItem(R.id.ml_menu_sortby_last_modified)
- item?.title = "${sortable.requireContext().getString(R.string.sortby_last_modified_date)} ${if (sort == Medialibrary.SORT_RELEASEDATE) if (!desc) "▼" else "▲" else ""}"
+ menu.appendSortOrder(sortable.requireActivity(), R.id.ml_menu_sortby_name, R.string.sortby_name, sort == Medialibrary.SORT_ALPHA, desc)
+ menu.appendSortOrder(sortable.requireActivity(), R.id.ml_menu_sortby_filename, R.string.sortby_filename, (sort == Medialibrary.SORT_FILENAME || sort == Medialibrary.SORT_DEFAULT), desc)
+ menu.appendSortOrder(sortable.requireActivity(), R.id.ml_menu_sortby_artist_name, R.string.sortby_artist_name, sort == Medialibrary.SORT_ARTIST, desc)
+ menu.appendSortOrder(sortable.requireActivity(), R.id.ml_menu_sortby_album_name, R.string.sortby_album_name, sort == Medialibrary.SORT_ALBUM, desc)
+ menu.appendSortOrder(sortable.requireActivity(), R.id.ml_menu_sortby_length, R.string.sortby_length, sort == Medialibrary.SORT_DURATION, desc)
+ menu.appendSortOrder(sortable.requireActivity(), R.id.ml_menu_sortby_date, R.string.sortby_date, sort == Medialibrary.SORT_RELEASEDATE, desc)
+ menu.appendSortOrder(sortable.requireActivity(),R.id.ml_menu_sortby_last_modified, R.string.sortby_last_modified_date, sort == Medialibrary.SORT_RELEASEDATE, desc)
// item = menu.findItem(R.id.ml_menu_sortby_number); TODO sort by track number
// if (item != null) item.setTitle(sort == Medialibrary.SORT_ && !desc ? R.string.sortby_number_desc : R.string.sortby_number);
}
+ /**
+ * Sets a [MenuItem] title and contentDescription depending on the sort it shows
+ *
+ * @param context the context to be used for strings
+ * @param id the [MenuItem] id
+ * @param titleRes the string resource to use as a title
+ * @param isCurrent is this the current sort
+ * @param desc is the sort descending
+ */
+ private fun Menu.appendSortOrder(context: Context, @IdRes id:Int, @StringRes titleRes:Int, isCurrent:Boolean, desc:Boolean) = findItem(id)?.let { menuItem ->
+ val title = context.getString(titleRes)
+ menuItem.title = if (!isCurrent) title else "$title ${if (desc) "▼" else "▲"}"
+ menuItem.contentDescription = if (!isCurrent) title else "$title. ${context.getString(if (desc) R.string.descending else R.string.ascending)}"
+ }
+
fun confirmExit(activity: Activity) {
AlertDialog.Builder(activity)
.setMessage(R.string.exit_app_msg)
More information about the Android
mailing list