motion-alert 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b501540738a246105b52baa7b52baf7271be567d
4
- data.tar.gz: 1a82b0b367074926040b30ce2e20889109ddeb97
3
+ metadata.gz: 97f3260713af60d3f73c7a450284d7e66858772f
4
+ data.tar.gz: 5a1e3d1a3b6f53d0d93bb20f4325cbe0d37f89cf
5
5
  SHA512:
6
- metadata.gz: 5ed39451a5e97e523fef53636c5587c494a9a6f962c9d2a06dbb98e867af3c5918fdd18b297b0fa9997ac9c8340e1b6f1d807ee2640afd1701daffeb70273cf4
7
- data.tar.gz: 8475263610ca21df2e3a04e0a42ca5687c8c9017533a82edd99efc17c153924e9b01e19ba78887e2273d5d011931c4a84399aed98f696915eadc70c8e750e9df
6
+ metadata.gz: a5e076597768c17a51c4b9c6ac31f9ef9660e50273a6fb254478ada281e90dbc66c089ef7ced475178d66524f7ad574477f396d3ed9bae72594108e9d7561162
7
+ data.tar.gz: a168079b9e79c86cb35519da6ff17f209cfbc4a2cf49e677b9698355a7dfc52dd8df62801a1de463ed166887f4f784930e0f797c61fed6b10cb0d606d37cc162
@@ -4,4 +4,8 @@ class AppDelegate
4
4
  def alertView(alert_view, clickedButtonAtIndex: index)
5
5
  Motion::Alert.instance.selected(index)
6
6
  end
7
+
8
+ def actionSheet(action_sheet, clickedButtonAtIndex: index)
9
+ Motion::Alert.instance.selected(index)
10
+ end
7
11
  end
@@ -1,10 +1,11 @@
1
1
  module Motion
2
2
  class Action
3
- attr_accessor :title, :action
3
+ attr_accessor :title, :action, :style
4
4
 
5
- def initialize(title, action=nil)
6
- self.title = title
7
- self.action = action
5
+ def initialize(options = {})
6
+ self.title = options.fetch(:title, nil)
7
+ self.action = options.fetch(:action, nil)
8
+ self.style = options.fetch(:style, nil) || UIAlertActionStyleDefault
8
9
  end
9
10
 
10
11
  def choose
@@ -3,6 +3,7 @@ module Motion
3
3
  attr_accessor :title
4
4
  attr_accessor :message
5
5
  attr_accessor :actions
6
+ attr_accessor :type
6
7
 
7
8
  def self.instance
8
9
  Dispatch.once { @instance ||= alloc.init }
@@ -13,17 +14,13 @@ module Motion
13
14
  instance.tap do |i|
14
15
  i.title = options.fetch(:title, nil)
15
16
  i.message = options.fetch(:message, nil)
17
+ i.type = options.fetch(:type, :alert)
16
18
  i.actions = Option.new
17
19
  end
18
20
  end
19
21
 
20
- def add_action(button_title, action_proc)
21
- puts "Deprecated: use actions.add will be removed in 0.3.0"
22
- actions.add(button_title, &action_proc)
23
- end
24
-
25
22
  def show
26
- show_as_controller || show_as_alert
23
+ show_as_controller || show_as_alert || show_as_action_sheet
27
24
  end
28
25
 
29
26
  def selected(index)
@@ -32,49 +29,61 @@ module Motion
32
29
 
33
30
  private
34
31
 
35
-
36
32
  def show_as_controller
37
33
  return nil if !can_show_controller?
38
34
 
39
35
  alert_controller.tap do |alert|
40
- self.actions.actions.each do |a|
41
- alert_action = UIAlertAction.actionWithTitle(
42
- a.title,
43
- style: UIAlertActionStyleDefault,
44
- handler: ->(arg) { a.action.call }
45
- )
46
- alert.addAction(alert_action)
47
- end
48
-
36
+ self.actions.attach_to(alert)
49
37
  root_controller.presentViewController(alert, animated: false, completion: nil)
50
38
  end
51
39
  end
52
40
 
53
41
  def show_as_alert
54
- alert_view.tap do |alert|
55
- self.actions.actions.each do |a|
56
- alert.addButtonWithTitle(a.title)
57
- end
58
- alert.show
42
+ if type == :alert
43
+ self.actions.attach_to(alert_view).show
59
44
  end
60
45
  end
61
46
 
47
+ def show_as_action_sheet
48
+ self.actions.attach_to(action_sheet).showInView(root_controller.view)
49
+ end
50
+
62
51
  def alert_controller
63
52
  UIAlertController.alertControllerWithTitle(
64
53
  title,
65
54
  message: message,
66
- preferredStyle: UIAlertControllerStyleAlert
55
+ preferredStyle: style
67
56
  )
68
57
  end
69
58
 
59
+ def style
60
+ case type
61
+ when :alert
62
+ UIAlertControllerStyleAlert
63
+ when :action_sheet
64
+ UIAlertControllerStyleActionSheet
65
+ else
66
+ UIAlertControllerStyleAlert
67
+ end
68
+ end
69
+
70
70
  def alert_view
71
71
  UIAlertView.alloc.initWithTitle(
72
72
  title,
73
73
  message: message,
74
74
  delegate: UIApplication.sharedApplication.delegate,
75
75
  cancelButtonTitle: nil,
76
- otherButtonTitles:
77
- nil
76
+ otherButtonTitles: nil
77
+ )
78
+ end
79
+
80
+ def action_sheet
81
+ UIActionSheet.alloc.initWithTitle(
82
+ title,
83
+ delegate: UIApplication.sharedApplication.delegate,
84
+ cancelButtonTitle: nil,
85
+ destructiveButtonTitle: nil,
86
+ otherButtonTitles: nil
78
87
  )
79
88
  end
80
89
 
@@ -1,12 +1,14 @@
1
1
  module Motion
2
2
  class Option
3
3
  attr_accessor :actions
4
+
4
5
  def initialize
5
6
  self.actions = []
7
+ self
6
8
  end
7
9
 
8
- def add(text, &block)
9
- actions << Action.new(text, block)
10
+ def add(text, style=nil, &block)
11
+ actions << Action.new(title: text, style: style, action: block)
10
12
  end
11
13
 
12
14
  def [](index)
@@ -20,5 +22,35 @@ module Motion
20
22
  def first
21
23
  actions.first
22
24
  end
25
+
26
+ def each
27
+ actions.each
28
+ end
29
+
30
+ def attach_to(view)
31
+ case view
32
+ when UIAlertView, UIActionSheet
33
+ actions.each_with_index do |action, index|
34
+ view.addButtonWithTitle(action.title)
35
+ case action.style
36
+ when UIAlertActionStyleCancel
37
+ view.cancelButtonIndex = index
38
+ when UIAlertActionStyleDestructive
39
+ view.destructiveButtonIndex = index
40
+ end
41
+ end
42
+ when UIAlertController
43
+ actions.each do |a|
44
+ alert_action = UIAlertAction.actionWithTitle(
45
+ a.title,
46
+ style: a.style,
47
+ handler: ->(arg) { a.action ? a.action.call : nil }
48
+ )
49
+ view.addAction(alert_action)
50
+ end
51
+ end
52
+
53
+ view
54
+ end
23
55
  end
24
56
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-alert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Larrabee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-05 00:00:00.000000000 Z
11
+ date: 2015-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake