[Android] Fix network provider

Geoffrey Métais git at videolan.org
Wed Apr 4 18:35:07 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Mar  9 17:49:25 2018 +0100| [bdad5a87c12db8b38edb83093374ee8abfa9a61e] | committer: Geoffrey Métais

Fix network provider

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

 .../videolan/vlc/gui/browser/BaseBrowserFragment.java  | 15 ++++++++-------
 .../vlc/gui/browser/NetworkBrowserFragment.java        |  8 ++++++--
 .../videolan/vlc/viewmodels/browser/BrowserProvider.kt |  3 ++-
 .../videolan/vlc/viewmodels/browser/NetworkProvider.kt | 18 ++++++++++--------
 4 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
index 79cc90354..a786f9d1b 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
@@ -146,6 +146,11 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment<BrowserPr
     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
         if (mAdapter == null) mAdapter = new BaseBrowserAdapter(this);
+        mLayoutManager = new LinearLayoutManager(getActivity());
+        mRecyclerView.setLayoutManager(mLayoutManager);
+        mRecyclerView.setAdapter(mAdapter);
+        registerForContextMenu(mRecyclerView);
+        mSwipeRefreshLayout.setOnRefreshListener(this);
         mProvider.getDataset().observe(this, new Observer<List<MediaLibraryItem>>() {
             @Override
             public void onChanged(@Nullable List<MediaLibraryItem> mediaLibraryItems) {
@@ -158,11 +163,6 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment<BrowserPr
                 mAdapter.notifyItemChanged(pair.getFirst(), pair.getSecond());
             }
         });
-        mLayoutManager = new LinearLayoutManager(getActivity());
-        mRecyclerView.setLayoutManager(mLayoutManager);
-        mRecyclerView.setAdapter(mAdapter);
-        registerForContextMenu(mRecyclerView);
-        mSwipeRefreshLayout.setOnRefreshListener(this);
         initFavorites();
     }
 
