[Android] Refactor the string separation in the whole app

Nicolas Pomepuy git at videolan.org
Fri Jan 21 13:32:36 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Jan 21 10:16:28 2022 +0100| [59de8bd5acf2e1e0860d3eb2462de4cd2a7ece3a] | committer: Nicolas Pomepuy

Refactor the string separation in the whole app

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

 .../moviepedia/database/models/MediaMetadata.kt     |  5 +++--
 .../moviepedia/models/identify/IdentifyResult.kt    |  3 ++-
 .../src/org/videolan/vlc/MediaParsingService.kt     |  6 ++----
 .../org/videolan/vlc/providers/BrowserProvider.kt   | 14 +++++---------
 .../src/org/videolan/vlc/util/Kextensions.kt        |  6 ++----
 .../src/org/videolan/vlc/util/TextUtils.kt          | 21 ++++++++++++++++++---
 6 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/application/moviepedia/src/main/java/org/videolan/moviepedia/database/models/MediaMetadata.kt b/application/moviepedia/src/main/java/org/videolan/moviepedia/database/models/MediaMetadata.kt
index 83f133542..7fdab2cf6 100644
--- a/application/moviepedia/src/main/java/org/videolan/moviepedia/database/models/MediaMetadata.kt
+++ b/application/moviepedia/src/main/java/org/videolan/moviepedia/database/models/MediaMetadata.kt
@@ -26,6 +26,7 @@ package org.videolan.moviepedia.database.models
 
 import androidx.room.*
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
+import org.videolan.vlc.util.TextUtils
 import java.text.SimpleDateFormat
 import java.util.*
 
