refactor PreferencesManager -> ChaosflixPreferenceManager

This commit is contained in:
Felix 2020-04-11 23:47:10 +02:00
parent d0ed9eaeae
commit 8a0795081a
10 changed files with 97 additions and 64 deletions

View file

@ -0,0 +1,60 @@
package de.nicidienase.chaosflix.common
import android.content.SharedPreferences
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class ChaosflixPreferenceManager(private val sharedPref: SharedPreferences) {
var channelId: Long by LongPreferenceDelegate(CHANNEL_ID, 0)
val externalPlayer: Boolean by BooleanPreferencesDelegate(keyAlwaysUseExternalPlayer, false)
val analyticsDisabled: Boolean by BooleanPreferencesDelegate(keyAnalyticsDisabled, false)
val downloadFolder: String? by StringPreferenceDelegate(keyDownloadFolder, "")
var autoselectRecording: Boolean by BooleanPreferencesDelegate(keyAutoselectRecording, false)
var autoselectStream: Boolean by BooleanPreferencesDelegate(keyAutoselectStream, false)
var recommendationsGenerated: Boolean by BooleanPreferencesDelegate(RECOMMENDATIONS_GENERATED, false)
private inner class BooleanPreferencesDelegate(key: String, default: Boolean) :
PreferencesDelegate<Boolean>(key,default, SharedPreferences::getBoolean, SharedPreferences.Editor::putBoolean)
private inner class StringPreferenceDelegate(key: String, default: String):
PreferencesDelegate<String>(key,default, SharedPreferences::getString, SharedPreferences.Editor::putString)
private inner class LongPreferenceDelegate(key: String, default: Long):
PreferencesDelegate<Long>(key,default, SharedPreferences::getLong, SharedPreferences.Editor::putLong)
abstract inner class PreferencesDelegate<T>(
private val key: String,
private val default: T,
private val getter: SharedPreferences.(String, T) -> T?,
private val setter: SharedPreferences.Editor.(String, T) -> SharedPreferences.Editor
) : ReadWriteProperty<ChaosflixPreferenceManager, T> {
override fun getValue(thisRef: ChaosflixPreferenceManager, property: KProperty<*>): T {
return sharedPref.getter(key, default) ?: default
}
override fun setValue(thisRef: ChaosflixPreferenceManager, property: KProperty<*>, value: T) {
sharedPref.edit().setter(key, value).apply()
}
}
fun getMetered() = sharedPref.getBoolean(keyMetered, false)
companion object {
private const val keyMetered = "allow_metered_networks"
private const val keyAutoselectStream = "auto_select_stream"
private const val keyAutoselectRecording = "auto_select_recording"
private const val keyAlwaysUseExternalPlayer = "auto_external_player"
private const val keyAnalyticsDisabled = "disable_analytics"
private const val keyDownloadFolder = "download_folder"
private const val CHANNEL_ID = "channelId"
private const val RECOMMENDATIONS_GENERATED = "recommendation_generated"
}
}

View file

@ -22,18 +22,18 @@ import de.nicidienase.chaosflix.common.userdata.entities.download.OfflineEvent
import de.nicidienase.chaosflix.common.userdata.entities.download.OfflineEventDao
import de.nicidienase.chaosflix.common.util.LiveEvent
import de.nicidienase.chaosflix.common.viewmodel.DetailsViewModel
import java.io.File
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
class OfflineItemManager(
context: Context,
private val offlineEventDao: OfflineEventDao,
private val preferencesManager: PreferencesManager
private val preferencesManager: ChaosflixPreferenceManager
) {
val downloadStatus: MutableMap<Long, DownloadStatus> = HashMap()
@ -171,7 +171,7 @@ class OfflineItemManager(
val context: Context,
val id: Long,
private val offlineEventDao: OfflineEventDao,
private val preferencesManager: PreferencesManager
private val preferencesManager: ChaosflixPreferenceManager
) : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
val downloadId = p1?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0)
@ -198,7 +198,11 @@ class OfflineItemManager(
private fun getMovieDir(): String {
val dir = preferencesManager.downloadFolder
return dir ?: Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).path
return if(dir.isNullOrBlank()){
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).path
} else {
dir
}
}
private fun getDownloadDir(): String {

View file

@ -1,40 +0,0 @@
package de.nicidienase.chaosflix.common
import android.content.SharedPreferences
class PreferencesManager(private val sharedPref: SharedPreferences) {
var channelId: Long
get() = sharedPref.getLong(CHANNEL_ID, 0)
set(value) { sharedPref.edit().putLong(CHANNEL_ID, value).apply() }
val externalPlayer: Boolean
get() = sharedPref.getBoolean(keyAlwaysUseExternalPlayer, false)
val analyticsDisabled: Boolean
get() = sharedPref.getBoolean(keyAnalyticsDisabled, false)
val downloadFolder: String?
get() = sharedPref.getString(keyDownloadFolder, null)
var autoselectRecording: Boolean
get() = sharedPref.getBoolean(keyAutoselectRecording, false)
set(value) = sharedPref.edit().putBoolean(keyAutoselectRecording, value).apply()
var autoselectStream: Boolean
get() = sharedPref.getBoolean(keyAutoselectStream, false)
set(value) = sharedPref.edit().putBoolean(keyAutoselectStream, value).apply()
fun getMetered() = sharedPref.getBoolean(keyMetered, false)
companion object {
private const val keyMetered = "allow_metered_networks"
private const val keyAutoselectStream = "auto_select_stream"
private const val keyAutoselectRecording = "auto_select_recording"
private const val keyAlwaysUseExternalPlayer = "auto_external_player"
private const val keyAnalyticsDisabled = "disable_analytics"
private const val keyDownloadFolder = "download_folder"
private const val CHANNEL_ID = "channelId"
}
}

View file

@ -10,7 +10,7 @@ import androidx.paging.PagedList
import de.nicidienase.chaosflix.R
import de.nicidienase.chaosflix.common.ChaosflixDatabase
import de.nicidienase.chaosflix.common.OfflineItemManager
import de.nicidienase.chaosflix.common.PreferencesManager
import de.nicidienase.chaosflix.common.ChaosflixPreferenceManager
import de.nicidienase.chaosflix.common.ResourcesFacade
import de.nicidienase.chaosflix.common.mediadata.MediaRepository
import de.nicidienase.chaosflix.common.mediadata.SearchResultDataSourceFactory
@ -26,12 +26,12 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class BrowseViewModel(
val offlineItemManager: OfflineItemManager,
private val mediaRepository: MediaRepository,
private val database: ChaosflixDatabase,
private val streamingRepository: StreamingRepository,
private val preferencesManager: PreferencesManager,
private val resources: ResourcesFacade
val offlineItemManager: OfflineItemManager,
private val mediaRepository: MediaRepository,
private val database: ChaosflixDatabase,
private val streamingRepository: StreamingRepository,
private val preferencesManager: ChaosflixPreferenceManager,
private val resources: ResourcesFacade
) : ViewModel() {
val state: SingleLiveEvent<LiveEvent<State, Event, String>> = SingleLiveEvent()

View file

@ -10,7 +10,7 @@ import androidx.lifecycle.viewModelScope
import de.nicidienase.chaosflix.common.ChaosflixDatabase
import de.nicidienase.chaosflix.common.ChaosflixUtil
import de.nicidienase.chaosflix.common.OfflineItemManager
import de.nicidienase.chaosflix.common.PreferencesManager
import de.nicidienase.chaosflix.common.ChaosflixPreferenceManager
import de.nicidienase.chaosflix.common.mediadata.MediaRepository
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Event
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Recording
@ -23,10 +23,10 @@ import java.io.File
import java.util.ArrayList
class DetailsViewModel(
private val database: ChaosflixDatabase,
private val offlineItemManager: OfflineItemManager,
private val preferencesManager: PreferencesManager,
private val mediaRepository: MediaRepository
private val database: ChaosflixDatabase,
private val offlineItemManager: OfflineItemManager,
private val preferencesManager: ChaosflixPreferenceManager,
private val mediaRepository: MediaRepository
) : ViewModel() {
private var eventId: Long = 0

View file

@ -8,7 +8,7 @@ import androidx.lifecycle.ViewModelProvider
import de.nicidienase.chaosflix.R
import de.nicidienase.chaosflix.common.ChaosflixDatabase
import de.nicidienase.chaosflix.common.OfflineItemManager
import de.nicidienase.chaosflix.common.PreferencesManager
import de.nicidienase.chaosflix.common.ChaosflixPreferenceManager
import de.nicidienase.chaosflix.common.ResourcesFacade
import de.nicidienase.chaosflix.common.SingletonHolder
import de.nicidienase.chaosflix.common.mediadata.MediaRepository
@ -22,7 +22,7 @@ class ViewModelFactory private constructor(context: Context) : ViewModelProvider
private val database by lazy { ChaosflixDatabase.getInstance(context) }
private val streamingRepository by lazy { StreamingRepository(apiFactory.streamingApi) }
private val preferencesManager =
PreferencesManager(PreferenceManager.getDefaultSharedPreferences(context.applicationContext))
ChaosflixPreferenceManager(PreferenceManager.getDefaultSharedPreferences(context.applicationContext))
private val offlineItemManager =
OfflineItemManager(
context.applicationContext,

View file

@ -2,12 +2,12 @@ package de.nicidienase.chaosflix
import android.preference.PreferenceManager
import de.nicidienase.chaosflix.common.AnalyticsWrapperImpl
import de.nicidienase.chaosflix.common.PreferencesManager
import de.nicidienase.chaosflix.common.ChaosflixPreferenceManager
object LibsInit : ChaosflixInitializer {
override fun init(chaosflixApplication: ChaosflixApplication) {
val preferencesManager =
PreferencesManager(PreferenceManager.getDefaultSharedPreferences(chaosflixApplication.applicationContext))
ChaosflixPreferenceManager(PreferenceManager.getDefaultSharedPreferences(chaosflixApplication.applicationContext))
if (!preferencesManager.analyticsDisabled) {
AnalyticsWrapperImpl.init(chaosflixApplication)

View file

@ -9,7 +9,7 @@ import androidx.tvprovider.media.tv.Channel
import androidx.tvprovider.media.tv.ChannelLogoUtils
import androidx.tvprovider.media.tv.PreviewProgram
import androidx.tvprovider.media.tv.TvContractCompat
import de.nicidienase.chaosflix.common.PreferencesManager
import de.nicidienase.chaosflix.common.ChaosflixPreferenceManager
import de.nicidienase.chaosflix.common.viewmodel.BrowseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@ -21,7 +21,7 @@ object ChannelManager {
}
suspend fun setupChannels(context: Context, viewmodel: BrowseViewModel, prefs: PreferencesManager) {
suspend fun setupChannels(context: Context, viewmodel: BrowseViewModel, prefs: ChaosflixPreferenceManager) {
withContext(Dispatchers.IO) {
if (prefs.channelId == 0L) {
val builder = Channel.Builder()

View file

@ -7,8 +7,10 @@ import android.app.PendingIntent
import android.app.TaskStackBuilder
import android.content.Context
import android.content.Intent
import android.preference.PreferenceManager
import android.util.Log
import androidx.core.app.NotificationCompat
import de.nicidienase.chaosflix.common.ChaosflixPreferenceManager
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Event
import de.nicidienase.chaosflix.common.viewmodel.ViewModelFactory
import de.nicidienase.chaosflix.leanback.conferences.ConferencesActivity
@ -27,6 +29,12 @@ class ChaosRecommendationsService : IntentService("ChaosRecommendationService")
Log.d(TAG, "Updating Recommendation")
val mediaRepository = ViewModelFactory.getInstance(this).mediaRepository
val preferenceManager = ChaosflixPreferenceManager(PreferenceManager.getDefaultSharedPreferences(applicationContext))
if(preferenceManager.recommendationsGenerated){
Log.d(TAG, "already generated, returning")
return
}
ioScope.launch {
val recommendations = mediaRepository.getRecommendations()
@ -59,6 +67,7 @@ class ChaosRecommendationsService : IntentService("ChaosRecommendationService")
} catch (e: IOException) {
Log.e(TAG, "Unable to update recommendation", e)
}
preferenceManager.recommendationsGenerated = true
}
}

View file

@ -5,7 +5,7 @@ import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import de.nicidienase.chaosflix.common.PreferencesManager
import de.nicidienase.chaosflix.common.ChaosflixPreferenceManager
import de.nicidienase.chaosflix.common.viewmodel.BrowseViewModel
import de.nicidienase.chaosflix.common.viewmodel.ViewModelFactory
import de.nicidienase.chaosflix.leanback.ChannelManager
@ -23,7 +23,7 @@ class ConferencesActivity : androidx.fragment.app.FragmentActivity() {
override fun onStart() {
super.onStart()
val prefs = PreferencesManager(PreferenceManager.getDefaultSharedPreferences(applicationContext))
val prefs = ChaosflixPreferenceManager(PreferenceManager.getDefaultSharedPreferences(applicationContext))
val viewmodel = ViewModelProvider(
this,ViewModelFactory.getInstance(this)).get(BrowseViewModel::class.java)