Class: AlertDialog

Inherits:
Android::App::DialogFragment
  • Object
show all
Defined in:
lib/project/alert_dialog/alert_dialog.rb

Overview

Generic AlertDialog

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ AlertDialog

Returns a new instance of AlertDialog.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/project/alert_dialog/alert_dialog.rb', line 21

def initialize(options={}, &block)

  # Defaults
  @options = {
    theme: Android::App::AlertDialog::THEME_HOLO_LIGHT,
    title: "Alert!",
    message: nil,
    positive_button: "OK",
    negative_button: "Cancel",
    positive_button_handler: self,
    negative_button_handler: self,
    style: nil,
    show: true
  }.merge(options)

  @callback = block

  self.show if @options[:show]
  self
end

Instance Method Details

#onClick(dialog, id) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/project/alert_dialog/alert_dialog.rb', line 74

def onClick(dialog, id)
  button_text = (id == Android::App::AlertDialog::BUTTON_POSITIVE) ? @options[:positive_button] : @options[:negative_button]

  # if a text_view is present, grab what the user gave us
  text_view = @text_view_id && dialog.findViewById(@text_view_id)
  input_text = text_view ? text_view.text.toString : nil

  @callback.call(button_text, input_text) if @callback
end

#onCreateDialog(saved_instance_state) ⇒ Object



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

def onCreateDialog(saved_instance_state)
  builder = Android::App::AlertDialog::Builder.new(activity, @options[:theme])

  builder.title = @options[:title]
  builder.message = @options[:message]

  # Add buttons if they are set
  builder.setPositiveButton(@options[:positive_button], @options[:positive_button_handler]) if @options[:positive_button]
  builder.setNegativeButton(@options[:negative_button], @options[:negative_button_handler]) if @options[:negative_button]

  # Add custom view?
  @options[:view] = simple_text_view if @options[:style] == :input
  builder.view = @options[:view] if @options[:view]

  # DONE!
  builder.create
end

#show(activity = rmq.activity) ⇒ Object



42
43
44
# File 'lib/project/alert_dialog/alert_dialog.rb', line 42

def show(activity=rmq.activity)
  super(activity.fragmentManager, "alert_dialog")
end

#simple_text_viewObject



64
65
66
67
68
69
70
71
72
# File 'lib/project/alert_dialog/alert_dialog.rb', line 64

def simple_text_view
  # Set up the input
  input = Potion::EditText.new(activity)
  input.singleLine = true
  input.id = @text_view_id = Potion::ViewIdGenerator.generate
  # possible input types - future feature
  #input.inputType = (Android::Text::InputType.TYPE_CLASS_TEXT | Android::Text::InputType.TYPE_TEXT_VARIATION_PASSWORD)
  input
end