Airplane refactoring all the things

This commit is contained in:
Jamon Holmgren
2014-05-28 00:34:24 -07:00
parent 293340e930
commit 5cab5dec77
9 changed files with 79 additions and 77 deletions

2
.gitignore vendored
View File

@@ -31,3 +31,5 @@ tmp
*.tmproject
tmtags
*.sublime-workspace
lib/ProMotion/delegate/delegate_module.rb

View File

@@ -4,16 +4,14 @@ module ProMotion
def self.new(*screens)
tab_bar_controller = alloc.init
view_controllers = []
screens = screens.flatten.map { |s| s.respond_to?(:new) ? s.new : s } # Initialize any classes
tag_index = 0
screens.each do |s|
view_controllers = screens.map do |s|
s.tabBarItem.tag = tag_index
s.tab_bar = WeakRef.new(tab_bar_controller) if s.respond_to?("tab_bar=")
view_controllers << (s.navigationController || s)
tag_index += 1
s.navigationController || s
end
tab_bar_controller.viewControllers = view_controllers
@@ -28,7 +26,7 @@ module ProMotion
end
if selected_tab_vc
self.selectedViewController = selected_tab_vc if selected_tab_vc
self.selectedViewController = selected_tab_vc
else
PM.logger.error "Unable to open tab #{tab.to_s} -- not found."
nil
@@ -36,7 +34,7 @@ module ProMotion
end
def find_tab(tab_title)
self.viewControllers.select{ |vc| vc.tabBarItem.title == tab_title }.first
self.viewControllers.find { |vc| vc.tabBarItem.title == tab_title }
end
end

View File

@@ -21,27 +21,27 @@ module ProMotion
end
def applicationDidBecomeActive(application)
on_activate if respond_to?(:on_activate)
try :on_activate
end
def applicationWillResignActive(application)
will_deactivate if respond_to?(:will_deactivate)
try :will_deactivate
end
def applicationDidEnterBackground(application)
on_enter_background if respond_to?(:on_enter_background)
try :on_enter_background
end
def applicationWillEnterForeground(application)
will_enter_foreground if respond_to?(:will_enter_foreground)
try :will_enter_foreground
end
def applicationWillTerminate(application)
on_unload if respond_to?(:on_unload)
try :on_unload
end
def application(application, openURL: url, sourceApplication:source_app, annotation: annotation)
on_open_url({ url: url, source_app: source_app, annotation: annotation }) if respond_to?(:on_open_url)
try :on_open_url, { url: url, source_app: source_app, annotation: annotation }
end
def app_delegate
@@ -49,15 +49,14 @@ module ProMotion
end
def app_window
self.window
window
end
def ui_window
(defined?(Motion) && defined?(Motion::Xray) && defined?(Motion::Xray::XrayWindow)) ? Motion::Xray::XrayWindow : UIWindow
end
def open_screen(screen, args={})
def open(screen, args={})
screen = screen.new if screen.respond_to?(:new)
self.home_screen = screen
@@ -69,18 +68,25 @@ module ProMotion
screen
end
alias :open :open_screen
alias :open_screen :open
alias :open_root_screen :open_screen
alias :home :open_screen
def status_bar?
UIApplication.sharedApplication.statusBarHidden
end
# private
def apply_status_bar
self.class.send(:apply_status_bar)
end
def status_bar?
UIApplication.sharedApplication.statusBarHidden
def try(method, *args)
send(method, *args) if respond_to?(method)
end
# public
module ClassMethods
def status_bar(visible = true, opts={})
@@ -105,9 +111,11 @@ module ProMotion
def tint_color(c)
@tint_color = c
end
def tint_color=(c)
@tint_color = c
end
def get_tint_color
@tint_color || nil
end

View File

