[Android] Set a global Settings singleton

Geoffrey Métais git at videolan.org
Fri Aug 10 15:09:36 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Aug 10 14:41:04 2018 +0200| [90e3cdfbc0b01d985438dfcb13a27cc8f5813f96] | committer: Geoffrey Métais

Set a global Settings singleton

Keep settings in cache to avoid I/O operations

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

 vlc-android/src/org/videolan/vlc/util/Kextensions.kt | 6 ++++--
 vlc-android/src/org/videolan/vlc/util/TvChannels.kt  | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index bb943cef1..1fa14e5f4 100644
--- a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -3,6 +3,7 @@ package org.videolan.vlc.util
 import android.arch.lifecycle.ViewModel
 import android.arch.lifecycle.ViewModelProviders
 import android.content.Context
+import android.content.SharedPreferences
 import android.support.v4.app.Fragment
 import android.support.v4.app.FragmentActivity
 import android.support.v7.preference.PreferenceManager
@@ -10,6 +11,7 @@ import kotlinx.coroutines.experimental.delay
 import kotlinx.coroutines.experimental.withContext
 import org.videolan.libvlc.Media
 import org.videolan.medialibrary.Medialibrary
+import org.videolan.tools.SingletonHolder
 import org.videolan.vlc.startMedialibrary
 import java.io.File
 import java.net.URI
@@ -17,6 +19,8 @@ import java.net.URISyntaxException
 import java.util.*
 import kotlin.coroutines.experimental.suspendCoroutine
 
+object Settings : SingletonHolder<SharedPreferences, Context>({ PreferenceManager.getDefaultSharedPreferences(it) })
+
 fun String.validateLocation(): Boolean {
     var location = this
     /* Check if the MRL contains a scheme */
@@ -56,8 +60,6 @@ fun Media?.canExpand() = this != null && (type == Media.Type.Directory || type =
 
 fun Context.getAppSystemService(name: String) = applicationContext.getSystemService(name)!!
 
-fun Context.getPreferences() = PreferenceManager.getDefaultSharedPreferences(this)!!
-
 fun Long.random() = (Random().nextFloat() * this).toLong()
 
 suspend inline fun <reified T> Context.getFromMl(crossinline block: Medialibrary.() -> T) = withContext(VLCIO) {
diff --git a/vlc-android/src/org/videolan/vlc/util/TvChannels.kt b/vlc-android/src/org/videolan/vlc/util/TvChannels.kt
index 7b235b099..6cd0d6c60 100644
--- a/vlc-android/src/org/videolan/vlc/util/TvChannels.kt
+++ b/vlc-android/src/org/videolan/vlc/util/TvChannels.kt
@@ -47,7 +47,7 @@ private const val MAX_RECOMMENDATIONS = 3
 @RequiresApi(Build.VERSION_CODES.O)
 fun setChannel(context: Context) = launch(start = CoroutineStart.UNDISPATCHED) {
     val channelId = withContext(VLCIO) {
-        val prefs = context.getPreferences()
+        val prefs = Settings.getInstance(context)
         val name = context.getString(R.string.tv_my_new_videos)
         createOrUpdateChannel(prefs, context, name, R.drawable.icon, BuildConfig.APPLICATION_ID)
     }



More information about the Android mailing list