I can't stop doing docs. I added networking, updated console, misc.

This commit is contained in:
Todd Werth
2015-04-18 23:01:59 -07:00
parent d47d0a5e02
commit 4f965397e7
6 changed files with 360 additions and 4 deletions

View File

@@ -271,7 +271,7 @@ class MyStylesheet < ApplicationStylesheet
end
```
In order to use this feature, you must add the `JMIMageCache` cocoapod to your project:
In order to use this feature, you must add the `JMIMageCache` cocoapod to your project (this is included in a new project):
```ruby
app.pods do

View File

@@ -4,6 +4,25 @@ In REPL, type: `live_stylesheets`
![image](http://clrsight.co/jh/LiveReload4.gif?+)
------
## Fancy printing
RedPotion uses [motion_print](https://github.com/OTGApps/motion_print) for fancy console output.
Instead of `puts`, use `mp`:
```ruby
> mp({b: "bee", a: 'a', see: 4})
{
a => a,
b => bee,
see => 4
}
```
------
## Generators
@@ -70,15 +89,129 @@ I recomend you play around with it, do this:
> potion create view test_view
```
------
## Console Fun
----
```ruby
rmq.log :tree
─── UIView ( root_view ) 4525363712 {l: 0, t: 0, w: 320, h: 568}
├─── UILabel ( hello_world ) 4526498928 {l: 60, t: 269, w: 200, h: 30}
├─── HelloWorldSection ( section ) 4614724352 {l: 60, t: 100, w: 200, h: 100}
├─── UIButton ( section_button ) 4614736128 {l: 5, t: 5, w: 80, h: 20}
├─── UIButtonLabel 4526568160 {l: 13, t: -1, w: 54.5, h: 21.5}
├─── UIButton ( open_table_button ) 4614749936 {l: 60, t: 428, w: 200, h: 20}
├─── UIButtonLabel 4526561536 {l: 26.5, t: -1, w: 147.5, h: 21.5}
├─── UIButton ( open_metal_table_button ) 4614761424 {l: 60, t: 458, w: 200, h: 20}
├─── UIButtonLabel 4526553984 {l: 1.5, t: -1, w: 197, h: 21.5}
├─── UIButton ( open_data_table_button ) 4649398928 {l: 60, t: 488, w: 200, h: 20}
├─── UIButtonLabel 4526547280 {l: 6, t: -1, w: 188.5, h: 21.5}
├─── UIButton ( open_example_controller_ ) 4649417104 {l: 60, t: 518, w: 200, h: 20}
├─── UIButtonLabel 4526537936 {l: 1, t: -1, w: 198, h: 21.5}
```
```ruby
find.all.log
object_id | class | style_name | frame |
sv id | superview | subviews count | tags |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4526498928 | UILabel | hello_world | {l: 60, t: 269, w: 200, h: 30} |
4525363712 | UIView | 0 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4614724352 | HelloWorldSection | section | {l: 60, t: 100, w: 200, h: 100} |
4525363712 | UIView | 1 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4614736128 | UIButton | section_button | {l: 5, t: 5, w: 80, h: 20} |
4614724352 | HelloWorldSection | 1 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4526568160 | UIButtonLabel | | {l: 13, t: -1, w: 54.5, h: 21.5}|
4614736128 | UIButton | 0 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4614749936 | UIButton | open_table_button | {l: 60, t: 428, w: 200, h: 20} |
4525363712 | UIView | 1 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4526561536 | UIButtonLabel | | {l: 26.5, t: -1, w: 147.5, h: 21.5}|
4614749936 | UIButton | 0 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4614761424 | UIButton | open_metal_table_button | {l: 60, t: 458, w: 200, h: 20} |
4525363712 | UIView | 1 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4526553984 | UIButtonLabel | | {l: 1.5, t: -1, w: 197, h: 21.5}|
4614761424 | UIButton | 0 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4649398928 | UIButton | open_data_table_button | {l: 60, t: 488, w: 200, h: 20} |
4525363712 | UIView | 1 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4526547280 | UIButtonLabel | | {l: 6, t: -1, w: 188.5, h: 21.5}|
4649398928 | UIButton | 0 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4649417104 | UIButton | open_example_controller_| {l: 60, t: 518, w: 200, h: 20} |
4525363712 | UIView | 1 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4526537936 | UIButtonLabel | | {l: 1, t: -1, w: 198, h: 21.5} |
4649417104 | UIButton | 0 | |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
RMQ 4649676656. 12 selected. selectors: []
```
```ruby
find.all.log :wide
find(UIView).frame.log
object_id | class | style_name | frame | sv id | superview | subviews count | tags |
- - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - | - - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
4526498928 | UILabel | hello_world | {l: 60, t: 269, w: 200, h: 30} | 4525363712 | UIView | 0 | |
4614724352 | HelloWorldSection | section | {l: 60, t: 100, w: 200, h: 100} | 4525363712 | UIView | 1 | |
4614736128 | UIButton | section_button | {l: 5, t: 5, w: 80, h: 20} | 4614724352 | HelloWorldSection | 1 | |
4526568160 | UIButtonLabel | | {l: 13, t: -1, w: 54.5, h: 21.5}| 4614736128 | UIButton | 0 | |
4614749936 | UIButton | open_table_button | {l: 60, t: 428, w: 200, h: 20} | 4525363712 | UIView | 1 | |
4526561536 | UIButtonLabel | | {l: 26.5, t: 1, w: 147.5, h: 21}| 4614749936 | UIButton | 0 | |
4614761424 | UIButton | open_metal_table_button | {l: 60, t: 458, w: 200, h: 20} | 4525363712 | UIView | 1 | |
4526553984 | UIButtonLabel | | {l: 1.5, t: -1, w: 197, h: 21.5}| 4614761424 | UIButton | 0 | |
4649398928 | UIButton | open_data_table_button | {l: 60, t: 488, w: 200, h: 20} | 4525363712 | UIView | 1 | |
4526547280 | UIButtonLabel | | {l: 6, t: -1, w: 188.5, h: 21.5}| 4649398928 | UIButton | 0 | |
4649417104 | UIButton | open_example_controller_| {l: 60, t: 518, w: 200, h: 20} | 4525363712 | UIView | 1 | |
4526537936 | UIButtonLabel | | {l: 1, t: -1, w: 198, h: 21.5} | 4649417104 | UIButton | 0 | |
RMQ 4668847808. 12 selected. selectors: []
```
```ruby
find(HelloWorldSection).frame.log
*****************---*******---**************************
* | | * window
* 100 top | * {w: 320, h: 568}
* | | *
* --- | * superview
* ***************|***** --- * {w: 320, h: 568}
* * | * | *
* * | * | *
* * 200 bottom * | * view
* 60 * | * | * {l: 60, t: 100,
*|-- left --|* | * | * w: 200, h: 100}
* * | * height 100 *
* * | * | * z_order: 1
* * 260 | * | * z_position: 0.0
*|------------------ right -+---|* | *
* * | * | 60 * Location in root view
* * | * |--+--from_right---|* {l: 60, t: 100,
* * --- * | * w: 200, h: 100}
* ***************---*** --- *
* | *
* |------ width - + --| *
* 200 | *
* | *
* | *
* 368 from_bottom *
* | *
* --- *
******
```
```ruby
find(UIView).show
find(UILabel).animations.blink
find(UIButton).nudge l: 10

View File

@@ -1,4 +1,24 @@
## Fancy printing
RedPotion uses [motion_print](https://github.com/OTGApps/motion_print) for fancy console output.
Instead of `puts`, use `mp`:
```ruby
> mp({b: "bee", a: 'a', see: 4})
{
a => a,
b => bee,
see => 4
}
```
------
## RMQ Debug
Adding rmq_debug=true to rake turns on some debugging features that are too slow or verbose to include in a normal build. It's great for normal use in the simulator, but you'll want to leave it off if you're measuring performance.
```
rake rmq_debug=true
```

View File

@@ -33,6 +33,34 @@ image.resource_resizable('foo', left: 10, top: 10, right: 10, bottom: 10)
image.from_view(my_view)
```
------
## Remote images
You can set `remote_image` to a URL string or an instance of `NSURL` and it will automatically fetch the image and set the image (with caching) using the power of [JMImageCache](https://github.com/jakemarsh/JMImageCache).
```ruby
class MyStylesheet < ApplicationStylesheet
def my_ui_image_view(st)
# placeholder_image= is just an alias to image=
# Set the placeholder image you want from your resources directory
st.placeholder_image = image.resource("my_placeholder")
# Set the remote URL. It will be applied to the UIImageView
# when downloaded or retrieved from the local cache.
st.remote_image = "http://www.rubymotion.com/img/rubymotion-logo.png"
# or st.remote_image = NSURL.urlWithString(...)
end
end
```
To assign a remote image to a UIImageView:
```ruby
your_ui_image_view.remote_image = "http://bit.ly/18iMhwc"
```
------
## Capped Images
Sometimes when you apply a background_image to a view you want the image to stretch to the size of the view without stretching the corners of the image, for example if you're making a rounded button. The SDK has a nice feature for this, called UIImage#resizableImageWithCapInsets. It stretches the center of your image, but not the corners.

174
docs/cookbook/networking.md Normal file
View File

@@ -0,0 +1,174 @@
RedPotion uses the following for networking:
* AFNetworking pod
* AFMotion gem
* JMImageCache
-------
## Remote images
You can set `remote_image` to a URL string or an instance of `NSURL` and it will automatically fetch the image and set the image (with caching) using the power of [JMImageCache](https://github.com/jakemarsh/JMImageCache).
You should always use `remote_image` for any image you download, it will cache it to memory and out to disk when it's appropriate. It does it async and is performant, even for a large table of images.
```ruby
class MyStylesheet < ApplicationStylesheet
def my_ui_image_view(st)
# placeholder_image= is just an alias to image=
# Set the placeholder image you want from your resources directory
st.placeholder_image = image.resource("my_placeholder")
# Set the remote URL. It will be applied to the UIImageView
# when downloaded or retrieved from the local cache.
st.remote_image = "http://www.rubymotion.com/img/rubymotion-logo.png"
# or st.remote_image = NSURL.urlWithString(...)
end
end
```
To assign a remote image to a UIImageView:
```ruby
your_ui_image_view.remote_image = "http://bit.ly/18iMhwc"
```
-------
## Get HTML or JSON directly
```ruby
AFMotion::HTTP.get("http://google.com") do |result|
mp result.body
end
AFMotion::JSON.get("http://jsonip.com") do |result|
mp result.object["ip"]
end
```
-------
## Setup a session client to reuse (best practice)
```ruby
@client = AFMotion::SessionClient.build("https://alpha-api.app.net/") do
session_configuration :default
header "Accept", "application/json"
response_serializer :json
end
```
Then use it:
```ruby
@client.get("stream/0/posts/stream/global") do |result|
# result.operation is the AFURLConnectionOperation instance
mp result.operation.inspect
mp result.status_code
if result.success?
# result.object depends on the type of operation.
# For JSON and PLIST, this is usually a Hash.
# For XML, this is an NSXMLParser
# For HTTP, this is an NSURLResponse
# For Image, this is a UIImage
p result.object
elsif result.failure?
# result.error is an NSError
mp result.error.localizedDescription
end
end
```
The client support methods of the form `Client#get/post/put/patch/delete(url, request_parameters)`. The `request_parameters` is a hash containing your parameters to attach as the request body or URL parameters, depending on request type. For example:
```ruby
@client.get("users", id: 1) do |result|
...
end
@client.post("users", name: "@clayallsopp", library: "AFMotion") do |result|
...
end
```
#### Multipart Requests
The session supports multipart form requests (i.e. for image uploading) - simply use `multipart_post` and it'll convert your parameters into properly encoded multipart data. For all other types of request data, use the `form_data` object passed to your callback:
```ruby
# an instance of UIImage
image = my_function.get_image
data = UIImagePNGRepresentation(image)
@client.multipart_post("avatars") do |result, form_data|
if form_data
# Called before request runs
# see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
form_data.appendPartWithFileData(data, name: "avatar", fileName:"avatar.png", mimeType: "image/png")
elsif result.success?
...
else
...
end
end
```
This is an instance of [`AFMultipartFormData`](http://cocoadocs.org/docsets/AFNetworking/2.0.0/Protocols/AFMultipartFormData.html).
If you want to track upload progress, you can add a third callback argument which returns the upload percentage between 0.0 and 1.0:
```ruby
@client.multipart_post("avatars") do |result, form_data, progress|
if form_data
# Called before request runs
# see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
form_data.appendPartWithFileData(data, name: "avatar", fileName:"avatar.png", mimeType: "image/png")
elsif progress
# 0.0 < progress < 1.0
my_widget.update_progress(progress)
else
...
end
```
#### Headers
You can set default HTTP headers using `@client.headers`, which is sort of like a `Hash`:
```ruby
@client.headers["Accept"]
#=> "application/json"
@client.headers["Accept"] = "something_else"
#=> "application/something_else"
@client.headers.delete "Accept"
#=> "application/something_else"
```
#### Client Building DSL
The session client DSLs allows the following properties:
- `header(header, value)`
- `authorization(username: ___, password: ____)` for HTTP Basic auth, or `authorization(token: ____)` for Token based auth.
- `request_serializer(serializer)`. Allows you to set an [`AFURLRequestSerialization`](http://cocoadocs.org/docsets/AFNetworking/2.0.0/Protocols/AFURLRequestSerialization.html) for all your client's requests, which determines how data is encoded on the way to the server. So if your API is always going to be JSON, you should set `operation(:json)`. Accepts `:json` and `:plist`, or any instance of `AFURLRequestSerialization` and must be called before calling `header` or `authorization` or else the [headers will not be applied](https://github.com/clayallsopp/afmotion/issues/78).
- `response_serializer(serializer)`. Allows you to set an [`AFURLResponseSerialization`](http://cocoadocs.org/docsets/AFNetworking/2.0.0/Protocols/AFURLResponseSerialization.html), which determines how data is decoded once the server respnds. Accepts `:json`, `:xml`, `:plist`, `:image`, `:http`, or any instance of `AFURLResponseSerialization`.
- `session_configuration(session_configuration, identifier = nil)`. Allows you to set the [`NSURLSessionConfiguration`](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/Reference/Reference.html#//apple_ref/occ/cl/NSURLSessionConfiguration). Accepts `:default`, `:ephemeral`, `:background` (with the `identifier` as a String), or an instance of `NSURLSessionConfiguration`.
You can also configure your client by passing it as a block argument:
```ruby
@client = AFMotion::SessionClient.build("https://alpha-api.app.net/") do |client|
client.session_configuration :default
client.header "Accept", @custom_header
end
```
See all of AFMotion's [docs here](https://github.com/clayallsopp/afmotion).

View File

@@ -7,7 +7,7 @@ pages:
- [redpotion_specific_features.md, Docs, "RedPotion specific features"]
- [contributing.md, Docs, "Contributing"]
- [cookbook/command_line.md, Cookbook, "Command-line and REPL tools"]
- [cookbook/project.md, Cookbook, "Find out about the project, are you in the simulator, dev, release, on what OS, what device, etc"]
- [cookbook/project.md, Cookbook, "Project info, are you in the simulator, dev, release, on what OS, what device, etc"]
- [cookbook/device.md, Cookbook, "The device"]
- [cookbook/app_delegate.md, Cookbook, "The app delegate"]
- [cookbook/app.md, Cookbook, "The app and how to get to it"]
@@ -25,8 +25,9 @@ pages:
- [cookbook/actions.md, Cookbook, "Misc actions, such as hiding, focusing, etc"]
- [cookbook/attributes.md, Cookbook, "Updating data or attributes"]
- [cookbook/tagging.md, Cookbook, "Tagging views"]
- [cookbook/format.md, Cookbook, "Formatting numbers and dates"]
- [cookbook/core_data.md, Cookbook, "Using a local database"]
- [cookbook/timers.md, Cookbook, "Timers and delays. Do something every second"]
- [cookbook/networking.md, Cookbook, "Networking, JSON, remote images"]
- [cookbook/validations.md, Cookbook, "Validating data, text, text boxes, etc"]
- [cookbook/format.md, Cookbook, "Formatting numbers and dates"]
- [cookbook/debugging.md, Cookbook, "Debugging"]