lib/routable/router.rb in routable-0.0.1 vs lib/routable/router.rb in routable-0.0.2

- old
+ new

@@ -1,7 +1,21 @@ module Routable class Router + TRANSITION_STYLES = { + :cover => UIModalTransitionStyleCoverVertical, + :flip => UIModalTransitionStyleFlipHorizontal, + :dissolve => UIModalTransitionStyleCrossDissolve, + :curl => UIModalTransitionStylePartialCurl + } + + PRESENTATION_STYLES = { + :full_screen => UIModalPresentationFullScreen, + :page_sheet => UIModalPresentationPageSheet, + :form_sheet => UIModalPresentationFormSheet, + :current => UIModalPresentationCurrentContext + } + # Singleton, for practical use (you might not want) # to have more than one router. class << self def router @router ||= Router.new @@ -26,13 +40,25 @@ # OPTIONS # :modal => true/false # - We present the VC modally (router is not shared between the new nav VC) # :shared => true/false # - If URL is called again, we pop to that VC if it's in memory. - + # :transition => [:cover, :flip, :dissolve, :curl] + # - A symbol to represented transition style used. Mapped to UIModalTransitionStyle. + # :presentation => [:full_screen, :page_sheet, :form_sheet, :current] + # - A symbol to represented presentation style used. Mapped to UIModalPresentationStyle. def map(url, klass, options = {}) format = url + + if options[:transition] && !TRANSITION_STYLES.keys.include?(options[:transition]) + raise ArgumentError, ":transition must be one of #{TRANSITION_STYLES.keys}" + end + + if options[:presentation] && !PRESENTATION_STYLES.keys.include?(options[:presentation]) + raise ArgumentError, ":presentation must be one of #{PRESENTATION_STYLES.keys}" + end + self.routes[format] = options.merge!(klass: klass) end # Push the UIViewController for the given url # EX @@ -48,10 +74,12 @@ if controller.class == UINavigationController self.navigation_controller.presentModalViewController(controller, animated: animated) else tempNavigationController = UINavigationController.alloc.init tempNavigationController.pushViewController(controller, animated: false) + tempNavigationController.modalTransitionStyle = controller.modalTransitionStyle + tempNavigationController.modalPresentationStyle = controller.modalPresentationStyle self.navigation_controller.presentModalViewController(tempNavigationController, animated: animated) end else if self.navigation_controller.viewControllers.member? controller self.navigation_controller.popToViewController(controller, animated:animated) @@ -59,10 +87,21 @@ self.navigation_controller.pushViewController(controller, animated:animated) end end end + # Pop the top level UIViewController + # EX + # router.pop + def pop(animated = true) + if self.navigation_controller.modalViewController + self.navigation_controller.dismissModalViewControllerAnimated(animated) + else + self.navigation_controller.popViewControllerAnimated(animated) + end + end + def options_for_url(url) # map of url => options @url_options_cache ||= {} if @url_options_cache[url] @@ -134,10 +173,11 @@ if controller.respond_to? :initWithParams controller = controller.initWithParams(open_params) else controller = controller.init end + if open_options[:shared] shared_vc_cache[url] = controller # when controller.viewDidUnload called, remove from cache. controller.add_block_method :new_dealloc do shared_vc_cache.delete url @@ -147,9 +187,20 @@ new_dealloc super end end end + + transition = open_options[:transition] + if transition + controller.modalTransitionStyle = TRANSITION_STYLES[transition] + end + + presentation = open_options[:presentation] + if presentation + controller.modalPresentationStyle = PRESENTATION_STYLES[presentation] + end + controller end private def shared_vc_cache \ No newline at end of file