@@ -257,7 +257,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment<BrowserPr
      */
     protected void updateEmptyView() {
         if (mSwipeRefreshLayout == null) return;
-        if (mAdapter.isEmpty()) {
+        if (Util.isListEmpty(getProvider().getDataset().getValue())) {
             if (mSwipeRefreshLayout.isRefreshing()) {
                 mEmptyView.setText(R.string.loading);
                 mEmptyView.setVisibility(View.VISIBLE);
@@ -588,9 +588,10 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment<BrowserPr
     }
 
     public void onUpdateFinished(RecyclerView.Adapter adapter) {
+        if (mSwipeRefreshLayout != null) mSwipeRefreshLayout.setRefreshing(false);
         mHandler.sendEmptyMessage(BrowserFragmentHandler.MSG_HIDE_LOADING);
         updateEmptyView();
-        if (!mAdapter.isEmpty()) {
+        if (!Util.isListEmpty(getProvider().getDataset().getValue())) {
             if (mSavedPosition > 0) {
                 mLayoutManager.scrollToPositionWithOffset(mSavedPosition, 0);
                 mSavedPosition = 0;
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java
index 50943ac74..662f57362 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java
@@ -23,6 +23,7 @@
 
 package org.videolan.vlc.gui.browser;
 
+import android.app.Activity;
 import android.arch.lifecycle.Observer;
 import android.arch.lifecycle.ViewModelProviders;
 import android.content.BroadcastReceiver;
@@ -51,6 +52,7 @@ import org.videolan.vlc.gui.SimpleAdapter;
 import org.videolan.vlc.gui.dialogs.NetworkServerDialog;
 import org.videolan.vlc.gui.dialogs.VlcLoginDialog;
 import org.videolan.vlc.media.MediaDatabase;
+import org.videolan.vlc.util.Util;
 import org.videolan.vlc.viewmodels.browser.NetworkProvider;
 
 import java.util.List;
@@ -75,6 +77,7 @@ public class NetworkBrowserFragment extends BaseBrowserFragment implements Simpl
         if (mRoot) ((NetworkProvider) mProvider).getFavorites().observe(this, new Observer<List<MediaLibraryItem>>() {
             @Override
             public void onChanged(@Nullable List<MediaLibraryItem> mediaLibraryItems) {
+                mBinding.favoritesTitle.setVisibility(Util.isListEmpty(mediaLibraryItems) ? View.GONE : View.VISIBLE);
                 favoritesAdapter.submitList(mediaLibraryItems);
             }
         });
@@ -216,7 +219,8 @@ public class NetworkBrowserFragment extends BaseBrowserFragment implements Simpl
             db.deleteNetworkFav(mCurrentMedia.getUri());
         else
             db.addNetworkFavItem(mCurrentMedia.getUri(), mCurrentMedia.getTitle(), mCurrentMedia.getArtworkURL());
-        getActivity().supportInvalidateOptionsMenu();
+        final Activity activity = getActivity();
+        if (activity!= null) activity.invalidateOptionsMenu();
     }
 
     /**
@@ -225,7 +229,7 @@ public class NetworkBrowserFragment extends BaseBrowserFragment implements Simpl
     protected void updateEmptyView() {
         if (mEmptyView == null) return;
         if (ExternalMonitor.connected.getValue()) {
-            if (mAdapter.isEmpty()) {
+            if (Util.isListEmpty(getProvider().getDataset().getValue())) {
                 if (mSwipeRefreshLayout == null || mSwipeRefreshLayout.isRefreshing()) {
                     mEmptyView.setText(R.string.loading);
                     mEmptyView.setVisibility(View.VISIBLE);
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserProvider.kt
index 1e91689ca..3301e2f9b 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserProvider.kt
@@ -54,7 +54,8 @@ abstract class BrowserProvider(val url: String?) : BaseModel<MediaLibraryItem>()
         } else browse(url)
     }
 
-    @Volatile private var refreshing = false
+    @Volatile
+    private var refreshing = false
     private val refreshList = mutableListOf<MediaLibraryItem>()
     override fun refresh(): Boolean {
         refreshing = true
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/browser/NetworkProvider.kt b/vlc-android/src/org/videolan/vlc/viewmodels/browser/NetworkProvider.kt
index 170c723b8..a4a1a0a08 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/browser/NetworkProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/browser/NetworkProvider.kt
@@ -3,10 +3,11 @@ package org.videolan.vlc.viewmodels.browser
 import android.arch.lifecycle.MutableLiveData
 import android.arch.lifecycle.ViewModel
 import android.arch.lifecycle.ViewModelProvider
+import kotlinx.coroutines.experimental.CommonPool
 import kotlinx.coroutines.experimental.CoroutineStart
 import kotlinx.coroutines.experimental.android.UI
-import kotlinx.coroutines.experimental.async
 import kotlinx.coroutines.experimental.launch
+import kotlinx.coroutines.experimental.withContext
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.vlc.ExternalMonitor
@@ -15,7 +16,6 @@ import java.util.*
 
 class NetworkProvider(url: String?): BrowserProvider(url) {
     val favorites by lazy {
-        launch(UI) { updateFavorites() }
         MutableLiveData<MutableList<MediaLibraryItem>>()
     }
     override fun browseRoot() {
@@ -24,16 +24,11 @@ class NetworkProvider(url: String?): BrowserProvider(url) {
             initBrowser()
             mediabrowser?.discoverNetworkShares()
         }
-
-    }
-
-    override fun refresh(): Boolean {
-        return url != null && return super.refresh()
     }
 
     private suspend fun updateFavorites() {
         if (ExternalMonitor.connected?.value != true) favorites.value = mutableListOf()
-        val favs: MutableList<MediaLibraryItem> = async { MediaDatabase.getInstance().allNetworkFav }.await().toMutableList()
+        val favs: MutableList<MediaLibraryItem> = withContext(CommonPool) { MediaDatabase.getInstance().allNetworkFav }.toMutableList()
         if (!allowLAN()) {
             val schemes = Arrays.asList("ftp", "sftp", "ftps", "http", "https")
             val toRemove = favs.filterNotTo(mutableListOf()) { schemes.contains((it as MediaWrapper).uri.scheme) }
@@ -42,6 +37,13 @@ class NetworkProvider(url: String?): BrowserProvider(url) {
         favorites.value = favs
     }
 
+    override fun fetch() {}
+
+    override fun refresh(): Boolean {
+        super.fetch()
+        return true
+    }
+
     private fun allowLAN(): Boolean {
         return ExternalMonitor.isLan() || ExternalMonitor.isVPN()
     }



More information about the Android mailing list