From ab3fd49a8c1a375c350dc67a6f8e8e7db39f0f24 Mon Sep 17 00:00:00 2001 From: Charles Ewert Date: Tue, 31 Oct 2023 23:38:33 -0400 Subject: [PATCH] Address reviewer feedback --- components/data/SceneManager.brs | 2 +- components/home/Home.brs | 6 ++++- components/settings/settings.brs | 7 +++-- components/tasks/PostTask.bs | 45 ++++++++++++++++++-------------- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/components/data/SceneManager.brs b/components/data/SceneManager.brs index 638d749e8..e294ba390 100755 --- a/components/data/SceneManager.brs +++ b/components/data/SceneManager.brs @@ -136,7 +136,7 @@ end function sub clearScenes() if m.content <> invalid then m.content.removeChildrenIndex(m.content.getChildCount(), 0) for each group in m.groups - if group.subtype() = "JFScreen" + if LCase(group.subtype()) = "jfscreen" group.callFunc("OnScreenHidden") end if end for diff --git a/components/home/Home.brs b/components/home/Home.brs index 8898a8f5a..9c070ea2d 100644 --- a/components/home/Home.brs +++ b/components/home/Home.brs @@ -23,8 +23,10 @@ sub loadLibraries() m.top.findNode("homeRows").callFunc("loadLibraries") end sub +' JFScreen hook that gets ran as needed. +' Used to update the foces, the state of the data, and tells the server about the device profile sub OnScreenShown() - if m.top.lastFocus <> invalid + if isValid(m.top.lastFocus) m.top.lastFocus.setFocus(true) else m.top.setFocus(true) @@ -42,6 +44,8 @@ sub OnScreenShown() end if end sub +' Triggered by m.postTask after completing a post. +' Empty the task data when finished. sub postFinished() m.postTask.unobserveField("responseCode") m.postTask.callFunc("empty") diff --git a/components/settings/settings.brs b/components/settings/settings.brs index 465d15338..de4600844 100644 --- a/components/settings/settings.brs +++ b/components/settings/settings.brs @@ -205,15 +205,18 @@ sub radioSettingChanged() set_user_setting(selectedSetting.settingName, m.radioSetting.content.getChild(m.radioSetting.checkedItem).id) end sub +' 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 sub OnScreenHidden() - ' some settings affect the device profile. - ' assume there were changes and always post device profile when leaving the settings screen m.postTask.arrayData = getDeviceCapabilities() m.postTask.apiUrl = "/Sessions/Capabilities/Full" m.postTask.control = "RUN" m.postTask.observeField("responseCode", "postFinished") end sub +' Triggered by m.postTask after completing a post. +' Empty the task data when finished. sub postFinished() m.postTask.unobserveField("responseCode") m.postTask.callFunc("empty") diff --git a/components/tasks/PostTask.bs b/components/tasks/PostTask.bs index 71aa8397a..d09679666 100644 --- a/components/tasks/PostTask.bs +++ b/components/tasks/PostTask.bs @@ -1,30 +1,35 @@ import "pkg:/source/api/baserequest.brs" +import "pkg:/source/utils/misc.brs" sub init() m.top.functionName = "postItems" end sub +' Main function for PostTask. +' Posts either an array of data +' or a string of data to an API endpoint. +' Saves the response information sub postItems() - if m.top.apiUrl <> "" - if m.top.arrayData.count() > 0 and m.top.stringData = "" - print "PostTask Started - Posting array to " + m.top.apiUrl - req = APIRequest(m.top.apiUrl) - req.SetRequest("POST") - httpResponse = asyncPost(req, FormatJson(m.top.arrayData)) - m.top.responseCode = httpResponse - print "PostTask Finished. " + m.top.apiUrl + " Response = " + httpResponse.toStr() - else if m.top.arrayData.count() = 0 and m.top.stringData <> "" - print "PostTask Started - Posting string(" + m.top.stringData + ") to " + m.top.apiUrl - req = APIRequest(m.top.apiUrl) - req.SetRequest("POST") - httpResponse = asyncPost(req, m.top.stringData) - m.top.responseCode = httpResponse - print "PostTask Finished. " + m.top.apiUrl + " Response = " + httpResponse.toStr() - else - print "ERROR processing data for PostTask" - end if - else + if m.top.apiUrl = "" print "ERROR in PostTask. Invalid API URL provided" + return + end if + if m.top.arrayData.count() > 0 and m.top.stringData = "" + print "PostTask Started - Posting array to " + m.top.apiUrl + req = APIRequest(m.top.apiUrl) + req.SetRequest("POST") + httpResponse = asyncPost(req, FormatJson(m.top.arrayData)) + m.top.responseCode = httpResponse + print "PostTask Finished. " + m.top.apiUrl + " Response = " + httpResponse.toStr() + else if m.top.arrayData.count() = 0 and m.top.stringData <> "" + print "PostTask Started - Posting string(" + m.top.stringData + ") to " + m.top.apiUrl + req = APIRequest(m.top.apiUrl) + req.SetRequest("POST") + httpResponse = asyncPost(req, m.top.stringData) + m.top.responseCode = httpResponse + print "PostTask Finished. " + m.top.apiUrl + " Response = " + httpResponse.toStr() + else + print "ERROR processing data for PostTask" end if end sub @@ -41,7 +46,7 @@ function asyncPost(req, data = "" as string) as integer resp = wait(m.top.timeoutSeconds * 1000, req.GetMessagePort()) respString = resp.GetString() - if respString <> invalid and respString <> "" + if isValidAndNotEmpty(respString) m.top.responseBody = ParseJson(respString) print "m.top.responseBody=", m.top.responseBody end if