@@ -96,7 +97,7 @@ fun MediaMetadataWithImages.movieSubtitle(): String {
     subtitle.add(metadata.genres)
     subtitle.add(metadata.countries)
 
-    return subtitle.filter { it.isNotEmpty() }.joinToString(separator = " · ") { it }
+    return TextUtils.separatedString(subtitle.toTypedArray())
 }
 
 fun MediaMetadataWithImages.tvshowSubtitle(): String {
@@ -108,7 +109,7 @@ fun MediaMetadataWithImages.tvshowSubtitle(): String {
     subtitle.add(show.title)
     subtitle.add("S${metadata.season.toString().padStart(2, '0')}E${metadata.episode.toString().padStart(2, '0')}")
 
-    return subtitle.filter { it.isNotEmpty() }.joinToString(separator = " · ") { it }
+    return TextUtils.separatedString(subtitle.toTypedArray())
 }
 
 fun MediaMetadataWithImages.tvEpisodeSubtitle(): String {
diff --git a/application/moviepedia/src/main/java/org/videolan/moviepedia/models/identify/IdentifyResult.kt b/application/moviepedia/src/main/java/org/videolan/moviepedia/models/identify/IdentifyResult.kt
index 50b57288e..e8ade994d 100644
--- a/application/moviepedia/src/main/java/org/videolan/moviepedia/models/identify/IdentifyResult.kt
+++ b/application/moviepedia/src/main/java/org/videolan/moviepedia/models/identify/IdentifyResult.kt
@@ -30,6 +30,7 @@ import org.videolan.moviepedia.models.resolver.ResolverImage
 import org.videolan.moviepedia.models.resolver.ResolverMedia
 import org.videolan.moviepedia.models.resolver.ResolverMediaType
 import org.videolan.moviepedia.models.resolver.ResolverResult
+import org.videolan.vlc.util.TextUtils
 import java.text.SimpleDateFormat
 import java.util.*
 import kotlin.Comparator
@@ -168,7 +169,7 @@ fun MediaType.toResolverClass(): ResolverMediaType = when (this) {
 }
 
 
-fun MoviepediaMedia.getShow() = "$showTitle · S${season.toString().padStart(2, '0')}E${episode.toString().padStart(2, '0')}"
+fun MoviepediaMedia.getShow() = TextUtils.separatedString(showTitle, "S${season.toString().padStart(2, '0')}E${episode.toString().padStart(2, '0')}")
 
 fun MoviepediaMedia.retrievePosters(languages: List<String>) = images?.posters?.sortedWith(Comparator { p0, p1 ->
     -(languages.indexOf(p0.language) - languages.indexOf(p1.language))
diff --git a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index ffc7f2ae0..d481fa012 100644
--- a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -55,10 +55,8 @@ import org.videolan.tools.*
 import org.videolan.vlc.gui.SendCrashActivity
 import org.videolan.vlc.gui.helpers.NotificationHelper
 import org.videolan.vlc.repository.DirectoryRepository
+import org.videolan.vlc.util.*
 import org.videolan.vlc.util.FileUtils
-import org.videolan.vlc.util.Util
-import org.videolan.vlc.util.cleanupWatchNextList
-import org.videolan.vlc.util.scanAllowed
 
 private const val TAG = "VLC/MediaParsingService"
 private const val NOTIFICATION_DELAY = 1000L
@@ -354,7 +352,7 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
         val discovery = withContext(Dispatchers.Default) {
             val progressText = when {
                 inDiscovery -> getString(R.string.ml_discovering) + " " + Uri.decode(currentDiscovery?.removeFileProtocole())
-                parsing > 0 -> getString(R.string.ml_parse_media) + " " + String.format("%.02f",parsing) + "% · $done/$scheduled"
+                parsing > 0 -> TextUtils.separatedString(getString(R.string.ml_parse_media) + " " + String.format("%.02f",parsing) + "%", "$done/$scheduled")
                 else -> getString(R.string.ml_parse_media)
             }
             if (!isActive) return at withContext ""
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index 16afdbb96..162b99b07 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -313,17 +313,13 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
     fun hasSubfolders(media: MediaWrapper): Boolean = foldersContentMap.get(media)?.map { it as MediaWrapper }?.filter { it.type == MediaWrapper.TYPE_DIR }?.size ?: 0 > 0
     fun hasMedias(media: MediaWrapper): Boolean = foldersContentMap.get(media)?.map { it as MediaWrapper }?.filter { it.type != MediaWrapper.TYPE_DIR }?.size ?: 0 > 0
 
-    private val sb = StringBuilder()
     open fun getDescription(folderCount: Int, mediaFileCount: Int): String {
         val res = context.resources
-        sb.clear()
-        if (folderCount > 0) {
-            sb.append("$folderCount $folderReplacementMarker")
-            if (mediaFileCount > 0) sb.append(" · ")
-        }
-        if (mediaFileCount > 0) sb.append("$mediaFileCount $fileReplacementMarker")
-        else if (folderCount == 0 && mediaFileCount == 0) sb.append(res.getString(R.string.empty_directory))
-        return sb.toString()
+        val texts = ArrayList<String>()
+        if (folderCount > 0)  texts.add("$folderCount $folderReplacementMarker")
+        if (mediaFileCount > 0) texts.add("$mediaFileCount $fileReplacementMarker")
+        if(texts.isEmpty()) texts.add(res.getString(R.string.empty_directory))
+        return TextUtils.separatedString(texts.toTypedArray())
     }
 
     protected open suspend fun findMedia(media: IMedia): MediaLibraryItem? {
diff --git a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index ffe9ec634..c6a8ebf04 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -175,9 +175,7 @@ fun asyncTextItem(view: TextView, item: MediaLibraryItem?) {
         return
     }
     val text = if (item is Playlist){
-        val sb = StringBuilder(view.context.getString(R.string.track_number, item.tracksCount))
-        if (item.duration != 0L) sb.append(" · ${Tools.millisToString(item.duration)}")
-        sb.toString()
+        TextUtils.separatedString(view.context.getString(R.string.track_number, item.tracksCount), if (item.duration != 0L) Tools.millisToString(item.duration) else null)
     } else item.description
     if (text.isNullOrEmpty()) {
         view.visibility = View.GONE
@@ -211,7 +209,7 @@ const val presentReplacementMarker = "§*§"
 const val missingReplacementMarker = "*§*"
 
 fun MediaLibraryItem.getPresenceDescription() = when (this) {
-    is VideoGroup -> "${this.presentCount} §*§ · ${this.mediaCount() - this.presentCount} *§*"
+    is VideoGroup -> TextUtils.separatedString("${this.presentCount} §*§", "${this.mediaCount() - this.presentCount} *§*")
     else -> ""
 }
 
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 208f84254..14b73ae7d 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/TextUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/TextUtils.kt
@@ -26,11 +26,26 @@ package org.videolan.vlc.util
 
 object TextUtils {
 
-    fun separator() = "·"
+    /**
+     * Common string separator used in the whole app
+     */
+    private const val separator = '·'
 
-    @JvmName("separatedStringArr")
+    /**
+     * Create a string separated by the common [separator]
+     *
+     * @param pieces the strings to join
+     * @return a string containing all the [pieces] if they are not blanked, spearated by the [separator]
+     */
+    @JvmName("separatedStringArgs")
     fun separatedString(vararg pieces: String?) = separatedString(arrayOf(*pieces))
 
-    private fun separatedString(pieces: Array<String?>) = pieces.filter { it?.isNotBlank() == true }.joinToString(separator = " ${separator()} ")
+    /**
+     * Create a string separated by the common [separator]
+     *
+     * @param pieces the strings to join in an [Array]
+     * @return a string containing all the [pieces] if they are not blanked, spearated by the [separator]
+     */
+    fun separatedString(pieces: Array<String?>) = pieces.filter { it?.isNotBlank() == true }.joinToString(separator = " $separator ")
 
 }
\ No newline at end of file



More information about the Android mailing list