Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ Add the **RedAlert** gem to your Gemfile.
puts "You clicked #{action_type}"
end

# Add input fields by setting the style
rmq.app.alert(title: "Text Field", message: "My style is :login", style: :login) do |action_type, fields|
puts "you entered '#{fields[:login].text}' as the login and '#{fields[:password].text}' as the password"
end

# Add input fields with settings for placeholder text, whether the field is secure, and the keyboard type by setting the style to :custom
rmq.app.alert(title: "Text Field", message: "My style is :custom", style: :custom, fields:
{phone: {placeholder: 'Phone', keyboard_type: :phone_pad},
email: {placeholder: 'Email', secure: false, keyboard_type: :email_address}}) do |action_type, fields|
puts "you entered '#{fields[:phone].text}' and '#{fields[:email].text}'"
end

# Utilize common templates
rmq.app.alert(message: "Would you like a sandwich?", actions: :yes_no_cancel, style: :sheet) do |action_type|
case action_type
Expand All @@ -74,7 +86,7 @@ end
```


You can even use the `make_button` helper to create custom UIAction buttons to add:
You can even use the `make_button` helper to create custom buttons to add:
```ruby
# Use custom UIAction buttons and add them
taco = rmq.app.make_button("Taco") {
Expand All @@ -89,12 +101,18 @@ You can even use the `make_button` helper to create custom UIAction buttons to a

## Available Templates

Templates are provided [HERE](https://github.com/GantMan/RedAlert/blob/master/lib/project/button_templates.rb)
Button templates are provided [HERE](https://github.com/GantMan/RedAlert/blob/master/lib/project/button_templates.rb)
* `:yes_no` = Simple yes and no buttons.
* `:yes_no_cancel` = Yes/no buttons with a separated cancel button.
* `:ok_cancel` = OK button with a separated cancel button.
* `:delete_cancel` = Delete button (red) with a separated cancel button.

Field templates are provided [HERE](https://github.com/GantMan/RedAlert/blob/master/lib/project/field_templates.rb)
* `:input` = One plaintext input field.
* `:secure` = One secure input field.
* `:login` = Two fields, one plaintext with placeholder text 'Login' and the other secure with placeholder text 'Password'.
* `:change_password` = Two fields, one secure with placeholder text 'Current Password' and the other secure with placeholder text 'New Password'.

:heartbeat: _More to come:_ be sure to submit a pull-request with your button template needs.


Expand All @@ -113,7 +131,7 @@ Because capabilities of iOS 7 & 8 alert-components are different, just a few edg
* `UIAlertView` cares about the order of your `:cancel` actions, so `[:ok, :cancel]` is shown different than `[:cancel, :ok]`.
* `UIActionSheet` also cares about the order. It's possible to put a `:cancel` first, which looks slightly awkward when shown. Try to put `:cancel` last.
* `UIAlertView`'s `alertViewStyles` are not available through RedAlert as they aren't compatible with iOS 8. You'll have to call that directly.

* `UIAlertView only supports up to 2 input fields.`

## Credits and Info

Expand Down
41 changes: 41 additions & 0 deletions app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,47 @@ def viewDidLoad
end
end

##############################
# Field examples
##############################

# Text field example with :input style.
acs.append(UIButton, :alert_controller_fields_one).on(:tap) do
rmq.app.alert(title: "Text Field", message: "My style is :input", style: :input) do |action_type, fields|
puts "you entered '#{fields[:text].text}"
end
end

# Text field example with :secure style.
acs.append(UIButton, :alert_controller_fields_two).on(:tap) do
rmq.app.alert(title: "Text Field", message: "My style is :secure", style: :secure) do |action_type, fields|
puts "you entered '#{fields[:text].text}'"
end
end

# Text field example with :login style.
acs.append(UIButton, :alert_controller_fields_three).on(:tap) do
rmq.app.alert(title: "Text Field", message: "My style is :login", style: :login) do |action_type, fields|
puts "you entered '#{fields[:login].text}' as the login and '#{fields[:password].text}' as the password"
end
end

# Text field example with :change_password style.
acs.append(UIButton, :alert_controller_fields_four).on(:tap) do
rmq.app.alert(title: "Text Field", message: "My style is :change_password", style: :change_password) do |action_type, fields|
puts "you entered '#{fields[:current_password].text}' as the current password and '#{fields[:new_password].text}' as the new password"
end
end

# Text field example with :custom style.
acs.append(UIButton, :alert_controller_fields_five).on(:tap) do
rmq.app.alert(title: "Text Field", message: "My style is :custom", style: :custom, fields:
{phone: {placeholder: 'Phone', keyboard_type: :phone_pad},
email: {placeholder: 'Email', secure: false, keyboard_type: :email_address}}) do |action_type, fields|
puts "you entered '#{fields[:phone].text}' and '#{fields[:email].text}'"
end
end

##############################
# Multiple button examples
##############################
Expand Down
25 changes: 25 additions & 0 deletions app/stylesheets/main_stylesheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@ def alert_controller_four st
st.text = "Title and :sheet Style"
end

def alert_controller_fields_one st
basic_button(st)
st.text = "Fields :input Style"
end

def alert_controller_fields_two st
basic_button(st)
st.text = "Fields :secure Style"
end

def alert_controller_fields_three st
basic_button(st)
st.text = "Fields :login Style"
end

def alert_controller_fields_four st
basic_button(st)
st.text = "Fields :change_password Style"
end

def alert_controller_fields_five st
basic_button(st)
st.text = "Fields :custom Style"
end

def custom_actions_helper_alert st
basic_button(st)
st.text = "Custom Alert Actions"
Expand Down
4 changes: 2 additions & 2 deletions lib/project/action_sheet_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ActionSheetProvider

attr_reader :action_sheet

def build(actions, opts={})
def build(actions, fieldset=nil, opts={})
raise ArgumentError.new "At least 1 action is required." unless actions && actions.length > 0
@actions = actions
@opts = opts
Expand Down Expand Up @@ -53,7 +53,7 @@ def actionSheet(actionSheet, didDismissWithButtonIndex:buttonIndex)
# pull from the view controller instance
@view_controller.dismissViewControllerAnimated @opts[:animated], completion: nil
action = @actions_in_display_order[buttonIndex]
action.handler.call(action.tag) if action.handler
action.handler.call(action.tag, nil) if action.handler
@view_controller = nil # forget the reference
end

Expand Down
21 changes: 18 additions & 3 deletions lib/project/alert_controller_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class AlertControllerProvider

attr_reader :alert_controller

def build(actions, opts={})
def build(actions, fieldset = nil, opts={})
raise ArgumentError.new("At least 1 action is required.") unless actions && actions.length > 0
@actions = actions
@opts = opts
Expand All @@ -14,6 +14,21 @@ def build(actions, opts={})
style = RubyMotionQuery::AlertConstants::ALERT_TYPES[@opts[:style]]
@alert_controller = UIAlertController.alertControllerWithTitle @opts[:title], message:@opts[:message], preferredStyle: style

# add our text fields and build up the callback hash
@text_fields = {}
if fieldset
fieldset[:fields].each_with_index do |field, index|
handler = lambda do |text_field|
text_field.placeholder = field.placeholder
text_field.secureTextEntry = field.secure_text_entry
text_field.keyboardType = RubyMotionQuery::Stylers::KEYBOARD_TYPES[field.keyboard_type]
end
@alert_controller.addTextFieldWithConfigurationHandler(handler)
@text_fields[field.name] = @alert_controller.textFields[index]
end
end


# load up the UIAlertController's actions
@actions.each do |alert_action|

Expand All @@ -22,10 +37,10 @@ def build(actions, opts={})

# convert the callback
handler = lambda do |action|
alert_action.handler.call(alert_action.tag) unless alert_action.handler.nil?
alert_action.handler.call(alert_action.tag, @text_fields) unless alert_action.handler.nil?
end if alert_action.handler

# create teh action
# create the action
action = UIAlertAction.actionWithTitle alert_action.title, style: ios_style, handler: handler

# add it to the UIAlertController
Expand Down
18 changes: 18 additions & 0 deletions lib/project/alert_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module RubyMotionQuery
class AlertField

attr_accessor :keyboard_type
attr_accessor :placeholder
attr_accessor :secure_text_entry
attr_accessor :name

def initialize(name, opts = {})
raise ArgumentError.new "A name parameter must be provided" unless name && name.length > 0
opts = {placeholder: opts, keyboard_type: :default} if opts.is_a? String
@name = name.is_a?(Symbol) ? name : name.strip.gsub(/\s+/,'_').to_sym
@keyboard_type = RubyMotionQuery::Stylers::KEYBOARD_TYPES.has_key?(opts[:keyboard_type]) ? opts[:keyboard_type] : :default
@placeholder = opts[:placeholder] || ''
@secure_text_entry = opts[:secure_text_entry] || false
end
end
end
18 changes: 16 additions & 2 deletions lib/project/alert_view_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class AlertViewProvider

attr_accessor :alert_view

def build(actions, opts={})
def build(actions, fieldset=nil, opts={})
raise ArgumentError.new("At least 1 action is required.") unless actions && actions.length > 0
@actions = actions
@opts = opts
Expand All @@ -26,6 +26,20 @@ def build(actions, opts={})
# mark where our special buttons are
@alert_view.cancelButtonIndex = @actions_in_display_order.length-1 if cancel_action

# add our text fields and build up the callback hash
@text_fields = {}

if fieldset
@alert_view.alertViewStyle = fieldset[:alert_view_style]
fieldset[:fields].each_with_index do |field, index|
text_field = @alert_view.textFieldAtIndex index
text_field.placeholder = field.placeholder
text_field.secureTextEntry = field.secure_text_entry
text_field.keyboardType = RubyMotionQuery::Stylers::KEYBOARD_TYPES[field.keyboard_type]
@text_fields[field.name] = text_field
end
end

self
end

Expand All @@ -41,7 +55,7 @@ def show
def alertView(alertView, didDismissWithButtonIndex:buttonIndex)
@view_controller.dismissViewControllerAnimated @opts[:animated], completion: nil
action = @actions_in_display_order[buttonIndex]
action.handler.call(action.tag) if action.handler
action.handler.call(action.tag, @text_fields) if action.handler
@view_controller = nil # forget the reference
end

Expand Down
48 changes: 48 additions & 0 deletions lib/project/field_templates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module RubyMotionQuery
class App

# When adding a new template do the following:
# 1 - Add it here
# 2 - Add the test
# 3 - Add symbol to the README.md list

def self.add_template_fieldset(template)

login = NSLocalizedString("Login", nil)
password = NSLocalizedString("Password", nil)
current_password = NSLocalizedString("Current Password", nil)
new_password = NSLocalizedString("New Password", nil)

fieldset = {alert_view_style: UIAlertViewStyleDefault, fields: [] }
case template
when :input
fieldset[:alert_view_style] = UIAlertViewStylePlainTextInput
fieldset[:fields] =
[
rmq.app.make_field(:text, keyboard_type: :default, secure_text_entry: false, placeholder: '')
]
when :secure
fieldset[:alert_view_style] = UIAlertViewStyleSecureTextInput
fieldset[:fields] =
[
rmq.app.make_field(:text, keyboard_type: :default, secure_text_entry: true, placeholder: '')
]
when :login
fieldset[:alert_view_style] = UIAlertViewStyleLoginAndPasswordInput
fieldset[:fields] =
[
rmq.app.make_field(:login, keyboard_type: :email_address, secure_text_entry: false, placeholder: login),
rmq.app.make_field(:password, keyboard_type: :default, secure_text_entry: true, placeholder: password)
]
when :change_password
fieldset[:alert_view_style] = UIAlertViewStyleLoginAndPasswordInput
fieldset[:fields] =
[
rmq.app.make_field(:current_password, keyboard_type: :default, secure_text_entry: true, placeholder: current_password),
rmq.app.make_field(:new_password, keyboard_type: :default, secure_text_entry: true, placeholder: new_password)
]
end
fieldset
end
end
end
Loading