From 3dcf2a251e0f78ca20b4bf95ea5c4c57f8f9a7f6 Mon Sep 17 00:00:00 2001 From: Jamon Holmgren Date: Fri, 21 Jun 2013 22:55:45 -0700 Subject: [PATCH] removing dependency on monkey-patching UIViewController --- .../cocoatouch/split_view_controller.rb | 48 +++++----- lib/ProMotion/cocoatouch/table_view_cell.rb | 1 - .../cocoatouch/view_controller_module.rb | 10 -- lib/ProMotion/containers/tabs.rb | 2 +- lib/ProMotion/delegate/delegate_helper.rb | 2 +- lib/ProMotion/{screen => map}/map_screen.rb | 0 lib/ProMotion/map/map_screen_annotation.rb | 94 ++++++++++--------- .../{screen => map}/map_screen_module.rb | 0 lib/ProMotion/screen/screen_navigation.rb | 4 +- .../{screen => table}/table_screen.rb | 0 .../{screen => table}/table_screen_module.rb | 0 lib/ProMotion/{screen => web}/web_screen.rb | 0 .../{screen => web}/web_screen_module.rb | 0 spec/functional/func_web_screen_spec.rb | 2 +- spec/unit/screen_spec.rb | 16 ---- spec/unit/split_screen_in_tab_bar_spec.rb | 4 +- spec/unit/split_screen_open_screen_spec.rb | 10 +- spec/unit/split_screen_spec.rb | 4 +- 18 files changed, 89 insertions(+), 108 deletions(-) delete mode 100644 lib/ProMotion/cocoatouch/view_controller_module.rb rename lib/ProMotion/{screen => map}/map_screen.rb (100%) rename lib/ProMotion/{screen => map}/map_screen_module.rb (100%) rename lib/ProMotion/{screen => table}/table_screen.rb (100%) rename lib/ProMotion/{screen => table}/table_screen_module.rb (100%) rename lib/ProMotion/{screen => web}/web_screen.rb (100%) rename lib/ProMotion/{screen => web}/web_screen_module.rb (100%) diff --git a/lib/ProMotion/cocoatouch/split_view_controller.rb b/lib/ProMotion/cocoatouch/split_view_controller.rb index b1612d4..5fcc77f 100644 --- a/lib/ProMotion/cocoatouch/split_view_controller.rb +++ b/lib/ProMotion/cocoatouch/split_view_controller.rb @@ -1,23 +1,29 @@ -class SplitViewController < UISplitViewController - def master_screen - s = self.viewControllers.first - s.respond_to?(:visibleViewController) ? s.visibleViewController : s - end - def detail_screen - s = self.viewControllers.last - s.respond_to?(:visibleViewController) ? s.visibleViewController : s - end - def master_screen=(s) - 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 +module ProMotion + class SplitViewController < UISplitViewController + def master_screen + s = self.viewControllers.first + s.respond_to?(:visibleViewController) ? s.visibleViewController : s + end + + def detail_screen + s = self.viewControllers.last + s.respond_to?(:visibleViewController) ? s.visibleViewController : s + end + + def master_screen=(s) + self.viewControllers = [ (s.navigationController || s), 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 - self.viewControllers = [self.viewControllers.first, s.pm_main_controller] + self.viewControllers = [self.viewControllers.first, (s.navigationController || s)] + end + + def screens=(s_array) + self.viewControllers = s_array + end end - def screens=(s_array) - self.viewControllers = s_array - end -end \ No newline at end of file +end diff --git a/lib/ProMotion/cocoatouch/table_view_cell.rb b/lib/ProMotion/cocoatouch/table_view_cell.rb index 89f02f3..e2d65a9 100644 --- a/lib/ProMotion/cocoatouch/table_view_cell.rb +++ b/lib/ProMotion/cocoatouch/table_view_cell.rb @@ -15,6 +15,5 @@ module ProMotion self.imageView.frame = CGRectInset(f, size_inset_x, size_inset_y) end end - end end diff --git a/lib/ProMotion/cocoatouch/view_controller_module.rb b/lib/ProMotion/cocoatouch/view_controller_module.rb deleted file mode 100644 index a4be7c3..0000000 --- a/lib/ProMotion/cocoatouch/view_controller_module.rb +++ /dev/null @@ -1,10 +0,0 @@ -module ProMotion - module ViewControllerModule - def pm_main_controller - navigationController || self - end - alias_method :main_controller, :pm_main_controller - end -end - -UIViewController.send :include, ProMotion::ViewControllerModule diff --git a/lib/ProMotion/containers/tabs.rb b/lib/ProMotion/containers/tabs.rb index 867ee18..f956c2c 100644 --- a/lib/ProMotion/containers/tabs.rb +++ b/lib/ProMotion/containers/tabs.rb @@ -16,7 +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=") - view_controllers << s.pm_main_controller + view_controllers << (s.navigationController || s) tag_index += 1 diff --git a/lib/ProMotion/delegate/delegate_helper.rb b/lib/ProMotion/delegate/delegate_helper.rb index ab8767f..32e99f8 100644 --- a/lib/ProMotion/delegate/delegate_helper.rb +++ b/lib/ProMotion/delegate/delegate_helper.rb @@ -45,7 +45,7 @@ module ProMotion self.home_screen = screen self.window ||= self.ui_window.alloc.initWithFrame(UIScreen.mainScreen.bounds) - self.window.rootViewController = screen.pm_main_controller + self.window.rootViewController = (screen.navigationController || screen) self.window.makeKeyAndVisible end diff --git a/lib/ProMotion/screen/map_screen.rb b/lib/ProMotion/map/map_screen.rb similarity index 100% rename from lib/ProMotion/screen/map_screen.rb rename to lib/ProMotion/map/map_screen.rb diff --git a/lib/ProMotion/map/map_screen_annotation.rb b/lib/ProMotion/map/map_screen_annotation.rb index 4846d62..267a408 100644 --- a/lib/ProMotion/map/map_screen_annotation.rb +++ b/lib/ProMotion/map/map_screen_annotation.rb @@ -1,54 +1,56 @@ -class MapScreenAnnotation +module ProMotion + class MapScreenAnnotation - #Creates the new crime object - def initialize(params = {}) - @params = params - set_defaults + #Creates the new crime object + def initialize(params = {}) + @params = params + set_defaults - unless @params[:latitude] && @params[:longitude] - PM.logger.error("You are required to specify :latitude and :longitude for annotations.") - return nil + unless @params[:latitude] && @params[:longitude] + PM.logger.error("You are required to specify :latitude and :longitude for annotations.") + return nil + end + @coordinate = CLLocationCoordinate2D.new(@params[:latitude], @params[:longitude]) end - @coordinate = CLLocationCoordinate2D.new(@params[:latitude], @params[:longitude]) - end - def set_defaults - @params = { - title: "Title", - pin_color: MKPinAnnotationColorRed, - identifier: "Annotation-#{@params[:pin_color]}", - show_callout: true, - animates_drop: false - }.merge(@params) - end - - def title - @params[:title] - end - - def subtitle - @params[:subtitle] ||= nil - end - - def coordinate - @coordinate - end - - def cllocation - CLLocation.alloc.initWithLatitude(@params[:latitude], longitude:@params[:longitude]) - end - - def setCoordinate(new_coordinate); - if new_coordinate.is_a? Hash - @coordinate = CLLocationCoordinate2D.new(new_coordinate[:latitude], new_coordinate[:longitude]) - else - @coordinate = new_coordinate + def set_defaults + @params = { + title: "Title", + pin_color: MKPinAnnotationColorRed, + identifier: "Annotation-#{@params[:pin_color]}", + show_callout: true, + animates_drop: false + }.merge(@params) end - end - # Allows for retrieving your own custom values on the annotation - def annotation_params - @params - end + def title + @params[:title] + end + def subtitle + @params[:subtitle] ||= nil + end + + def coordinate + @coordinate + end + + def cllocation + CLLocation.alloc.initWithLatitude(@params[:latitude], longitude:@params[:longitude]) + end + + def setCoordinate(new_coordinate); + if new_coordinate.is_a? Hash + @coordinate = CLLocationCoordinate2D.new(new_coordinate[:latitude], new_coordinate[:longitude]) + else + @coordinate = new_coordinate + end + end + + # Allows for retrieving your own custom values on the annotation + def annotation_params + @params + end + + end end diff --git a/lib/ProMotion/screen/map_screen_module.rb b/lib/ProMotion/map/map_screen_module.rb similarity index 100% rename from lib/ProMotion/screen/map_screen_module.rb rename to lib/ProMotion/map/map_screen_module.rb diff --git a/lib/ProMotion/screen/screen_navigation.rb b/lib/ProMotion/screen/screen_navigation.rb index 2232b24..7571d0d 100644 --- a/lib/ProMotion/screen/screen_navigation.rb +++ b/lib/ProMotion/screen/screen_navigation.rb @@ -29,7 +29,7 @@ module ProMotion push_view_controller screen else - open_root_screen screen.pm_main_controller + open_root_screen (screen.navigationController || screen) end @@ -120,7 +120,7 @@ module ProMotion end def present_modal_view_controller(screen, animated) - self.presentModalViewController(screen.pm_main_controller, animated:animated) + self.presentModalViewController((screen.navigationController || screen), animated:animated) end def present_view_controller_in_tab_bar_controller(screen, tab_name) diff --git a/lib/ProMotion/screen/table_screen.rb b/lib/ProMotion/table/table_screen.rb similarity index 100% rename from lib/ProMotion/screen/table_screen.rb rename to lib/ProMotion/table/table_screen.rb diff --git a/lib/ProMotion/screen/table_screen_module.rb b/lib/ProMotion/table/table_screen_module.rb similarity index 100% rename from lib/ProMotion/screen/table_screen_module.rb rename to lib/ProMotion/table/table_screen_module.rb diff --git a/lib/ProMotion/screen/web_screen.rb b/lib/ProMotion/web/web_screen.rb similarity index 100% rename from lib/ProMotion/screen/web_screen.rb rename to lib/ProMotion/web/web_screen.rb diff --git a/lib/ProMotion/screen/web_screen_module.rb b/lib/ProMotion/web/web_screen_module.rb similarity index 100% rename from lib/ProMotion/screen/web_screen_module.rb rename to lib/ProMotion/web/web_screen_module.rb diff --git a/spec/functional/func_web_screen_spec.rb b/spec/functional/func_web_screen_spec.rb index e2d8045..599ddba 100644 --- a/spec/functional/func_web_screen_spec.rb +++ b/spec/functional/func_web_screen_spec.rb @@ -18,7 +18,7 @@ describe "ProMotion::TestWebScreen functionality" do @webscreen.set_content(file_name) @loaded_file = File.read(File.join(NSBundle.mainBundle.resourcePath, file_name)) - wait 0.5 do + wait 0.75 do @webscreen.html.should == @loaded_file end end diff --git a/spec/unit/screen_spec.rb b/spec/unit/screen_spec.rb index b157831..f63b33d 100644 --- a/spec/unit/screen_spec.rb +++ b/spec/unit/screen_spec.rb @@ -109,22 +109,6 @@ 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/unit/split_screen_in_tab_bar_spec.rb b/spec/unit/split_screen_in_tab_bar_spec.rb index 28bc4c4..241b44e 100644 --- a/spec/unit/split_screen_in_tab_bar_spec.rb +++ b/spec/unit/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.pm_main_controller + @split_screen.viewControllers.first.should == (@master_screen.navigationController || @master_screen) 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.pm_main_controller + @split_screen.viewControllers.last.should == (@detail_screen.navigationController || @detail_screen) end it "should set the tab bar first viewController to the split screen" do diff --git a/spec/unit/split_screen_open_screen_spec.rb b/spec/unit/split_screen_open_screen_spec.rb index 97e375e..cd50e3a 100644 --- a/spec/unit/split_screen_open_screen_spec.rb +++ b/spec/unit/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.pm_main_controller - @split_screen.viewControllers.last.should == @detail_screen_2.pm_main_controller + @split_screen.viewControllers.first.should == (@master_screen.navigationController || @master_screen) + @split_screen.viewControllers.last.should == (@detail_screen_2.navigationController || @detail_screen_2) 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.pm_main_controller - @split_screen.viewControllers.last.should == @detail_screen_1.pm_main_controller + @split_screen.viewControllers.first.should == (@detail_screen_2.navigationController || @detail_screen_2) + @split_screen.viewControllers.last.should == (@detail_screen_1.navigationController || @detail_screen_1) 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.pm_main_controller + @detail_screen_1.presentedViewController.should == (@detail_screen_2.navigationController || @detail_screen_2) end it "should not interfere with normal non-split screen navigation" do diff --git a/spec/unit/split_screen_spec.rb b/spec/unit/split_screen_spec.rb index 0ed00d9..ad459b1 100644 --- a/spec/unit/split_screen_spec.rb +++ b/spec/unit/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.pm_main_controller + @split_screen.viewControllers.first.should == (@master_screen.navigationController || @master_screen) end it "should set the second viewController to DetailScreen" do @split_screen.detail_screen.should == @detail_screen - @split_screen.viewControllers.last.should == @detail_screen.pm_main_controller + @split_screen.viewControllers.last.should == (@detail_screen.navigationController || @detail_screen) end it "should set the title on both screens" do