[Android] PlaylistAdapter: code cleaning

Geoffrey Métais git at videolan.org
Mon Dec 2 09:52:49 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Dec  2 09:52:29 2019 +0100| [5b3fd74cd2ccfbc8ebcf61018da5a35457af64e8] | committer: Geoffrey Métais

PlaylistAdapter: code cleaning

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

 .../org/videolan/vlc/gui/audio/PlaylistAdapter.kt  | 43 ++++++++--------------
 1 file changed, 16 insertions(+), 27 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.kt b/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.kt
index c4de1e6b0..ea56100b4 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.kt
@@ -57,13 +57,13 @@ import org.videolan.vlc.util.WeakHandler
 import org.videolan.vlc.viewmodels.PlaylistModel
 import java.util.*
 
+ at ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
- at UseExperimental(ObsoleteCoroutinesApi::class)
 class PlaylistAdapter(private val player: IPlayer) : DiffUtilAdapter<AbstractMediaWrapper, PlaylistAdapter.ViewHolder>(), SwipeDragHelperAdapter {
 
     private var defaultCoverVideo: BitmapDrawable
     private var defaultCoverAudio: BitmapDrawable
-    private var mModel: PlaylistModel? = null
+    private var model: PlaylistModel? = null
     private var currentPlayingVisu: MiniVisualizer? = null
 
     init {
@@ -109,7 +109,7 @@ class PlaylistAdapter(private val player: IPlayer) : DiffUtilAdapter<AbstractMed
         holder.binding.subTitle = MediaUtils.getMediaSubtitle(media)
         holder.binding.scaleType = ImageView.ScaleType.CENTER_CROP
         if (currentIndex == position) {
-            if (mModel?.playing != false) holder.binding.playing.start() else holder.binding.playing.stop()
+            if (model?.playing != false) holder.binding.playing.start() else holder.binding.playing.stop()
             holder.binding.playing.visibility = View.VISIBLE
             holder.binding.coverImage.visibility = View.INVISIBLE
             holder.binding.audioItemTitle.setTypeface(null, Typeface.BOLD)
@@ -137,27 +137,18 @@ class PlaylistAdapter(private val player: IPlayer) : DiffUtilAdapter<AbstractMed
         currentPlayingVisu = null
     }
 
-    override fun getItemCount(): Int {
-        return dataset.size
-    }
+    override fun getItemCount() = dataset.size
 
     @MainThread
-    override fun getItem(position: Int): AbstractMediaWrapper {
-        return dataset[position]
-    }
-
-    fun getLocation(position: Int): String {
-        return getItem(position).location
-    }
+    override fun getItem(position: Int) = dataset[position]
 
     override fun onUpdateFinished() {
-        if (mModel != null) currentIndex = mModel!!.selection
+        model?.run { currentIndex = selection }
     }
 
     @MainThread
     fun remove(position: Int) {
-        if (mModel == null) return
-        mModel!!.remove(position)
+        model?.run { remove(position) }
     }
 
     override fun onItemMove(fromPosition: Int, toPosition: Int) {
@@ -174,7 +165,7 @@ class PlaylistAdapter(private val player: IPlayer) : DiffUtilAdapter<AbstractMed
         val message = String.format(VLCApplication.appResources.getString(R.string.remove_playlist_item), media.title)
         if (player is Fragment) {
             val v = (player as Fragment).view
-            val cancelAction = Runnable { mModel!!.insertMedia(position, media) }
+            val cancelAction = Runnable { model?.run { insertMedia(position, media) } }
             UiTools.snackerWithCancel(v!!, message, null, cancelAction)
         } else if (player is Context) {
             Toast.makeText(VLCApplication.appContext, message, Toast.LENGTH_SHORT).show()
@@ -183,7 +174,7 @@ class PlaylistAdapter(private val player: IPlayer) : DiffUtilAdapter<AbstractMed
     }
 
     fun setModel(model: PlaylistModel) {
-        mModel = model
+        this.model = model
     }
 
     inner class ViewHolder @TargetApi(Build.VERSION_CODES.M)
@@ -193,13 +184,13 @@ class PlaylistAdapter(private val player: IPlayer) : DiffUtilAdapter<AbstractMed
         init {
             binding.holder = this
             if (AndroidUtil.isMarshMallowOrLater)
-                itemView.setOnContextClickListener { v ->
-                    onMoreClick(v)
+                itemView.setOnContextClickListener { view ->
+                    onMoreClick(view)
                     true
                 }
         }
 
-        fun onClick(v: View, media: AbstractMediaWrapper) {
+        fun onClick(@Suppress("UNUSED_PARAMETER") v: View, media: AbstractMediaWrapper) {
             val position = layoutPosition //getMediaPosition(media);
             player.playItem(position, media)
         }
@@ -224,10 +215,10 @@ class PlaylistAdapter(private val player: IPlayer) : DiffUtilAdapter<AbstractMed
                     sendEmptyMessageDelayed(ACTION_MOVED, 1000)
                 }
                 ACTION_MOVED -> {
-                    val model = owner?.mModel
-                    if (from != -1 && to != -1 && model == null) return
+                    val model = owner?.model ?: return
+                    if (from != -1 && to != -1) return
                     if (to > from) ++to
-                    model!!.move(from, to)
+                    model.move(from, to)
                     to = -1
                     from = to
                 }
@@ -241,9 +232,7 @@ class PlaylistAdapter(private val player: IPlayer) : DiffUtilAdapter<AbstractMed
         }
     }
 
-    override fun createCB(): DiffCallback<AbstractMediaWrapper> {
-        return MediaItemDiffCallback()
-    }
+    override fun createCB(): DiffCallback<AbstractMediaWrapper> = MediaItemDiffCallback()
 
     fun setCurrentlyPlaying(playing: Boolean) {
         if (playing) currentPlayingVisu?.start() else currentPlayingVisu?.stop()



More information about the Android mailing list