Commit 90e3cdfb authored by Geoffrey Métais's avatar Geoffrey Métais

Set a global Settings singleton

Keep settings in cache to avoid I/O operations
parent d65729c3
...@@ -3,6 +3,7 @@ package org.videolan.vlc.util ...@@ -3,6 +3,7 @@ package org.videolan.vlc.util
import android.arch.lifecycle.ViewModel import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProviders import android.arch.lifecycle.ViewModelProviders
import android.content.Context import android.content.Context
import android.content.SharedPreferences
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity import android.support.v4.app.FragmentActivity
import android.support.v7.preference.PreferenceManager import android.support.v7.preference.PreferenceManager
...@@ -10,6 +11,7 @@ import kotlinx.coroutines.experimental.delay ...@@ -10,6 +11,7 @@ import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.withContext import kotlinx.coroutines.experimental.withContext
import org.videolan.libvlc.Media import org.videolan.libvlc.Media
import org.videolan.medialibrary.Medialibrary import org.videolan.medialibrary.Medialibrary
import org.videolan.tools.SingletonHolder
import org.videolan.vlc.startMedialibrary import org.videolan.vlc.startMedialibrary
import java.io.File import java.io.File
import java.net.URI import java.net.URI
...@@ -17,6 +19,8 @@ import java.net.URISyntaxException ...@@ -17,6 +19,8 @@ import java.net.URISyntaxException
import java.util.* import java.util.*
import kotlin.coroutines.experimental.suspendCoroutine import kotlin.coroutines.experimental.suspendCoroutine
object Settings : SingletonHolder<SharedPreferences, Context>({ PreferenceManager.getDefaultSharedPreferences(it) })
fun String.validateLocation(): Boolean { fun String.validateLocation(): Boolean {
var location = this var location = this
/* Check if the MRL contains a scheme */ /* Check if the MRL contains a scheme */
...@@ -56,8 +60,6 @@ fun Media?.canExpand() = this != null && (type == Media.Type.Directory || type = ...@@ -56,8 +60,6 @@ fun Media?.canExpand() = this != null && (type == Media.Type.Directory || type =
fun Context.getAppSystemService(name: String) = applicationContext.getSystemService(name)!! fun Context.getAppSystemService(name: String) = applicationContext.getSystemService(name)!!
fun Context.getPreferences() = PreferenceManager.getDefaultSharedPreferences(this)!!
fun Long.random() = (Random().nextFloat() * this).toLong() fun Long.random() = (Random().nextFloat() * this).toLong()
suspend inline fun <reified T> Context.getFromMl(crossinline block: Medialibrary.() -> T) = withContext(VLCIO) { suspend inline fun <reified T> Context.getFromMl(crossinline block: Medialibrary.() -> T) = withContext(VLCIO) {
......
...@@ -47,7 +47,7 @@ private const val MAX_RECOMMENDATIONS = 3 ...@@ -47,7 +47,7 @@ private const val MAX_RECOMMENDATIONS = 3
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
fun setChannel(context: Context) = launch(start = CoroutineStart.UNDISPATCHED) { fun setChannel(context: Context) = launch(start = CoroutineStart.UNDISPATCHED) {
val channelId = withContext(VLCIO) { val channelId = withContext(VLCIO) {
val prefs = context.getPreferences() val prefs = Settings.getInstance(context)
val name = context.getString(R.string.tv_my_new_videos) val name = context.getString(R.string.tv_my_new_videos)
createOrUpdateChannel(prefs, context, name, R.drawable.icon, BuildConfig.APPLICATION_ID) createOrUpdateChannel(prefs, context, name, R.drawable.icon, BuildConfig.APPLICATION_ID)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment