Skip to content

Commit

Permalink
Fixed some bugs with custom events. Added more tests to events. Added…
Browse files Browse the repository at this point in the history
… custom_event? and sdk_event? to a RubyMotionQuery::Event. Do not set userInteractionEnabled to true on custom events.
  • Loading branch information
twerth committed Jul 10, 2015
1 parent 0606d93 commit 0688580
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
5 changes: 5 additions & 0 deletions app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def viewDidLoad
init_popup_section
init_validation_section

rmq(UIView).on(:custom_event) do |sender|
puts sender.get.class.name
end

rmq.append Section
end

Expand All @@ -36,6 +40,7 @@ def init_nav
end

def nav_left_button
rmq.all.trigger(:custom_event)
puts 'Left button'
end

Expand Down
18 changes: 15 additions & 3 deletions motion/ruby_motion_query/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ def initialize(sender, event, block)
return ValidationEvent.new(block)
elsif @sdk_event_or_recognizer = VIEW_GESTURES[event]
@gesture = true
elsif sender.is_a?(UIControl)
@custom_event = false
elsif sender.is_a?(UIControl) && (@sdk_event_or_recognizer = CONTROL_EVENTS[event])
@gesture = false
@sdk_event_or_recognizer = CONTROL_EVENTS[event]
@custom_event = false
else
@gesture = false
@sdk_event_or_recognizer = nil
@custom_event = true
end

@sender = sender
Expand Down Expand Up @@ -76,7 +81,6 @@ def set_options(opts)
when :swipe_right then o.direction = UISwipeGestureRecognizerDirectionRight
end


if opts.include?(:init)
opts[:init].call(@recognizer)
end
Expand All @@ -88,6 +92,14 @@ def gesture?
@gesture
end

def sdk_event?
!@custom_event && !@gesture
end

def custom_event?
@custom_event
end

def location
if gesture?
@recognizer.locationInView(@sender)
Expand Down
4 changes: 3 additions & 1 deletion motion/ruby_motion_query/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ def on(view, event, args = {}, &block)
raise "[RMQ Error] Event already exists on this object: #{event}. Remove first, using .off" if @event_set[event]

if rmqe = event_instance(view, event, block)
view.userInteractionEnabled = true
if rmqe.is_a?(RubyMotionQuery::Event) && !rmqe.custom_event?
view.userInteractionEnabled = true
end

rmqe.set_options(args) if rmqe.respond_to? :set_options

Expand Down
31 changes: 28 additions & 3 deletions spec/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def dealloc
q.on(:touch) do |sender, event|
sender.should == q
event.is_a?(RubyMotionQuery::Event).should == true
event.gesture?.should == false
event.sdk_event?.should == true
event.custom_event?.should == false
end
q.get.allTargets.count.should == 1
# TODO fire the event
Expand All @@ -55,8 +58,13 @@ def dealloc
end

it 'should add gesture on a view' do
# TODO dup the events above and do gestures, minor test
1.should == 1
view = @vc.rmq.append!(UILabel)
view.rmq.on(:tap) do |sender_q, rmq_event|
sender_q.should == view.rmq
rmq_event.gesture?.should == true
rmq_event.sdk_event?.should == false
rmq_event.custom_event?.should == false
end
end

it 'should add gesture on multiple views' do
Expand Down Expand Up @@ -142,6 +150,15 @@ def dealloc
should.not.raise { @vc.rmq(view).on(:custom) {|o| ;} }
end

it 'should not be a gesture or sdk_event, but be custom_event when adding a custom event' do
@vc.rmq.append(UILabel).on(:foo) do |sender_q, rmq_event|
rmq_event.gesture?.should == false
rmq_event.sdk_event?.should == false
rmq_event.custom_event?.should == true
sender_q.get.allTargets.count.should == 0
end
end

it 'should trigger the custom event' do
view = UIView.new
@vc.rmq.append(view)
Expand Down Expand Up @@ -176,10 +193,18 @@ def dealloc
end
end

@vc.rmq(view_1, view_2).trigger(:foo)
@vc.rmq.all.trigger(:foo)
view_1_triggerd.should == true
view_2_triggerd.should == true
end

it 'should not enable user interaction when adding a custom event' do
view_1 = @vc.rmq.append!(UILabel)

view_1.userInteractionEnabled?.should == false
@vc.rmq(view_1).on(:foo){|sender| ; }
view_1.userInteractionEnabled?.should == false
end
end

describe 'events' do
Expand Down

0 comments on commit 0688580

Please sign in to comment.