package com.lagradost.cloudstream3.utils import android.content.Context import android.content.SharedPreferences import androidx.preference.PreferenceManager import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.json.JsonMapper import com.fasterxml.jackson.module.kotlin.KotlinModule import com.lagradost.cloudstream3.mvvm.logError const val DOWNLOAD_HEADER_CACHE = "download_header_cache" //const val WATCH_HEADER_CACHE = "watch_header_cache" const val DOWNLOAD_EPISODE_CACHE = "download_episode_cache" const val VIDEO_PLAYER_BRIGHTNESS = "video_player_alpha_key" const val USER_SELECTED_HOMEPAGE_API = "home_api_used" const val USER_PROVIDER_API = "user_custom_sites" const val PREFERENCES_NAME = "rebuild_preference" object DataStore { val mapper: JsonMapper = JsonMapper.builder().addModule(KotlinModule()) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).build() private fun getPreferences(context: Context): SharedPreferences { return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE) } fun Context.getSharedPrefs(): SharedPreferences { return getPreferences(this) } fun getFolderName(folder: String, path: String): String { return "${folder}/${path}" } fun Context.setKeyRaw(path: String, value: T, isEditingAppSettings: Boolean = false) { try { val editor: SharedPreferences.Editor = if (isEditingAppSettings) getDefaultSharedPrefs().edit() else getSharedPrefs().edit() when (value) { is Boolean -> editor.putBoolean(path, value) is Int -> editor.putInt(path, value) is String -> editor.putString(path, value) is Float -> editor.putFloat(path, value) is Long -> editor.putLong(path, value) (value as? Set != null) -> editor.putStringSet(path, value as Set) } editor.apply() } catch (e: Exception) { logError(e) } } fun Context.getDefaultSharedPrefs(): SharedPreferences { return PreferenceManager.getDefaultSharedPreferences(this) } fun Context.getKeys(folder: String): List { return this.getSharedPrefs().all.keys.filter { it.startsWith(folder) } } fun Context.removeKey(folder: String, path: String) { removeKey(getFolderName(folder, path)) } fun Context.containsKey(folder: String, path: String): Boolean { return containsKey(getFolderName(folder, path)) } fun Context.containsKey(path: String): Boolean { val prefs = getSharedPrefs() return prefs.contains(path) } fun Context.removeKey(path: String) { try { val prefs = getSharedPrefs() if (prefs.contains(path)) { val editor: SharedPreferences.Editor = prefs.edit() editor.remove(path) editor.apply() } } catch (e: Exception) { logError(e) } } fun Context.removeKeys(folder: String): Int { val keys = getKeys(folder) keys.forEach { value -> removeKey(value) } return keys.size } fun Context.setKey(path: String, value: T) { try { val editor: SharedPreferences.Editor = getSharedPrefs().edit() editor.putString(path, mapper.writeValueAsString(value)) editor.apply() } catch (e: Exception) { logError(e) } } fun Context.setKey(folder: String, path: String, value: T) { setKey(getFolderName(folder, path), value) } inline fun String.toKotlinObject(): T { return mapper.readValue(this, T::class.java) } // GET KEY GIVEN PATH AND DEFAULT VALUE, NULL IF ERROR inline fun Context.getKey(path: String, defVal: T?): T? { try { val json: String = getSharedPrefs().getString(path, null) ?: return defVal return json.toKotlinObject() } catch (e: Exception) { return null } } inline fun Context.getKey(path: String): T? { return getKey(path, null) } inline fun Context.getKey(folder: String, path: String): T? { return getKey(getFolderName(folder, path), null) } inline fun Context.getKey(folder: String, path: String, defVal: T?): T? { return getKey(getFolderName(folder, path), defVal) ?: defVal } }