Class: RubyMotionQuery::ActionSheetProvider

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

Overview

Brings the “I’m not dead yet” iOS 7 UIActionSheet to RedAlert.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#action_sheetObject (readonly)

Returns the value of attribute action_sheet.



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

def action_sheet
  @action_sheet
end

Instance Method Details

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



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
# File 'lib/project/action_sheet_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

  raise ArgumentError.new "Please provide a :source view to use :sheet on iPad" if rmq.device.ipad? and !@opts[:source]

  # grab the first cancel action
  cancel_action = actions.find { |action| action.cancel? }

  # grab the first destructive action (UIActionSheet only supports one)
  destructive_action = actions.find { |action| action.destructive? }

  # let's put our actions in the correct display order for our callback
  @actions_in_display_order = actions.find_all { |action| action.default? }
  @actions_in_display_order << destructive_action if destructive_action
  @actions_in_display_order << cancel_action if cancel_action

  # create our action sheet
  @action_sheet = UIActionSheet.alloc.initWithTitle(
      @opts[:title] || @opts[:message],
      delegate: self,
      cancelButtonTitle: nil,
      destructiveButtonTitle: nil,
      otherButtonTitles: nil
      )

  # then append our other buttons in later
  @actions_in_display_order.each { |action| @action_sheet.addButtonWithTitle(action.title) }

  # mark where our special buttons are
  @action_sheet.destructiveButtonIndex = @actions_in_display_order.index { |action| action.destructive? } || -1
  @action_sheet.cancelButtonIndex = @actions_in_display_order.index { |action| action.cancel? } || -1

  self
end

#showObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/project/action_sheet_provider.rb', line 45

def show
  # when we show, the view controller will disappear because a different _UIAlertOverlayWindow window will take its place
  @view_controller = rmq.view_controller

  if rmq.device.ipad?
    source = @opts[:source]
    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