From 54c013c6d7b99a8ac4ab99fa4ca5353bf6da280a Mon Sep 17 00:00:00 2001 From: Matt Brewer Date: Mon, 20 May 2013 11:15:33 -0400 Subject: [PATCH 1/2] Added PM::BehavesLikeScreen module to inject into UIViewController --- lib/ProMotion/cocoatouch/SplitViewController.rb | 6 ++---- lib/ProMotion/delegate.rb | 2 +- lib/ProMotion/screen_helpers/screen_navigation.rb | 6 ++---- lib/ProMotion/screen_helpers/screen_tabs.rb | 3 +-- lib/ProMotion/screens/behaves_like_screen.rb | 9 +++++++++ spec/screen_spec.rb | 15 +++++++++++++++ spec/split_screen_in_tab_bar_spec.rb | 4 ++-- spec/split_screen_open_screen_spec.rb | 10 +++++----- spec/split_screen_spec.rb | 4 ++-- 9 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 lib/ProMotion/screens/behaves_like_screen.rb diff --git a/lib/ProMotion/cocoatouch/SplitViewController.rb b/lib/ProMotion/cocoatouch/SplitViewController.rb index ad955c4..b1612d4 100644 --- a/lib/ProMotion/cocoatouch/SplitViewController.rb +++ b/lib/ProMotion/cocoatouch/SplitViewController.rb @@ -8,16 +8,14 @@ class SplitViewController < UISplitViewController s.respond_to?(:visibleViewController) ? s.visibleViewController : s end def master_screen=(s) - self.viewControllers = [s.main_controller, self.viewControllers.last] + self.viewControllers = [s.pm_main_controller, self.viewControllers.last] end def detail_screen=(s) # set the button from the old detail screen to the new one button = detail_screen.navigationItem.leftBarButtonItem s.navigationItem.leftBarButtonItem = button - - vc = s.respond_to?(:main_controller) ? s.main_controller : s - self.viewControllers = [self.viewControllers.first, vc] + self.viewControllers = [self.viewControllers.first, s.pm_main_controller] end def screens=(s_array) self.viewControllers = s_array diff --git a/lib/ProMotion/delegate.rb b/lib/ProMotion/delegate.rb index 8a59494..172f338 100644 --- a/lib/ProMotion/delegate.rb +++ b/lib/ProMotion/delegate.rb @@ -28,7 +28,7 @@ module ProMotion end def load_root_screen(new_screen) - new_screen = new_screen.main_controller if new_screen.respond_to?(:main_controller) + new_screen = new_screen.pm_main_controller self.window ||= self.ui_window.alloc.initWithFrame(UIScreen.mainScreen.bounds) self.window.rootViewController = new_screen diff --git a/lib/ProMotion/screen_helpers/screen_navigation.rb b/lib/ProMotion/screen_helpers/screen_navigation.rb index cd50e22..480b8e3 100644 --- a/lib/ProMotion/screen_helpers/screen_navigation.rb +++ b/lib/ProMotion/screen_helpers/screen_navigation.rb @@ -29,7 +29,7 @@ module ProMotion push_view_controller screen else - open_root_screen screen + open_root_screen screen.pm_main_controller end @@ -117,9 +117,7 @@ module ProMotion end def present_modal_view_controller(screen, animated) - vc = screen - vc = screen.main_controller if screen.respond_to?(:main_controller) - self.presentModalViewController(vc, animated:animated) + self.presentModalViewController(screen.pm_main_controller, animated:animated) end def present_view_controller_in_tab_bar_controller(screen, tab_name) diff --git a/lib/ProMotion/screen_helpers/screen_tabs.rb b/lib/ProMotion/screen_helpers/screen_tabs.rb index f933d7d..f95ccee 100644 --- a/lib/ProMotion/screen_helpers/screen_tabs.rb +++ b/lib/ProMotion/screen_helpers/screen_tabs.rb @@ -16,8 +16,7 @@ module ProMotion s.parent_screen = self if self.is_a?(UIViewController) && s.respond_to?("parent_screen=") s.tab_bar = tab_bar_controller if s.respond_to?("tab_bar=") - vc = s.respond_to?(:main_controller) ? s.main_controller : s - view_controllers << vc + view_controllers << s.pm_main_controller tag_index += 1 diff --git a/lib/ProMotion/screens/behaves_like_screen.rb b/lib/ProMotion/screens/behaves_like_screen.rb new file mode 100644 index 0000000..e84538a --- /dev/null +++ b/lib/ProMotion/screens/behaves_like_screen.rb @@ -0,0 +1,9 @@ +module ProMotion + module BehavesLikeScreen + def pm_main_controller + respond_to?(:main_controller) ? main_controller : self + end + end +end + +UIViewController.send :include, ProMotion::BehavesLikeScreen diff --git a/spec/screen_spec.rb b/spec/screen_spec.rb index 0326d14..6c42e33 100644 --- a/spec/screen_spec.rb +++ b/spec/screen_spec.rb @@ -97,6 +97,21 @@ describe "screen properties" do end + describe "pm_main_controller" do + + it "should return the navigation controller wrapper" do + @screen.pm_main_controller.should.be.instance_of ProMotion::NavigationController + end + + it "should return itself when screen is a plain UIViewController" do + vc = UIViewController.alloc.initWithNibName(nil, bundle: nil) + vc.respond_to?(:pm_main_controller).should == true + vc.pm_main_controller.should == vc + end + + end + + describe "navigation controller behavior" do it "should have a nav bar" do diff --git a/spec/split_screen_in_tab_bar_spec.rb b/spec/split_screen_in_tab_bar_spec.rb index 03718e2..28bc4c4 100644 --- a/spec/split_screen_in_tab_bar_spec.rb +++ b/spec/split_screen_in_tab_bar_spec.rb @@ -38,12 +38,12 @@ describe "split screen in tab bar functionality" do it "should set the first viewController to HomeScreen's main controller" do @split_screen.master_screen.should == @master_screen - @split_screen.viewControllers.first.should == @master_screen.main_controller + @split_screen.viewControllers.first.should == @master_screen.pm_main_controller end it "should set the second viewController to BasicScreen's main controller" do @split_screen.detail_screen.should == @detail_screen - @split_screen.viewControllers.last.should == @detail_screen.main_controller + @split_screen.viewControllers.last.should == @detail_screen.pm_main_controller end it "should set the tab bar first viewController to the split screen" do diff --git a/spec/split_screen_open_screen_spec.rb b/spec/split_screen_open_screen_spec.rb index 10bbdb5..97e375e 100644 --- a/spec/split_screen_open_screen_spec.rb +++ b/spec/split_screen_open_screen_spec.rb @@ -17,15 +17,15 @@ describe "split screen `open` functionality" do it "should open a new screen in the detail view" do @master_screen.open @detail_screen_2, in_detail: true @split_screen.detail_screen.should == @detail_screen_2 - @split_screen.viewControllers.first.should == @master_screen.main_controller - @split_screen.viewControllers.last.should == @detail_screen_2.main_controller + @split_screen.viewControllers.first.should == @master_screen.pm_main_controller + @split_screen.viewControllers.last.should == @detail_screen_2.pm_main_controller end it "should open a new screen in the master view" do @detail_screen_1.open @detail_screen_2, in_master: true @split_screen.master_screen.should == @detail_screen_2 - @split_screen.viewControllers.first.should == @detail_screen_2.main_controller - @split_screen.viewControllers.last.should == @detail_screen_1.main_controller + @split_screen.viewControllers.first.should == @detail_screen_2.pm_main_controller + @split_screen.viewControllers.last.should == @detail_screen_1.pm_main_controller end it "should open a new screen in the master view's navigation controller" do @@ -37,7 +37,7 @@ describe "split screen `open` functionality" do it "should open a new modal screen in the detail view" do @detail_screen_1.open @detail_screen_2, modal: true @split_screen.detail_screen.should == @detail_screen_1 - @detail_screen_1.presentedViewController.should == @detail_screen_2.main_controller + @detail_screen_1.presentedViewController.should == @detail_screen_2.pm_main_controller end it "should not interfere with normal non-split screen navigation" do diff --git a/spec/split_screen_spec.rb b/spec/split_screen_spec.rb index 83de801..0ed00d9 100644 --- a/spec/split_screen_spec.rb +++ b/spec/split_screen_spec.rb @@ -28,12 +28,12 @@ describe "split screen functionality" do it "should set the first viewController to MasterScreen" do @split_screen.master_screen.should == @master_screen - @split_screen.viewControllers.first.should == @master_screen.main_controller + @split_screen.viewControllers.first.should == @master_screen.pm_main_controller end it "should set the second viewController to DetailScreen" do @split_screen.detail_screen.should == @detail_screen - @split_screen.viewControllers.last.should == @detail_screen.main_controller + @split_screen.viewControllers.last.should == @detail_screen.pm_main_controller end it "should set the title on both screens" do From 0ed76baef34995b224874497fc4bee9f7d132561 Mon Sep 17 00:00:00 2001 From: Matt Brewer Date: Mon, 20 May 2013 12:16:49 -0400 Subject: [PATCH 2/2] alias :main_controller method to :pm_main_controller --- lib/ProMotion/screens/_screen_module.rb | 4 ---- lib/ProMotion/screens/behaves_like_screen.rb | 11 ++++++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/ProMotion/screens/_screen_module.rb b/lib/ProMotion/screens/_screen_module.rb index f933244..1aef2ec 100644 --- a/lib/ProMotion/screens/_screen_module.rb +++ b/lib/ProMotion/screens/_screen_module.rb @@ -140,10 +140,6 @@ module ProMotion end def on_disappear; end - def main_controller - self.navigation_controller || self - end - def view_controller warn "[DEPRECATION] `view_controller` is deprecated, as screens are now UIViewController subclasses." self diff --git a/lib/ProMotion/screens/behaves_like_screen.rb b/lib/ProMotion/screens/behaves_like_screen.rb index e84538a..eaf1f17 100644 --- a/lib/ProMotion/screens/behaves_like_screen.rb +++ b/lib/ProMotion/screens/behaves_like_screen.rb @@ -1,9 +1,10 @@ module ProMotion - module BehavesLikeScreen - def pm_main_controller - respond_to?(:main_controller) ? main_controller : self - end - end + module BehavesLikeScreen + def pm_main_controller + navigationController || self + end + alias_method :main_controller, :pm_main_controller + end end UIViewController.send :include, ProMotion::BehavesLikeScreen