Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add transitions option

  • Loading branch information...
commit 6ddd06f4fe86359bdb0951ee1d759b657ed1af9c 1 parent b3a3afc
@siuying siuying authored
Showing with 52 additions and 3 deletions.
  1. +11 −1 Rakefile
  2. +22 −2 lib/routable/router.rb
  3. +19 −0 spec/main_spec.rb
View
12 Rakefile
@@ -1 +1,11 @@
-require "bundler/gem_tasks"
+$:.unshift("/Library/RubyMotion/lib")
+require 'motion/project'
+require "bundler/gem_tasks"
+
+$:.unshift("./lib/")
+require './lib/routable'
+
+Motion::Project::App.setup do |app|
+ # Use `rake config' to see complete project settings.
+ app.name = 'Routable'
+end
View
24 lib/routable/router.rb
@@ -1,5 +1,12 @@
module Routable
class Router
+ TRANSITIONS = {
+ :cover => UIModalTransitionStyleCoverVertical,
+ :flip => UIModalTransitionStyleFlipHorizontal,
+ :dissolve => UIModalTransitionStyleCrossDissolve,
+ :curl => UIModalTransitionStylePartialCurl
+ }
+
# Singleton, for practical use (you might not want)
# to have more than one router.
class << self
@@ -28,9 +35,15 @@ def routes
# - 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.
def map(url, klass, options = {})
format = url
+
+ if options[:transition] && !TRANSITIONS.keys.include?(options[:transition])
+ raise ArgumentError, "transition must be one of #{TRANSITIONS.keys}"
+ end
+
self.routes[format] = options.merge!(klass: klass)
end
@@ -141,12 +154,14 @@ def controller_for_url(url)
open_options = options_for_url(url)
open_params = open_options[:open_params]
open_klass = open_options[:klass]
+ transition = open_options[:transition]
controller = open_klass.alloc
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.
@@ -160,6 +175,11 @@ def viewDidUnload
end
end
end
+
+ if transition
+ controller.modalTransitionStyle = TRANSITIONS[transition]
+ end
+
controller
end
@@ -168,4 +188,4 @@ def shared_vc_cache
@shared_vc_cache ||= {}
end
end
-end
+ end
View
19 spec/main_spec.rb
@@ -11,6 +11,9 @@ def initWithParams(params = {})
class NoParamsController < UIViewController
end
+class LoginController < UIViewController
+end
+
describe "the url router" do
before do
@router = Routable::Router.new
@@ -68,4 +71,20 @@ def make_test_controller_route
@router.open("users")
@nav_controller.viewControllers.count.should == 3
end
+
+ it "should use transitions on modal" do
+ [:cover, :flip, :dissolve, :curl].each do |transition|
+ url = "login/#{transition.to_s}"
+ @router.map(url, LoginController, modal: true, transition:transition)
+ controller = @router.controller_for_url(url)
+ controller.modalTransitionStyle.should == Routable::Router::TRANSITIONS[transition]
+ end
+ end
+
+ it "should raise error when transition is unexpected value" do
+ lambda do
+ @router.map("login", LoginController, modal: true, transition: :unexpected)
+ end.should.raise(ArgumentError)
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.