lib/ProMotion/_modules/ScreenNavigation.rb in ProMotion-0.1.2 vs lib/ProMotion/_modules/ScreenNavigation.rb in ProMotion-0.2.0

- old
+ new

@@ -1,36 +1,52 @@ module ProMotion module ScreenNavigation def open_screen(screen, args = {}) # Instantiate screen if given a class instead - screen = screen.new if screen.respond_to? :new + screen = screen.new if screen.respond_to?(:new) screen.parent_screen = self screen.view_controller.title = args[:title] if args[:title] screen.add_nav_bar if args[:nav_bar] + unless args[:close_all] || args[:modal] screen.navigation_controller ||= self.navigation_controller + screen.tab_bar ||= self.tab_bar end - - screen.main_controller.hidesBottomBarWhenPushed = args[:hide_tab_bar] if args[:hide_tab_bar] screen.modal = args[:modal] if args[:modal] - screen.send(:on_load) if screen.respond_to?(:on_load) + screen.view_controller.hidesBottomBarWhenPushed = args[:hide_tab_bar] if args[:hide_tab_bar] if args[:close_all] fresh_start(screen) elsif args[:modal] self.view_controller.presentModalViewController(screen.main_controller, animated:true) + elsif args[:in_tab] && self.tab_bar + vc = open_tab(args[:in_tab]) + $stderr.puts "Found a #{vc.to_s}" + if vc + if vc.is_a? UINavigationController + push_view_controller(screen.view_controller, vc) + else + self.tab_bar.selectedIndex = vc.tabBarItem.tag + $stderr.puts "#{self.tab_bar.selectedIndex} is selected and should be #{vc.tabBarItem.tag}" + # PM::TabBar.replace_current_item(self.tab_bar, view_controller: screen.view_controller) + end + else + $stderr.puts "No tab bar item '#{args[:in_tab]}'" + end elsif self.navigation_controller push_view_controller screen.view_controller else open_view_controller screen.main_controller end - screen.send(:on_opened) if screen.respond_to?(:on_opened) + if screen.respond_to?(:on_opened) + screen.send(:on_opened) + end end def fresh_start(screen) app_delegate.fresh_start(screen) end @@ -62,63 +78,65 @@ def tab_bar_controller(*screens) tab_bar_controller = UITabBarController.alloc.init view_controllers = [] + tag_index = 0 screens.each do |s| if s.is_a? Screen - s = s.new if s.respond_to? :new + s = s.new if s.respond_to?(:new) + s.view_controller.tabBarItem.tag = tag_index view_controllers << s.main_controller + tag_index += 1 else Console.log("Non-Screen passed into tab_bar_controller: #{s.to_s}", withColor: Console::RED_COLOR) end end tab_bar_controller.viewControllers = view_controllers tab_bar_controller end + # Open a UITabBarController with the specified screens as the + # root view controller of the current app. + # @param [Array] A comma-delimited list of screen classes or instances. + # @return [UITabBarController] (this may change in the future; please + # do not rely on it right now) def open_tab_bar(*screens) tab_bar = tab_bar_controller(*screens) screens.each do |s| - s.parent_screen = self if s.respond_to? "parent_screen=" - s.on_load if s.respond_to? :on_load + s.parent_screen = self if s.respond_to?("parent_screen=") + s.tab_bar = tab_bar + s.on_load if s.respond_to?(:on_load) end - open_view_controller tab_bar + open_view_controller(tab_bar) screens.each do |s| - s.on_opened if s.respond_to? :on_opened + s.on_opened if s.respond_to?(:on_opened) end tab_bar end - def push_tab_bar(*screens) - tab_bar = tab_bar_controller(*screens) - - screens.each do |s| - s.parent_screen = self if s.respond_to? "parent_screen=" - s.on_load if s.respond_to? :on_load + def open_tab(tab) + if tab.is_a? String + $stderr.puts "Opening tab in #{self.tab_bar.to_s} : #{tab}" + return PM::TabBar.select(self.tab_bar, title: tab) + else + $stderr.puts "Unable to open tab #{tab.to_s} because it isn't a string." end - - push_view_controller tab_bar - - screens.each do |s| - s.on_opened if s.respond_to? :on_opened - end - - tab_bar end def open_view_controller(vc) UIApplication.sharedApplication.delegate.load_root_view vc end - def push_view_controller(vc) + def push_view_controller(vc, nav_controller=nil) # vc.hidesBottomBarWhenPushed = true if args[:hide_tab_bar] Console.log(" You need a nav_bar if you are going to push #{vc.to_s} onto it.", withColor: Console::RED_COLOR) unless self.navigation_controller - self.navigation_controller.pushViewController(vc, animated: true) + nav_controller ||= self.navigation_controller + nav_controller.pushViewController(vc, animated: true) end end end \ No newline at end of file