[Android] Update chapter title formatting

Robert Stone git at videolan.org
Thu Mar 16 13:19:33 UTC 2023


vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Sat Mar 11 11:37:27 2023 -0800| [a1e4fb4ad04b5e3f620c632d576b6e49560c07c5] | committer: Nicolas Pomepuy

Update chapter title formatting

> https://code.videolan.org/videolan/vlc-android/commit/a1e4fb4ad04b5e3f620c632d576b6e49560c07c5
---

 .../vlc-android/src/org/videolan/vlc/PlaybackService.kt   | 13 +++++++------
 .../src/org/videolan/vlc/gui/audio/AudioPlayer.kt         |  2 +-
 .../org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt   |  2 +-
 .../src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt   |  8 ++++++--
 .../vlc-android/src/org/videolan/vlc/util/TextUtils.kt    | 15 ++++++++++++---
 5 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index c0a34203fa..c2a440de36 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -406,12 +406,13 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
             return next?.artworkMrl
         }
 
-    suspend fun getCurrentChapter(formatted: Boolean = false): String? {
-        val currentChapter = withContext(Dispatchers.IO) {
-            val chapters = getChapters(-1)
-            if (chapters?.size ?: 0 > 0 && chapterIdx >= 0) chapters?.get(chapterIdx)?.name else null
+    fun getCurrentChapter(): String? {
+        return getChapters(-1)?.let { chapters ->
+            val curChapter = chapterIdx
+            if (curChapter >= 0 && chapters.isNotEmpty()) {
+                TextUtils.formatChapterTitle(this, curChapter + 1, chapters[curChapter].name)
+            } else null
         }
-        return if (currentChapter == null) null else if (formatted) TextUtils.formatChapterTitle(this, currentChapter) else currentChapter
     }
 
     suspend fun trackInfo(): String? {
@@ -1011,7 +1012,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
         val ctx = this at PlaybackService
         val length = length
         lastLength = length
-        val chapterTitle = if (lastChaptersCount > 0) getCurrentChapter(true) else null
+        val chapterTitle = if (lastChaptersCount > 0) getCurrentChapter() else null
         val displayMsg = subtitleMessage.poll()
         val bob = withContext(Dispatchers.Default) {
             val carMode = isCarMode()
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
index 9745c310c6..2be5d4999a 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
@@ -353,7 +353,7 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
             binding.trackInfoContainer?.contentDescription = getString(R.string.talkback_audio_player,TalkbackUtil.getAudioTrack(requireActivity(), it))
         }
 
-        val chapter = playlistModel.service?.getCurrentChapter(true)
+        val chapter = playlistModel.service?.getCurrentChapter()
         binding.songTitle?.text = if (!chapter.isNullOrEmpty()) chapter else  playlistModel.title
         binding.songSubtitle?.text = if (!chapter.isNullOrEmpty()) TextUtils.separatedString(playlistModel.title, playlistModel.artist) else TextUtils.separatedString(playlistModel.artist, playlistModel.album)
         binding.songTitle?.isSelected = true
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt
index ab5f315791..afddda093b 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt
@@ -78,7 +78,7 @@ class SelectChapterDialog : VLCBottomSheetDialogFragment(), IOnChapterSelectedLi
         val chapterData = ArrayList<Chapter>()
 
         for (i in chapters.indices) {
-            val name: String = TextUtils.formatChapterTitle(requireActivity(), chapters[i].name)
+            val name: String = TextUtils.formatChapterTitle(requireActivity(), i + 1, chapters[i].name)
             chapterData.add(Chapter(name, Tools.millisToString(chapters[i].timeOffset)))
         }
 
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt b/application/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt
index d3fe54f887..927c9d1866 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt
@@ -102,8 +102,12 @@ abstract class AudioMediaSwitcher(context: Context, attrs: AttributeSet) : Fling
             addMediaView(inflater, service.titlePrev, service.artistPrev, service.albumPrev, coverPrev, prevTrackInfo)
             hasPrevious = true
         }
-        val chapter = service.getCurrentChapter(true)
-        if (service.hasMedia()) addMediaView(inflater, if (!chapter.isNullOrEmpty()) chapter else  service.title, if (!chapter.isNullOrEmpty()) service.title else service.artist, if (!chapter.isNullOrEmpty()) service.artist else service.album, coverCurrent, trackInfo)
+        val chapter = service.getCurrentChapter()
+        val (titleCurrent, artistCurrent, albumCurrent) = when {
+            !chapter.isNullOrEmpty() -> arrayOf(chapter, service.title, service.artist)
+            else -> arrayOf(service.title, service.artist, service.album);
+        }
+        if (service.hasMedia()) addMediaView(inflater, titleCurrent, artistCurrent, albumCurrent, coverCurrent, trackInfo)
         if (service.hasNext()) addMediaView(inflater, service.titleNext, service.artistNext, service.albumNext, coverNext, nextTrackInfo)
 
         if (service.hasPrevious() && service.hasMedia()) {
diff --git a/application/vlc-android/src/org/videolan/vlc/util/TextUtils.kt b/application/vlc-android/src/org/videolan/vlc/util/TextUtils.kt
index 122c961e56..837ba0961b 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/TextUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/TextUtils.kt
@@ -69,12 +69,21 @@ object TextUtils {
     fun separatedString(separator: Char, pieces: Array<String?>) = pieces.filter { it?.isNotBlank() == true }.joinToString(separator = " $separator ")
 
     /**
-     * Formats the chapter title by prepending "Chapter:" if the current title is made of only non alpha chars
+     * Format the chapter title.
+     * If title is null return "Chapter: <num>"
+     * If title contains letters only prepend "Chapter: <title>"
+     * If title contains any non alpha characters return as-is
      *
      * @param context the context to use to retrieve the string
+     * @param chapterNum the current chapter number
      * @param title the title to format
      * @return a formatted string
      */
-    fun formatChapterTitle(context: Context, title: String?) = if (title?.firstOrNull { it.isLetter() } == null) context.getString(R.string.current_chapter, title) else title
-
+    fun formatChapterTitle(context: Context, chapterNum: Int, title: String?): String {
+        return when {
+            title.isNullOrBlank() -> context.getString(R.string.current_chapter, chapterNum.toString())
+            title.all { it.isLetter() } -> context.getString(R.string.current_chapter, title)
+            else -> title
+        }
+    }
 }
\ No newline at end of file



More information about the Android mailing list