Creating the Helper Methods
When RSS feeds are being downloaded, you would display a message on the screen so that users know that the application is busy (see
Figure 4).
 | |
Figure 4. Operation Status: The device displays a message informing the user of the operation's status. |
For this purpose, you can improvise with the aid of the Panel and Label controls.
Define the CreatePanel() function so that you can dynamically create the message panel using a couple of Panel controls and a Label control, as shown in Listing 1.
Next, define the IsConnected() function to test whether the user is connected to the Internet:
'---check whether you are connected to the Internet---
Private Function IsConnected() As Boolean
Try
Dim hostName As String = Dns.GetHostName()
Dim curhost As IPHostEntry = Dns.GetHostEntry(hostName)
Return curhost.AddressList(0).ToString() <> _
IPAddress.Loopback.ToString()
Catch ex As Exception
Return False
End Try
End Function
Basically, you compare the IP address of the device with that of
localhost (127.0.0.1). As long as the device's IP address is not 127.0.0.1, it is assumed that there is Internet connectivity (as shown in
Listing 2).
Each post title and its description are separated by the ASCII character 3 and each posting is separated with the ASCII character 12. Notice that after the XML feed for an URL is downloaded, it is saved onto storage. This is to ensure that the application continues to work in offline mode (i.e. when user disconnects from the Internet). The URL of the feed is used as the filename, minus all the special characters inside within the URL, with the ".xml" extension appended. To strip off all the special characters in the URL, define the RemoveSpecialChars() function as follows:
 | |
Figure 5. Adding Posts: Adding each post to the TreeView control. |
'---removes special chars from an URL string---
Private Function RemoveSpecialChars(ByVal str As String) As String
Dim NewString As String = String.Empty
Dim reg As Regex = New Regex("[A-Z]|[a-z]")
Dim coll As MatchCollection = reg.Matches(str)
For i As Integer = 0 To coll.Count - 1
NewString = NewString + coll(i).Value
Next
Return NewString
End Function
Next, define the
SubscribeFeed() function to subscribe to a feed and then add each individual post to the
TreeView control (see
Figure 5).
Next, for each node representing a feed, the Text property is set to the feed's title and its URL is stored in the Tag property of the node. For each node representing a posting, the Text property is set to the posting's title and its description is stored in the Tag property (see Listing 3).