@@ -2,11 +2,8 @@ module ProMotion
# @requires class:SplitViewController
module SplitScreen
def split_screen_controller(master, detail)
master_main = master.navigationController ? master.navigationController : master
detail_main = detail.navigationController ? detail.navigationController : detail
split = SplitViewController.alloc.init
split.viewControllers = [ master_main, detail_main ]
split.viewControllers = [ (master.navigationController || master), (detail.navigationController || detail) ]
split.delegate = self
[ master, detail ].map { |s| s.split_screen = split if s.respond_to?(:split_screen=) }
@@ -17,20 +14,8 @@ module ProMotion
def create_split_screen(master, detail, args={})
master = master.new if master.respond_to?(:new)
detail = detail.new if detail.respond_to?(:new)
split = split_screen_controller master, detail
if args.has_key?(:icon) or args.has_key?(:title)
split.tabBarItem = create_tab_bar_item(args)
end
if args.has_key?(:button_title)
@button_title = args[:button_title]
end
if args.has_key?(:swipe)
split.presentsWithGesture = args[:swipe]
end
split = split_screen_controller(master, detail)
split_screen_setup(split, args)
split
end
@@ -43,13 +28,24 @@ module ProMotion
# UISplitViewControllerDelegate methods
def splitViewController(svc, willHideViewController: vc, withBarButtonItem: button, forPopoverController: pc)
button.title = @button_title || vc.title
svc.detail_screen.navigationItem.leftBarButtonItem = button;
button.title = @pm_split_screen_button_title || vc.title
svc.detail_screen.navigationItem.leftBarButtonItem = button
end
def splitViewController(svc, willShowViewController: vc, invalidatingBarButtonItem: barButtonItem)
svc.detail_screen.navigationItem.leftBarButtonItem = nil
end
private
def split_screen_setup(split, args)
if (args[:icon] || args[:title]) && respond_to?(:create_tab_bar_item)
split.tabBarItem = create_tab_bar_item(args)
end
@pm_split_screen_button_title = args[:button_title] if args.has_key?(:button_title)
split.presentsWithGesture = args[:swipe] if args.has_key?(:swipe)
end
end
end

View File

@@ -39,23 +39,23 @@ module ProMotion
end
def error(message)
self.log('ERROR', message, :red) if self.levels.include?(:error)
log('ERROR', message, :red) if self.levels.include?(:error)
end
def deprecated(message)
self.log('DEPRECATED', message, :yellow) if self.levels.include?(:warn)
log('DEPRECATED', message, :yellow) if self.levels.include?(:warn)
end
def warn(message)
self.log('WARN', message, :yellow) if self.levels.include?(:warn)
log('WARN', message, :yellow) if self.levels.include?(:warn)
end
def debug(message)
self.log('DEBUG', message, :purple) if self.levels.include?(:debug)
log('DEBUG', message, :purple) if self.levels.include?(:debug)
end
def info(message)
self.log('INFO', message, :green) if self.levels.include?(:info)
log('INFO', message, :green) if self.levels.include?(:info)
end
end

View File

