Android Question how to make DownloadAndSave sub as a single function in the project

AnandGupta

Expert
Licensed User
Longtime User
I have enhanced the B4XGoodies check app, https://www.b4x.com/android/forum/threads/b4xgoodies-check-app.110902/, to 1.002, but I am unable to remove duplicate "DownloadAndSave" sub.

You will find that it is in both main and download_html activities. I added a code module for it but it is giving error.
Static code modules cannot handle events.
Code modules do not support Me keyword.

So, how to make DownloadAndSave sub as a single function in the project ?

Regards,

Anand
 

DonManfred

Expert
Licensed User
Longtime User
You can not use wait for in a code module.

B4X:
Sub CheckVersion
    Private cURL, cFile, cText, cTxtLast As String

    cURL = "https://www.dropbox.com/s/ards6k5w3wuzy5n/version.txt?dl=1"
    cFile = "version.txt"
    wait for (DownloadAndSaveFile(cURL, Starter.SourceFolder, cFile)) Complete (Result As Boolean)
    If Result Then
        If File.Exists(Starter.SourceFolder, cFile) Then
        cText = File.ReadString(Starter.SourceFolder, cFile)
        cTxtLast = File.ReadString(Starter.SourceFolder, "last_" & cFile)
        If cText == cTxtLast Then
            lblMesg.Text = "Same Version"
        Else
            lblMesg.Text = "New Version Found : " & CRLF & "Old  = " & cTxtLast & CRLF & "New = " & cText
            Private cURL, cFile, cText As String
            cURL = "https://www.dropbox.com/s/31w3ksayw80n6b8/b4xgoodies.html?dl=1"
            cFile = "b4xgoodies.html"
            wait for (DownloadAndSaveFile(cURL, Starter.SourceFolder, cFile)) Complete (Result As Boolean)
            If Result Then
                lblMesg.Text = lblMesg.Text&CRLF&"New Version downloaded"
            End If
        End If
    End If
    Else
        MsgboxAsync("No internet connection" & CRLF & "or error downloading " & cFile, "Check Version")
    End If
  
End Sub

Sub DownloadAndSaveFile(Link As String, cFolder As String, cOutPutFile As String) As ResumableSub
    Dim oJob As HttpJob
    oJob.Initialize("", Me)
    oJob.Download(Link)
    Log("oJob start ")
    Dim result As Boolean = False
    Wait For (oJob) JobDone(oJob As HttpJob)
    Log("oJob.Success = " & oJob.Success)
    If oJob.Success Then
        Dim out As OutputStream = File.OpenOutput(cFolder, cOutPutFile, False)
        File.Copy2(oJob.GetInputStream, out)
        out.Close '<------ very important
        Log($"${cOutPutFile} written to ${cFolder}"$)
        result = True
    End If
    oJob.Release
    Return result
End Sub
 
Last edited:
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Another option is to move the downloadandsave to the starterservice and just call it from the activity.
Thanks for your advise. I followed it but now ide is showing error "Array expected."

2019-11-10_010314.png

Any help how to fix this error.

Regards,

Anand
 

Attachments

  • B4XGoodies_Check_error.zip
    13.7 KB · Views: 252
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Create a class named DownloadHelper.
WOW ! So simple !! I updated B4XGoodies_Check_1.003.zip
Thanks for the help, Erel.

I took the lead from your message and searched the forum for such class code and I found many 'service module', like in ImageDownloader, FlickrViewer from you. Now may I ask which is better : the class module you suggested here or the service module in those threads ?

Regards,

Anand
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
There are advantages for using classes as you can access them directly.
Thanks. I will try to write class, next time I get stuck in a sub/function.
This class module looks more natural to me than the service module, as per my basic knowledge of B4A.

Regards,

Anand
 
Upvote 0
Top