commit 7eeb0a507e62fc26ea9560c28a8a2a0a8e4a2fe9
parent bbc5d1c070242dbfbc577bbf49477d90c7d83618
Author: Brian C. Lane <bcl@brianlane.com>
Date:   Sun, 13 Nov 2022 10:40:00 -0800
Add url validation task
This is used to check the server url that the user enters. It has to be
done in a task because you cannot make network requests from the render
thread in SceneGraph.
Diffstat:
6 files changed, 72 insertions(+), 13 deletions(-)
diff --git a/HMS/components/MainScene.brs b/HMS/components/MainScene.brs
@@ -6,8 +6,13 @@ sub Init()
     print "MainScene->Init()"
     m.top.ObserveField("serverurl", "RunContentTask")
 
-    RunSetupServerDialog()
-
+    url = RegRead("ServerURL")
+    if url = invalid then
+        RunSetupServerDialog()
+    else
+        ' Validate the url
+        RunValidateURLTask(url)
+    end if
 end sub
 
 
@@ -28,6 +33,28 @@ sub OnMainContentLoaded()
 '    m.GridScreen.content = m.contentTask.content
 end sub
 
+sub RunValidateURLTask(url as string)
+    print "MainScene->RunValidateURLTask()"
+
+    m.validateTask = CreateObject("roSGNode", "ValidateURLTask")
+    m.validateTask.serverurl = url
+    m.validateTask.ObserveField("valid", "OnValidateChanged")
+    m.validateTask.control = "run"
+end sub
+
+sub OnValidateChanged()
+    print "MainScene->OnValidateChanged"
+    print m.validateTask.serverurl
+    print m.validateTask.valid
+    if not m.validateTask.valid then
+        ' Still invalid, run it again
+        RunSetupServerDialog()
+    else
+        ' Valid url, trigger the content load
+        m.top.serverurl = m.validateTask.serverurl
+    end if
+end sub
+
 sub RunSetupServerDialog()
     print "MainScene->RunSetupServerDialog()"
     m.serverDialog = createObject("roSGNode", "SetupServerDialog")
@@ -39,6 +66,5 @@ sub OnSetupServerURL()
     print "MainScene->OnSetupServerURL()"
     print m.serverDialog.serverurl
 
-    ' pretend it was ok
-    m.top.serverurl = m.serverDialog.serverurl
+    RunValidateURLTask(m.serverDialog.serverurl)
 end sub
diff --git a/HMS/components/MainScene.xml b/HMS/components/MainScene.xml
@@ -1,5 +1,7 @@
 <?xml version="1.0" encoding="utf-8" ?>
 <component name="MainScene" extends="Scene" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://devtools.web.roku.com/schema/RokuSceneGraph.xsd">
+    <script type="text/brightscript" uri="pkg:/source/generalUtils.brs" />
+    <script type="text/brightscript" uri="pkg:/source/urlUtils.brs" />
     <script type="text/brightscript" uri="MainScene.brs" />
 
     <interface>
diff --git a/HMS/components/ValidateURLTask.brs b/HMS/components/ValidateURLTask.brs
@@ -0,0 +1,22 @@
+'********************************************************************
+'**  Home Media Server Application - ValidateURLTask
+'**  Copyright (c) 2022 Brian C. Lane All Rights Reserved.
+'********************************************************************
+sub Init()
+    print "ValidateURLTask->Init()"
+    m.top.functionName = "ValidateURL"
+end sub
+
+' ValidateURL is executed when  m.validateURLTask.control = "run"
+' It checks serverurl and sets valid to true/false
+sub ValidateURL()
+    print "ValidateURLTask->GetContent()"
+    print m.top.serverurl
+
+    m.top.valid = isURLValid(m.top.serverurl)
+    if m.top.valid then
+        print "Is VALID"
+    else
+        print "Is NOT VALID"
+    end if
+end sub
diff --git a/HMS/components/ValidateURLTask.xml b/HMS/components/ValidateURLTask.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<component name="ValidateURLTask" extends="Task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://devtools.web.roku.com/schema/RokuSceneGraph.xsd">
+    <script type="text/brightscript" uri="pkg:/source/urlUtils.brs" />
+    <script type="text/brightscript" uri="ValidateURLTask.brs" />
+
+    <interface>
+        <field id="serverurl" type="uri" />
+        <field id="valid" type="bool" alwaysNotify="true" />
+    </interface>
+</component>
diff --git a/HMS/source/checkServerUrl.brs b/HMS/source/checkServerUrl.brs
@@ -49,12 +49,3 @@ Function checkServerUrl(forceEdit As Boolean) As Boolean
         endif
     end while
 End Function
-
-'************************************************************
-'** Check a URL to see if it is valid
-'************************************************************
-Function isUrlValid( url As String ) As Boolean
-    result = getHTMLWithTimeout(url, 60)
-    return not result.error
-End Function
-
diff --git a/HMS/source/urlUtils.brs b/HMS/source/urlUtils.brs
@@ -285,3 +285,11 @@ Function postHTMLWithTimeout(url As String, content As String, seconds As Intege
 
     return result
 End Function
+
+'************************************************************
+'** Check a URL to see if it is valid
+'************************************************************
+Function isUrlValid( url As String ) As Boolean
+    result = getHTMLWithTimeout(url, 60)
+    return not result.error
+End Function