Files
playlet-piped/playlet-lib/src/source/services/PreferencesService.bs
T
Brahim Hadricheandgithub-action linter 8f8c7d38a8 Preferences / Web App (#79)
* Add basic router

* Add a preferences file

* small changes

* Basic settings navigation menu

* Lint fix

* Add bunch of controls

* Lint fix

* StringControl

* Lint fix

* Add description to keyboard

* Settings saving and loading

* Lint fix

* Settings migration stuff

* GET web api

* Lint fix

* PUT and DELETE (I really fucking hate BrightScript)

* plug in sponsorblock settings

* autoplay, quality, and video proxying

* Remove unused stuff

* Lint fix

* Invidious instance stuff

* cache public instances on start

* Start loading home screen after fetching instances

* Move web app out of settings to its own screen

* picture in picture when in settings

* Web app (#80)

* Update instance stuff

* Update dependencies

* Add basic layout

* Info and settings page, missing styling

* Navbar fixes

* Settings page formatting

* Theme stuff

* Feedback link

* Fix username badge

* Fix svelte warnings

* A wip search page

* Add a couple of api functions

* Wip home screen

* user country code

* Expose token and proxy auth calls

* Use auth requests server side

* Some cleanup

* Add svelte vs code extension

* remove old web app

* More layout stuff

* Tweak for string control

* Filter out old search suggestions

* Search history stuff

* img size for lazy loading

* Basic modal to play video

* Lint fix

* Dependencies bump

* Add video duration and LIVE indicator to cell

* Delete legacy stuff

* Add dev menu

* Lint fix

* Video cell layout

* Cell stuff

* prefs visibility

* Modal layout

* Drag and drop functionality

* Clear search history

* Move web calls to main app

* changelog

* changelog

* Mostly comments

* Lint fix

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
2023-06-24 15:33:09 -04:00

123 lines
3.9 KiB
Plaintext

import "pkg:/source/utils/RegistryUtils.bs"
class PreferencesService
private version = 1
private prefs_model
private user_prefs
function new()
m.LoadConfigFile()
m.LoadUserPreferences()
end function
function LoadConfigFile()
m.prefs_model = ParseJson(ReadAsciiFile("libpkg:/config/preferences.json"))
end function
function LoadUserPreferences()
' Step 1: Load user preferences from registry
m.user_prefs = RegistryUtils.Read(RegistryUtils.USER_PREFERENCES)
if m.user_prefs = invalid
m.user_prefs = "{}"
end if
m.user_prefs = ParseJson(m.user_prefs)
' Step 2: Migrate existing preferences to new format (if needed)
isDirty = m.MigrateExistingPreferences()
' Step 3: Apply defaults to user preferences (if needed)
isDirty = m.ApplyDefaultsToUserPreferences() or isDirty
if isDirty
m.SaveUserPreferences()
end if
end function
function SaveUserPreferences()
m.user_prefs.__version = m.version
RegistryUtils.Write(RegistryUtils.USER_PREFERENCES, FormatJson(m.user_prefs))
end function
function MigrateExistingPreferences() as boolean
isDirty = false
' v0 -> v1
instances = RegistryUtils.Read(RegistryUtils.INVIDIOUS_INSTANCES)
if instances <> invalid
currentInstance = m.user_prefs["invidious.instance"]
if currentInstance = invalid or currentInstance = ""
instances = ParseJson(instances)
if instances.Count() > 0
instance = instances[0]
if instance <> invalid and instance <> ""
m.user_prefs["invidious.instance"] = instance
isDirty = true
end if
end if
end if
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_INSTANCES)
end if
' v1 -> v2 goes here (if needed)
' if m.user_prefs.__version = 1
' 'do stuff
' end if
' TODO: handle case where saved preferences version is higher than current version (e.g. user downgraded)
return isDirty
end function
function ApplyDefaultsToUserPreferences() as boolean
isDirty = false
for each child in m.prefs_model
isDirty = m.ApplyToUserPreferences(m.user_prefs, child) or isDirty
end for
return isDirty
end function
function ApplyToUserPreferences(userPrefs as object, model as object) as boolean
isDirty = false
if model.children <> invalid
for each child in model.children
isDirty = m.ApplyToUserPreferences(userPrefs, child) or isDirty
end for
end if
if model.defaultValue <> invalid and (not userPrefs.DoesExist(model.key) or userPrefs[model.key] = invalid)
userPrefs[model.key] = model.defaultValue
isDirty = true
end if
return isDirty
end function
function GetUserPreferences() as object
return m.user_prefs
end function
function ApplyUserPreferences(preferences as object) as void
if preferences = invalid
return
end if
' TODO: user input validation
for each key in preferences
m.user_prefs[key] = preferences[key]
end for
m.SaveUserPreferences()
end function
function ClearUserPreferences() as void
m.user_prefs = {}
m.ApplyDefaultsToUserPreferences()
m.SaveUserPreferences()
end function
function GetValue(key as string) as dynamic
return m.user_prefs[key]
end function
function SetValue(key as string, value as dynamic) as void
if m.user_prefs[key] = value
return
end if
m.user_prefs[key] = value
m.SaveUserPreferences()
end function
end class