Class: RubyMotionQuery::App

Inherits:
Object
  • Object
show all
Defined in:
lib/project/red_alert.rb,
lib/project/core_alert.rb,
lib/project/classic_alert.rb,
lib/project/button_templates.rb

Class Method Summary collapse

Class Method Details

.add_template_actions(uiac, template, &block) ⇒ Object

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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/project/button_templates.rb', line 8

def self.add_template_actions(uiac, template, &block)
  case template
  when :yes_no
    uiac.addAction(rmq.app.make_button("Yes", &block))
    uiac.addAction(rmq.app.make_button("No", &block))
  when :yes_no_cancel
    uiac.addAction(rmq.app.make_button("Yes", &block))
    uiac.addAction(rmq.app.make_button("No", &block))
    uiac.addAction(rmq.app.make_button({title: "Cancel", style: :cancel}, &block))
  when :ok_cancel
    uiac.addAction(rmq.app.make_button("OK", &block))
    uiac.addAction(rmq.app.make_button({title: "Cancel", style: :cancel}, &block))
  when :delete_cancel
    uiac.addAction(rmq.app.make_button({title: "Delete", style: :destructive}, &block))
    uiac.addAction(rmq.app.make_button({title: "Cancel", style: :cancel}, &block))
  end
end

.alert(opts = {}, &block) ⇒ UIAlertController

Creates and shows the UIAlertController. Usage Example:

rmq.app.alert(message: "This is a test")
rmq.app.alert(title: "Hey there", message: "Are you happy?")

Returns:

  • (UIAlertController)

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
# File 'lib/project/red_alert.rb', line 10

def alert(opts = {}, &block)
  # Shortcut: assume a string is the message
  opts = {message: opts} if opts.is_a? String

  # An alert is nothing without a message
  raise(ArgumentError, "RedAlert alert requires a message") if RubyMotionQuery::RMQ.is_blank?(opts[:message])
  # iOS8 and above only for UIAlertController
  raise "RedAlert requires iOS8 for alerts.  Please try `rmq.app.alert_view`" unless rmq.device.ios_at_least? 8
  core_alert(opts, &block)
end

.alert_view(opts = {}) ⇒ UIAlertView

Creates and shows the old UIAlertView. Added here for use in fallback. Fallback won’t run actions, but the old system needed delegates anyhow. Usage Example:

rmq.app.alert_view("This is a test")
rmq.app.alert_view(title: "Hey there", message: "Enjoying this?")

Returns:

  • (UIAlertView)

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/project/classic_alert.rb', line 10

def self.alert_view(opts = {})

  # shortcut sending a string
  opts = {message: opts} if opts.is_a? String
  # An alert is nothing without a message
  raise(ArgumentError, "RedAlert requires a message") if opts[:message].nil? || opts[:message].empty?

  opts = {
    title: "Alert!",
    cancel_button: "OK",
    other_buttons: [],
    delegate: nil,
    view_style: UIAlertViewStyleDefault,
    show_now: true,
  }.merge(opts)

  alert_view = UIAlertView.alloc.initWithTitle(
    opts[:title],
    message: opts[:message],
    delegate: opts[:delegate],
    cancelButtonTitle: opts[:cancel_button],
    otherButtonTitles: nil
  )
  Array(opts[:other_buttons]).each { |button| alert_view.addButtonWithTitle(button) }

  alert_view.alertViewStyle = opts[:view_style]

  alert_view.show if opts[:show_now]
  alert_view
end

.core_alert(opts = {}, &block) ⇒ Object

UIAlertController Magic



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/project/core_alert.rb', line 6

def core_alert(opts = {}, &block)

  opts = {
    title: "Alert!",
    style: :alert,
    actions: [make_button(&block)],
    animated: true,
    show_now: true,
  }.merge(opts)
  style = RubyMotionQuery::AlertConstants::ALERT_TYPES[opts[:style]] || opts[:style]

  ac = UIAlertController.alertControllerWithTitle(opts[:title], message:opts[:message], preferredStyle: style)

  # Add correct actions (buttons) to the UIAC
  if opts[:actions].is_a? Symbol
    template_symbol = opts[:actions]
    add_template_actions(ac, template_symbol, &block)
  elsif opts[:actions].is_a? Array
    opts[:actions].each do |action|
      if action.is_a? UIAlertAction
        ac.addAction(action)
      else
        raise ArgumentError, "RedAlert actions array must contain UIAlertAction objects"
      end
    end
  else
    raise ArgumentError, "RedAlert actions parameter must be an Array or a Template symbol"
  end

  # Present it, if that's what we want
  rmq.view_controller.presentViewController(ac, animated: opts[:animated], completion: nil) if opts[:show_now]

  # return controller (should I wrap it in RMQ?)
  ac
end

.make_button(opts = {}, &block) ⇒ UIAlertAction

Returns a UIAlertAction from given parameters Usage Example:

yes = rmq.make_button("Yes")
cancel = rmq.make_button(title: "Cancel", style: :cancel) {
   puts "Cancel pressed"
}

Returns:

  • (UIAlertAction)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/project/red_alert.rb', line 28

def make_button (opts = {}, &block)
  # shortcut sending a string
  opts = {title: opts} if opts.is_a? String

  opts = {
    title: "OK",
    style: :default,
  }.merge(opts)

  style = RubyMotionQuery::AlertConstants::ALERT_ACTION_STYLE[opts[:style]] || opts[:style]

  UIAlertAction.actionWithTitle(opts[:title], style: style, handler: -> (action) {
    title_symbol = action.title.gsub(/\s+/,"_").downcase.to_sym
    block.call(title_symbol) unless block.nil?
  })
end