[Android] Headers: Fix nullity check

Geoffrey Métais git at videolan.org
Mon Sep 23 18:10:31 CEST 2019


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Sep 12 12:18:39 2019 +0200| [9a1d89322df74e6a0dc444434d4c4648c73e9ab4] | committer: Geoffrey Métais

Headers: Fix nullity check

(cherry picked from commit 3284ae7a950092025580467c5a2869931badb445)

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

 .../providers/medialibrary/MedialibraryProvider.kt |  2 +-
 .../src/org/videolan/vlc/util/ModelsHelper.kt      | 22 +++++++++++-----------
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt b/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
index c58739be0..8f57b404a 100644
--- a/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
@@ -94,7 +94,7 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
 
     fun isEmpty() = pagedList.value.isNullOrEmpty()
 
-    fun completeHeaders(list: Array<T>, startposition: Int) {
+    fun completeHeaders(list: Array<T?>, startposition: Int) {
         for ((position, item) in list.withIndex()) {
             val previous = when {
                 position > 0 -> list[position - 1]
diff --git a/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt b/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
index 583bb4192..45e09f455 100644
--- a/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
+++ b/vlc-android/src/org/videolan/vlc/util/ModelsHelper.kt
@@ -110,41 +110,41 @@ object ModelsHelper {
         return if (item.title.isEmpty() || !Character.isLetter(item.title[0]) || isSpecialItem(item)) "#" else item.title.substring(0, 1).toUpperCase()
     }
 
-    fun getHeader(context: Context?, sort: Int, item: MediaLibraryItem?, aboveItem: MediaLibraryItem?) = if (context !== null || item == null) when (sort) {
+    fun getHeader(context: Context?, sort: Int, item: MediaLibraryItem?, aboveItem: MediaLibraryItem?) = if (context !== null && item != null) when (sort) {
         SORT_DEFAULT,
         SORT_FILENAME,
         SORT_ALPHA -> {
-            val letter = if (item!!.title.isEmpty() || !Character.isLetter(item.title[0]) || ModelsHelper.isSpecialItem(item)) "#" else item.title.substring(0, 1).toUpperCase()
+            val letter = if (item.title.isEmpty() || !Character.isLetter(item.title[0]) || ModelsHelper.isSpecialItem(item)) "#" else item.title.substring(0, 1).toUpperCase()
             if (aboveItem == null) letter
             else {
                 val previous = if (aboveItem.title.isEmpty() || !Character.isLetter(aboveItem.title[0]) || ModelsHelper.isSpecialItem(aboveItem)) "#" else aboveItem.title.substring(0, 1).toUpperCase()
-                if (letter != previous) letter else null
+                letter.takeIf { it != previous }
             }
         }
         SORT_DURATION -> {
-            val length = getLength(item!!)
+            val length = getLength(item)
             val lengthCategory = lengthToCategory(length)
             if (aboveItem == null) lengthCategory
             else {
                 val previous = lengthToCategory(getLength(aboveItem))
-                if (lengthCategory != previous) lengthCategory else null
+                lengthCategory.takeIf { it != previous }
             }
         }
         SORT_RELEASEDATE -> {
-            val year = getYear(item!!)
+            val year = getYear(item)
             if (aboveItem == null) year
             else {
                 val previous = getYear(aboveItem)
-                if (year != previous) year else null
+                year.takeIf { it != previous }
             }
         }
         SORT_LASTMODIFICATIONDATE -> {
             val timestamp = (item as AbstractMediaWrapper).lastModified
             val category = getTimeCategory(timestamp)
-            if (aboveItem == null) getTimeCategoryString(context!!, category)
+            if (aboveItem == null) getTimeCategoryString(context, category)
             else {
                 val prevCat = getTimeCategory((aboveItem as AbstractMediaWrapper).lastModified)
-                if (prevCat != category) getTimeCategoryString(context!!, category) else null
+                if (prevCat != category) getTimeCategoryString(context, category) else null
             }
         }
         SORT_ARTIST -> {
@@ -152,7 +152,7 @@ object ModelsHelper {
             if (aboveItem == null) artist
             else {
                 val previous = (aboveItem as AbstractMediaWrapper).artist ?: ""
-                if (artist != previous) artist else null
+                artist.takeIf { it != previous }
             }
         }
         SORT_ALBUM -> {
@@ -160,7 +160,7 @@ object ModelsHelper {
             if (aboveItem == null) album
             else {
                 val previous = (aboveItem as AbstractMediaWrapper).album ?: ""
-                if (album != previous) album else null
+                album.takeIf { it != previous }
             }
         }
         else -> null



More information about the Android mailing list