Skip to content
Closed
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ You can even use the `make_button` helper to create custom buttons to add:
rmq.app.alert(title: "Actions!", message: "Actions created with `make_button` helper.", actions: button_list)
```


To present your alert from a popover, you can pass a `:popover` option and provide the source (either a UIView or a UIBarButtonItem)
```ruby
rmq.append(UIButton, :my_button).on(:tap) do |sender|
rmq.app.alert(title: "Actions!", message: "Alert from a Popover.", actions: [:ok, :cancel], popover: sender)
end
```
You can also provide a :modal option if you want the popover to be modal

## Available Templates

Button templates are provided [HERE](https://github.com/GantMan/RedAlert/blob/master/lib/project/button_templates.rb)
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Motion::Project::App.setup do |app|
app.identifier = 'com.gantlaborde.red_alert'
app.name = 'RedAlert'
app.deployment_target = ENV["DEPLOYMENT_TARGET"] if ENV["DEPLOYMENT_TARGET"]
app.device_family = [:iphone, :ipad]
end
24 changes: 18 additions & 6 deletions app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def viewDidLoad
end

# Sheet with no message
acs.append(UIButton, :alert_controller_five).on(:tap) do
acs.append(UIButton, :alert_controller_no_message).on(:tap) do
rmq.app.alert(message: nil, style: :sheet, actions: :yes_no_cancel ) do |action_type|
puts "you clicked #{action_type}"
end
Expand All @@ -59,33 +59,40 @@ def viewDidLoad
# 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}"
puts "you entered '#{fields[:text].text}'"
end
end

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

# Text field example with :secure style.
acs.append(UIButton, :alert_controller_fields_three).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
acs.append(UIButton, :alert_controller_fields_four).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
acs.append(UIButton, :alert_controller_fields_five).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
acs.append(UIButton, :alert_controller_fields_six).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|
Expand Down Expand Up @@ -144,6 +151,11 @@ def viewDidLoad
rmq.app.alert(title: "Actions!", message: "Actions created with `make_button` helper.", actions: button_list, style: :sheet)
end

# Alert from popover
acs.append(UIButton, :alert_from_popover).on(:tap) do |sender|
rmq.app.alert(title: "Popover", message: "Presented from popover (if iPad)", actions: [:ok], popover: sender)
end

acs.append(UILabel, :template_tour)


Expand Down
18 changes: 14 additions & 4 deletions app/stylesheets/main_stylesheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def alert_controller_four st
st.text = "Title and :sheet Style"
end

def alert_controller_five st
def alert_controller_no_message st
basic_button(st)
st.text = ":sheet Style with no message"
end
Expand All @@ -80,20 +80,25 @@ def alert_controller_fields_one st

def alert_controller_fields_two st
basic_button(st)
st.text = "Fields :secure Style"
st.text = "Fields :input w/ placeholder"
end

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

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

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

def alert_controller_fields_six st
basic_button(st)
st.text = "Fields :custom Style"
end
Expand All @@ -108,6 +113,11 @@ def custom_actions_helper_sheet st
st.text = "Custom Sheet Actions"
end

def alert_from_popover st
basic_button(st)
st.text = "Alert From Popover on iPad"
end

def usage_tour st
st.frame = {bp: 10, w: screen_width - 20, l: 10, h:20}
st.clips_to_bounds = false
Expand Down
12 changes: 11 additions & 1 deletion lib/project/action_sheet_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,17 @@ def build(actions, fieldset=nil, opts={})
def show
# when we show, the view controller will disappear because a different _UIAlertOverlayWindow window will take its place
@view_controller = rmq.view_controller
@action_sheet.showInView(@view_controller.view)

if @opts[:popover] and rmq.device.ipad?
source = @opts[:popover]
if source.is_a?(UIBarButtonItem)
@action_sheet.showFromBarButtonItem(source, animated: true)
else
@action_sheet.showFromRect(source.frame, inView: @view_controller.view, animated: true)
end
else
@action_sheet.showInView(@view_controller.view)
end
end

private
Expand Down
15 changes: 15 additions & 0 deletions lib/project/alert_controller_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ def build(actions, fieldset = nil, opts={})
@alert_controller.addAction action
end

# popover
if @opts[:popover] and rmq.device.ipad?
source = @opts[:popover]
@alert_controller.setModalPresentationStyle(UIModalPresentationPopover)
if @opts[:modal]
@alert_controller.setModalInPopover(true)
end
if source.is_a?(UIBarButtonItem)
@alert_controller.popoverPresentationController.barButtonItem = source
else
@alert_controller.popoverPresentationController.sourceView = source
end
@alert_controller.popoverPresentationController.sourceRect = source.bounds
end

self
end

Expand Down
6 changes: 3 additions & 3 deletions lib/project/field_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class App
# 2 - Add the test
# 3 - Add symbol to the README.md list

def self.add_template_fieldset(template)
def self.add_template_fieldset(template, opts={})

login = NSLocalizedString("Login", nil)
password = NSLocalizedString("Password", nil)
Expand All @@ -19,13 +19,13 @@ def self.add_template_fieldset(template)
fieldset[:alert_view_style] = UIAlertViewStylePlainTextInput
fieldset[:fields] =
[
rmq.app.make_field(:text, keyboard_type: :default, secure_text_entry: false, placeholder: '')
rmq.app.make_field(:text, keyboard_type: :default, secure_text_entry: false, placeholder: opts.fetch(:placeholder,''))
]
when :secure
fieldset[:alert_view_style] = UIAlertViewStyleSecureTextInput
fieldset[:fields] =
[
rmq.app.make_field(:text, keyboard_type: :default, secure_text_entry: true, placeholder: '')
rmq.app.make_field(:text, keyboard_type: :default, secure_text_entry: true, placeholder: opts.fetch(:placeholder,''))
]
when :login
fieldset[:alert_view_style] = UIAlertViewStyleLoginAndPasswordInput
Expand Down
9 changes: 6 additions & 3 deletions lib/project/red_alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ def alert(opts={}, &block)
# ------------------------------------
opts = {message: opts} if opts.is_a? String
opts = {style: :alert, animated: true, show_now: true}.merge(opts)

# Ability to make no message
opts[:message] = if opts.has_key?(:message)
opts[:message].nil? ? nil : opts[:message].to_s
else
NSLocalizedString("Alert!", nil)
end

opts[:style] = VALID_STYLES.include?(opts[:style]) ? opts[:style] : :alert
style = VALID_STYLES.include?(opts[:style]) ? opts[:style] : :alert
# force the style to :sheet if :popover on ipad (needed for popover)
style = :sheet if opts[:popover] and rmq.device.ipad?
opts[:style] = style
opts[:fields] = opts[:style] == :custom && opts[:fields] ? opts[:fields] : {text: {placeholder: ''}}
api = rmq.device.ios_at_least?(8) ? :modern : :deprecated
api = :deprecated if rmq.device.ios_at_least?(8) && opts[:api] == :deprecated
Expand All @@ -53,7 +56,7 @@ def alert(opts={}, &block)
fieldset = {alert_view_style: UIAlertViewStyleDefault, fields: []}

if TEMPLATE_FIELD_STYLES.include?(opts[:style])
fieldset = add_template_fieldset(opts[:style])
fieldset = add_template_fieldset(opts[:style], opts)
elsif opts[:style] == :custom
fieldset = custom_fieldset(opts[:api], opts[:fields])
end
Expand Down
20 changes: 16 additions & 4 deletions spec/field_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
field.placeholder.should == ""
end

it "should have a placeholder when specified" do
a = RubyMotionQuery::App.add_template_fieldset(:input, {placeholder: "test placeholder"})
field = a[:fields].first
field.placeholder.should == "test placeholder"
end

end

describe ":secure" do
Expand All @@ -29,6 +35,12 @@
field.placeholder.should == ""
end

it "should have a placeholder when specified" do
a = RubyMotionQuery::App.add_template_fieldset(:secure, {placeholder: "secure test placeholder"})
field = a[:fields].first
field.placeholder.should == "secure test placeholder"
end

end

describe ":login" do
Expand All @@ -42,11 +54,11 @@
login.name.should == :login
login.keyboard_type.should == :email_address
login.secure_text_entry.should == false
login.placeholder.should == "Login"
login.placeholder.should == NSLocalizedString("Login", nil)
password.name.should == :password
password.keyboard_type.should == :default
password.secure_text_entry.should == true
password.placeholder.should == "Password"
password.placeholder.should == NSLocalizedString("Password", nil)
end

end
Expand All @@ -62,11 +74,11 @@
current_password.name.should == :current_password
current_password.keyboard_type.should == :default
current_password.secure_text_entry.should == true
current_password.placeholder.should == "Current Password"
current_password.placeholder.should == NSLocalizedString("Current Password", nil)
new_password.name.should == :new_password
new_password.keyboard_type.should == :default
new_password.secure_text_entry.should == true
new_password.placeholder.should == "New Password"
new_password.placeholder.should == NSLocalizedString("New Password", nil)
end

end
Expand Down