Class: RubyMotionQuery::AlertControllerProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/project/alert_controller_provider.rb

Overview

The new dual-purpose iOS 8 UIAlertControllerProvider

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#alert_controllerObject (readonly)

Returns the value of attribute alert_controller.



6
7
8
# File 'lib/project/alert_controller_provider.rb', line 6

def alert_controller
  @alert_controller
end

Instance Method Details

#build(actions, fieldset = nil, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/project/alert_controller_provider.rb', line 8

def build(actions, fieldset = nil, opts={})
  raise ArgumentError.new("At least 1 action is required.") unless actions && actions.length > 0
  @actions = actions
  @opts = opts

  # create our alert controller
  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 if field.placeholder.is_a? String
        text_field.secureTextEntry = field.secure_text_entry if field.secure_text_entry.is_a? String
        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|

    # convert the style
    ios_style = RubyMotionQuery::AlertConstants::ALERT_ACTION_STYLE[alert_action.style]

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

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

    # add it to the UIAlertController
    @alert_controller.addAction action
  end

  # popover
  if @opts[:style] == :sheet and rmq.device.ipad?
    raise ArgumentError.new "Please provide a :source view to use :sheet on iPad" unless @opts[:source]
    source = @opts[:source]
    @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

    if @opts[:arrow_direction]
      directions = @opts[:arrow_direction]
      unless directions.is_a?(Array)
        directions = [directions]
      end
      arrow_direction = 0
      directions.each do |direction|
        arrow_direction |= RubyMotionQuery::AlertConstants::ALERT_POPOVER_ARROW_DIRECTION[direction]
      end
      @alert_controller.popoverPresentationController.permittedArrowDirections = arrow_direction
    end
  end

  self
end

#showObject



81
82
83
84
# File 'lib/project/alert_controller_provider.rb', line 81

def show
  view_controller = rmq.view_controller.childModalViewController || rmq.view_controller
  view_controller.presentViewController(@alert_controller, animated: @opts[:animated], completion: nil)
end