Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Created November 29, 2018 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/e606cc7fb646f171c683dd1eb82df952 to your computer and use it in GitHub Desktop.
Save amirrajan/e606cc7fb646f171c683dd1eb82df952 to your computer and use it in GitHub Desktop.
module ADR
class CustomAlertView
include Accessibility
def event_text
@event_text
end
def initialize(control_creator)
@modal_container = UIView.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@modal_container.backgroundColor = UIColor.blackColor.colorWithAlphaComponent(0.5)
mx, my, mw, mh = [modal_left, modal_top, modal_width, modal_height]
@main_container = UIView.alloc.initWithFrame(CGRectMake(mx, my, mw, mh))
@main_container.backgroundColor = UIColor.whiteColor
@main_container.layer.borderColor = UIColor.blackColor
@main_container.layer.borderWidth = 1.0
@modal_container.addSubview @main_container
@event_title = control_creator.label(10, 10, modal_width - 20, 40, text: "", font_size: 20)
@event_title.textColor = UIColor.blackColor
@event_title.textAlignment = UITextAlignmentCenter
@event_text = control_creator.label(10, 50, modal_width - 20, 150)
@event_text.textColor = UIColor.blackColor
@event_text.text = ""
@event_text.textAlignment = UITextAlignmentCenter
@main_container.addSubview(@event_title)
@main_container.addSubview(@event_text)
@buttons = []
@control_creator = control_creator
end
def modal_left
(device_width - modal_width) / 2
end
def modal_top
(device_height - modal_height) / 2
end
def modal_bottom
modal_top + modal_height
end
def device_width
UIScreen.mainScreen.bounds.size.width
end
def device_height
UIScreen.mainScreen.bounds.size.height
end
def modal_height
460
end
def modal_width
300
end
def initWithTitle(title, opts = { })
@title = title
@message = opts[:message]
@delegate = opts[:delegate]
@cancel_button_title = opts[:cancelButtonTitle]
@other_button_titles = opts[:otherButtonTitles] || []
@a11y = opts[:a11y]
@parent = opts[:parent]
end
def button_height
40
end
def button_padding
5
end
def from_bottom(index)
index += 1
(modal_bottom) - (button_padding * index) - (button_height * index)
end
def selected(index)
@delegate.alertView(self, clickedButtonAtIndex: index)
@modal_container.fade_out(0.1)
BubbleWrap::App.run_after(0.3) {
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, @a11y) if accessibility_enabled?
@modal_container.removeFromSuperview
}
end
def add_button(index, text)
button = @control_creator.game_button(text)
button.geometry modal_left + 10, from_bottom(index), modal_width - 20, button_height
button.setTitleColor(UIColor.blackColor, forState: UIControlStateNormal)
button.setTitleColor(bluish, forState: UIControlStateHighlighted)
button.when(UIControlEventTouchUpInside) do
selected(text)
end
@buttons << button
@modal_container.addSubview(button)
end
def show
@buttons.each { |b| b.removeFromSuperview }
@buttons.clear
@event_text.text = @message
@event_title.text = @title
@modal_container.hide
@parent.addSubview(@modal_container)
@parent.bringSubviewToFront(@modal_container)
cancel_button_added = false
if @cancel_button_title
add_button(0, @cancel_button_title)
cancel_button_added = true
end
offset = 0
offset = 1 if cancel_button_added
@other_button_titles.reverse.each_with_index do |v, i|
add_button(i + offset, v)
end
@modal_container.fade_in(0.1)
BubbleWrap::App.run_after(0.5) {
if accessibility_enabled?
control_notification = @event_text
if(@event_title.text != "")
control_notification = @event_title
end
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, control_notification)
end
}
end
def label x, y, width, height, options = { }
UILabel.alloc.initWithFrame(CGRectMake(x, y, width, height))
end
def bluish
return UIColor.colorWithRed(0.0/255.0,
green: 96.0/255.0,
blue: 255.0/255.0,
alpha: 1)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment