[Android] Load media from ViewModels

Geoffrey Métais git at videolan.org
Fri Sep 7 09:30:57 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Sep  7 09:30:38 2018 +0200| [0732b4fa1a32e5a1cfd2ce158de978303dc111a8] | committer: Geoffrey Métais

Load media from ViewModels

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

 .../org/videolan/vlc/gui/browser/BaseBrowserFragment.kt |  2 +-
 .../org/videolan/vlc/gui/video/VideoGridFragment.java   |  8 ++++----
 .../org/videolan/vlc/gui/video/VideoListAdapter.java    | 17 -----------------
 .../src/org/videolan/vlc/viewmodels/VideosModel.kt      | 15 +++++++++++++++
 4 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
index 486279c90..1af77cec4 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
@@ -299,7 +299,7 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
     private fun playAll(mw: MediaWrapper?) {
         var positionInPlaylist = 0
         val mediaLocations = LinkedList<MediaWrapper>()
-        for (file in adapter.all)
+        for (file in viewModel.dataset.value)
             if (file is MediaWrapper) {
                 if (file.type == MediaWrapper.TYPE_VIDEO || file.type == MediaWrapper.TYPE_AUDIO) {
                     mediaLocations.add(file)
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index 95963bb63..7d8dacf82 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -203,7 +203,7 @@ public class VideoGridFragment extends MediaBrowserFragment<VideosModel> impleme
     @Override
     public void onFabPlayClick(View view) {
         List<MediaWrapper> playList = new ArrayList<>();
-        MediaUtils.INSTANCE.openList(getActivity(), playList, mAdapter.getListWithPosition(playList, 0));
+        MediaUtils.INSTANCE.openList(getActivity(), playList, viewModel.getListWithPosition(playList, 0));
     }
 
     @MainThread
@@ -338,7 +338,7 @@ public class VideoGridFragment extends MediaBrowserFragment<VideosModel> impleme
             final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(v.getContext());
             if (settings.getBoolean("force_play_all", false)) {
                 final List<MediaWrapper> playList = new ArrayList<>();
-                MediaUtils.INSTANCE.openList(activity, playList, mAdapter.getListWithPosition(playList, position));
+                MediaUtils.INSTANCE.openList(activity, playList, viewModel.getListWithPosition(playList, position));
             } else {
                 playVideo(media, false);
             }
@@ -388,7 +388,7 @@ public class VideoGridFragment extends MediaBrowserFragment<VideosModel> impleme
                 break;
             case Constants.CTX_PLAY_ALL:
                 final List<MediaWrapper> playList = new ArrayList<>();
-                MediaUtils.INSTANCE.openList(getActivity(), playList, mAdapter.getListWithPosition(playList, position));
+                MediaUtils.INSTANCE.openList(getActivity(), playList, viewModel.getListWithPosition(playList, position));
                 break;
             case Constants.CTX_INFORMATION:
                 showInfoDialog(media);
@@ -404,7 +404,7 @@ public class VideoGridFragment extends MediaBrowserFragment<VideosModel> impleme
                 else MediaUtils.INSTANCE.appendMedia(getActivity(), media);
                 break;
             case Constants.CTX_DOWNLOAD_SUBTITLES:
-                MediaUtils.INSTANCE.getSubs(getActivity(), media);
+                MediaUtils.INSTANCE.getSubs(requireActivity(), media);
                 break;
         }
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
index d53fe05d0..d7f5a52ff 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -47,7 +47,6 @@ import org.videolan.vlc.gui.helpers.ImageLoaderKt;
 import org.videolan.vlc.gui.helpers.SelectorViewHolder;
 import org.videolan.vlc.gui.helpers.UiTools;
 import org.videolan.vlc.interfaces.IEventsHandler;
-import org.videolan.vlc.media.MediaGroup;
 import org.videolan.vlc.util.Constants;
 import org.videolan.vlc.util.MediaItemDiffCallback;
 
@@ -207,22 +206,6 @@ public class VideoListAdapter extends DiffUtilAdapter<MediaWrapper, VideoListAda
         return super.getItemViewType(position);
     }
 
-    int getListWithPosition(List<MediaWrapper>  list, int position) {
-        MediaWrapper mw;
-        int offset = 0;
-        for (int i = 0; i < getItemCount(); ++i) {
-            mw = getDataset().get(i);
-            if (mw instanceof MediaGroup) {
-                for (MediaWrapper item : ((MediaGroup) mw).getAll())
-                    list.add(item);
-                if (i < position)
-                    offset += ((MediaGroup)mw).size()-1;
-            } else
-                list.add(mw);
-        }
-        return position+offset;
-    }
-
     public class ViewHolder extends SelectorViewHolder<ViewDataBinding> implements View.OnFocusChangeListener {
         private ImageView thumbView;
 
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/VideosModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/VideosModel.kt
index 30178682d..9db4ce5eb 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/VideosModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/VideosModel.kt
@@ -95,6 +95,21 @@ open class VideosModel(context: Context, private val group: String?, private val
         Medialibrary.lastThumb.removeObserver(thumbObs)
     }
 
+    fun getListWithPosition(list: MutableList<MediaWrapper>, position: Int): Int {
+        if (group != null || minGroupLen <= 0) {
+            list.addAll(dataset.value)
+            return position
+        }
+        var offset = 0
+        for ((i, mw) in dataset.value.withIndex()) {
+            if (mw is MediaGroup) {
+                for (item in mw.all) list.add(item)
+                if (i < position) offset += mw.size() - 1
+            } else list.add(mw)
+        }
+        return position + offset
+    }
+
     class Factory(private val context: Context, val group: String?, private val minGroupLen : Int, private val sort : Int, private val desc : Boolean?): ViewModelProvider.NewInstanceFactory() {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {
             val length = if (minGroupLen == 0) Integer.valueOf(Settings.getInstance(context).getString("video_min_group_length", "6")) else minGroupLen



More information about the Android mailing list