scarpe-team / scarpe

Scarpe - shoes but running on webview

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rework download widget

noahgibbs opened this issue · comments

It looks like download starts a background thread, which is probably a no-no -- Webview doesn't allow calling GUI methods from background threads for instance.

It doesn't currently return the value anywhere, or call a block if given, or fire events or anything. Here's the manual entry for download:

=== download(url: a string, styles) ===

Starts a download thread (much like XMLHttpRequest, if you're familiar with
JavaScript.)  This method returns immediately and runs the download in the
background.  Each download thread also fires `start`, `progress` and `finish`
events.  You can send the download to a file or just get back a string (in the
`finish` event.)

If you attach a block to a download, it'll get called as the `finish` event.

{{{
 #!ruby
 Shoes.app do
   stack do
     title "Searching Google", size: 16
     @status = para "One moment..."

     # Search Google for 'shoes' and print the HTTP headers
     download "http://www.google.com/search?q=shoes" do |goog|
       @status.text = "Headers: " + goog.response.headers.inspect
     end
   end
 end
}}}

And, if we wanted to use the downloaded data, we'd get it using
`goog.response.body`.  This example is truly the simplest form of `download`:
pulling some web data down into memory and handling it once it's done.

Another simple use of `download` is to save some web data to a file, using the
`:save` style.

{{{
 #!ruby
 Shoes.app do
   stack do
     title "Downloading Google image", size: 16
     @status = para "One moment..."

     download "http://www.google.com/logos/nasa50th.gif",
       save: "nasa50th.gif" do
         @status.text = "Okay, is downloaded."
     end
   end
 end
}}}

In this case, you can still get the headers for the downloaded file, but
`response.body` will be `nil`, since the data wasn't saved to memory.  You will
need to open the file to get the downloaded goods.

If you need to send certain headers or actions to the web server, you can use
the `:method`, `:headers` and `:body` styles to customize the HTTP request.
(And, if you need to go beyond these, you can always break out Ruby's OpenURI
class.)

{{{
 #!ruby
 Shoes.app do
   stack do
     title "GET Google", size: 16
     @status = para "One moment..."

     download "http://www.google.com/search?q=shoes",
         method: "GET" do |dump|
       @status.text = dump.response.body
     end
   end
 end
}}}

As you can see from the above example, Shoes makes use of the "GET" method to
query google's search engine.

PR #304 does a lot of this, including fixing the basic API.

It's still using a background thread, which longer-term we'd like to avoid. But that's harder to work around. I'll close this and open an issue related to concurrency -- download isn't the only API that may have concurrency issues.