[Android] Pass actor instead of fragment to adapter

Habib Kazemi git at videolan.org
Mon Aug 6 16:18:26 CEST 2018


vlc-android | branch: master | Habib Kazemi <kazemihabib1996 at gmail.com> | Mon Jul 30 19:59:10 2018 +0430| [c3349a821b91ca2cf4e4657848b402730a90fdc4] | committer: Geoffrey Métais

Pass actor instead of fragment to adapter

Signed-off-by: Geoffrey Métais <geoffrey.metais at gmail.com>

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

 vlc-android/res/layout/mrl_panel.xml                         |  2 --
 vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.kt   | 11 ++++-------
 .../src/org/videolan/vlc/gui/network/MRLPanelFragment.kt     | 12 +++++++++---
 3 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/vlc-android/res/layout/mrl_panel.xml b/vlc-android/res/layout/mrl_panel.xml
index 3a496e57d..5a217fd6f 100644
--- a/vlc-android/res/layout/mrl_panel.xml
+++ b/vlc-android/res/layout/mrl_panel.xml
@@ -3,8 +3,6 @@
     <data>
         <variable name="viewmodel"
             type="org.videolan.vlc.viewmodels.MRLPanelModel"/>
-        <variable name="handler"
-            type="org.videolan.vlc.gui.network.MRLAdapter.MediaPlayerController"/>
     </data>
 
     <android.support.v4.widget.NestedScrollView
diff --git a/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.kt b/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.kt
index 837aba412..cc188225e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.kt
@@ -28,22 +28,19 @@ import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import android.widget.TextView
+import kotlinx.coroutines.experimental.channels.SendChannel
+import org.videolan.libvlc.Media
 
 import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.vlc.R
 import org.videolan.vlc.databinding.MrlItemBinding
 
-internal class MRLAdapter(private val playerController: MediaPlayerController) : RecyclerView.Adapter<MRLAdapter.ViewHolder>() {
+internal class MRLAdapter(private val eventActor: SendChannel<MediaWrapper>) : RecyclerView.Adapter<MRLAdapter.ViewHolder>() {
     private var dataset: Array<MediaWrapper>? = null
 
     val isEmpty: Boolean
         get() = itemCount == 0
 
-    internal interface MediaPlayerController {
-        fun playMedia(mw: MediaWrapper)
-    }
-
-
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MRLAdapter.ViewHolder {
         val inflater = LayoutInflater.from(parent.context)
         val binding: MrlItemBinding = DataBindingUtil.inflate(inflater, R.layout.mrl_item, parent, false)
@@ -78,7 +75,7 @@ internal class MRLAdapter(private val playerController: MediaPlayerController) :
         }
 
         override fun onClick(v: View) {
-            dataset?.get(layoutPosition)?.let { playerController.playMedia(it) }
+            dataset?.get(layoutPosition)?.let { eventActor.offer(it) }
         }
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt b/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
index c96f1c6f1..02f962446 100644
--- a/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/network/MRLPanelFragment.kt
@@ -35,6 +35,8 @@ import android.view.View
 import android.view.ViewGroup
 import android.view.inputmethod.EditorInfo
 import android.widget.TextView
+import kotlinx.coroutines.experimental.android.UI
+import kotlinx.coroutines.experimental.channels.actor
 
 import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.vlc.R
@@ -46,11 +48,15 @@ import org.videolan.vlc.viewmodels.MRLPanelModel
 
 const val TAG = "VLC/MrlPanelFragment"
 
-class MRLPanelFragment : DialogFragment(), View.OnKeyListener, TextView.OnEditorActionListener, View.OnClickListener, MRLAdapter.MediaPlayerController {
+class MRLPanelFragment : DialogFragment(), View.OnKeyListener, TextView.OnEditorActionListener, View.OnClickListener {
     private lateinit var adapter: MRLAdapter
     private lateinit var editText: TextInputLayout
     private lateinit var viewModel: MRLPanelModel
 
+    private val listEventActor = actor<MediaWrapper>(UI) {
+        for (event in channel) playMedia(event)
+    }
+
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setStyle(DialogFragment.STYLE_NO_FRAME, 0)
@@ -64,7 +70,7 @@ class MRLPanelFragment : DialogFragment(), View.OnKeyListener, TextView.OnEditor
         editText.editText?.setOnKeyListener(this)
         editText.editText?.setOnEditorActionListener(this)
 
-        adapter = MRLAdapter(this)
+        adapter = MRLAdapter(listEventActor)
         val recyclerView = binding.mrlList
         recyclerView.addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL))
         recyclerView.layoutManager = LinearLayoutManager(activity)
@@ -97,7 +103,7 @@ class MRLPanelFragment : DialogFragment(), View.OnKeyListener, TextView.OnEditor
         return false
     }
 
-    override fun playMedia(mw: MediaWrapper) {
+    private fun playMedia(mw: MediaWrapper) {
         mw.type = MediaWrapper.TYPE_STREAM
         MediaUtils.openMedia(activity, mw)
         viewModel.updateHistory()



More information about the Android mailing list