jf-roku/components/settings/settings.bs

255 lines
8.3 KiB
Plaintext
Raw Normal View History

import "pkg:/source/utils/config.bs"
import "pkg:/source/utils/misc.bs"
2023-10-03 16:27:07 +00:00
import "pkg:/source/roku_modules/log/LogMixin.brs"
2023-10-30 22:04:09 +00:00
' post device profile
2023-11-11 00:42:33 +00:00
import "pkg:/source/utils/deviceCapabilities.bs"
2022-05-01 10:51:28 +00:00
sub init()
m.log = log.Logger("Settings")
2022-05-01 10:51:28 +00:00
m.top.optionsAvailable = false
m.userLocation = []
m.settingsMenu = m.top.findNode("settingsMenu")
m.settingDetail = m.top.findNode("settingDetail")
m.settingDesc = m.top.findNode("settingDesc")
m.path = m.top.findNode("path")
m.boolSetting = m.top.findNode("boolSetting")
2022-09-04 11:22:08 +00:00
m.integerSetting = m.top.findNode("integerSetting")
2022-12-20 02:03:35 +00:00
m.radioSetting = m.top.findNode("radioSetting")
2022-09-04 11:22:08 +00:00
m.integerSetting.observeField("submit", "onKeyGridSubmit")
m.integerSetting.observeField("escape", "onKeyGridEscape")
2022-05-01 10:51:28 +00:00
m.settingsMenu.setFocus(true)
m.settingsMenu.observeField("itemFocused", "settingFocused")
m.settingsMenu.observeField("itemSelected", "settingSelected")
m.boolSetting.observeField("checkedItem", "boolSettingChanged")
2022-12-20 02:03:35 +00:00
m.radioSetting.observeField("checkedItem", "radioSettingChanged")
2022-05-01 10:51:28 +00:00
2023-10-30 22:04:09 +00:00
m.postTask = createObject("roSGNode", "PostTask")
2022-05-01 10:51:28 +00:00
' Load Configuration Tree
m.configTree = GetConfigTree()
LoadMenu({ children: m.configTree })
end sub
2022-09-04 11:22:08 +00:00
sub onKeyGridSubmit()
selectedSetting = m.userLocation.peek().children[m.settingsMenu.itemFocused]
set_user_setting(selectedSetting.settingName, m.integerSetting.text)
m.settingsMenu.setFocus(true)
end sub
sub onKeyGridEscape()
if m.integerSetting.escape = "left" or m.integerSetting.escape = "back"
m.settingsMenu.setFocus(true)
end if
end sub
2022-05-01 10:51:28 +00:00
sub LoadMenu(configSection)
if configSection.children = invalid
' Load parent menu
m.userLocation.pop()
configSection = m.userLocation.peek()
else
if m.userLocation.Count() > 0 then m.userLocation.peek().selectedIndex = m.settingsMenu.itemFocused
m.userLocation.push(configSection)
end if
result = CreateObject("roSGNode", "ContentNode")
for each item in configSection.children
listItem = result.CreateChild("ContentNode")
listItem.title = tr(item.title)
listItem.Description = tr(item.description)
listItem.id = item.id
end for
m.settingsMenu.content = result
if configSection.selectedIndex <> invalid and configSection.selectedIndex > -1
m.settingsMenu.jumpToItem = configSection.selectedIndex
end if
' Set Path display
m.path.text = tr("Settings")
2022-05-01 10:51:28 +00:00
for each level in m.userLocation
if level.title <> invalid then m.path.text += " / " + tr(level.title)
end for
end sub
sub settingFocused()
selectedSetting = m.userLocation.peek().children[m.settingsMenu.itemFocused]
m.settingDesc.text = tr(selectedSetting.Description)
m.top.overhangTitle = tr(selectedSetting.Title)
2022-05-01 10:51:28 +00:00
' Hide Settings
m.boolSetting.visible = false
2022-09-04 11:22:08 +00:00
m.integerSetting.visible = false
2022-12-20 02:03:35 +00:00
m.radioSetting.visible = false
2022-05-01 10:51:28 +00:00
if selectedSetting.type = invalid
return
else if selectedSetting.type = "bool"
m.boolSetting.visible = true
if m.global.session.user.settings[selectedSetting.settingName] = true
2022-05-01 10:51:28 +00:00
m.boolSetting.checkedItem = 1
else
m.boolSetting.checkedItem = 0
end if
2022-09-04 11:22:08 +00:00
else if selectedSetting.type = "integer"
integerValue = m.global.session.user.settings[selectedSetting.settingName].ToStr()
2022-09-04 11:22:08 +00:00
if isValid(integerValue)
m.integerSetting.text = integerValue
end if
m.integerSetting.visible = true
2022-12-20 02:03:35 +00:00
else if LCase(selectedSetting.type) = "radio"
selectedValue = m.global.session.user.settings[selectedSetting.settingName]
2022-12-20 02:03:35 +00:00
radioContent = CreateObject("roSGNode", "ContentNode")
itemIndex = 0
for each item in m.userLocation.peek().children[m.settingsMenu.itemFocused].options
listItem = radioContent.CreateChild("ContentNode")
listItem.title = tr(item.title)
listItem.id = item.id
if selectedValue = item.id
m.radioSetting.checkedItem = itemIndex
end if
itemIndex++
end for
m.radioSetting.content = radioContent
m.radioSetting.visible = true
2022-05-01 10:51:28 +00:00
else
m.log.warn("Unknown setting type", selectedSetting.type)
2022-05-01 10:51:28 +00:00
end if
end sub
sub settingSelected()
selectedItem = m.userLocation.peek().children[m.settingsMenu.itemFocused]
if selectedItem.type <> invalid ' Show setting
if selectedItem.type = "bool"
m.boolSetting.setFocus(true)
end if
2022-09-04 11:22:08 +00:00
if selectedItem.type = "integer"
m.integerSetting.setFocus(true)
end if
2022-12-20 02:03:35 +00:00
if (selectedItem.type) = "radio"
m.radioSetting.setFocus(true)
end if
2022-05-01 10:51:28 +00:00
else if selectedItem.children <> invalid and selectedItem.children.Count() > 0 ' Show sub menu
LoadMenu(selectedItem)
m.settingsMenu.setFocus(true)
else
return
end if
m.settingDesc.text = m.settingsMenu.content.GetChild(m.settingsMenu.itemFocused).Description
end sub
sub boolSettingChanged()
if m.boolSetting.focusedChild = invalid then return
selectedSetting = m.userLocation.peek().children[m.settingsMenu.itemFocused]
if m.boolSetting.checkedItem
2023-09-14 21:51:41 +00:00
session.user.settings.Save(selectedSetting.settingName, "true")
if Left(selectedSetting.settingName, 7) = "global."
' global user setting
' save to main registry block
set_setting(selectedSetting.settingName, "true")
' setting specific triggers
if selectedSetting.settingName = "global.rememberme"
print "m.global.session.user.id=", m.global.session.user.id
set_setting("active_user", m.global.session.user.id)
end if
else
' regular user setting
' save to user specific registry block
set_user_setting(selectedSetting.settingName, "true")
end if
2022-05-01 10:51:28 +00:00
else
2023-09-14 21:51:41 +00:00
session.user.settings.Save(selectedSetting.settingName, "false")
if Left(selectedSetting.settingName, 7) = "global."
' global user setting
' save to main registry block
set_setting(selectedSetting.settingName, "false")
' setting specific triggers
if selectedSetting.settingName = "global.rememberme"
unset_setting("active_user")
end if
else
' regular user setting
' save to user specific registry block
set_user_setting(selectedSetting.settingName, "false")
end if
2022-05-01 10:51:28 +00:00
end if
end sub
2022-12-20 02:03:35 +00:00
sub radioSettingChanged()
if m.radioSetting.focusedChild = invalid then return
selectedSetting = m.userLocation.peek().children[m.settingsMenu.itemFocused]
set_user_setting(selectedSetting.settingName, m.radioSetting.content.getChild(m.radioSetting.checkedItem).id)
end sub
2022-05-01 10:51:28 +00:00
2023-11-01 03:38:33 +00:00
' JFScreen hook that gets ran as needed.
' Assumes settings were changed and they affect the device profile.
' Posts a new device profile to the server using the task thread
2023-10-30 22:04:09 +00:00
sub OnScreenHidden()
m.postTask.arrayData = getDeviceCapabilities()
m.postTask.apiUrl = "/Sessions/Capabilities/Full"
m.postTask.control = "RUN"
m.postTask.observeField("responseCode", "postFinished")
end sub
2023-11-01 03:38:33 +00:00
' Triggered by m.postTask after completing a post.
' Empty the task data when finished.
2023-10-30 22:04:09 +00:00
sub postFinished()
m.postTask.unobserveField("responseCode")
2023-10-31 00:15:05 +00:00
m.postTask.callFunc("empty")
2023-10-30 22:04:09 +00:00
end sub
2023-09-24 02:14:47 +00:00
' Returns true if any of the data entry forms are in focus
function isFormInFocus() as boolean
2023-10-31 21:01:31 +00:00
if isValid(m.settingDetail.focusedChild) or m.radioSetting.hasFocus() or m.boolSetting.hasFocus() or m.integerSetting.hasFocus()
2023-09-24 02:14:47 +00:00
return true
end if
return false
end function
2022-05-01 10:51:28 +00:00
function onKeyEvent(key as string, press as boolean) as boolean
if not press then return false
if (key = "back" or key = "left") and m.settingsMenu.focusedChild <> invalid and m.userLocation.Count() > 1
LoadMenu({})
return true
2023-10-31 21:01:31 +00:00
else if (key = "back" or key = "left") and isFormInFocus()
2022-12-20 02:03:35 +00:00
m.settingsMenu.setFocus(true)
return true
2022-05-01 10:51:28 +00:00
end if
if key = "options"
m.global.sceneManager.callFunc("popScene")
2023-02-02 00:03:12 +00:00
return true
end if
2022-05-01 10:51:28 +00:00
if key = "right"
settingSelected()
end if
return false
2022-05-30 12:57:40 +00:00
end function