From e551377d5d5518eab557a7176597f4200fb82c89 Mon Sep 17 00:00:00 2001 From: Gant Date: Sat, 4 Apr 2015 14:40:01 -0500 Subject: [PATCH 1/3] code to hide keyboard from rmq.app added --- motion/ruby_motion_query/app.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/motion/ruby_motion_query/app.rb b/motion/ruby_motion_query/app.rb index 702927c..8f8e901 100644 --- a/motion/ruby_motion_query/app.rb +++ b/motion/ruby_motion_query/app.rb @@ -32,6 +32,19 @@ module RubyMotionQuery UIApplication.sharedApplication.delegate end + # @return [UIApplication] + def get + UIApplication.sharedApplication + end + + # Returns boolean of success of hiding + # @return [Boolean] + def hide_keyboard + self.get.sendAction(:resignFirstResponder, to:nil, from:nil, forEvent:nil) + end + alias :resign_responders :hide_keyboard + alias :end_editing :hide_keyboard + # @return [Symbol] Environment the app is running it def environment @_environment ||= RUBYMOTION_ENV.to_sym From c4508d542f1fd09aedbba5670cdd0add16e87b48 Mon Sep 17 00:00:00 2001 From: Gant Date: Sat, 4 Apr 2015 15:31:33 -0500 Subject: [PATCH 2/3] adding tests for :star: --- app/controllers/main_controller.rb | 15 ++++++++++++ motion/ruby_motion_query/app.rb | 1 + spec/controllers/main_controller.rb | 37 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 spec/controllers/main_controller.rb diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb index 089ceca..9033acc 100644 --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -1,4 +1,11 @@ class MainController < UIViewController + attr_accessor :keyboard_visible + + def viewWillAppear(animated) + # Adding these for tests + NSNotificationCenter.defaultCenter.addObserver(self, selector:'handleKeyboardDidShow:', name:UIKeyboardDidShowNotification, object:nil) + NSNotificationCenter.defaultCenter.addObserver(self, selector:'handleKeyboardWillHide:', name:UIKeyboardWillHideNotification, object:nil) + end def viewDidLoad super @@ -168,4 +175,12 @@ class MainController < UIViewController sender.resignFirstResponder true end + + def handleKeyboardDidShow(notification) + @keyboard_visible = true + end + + def handleKeyboardWillHide(notification) + @keyboard_visible = false + end end diff --git a/motion/ruby_motion_query/app.rb b/motion/ruby_motion_query/app.rb index 8f8e901..546ee0e 100644 --- a/motion/ruby_motion_query/app.rb +++ b/motion/ruby_motion_query/app.rb @@ -38,6 +38,7 @@ module RubyMotionQuery end # Returns boolean of success of hiding + # Tested in the example app! # @return [Boolean] def hide_keyboard self.get.sendAction(:resignFirstResponder, to:nil, from:nil, forEvent:nil) diff --git a/spec/controllers/main_controller.rb b/spec/controllers/main_controller.rb new file mode 100644 index 0000000..456605d --- /dev/null +++ b/spec/controllers/main_controller.rb @@ -0,0 +1,37 @@ +describe "MainController" do + tests MainController + + DELAY = 0.2 + # This line is needed if this test is ever run independently + UIView.setAnimationsEnabled false + + it 'starts with no keyboard shown' do + controller.keyboard_visible.nil?.should == true + controller.rmq.app.hide_keyboard.should == false + end + + it 'can hide the keyboard with rmq.app.hide_keyboard' do + # force keyboard to show + controller.rmq(:only_digits).get.becomeFirstResponder + wait DELAY do + controller.keyboard_visible.should == true + end + + # hide keyboard (have to wait for first delay to finish) + wait DELAY * 2 do + controller.rmq.app.hide_keyboard.should == true + wait DELAY do + controller.keyboard_visible.should == false + end + end + end + + + + it 'has aliases for rmq.app.hide_keyboard' do + rmq.app.respond_to?(:resign_responders).should == true + rmq.app.respond_to?(:end_editing).should == true + controller.rmq.app.resign_responders.should == false + controller.rmq.app.end_editing.should == false + end +end \ No newline at end of file From 06d3c1553040c7f4f815c9cba5d332d4e3811765 Mon Sep 17 00:00:00 2001 From: Gant Date: Sat, 4 Apr 2015 15:46:25 -0500 Subject: [PATCH 3/3] fix for specs - strange behavior --- spec/controllers/main_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/controllers/main_controller.rb b/spec/controllers/main_controller.rb index 456605d..6608ef0 100644 --- a/spec/controllers/main_controller.rb +++ b/spec/controllers/main_controller.rb @@ -2,8 +2,6 @@ describe "MainController" do tests MainController DELAY = 0.2 - # This line is needed if this test is ever run independently - UIView.setAnimationsEnabled false it 'starts with no keyboard shown' do controller.keyboard_visible.nil?.should == true @@ -11,6 +9,8 @@ describe "MainController" do end it 'can hide the keyboard with rmq.app.hide_keyboard' do + UIView.setAnimationsEnabled false + # force keyboard to show controller.rmq(:only_digits).get.becomeFirstResponder wait DELAY do