[Android] Use viewModel instead of calling Repo directly

Habib Kazemi git at videolan.org
Thu Aug 30 10:10:07 CEST 2018


vlc-android | branch: master | Habib Kazemi <kazemihabib1996 at gmail.com> | Tue Aug 28 17:58:48 2018 +0430| [6ba53002165ec305d6010dcaa3c3f5c25a57f912] | committer: Geoffrey Métais

Use viewModel instead of calling Repo directly

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

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

 .../org/videolan/vlc/gui/browser/StorageBrowserFragment.kt |  7 +++----
 .../org/videolan/vlc/viewmodels/browser/BrowserModel.kt    | 14 ++++++++++----
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.kt
index fd087faed..94333558f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.kt
@@ -55,7 +55,6 @@ import org.videolan.vlc.gui.AudioPlayerContainerActivity
 import org.videolan.vlc.gui.dialogs.*
 import org.videolan.vlc.gui.helpers.ThreeStatesCheckbox
 import org.videolan.vlc.gui.helpers.UiTools
-import org.videolan.vlc.repository.DirectoryRepository
 import org.videolan.vlc.util.*
 import org.videolan.vlc.viewmodels.browser.BrowserModel
 import org.videolan.vlc.viewmodels.browser.*
@@ -147,7 +146,7 @@ class StorageBrowserFragment : FileBrowserFragment(), EntryPointsEventsCb {
         if (isRootDirectory) {
             val storage = adapter.getItem(position) as Storage
             launch(UI.immediate) {
-                val isCustom = DirectoryRepository.getInstance(requireContext()).customDirectoryExists(storage.uri.path)
+                val isCustom = viewModel.customDirectoryExists(storage.uri.path)
                 if (isCustom) showContext(requireActivity(), this at StorageBrowserFragment, position, item.title, CTX_CUSTOM_REMOVE)
             }
         }
@@ -155,7 +154,7 @@ class StorageBrowserFragment : FileBrowserFragment(), EntryPointsEventsCb {
 
     override fun onCtxAction(position: Int, option: Int) {
         val storage = adapter.getItem(position) as Storage
-        DirectoryRepository.getInstance(requireContext()).deleteCustomDirectory(storage.uri.path)
+        viewModel.deleteCustomDirectory(storage.uri.path)
         viewModel.remove(storage)
         (activity as AudioPlayerContainerActivity).updateLib()
     }
@@ -224,7 +223,7 @@ class StorageBrowserFragment : FileBrowserFragment(), EntryPointsEventsCb {
             }
 
             launch(UI.immediate + CoroutineExceptionHandler{ _, _ ->}) {
-                DirectoryRepository.getInstance(requireContext()).addCustomDirectory(f.canonicalPath).join()
+                viewModel.addCustomDirectory(f.canonicalPath).join()
                 (activity as AudioPlayerContainerActivity).updateLib()
             }
         })
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
index 711d201a0..12239765f 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/browser/BrowserModel.kt
@@ -24,14 +24,12 @@ import android.arch.lifecycle.ViewModel
 import android.arch.lifecycle.ViewModelProvider
 import android.content.Context
 import android.support.annotation.MainThread
-import kotlinx.coroutines.experimental.CommonPool
-import kotlinx.coroutines.experimental.CoroutineStart
+import kotlinx.coroutines.experimental.*
 import kotlinx.coroutines.experimental.android.UI
-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.providers.*
+import org.videolan.vlc.repository.DirectoryRepository
 import org.videolan.vlc.viewmodels.BaseModel
 
 const val TYPE_FILE = 0
@@ -75,6 +73,14 @@ open class BrowserModel(context: Context, val url: String?, type: Int, showHidde
         provider.release()
     }
 
+    fun addCustomDirectory(path: String) = DirectoryRepository.getInstance(context).addCustomDirectory(path)
+
+    fun deleteCustomDirectory(path: String) = DirectoryRepository.getInstance(context).deleteCustomDirectory(path)
+
+    suspend fun customDirectoryExists(path: String) = DirectoryRepository.getInstance(context).customDirectoryExists(path)
+
+    suspend fun getMediaDirectories() = DirectoryRepository.getInstance(context).getMediaDirectories()
+
     class Factory(val context: Context, val url: String?, private val type: Int,  private val showHiddenFiles: Boolean): ViewModelProvider.NewInstanceFactory() {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {
             @Suppress("UNCHECKED_CAST")



More information about the Android mailing list