@@ -10,24 +10,13 @@ module ProMotion
end
def navigation_controller=(nav)
@navigationController = nav
self.navigationController = nav
end
def navigationController=(nav)
@navigationController = nav
end
def add_nav_bar(args = {})
self.navigationController ||= begin
self.first_screen = true if self.respond_to?(:first_screen=)
nav = NavigationController.alloc.initWithRootViewController(self)
nav.setModalTransitionStyle(args[:transition_style]) if args[:transition_style]
nav.setModalPresentationStyle(args[:presentation_style]) if args[:presentation_style]
nav
end
self.navigationController.toolbarHidden = !args[:toolbar] unless args[:toolbar].nil?
end
def set_nav_bar_button(side, args={})
button = create_toolbar_button(args)
button.setTintColor args[:tint_color] if args[:tint_color]
@@ -39,11 +28,6 @@ module ProMotion
button
end
def create_toolbar_button(args = {})
button_type = args[:image] || args[:button] || args[:custom_view] || args[:title] || "Button"
bar_button_item button_type, args
end
def set_toolbar_items(buttons = [], animated = true)
self.toolbarItems = Array(buttons).map{|b| b.is_a?(UIBarButtonItem) ? b : create_toolbar_button(b) }
navigationController.setToolbarHidden(false, animated:animated)
@@ -51,6 +35,24 @@ module ProMotion
alias_method :set_toolbar_buttons, :set_toolbar_items
alias_method :set_toolbar_button, :set_toolbar_items
def add_nav_bar(args = {})
self.navigationController ||= begin
self.first_screen = true if self.respond_to?(:first_screen=)
nav = NavigationController.alloc.initWithRootViewController(self)
nav.setModalTransitionStyle(args[:transition_style]) if args[:transition_style]
nav.setModalPresentationStyle(args[:presentation_style]) if args[:presentation_style]
nav
end
self.navigationController.toolbarHidden = !args[:toolbar] unless args[:toolbar].nil?
end
private
def create_toolbar_button(args = {})
button_type = args[:image] || args[:button] || args[:custom_view] || args[:title] || "Button"
bar_button_item button_type, args
end
def bar_button_item(button_type, args)
return button_type if button_type.is_a?(UIBarButtonItem)
return bar_button_item_system_item(args) if args[:system_item]
@@ -107,9 +109,9 @@ module ProMotion
def map_bar_button_item_style(symbol)
{
plain: UIBarButtonItemStylePlain,
bordered: UIBarButtonItemStyleBordered,
done: UIBarButtonItemStyleDone
plain: UIBarButtonItemStylePlain,
bordered: UIBarButtonItemStyleBordered,
done: UIBarButtonItemStyleDone
}[symbol] || UIBarButtonItemStyleDone
end

View File

@@ -9,7 +9,7 @@ module ProMotion
# @require module:Tabs
include ProMotion::Tabs
# @require module:SplitScreen
include ProMotion::SplitScreen if NSBundle.mainBundle.infoDictionary["UIDeviceFamily"].include?("2")
include ProMotion::SplitScreen if UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad
attr_accessor :parent_screen, :first_screen, :modal, :split_screen
@@ -19,8 +19,8 @@ module ProMotion
tab_bar_setup
set_attributes self, args
add_nav_bar(args) if args[:nav_bar]
screen_setup if self.respond_to?(:screen_setup)
on_init if self.respond_to?(:on_init)
try :screen_setup
try :on_init
end
def modal?
@@ -159,6 +159,10 @@ module ProMotion
end
end
def try(method, *args)
send(method, *args) if respond_to?(method)
end
# Class methods
module ClassMethods
def title(t=nil)

View File

@@ -9,7 +9,7 @@ describe "ProMotion::Screen functional" do
end
it "should have a navigation bar" do
wait 1 do
wait 0.5 do
view("Functional").should.be.kind_of UINavigationItemView
end
end

View File

@@ -123,7 +123,7 @@ describe "screen helpers" do
end
it "should apply properties when opening a new screen" do
new_screen = @screen.send(:set_up_screen_for_open, BasicScreen, title: 'Some Title', modal: true, hide_tab_bar: true, nav_bar: true)
new_screen = @screen.send(:set_up_screen_for_open, BasicScreen, { title: 'Some Title', modal: true, hide_tab_bar: true, nav_bar: true })
new_screen.parent_screen.should == @screen
new_screen.title.should == 'Some Title'
@@ -143,14 +143,6 @@ describe "screen helpers" do
@screen.send(:present_modal_view_controller, new_screen, { animated: true, completion: nil })
end
# it "should push screen onto nav controller stack inside a tab bar" do
# # TODO: Implement this test
# end
# it "should set the tab bar selectedIndex when opening a screen inside a tab bar" do
# # TODO: Implement this test
# end
it "should open a root screen if :close_all is provided" do
@screen.mock!(:open_root_screen) { |screen| screen.should.be.instance_of BasicScreen }
screen = @screen.open BasicScreen, close_all: true