Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added add_child_screen and remove_child_screen methods. #677

Merged
merged 3 commits into from
May 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/Reference/API Reference - ProMotion Screen.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,29 @@ def on_appear
end
```

#### add_child_screen(screen)

Adds a screen as a childViewController. Usually used if you plan to add the child screen's primary view as a subview. Returns the child screen instance. Will also instantiate the instance if given a class.

```ruby
def on_load
@child = add_child_screen MyLittleScreen
add @child.view, { frame: [[ 100, 100 ], [ 100, 50 ]] }
end
```

The parent screen will be accessible from the child as `parent_screen`.

#### remove_child_screen(screen)

Removes a child screen from the current screen.

```ruby
def foo
remove_child_screen @child
end
```

---

### Class Methods
Expand Down
95 changes: 55 additions & 40 deletions lib/ProMotion/screen/screen_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,6 @@ def modal?
self.modal == true
end

def resolve_title
case self.class.title_type
when :text then self.title = self.class.title
when :view then self.navigationItem.titleView = self.class.title
when :image then self.navigationItem.titleView = UIImageView.alloc.initWithImage(self.class.title)
else
PM.logger.warn("title expects string, UIView, or UIImage, but #{self.class.title.class.to_s} given.")
end
end

def resolve_status_bar
case self.class.status_bar_type
when :none
status_bar_hidden true
when :light
status_bar_hidden false
status_bar_style UIStatusBarStyleLightContent
when :dark
status_bar_hidden false
status_bar_style UIStatusBarStyleDefault
else
status_bar_hidden false
global_style = NSBundle.mainBundle.objectForInfoDictionaryKey("UIStatusBarStyle")
status_bar_style global_style ? Object.const_get(global_style) : UIStatusBarStyleDefault
end
end

def add_nav_bar_buttons
set_nav_bar_button(self.class.get_nav_bar_button[:side], self.class.get_nav_bar_button) if self.class.get_nav_bar_button
end

def status_bar_hidden(hidden)
UIApplication.sharedApplication.setStatusBarHidden(hidden, withAnimation:self.class.status_bar_animation)
end

def status_bar_style(style)
UIApplication.sharedApplication.setStatusBarStyle(style)
end

def parent_screen=(parent)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made these methods private, like they should have been. No other changes, just moved them to the private method section.

@parent_screen = WeakRef.new(parent)
end
Expand Down Expand Up @@ -176,10 +137,64 @@ def frame
return self.view_or_self.frame
end

def add_child_screen(screen)
screen = screen.new if screen.respond_to?(:new)
addChildViewController(screen)
screen.parent_screen = WeakRef.new(self)
screen.didMoveToParentViewController(self) # Required
screen
end

def remove_child_screen(screen)
screen.parent_screen = nil
screen.willMoveToParentViewController(nil) # Required
screen.removeFromParentViewController
screen
end
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section is the main addition.


private

def resolve_title
case self.class.title_type
when :text then self.title = self.class.title
when :view then self.navigationItem.titleView = self.class.title
when :image then self.navigationItem.titleView = UIImageView.alloc.initWithImage(self.class.title)
else
PM.logger.warn("title expects string, UIView, or UIImage, but #{self.class.title.class.to_s} given.")
end
end

def resolve_status_bar
case self.class.status_bar_type
when :none
status_bar_hidden true
when :light
status_bar_hidden false
status_bar_style UIStatusBarStyleLightContent
when :dark
status_bar_hidden false
status_bar_style UIStatusBarStyleDefault
else
status_bar_hidden false
global_style = NSBundle.mainBundle.objectForInfoDictionaryKey("UIStatusBarStyle")
status_bar_style global_style ? Object.const_get(global_style) : UIStatusBarStyleDefault
end
end

def add_nav_bar_buttons
set_nav_bar_button(self.class.get_nav_bar_button[:side], self.class.get_nav_bar_button) if self.class.get_nav_bar_button
end

def status_bar_hidden(hidden)
UIApplication.sharedApplication.setStatusBarHidden(hidden, withAnimation:self.class.status_bar_animation)
end

def status_bar_style(style)
UIApplication.sharedApplication.setStatusBarStyle(style)
end

def apply_properties(args)
reserved_args = [ :nav_bar, :hide_nav_bar, :hide_tab_bar, :animated, :close_all, :in_tab, :in_detail, :in_master, :to_screen ]
reserved_args = [ :nav_bar, :hide_nav_bar, :hide_tab_bar, :animated, :close_all, :in_tab, :in_detail, :in_master, :to_screen, :toolbar ]
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added :toolbar because I was getting warnings in the tests.

set_attributes self, args.dup.delete_if { |k,v| reserved_args.include?(k) }
end

Expand Down
File renamed without changes.
45 changes: 35 additions & 10 deletions spec/unit/screen_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,19 +316,19 @@

it "showing" do
# Simulate AppDelegate setup of main screen
screen = HomeScreen.new modal: true, nav_bar: true, toolbar: true
screen = HomeScreen.new(nav_bar: true, modal: true, toolbar: true)
screen.on_load
screen.navigationController.toolbarHidden?.should == false
end

it "hidden" do
screen = HomeScreen.new modal: true, nav_bar: true, toolbar: false
screen = HomeScreen.new(nav_bar: true, modal: true, toolbar: false)
screen.on_load
screen.navigationController.toolbarHidden?.should == true
end

it "adds a single item" do
screen = HomeScreen.new modal: true, nav_bar: true, toolbar: true
screen = HomeScreen.new(nav_bar: true, modal: true, toolbar: true)
screen.on_load
screen.set_toolbar_button([{title: "Testing Toolbar"}])

Expand All @@ -339,7 +339,7 @@
end

it "adds multiple items" do
screen = HomeScreen.new modal: true, nav_bar: true, toolbar: true
screen = HomeScreen.new(nav_bar: true, modal: true, toolbar: true)
screen.set_toolbar_buttons [{title: "Testing Toolbar"}, {title: "Another Test"}]

screen.navigationController.toolbar.items.should.be.instance_of Array
Expand All @@ -349,29 +349,29 @@
end

it "shows the toolbar when setting items" do
screen = HomeScreen.new modal: true, nav_bar: true, toolbar: false
screen = HomeScreen.new(nav_bar: true, modal: true, toolbar: false)
screen.on_load
screen.navigationController.toolbarHidden?.should == true
screen.set_toolbar_button([{title: "Testing Toolbar"}], false)
screen.navigationController.toolbarHidden?.should == false
end

it "doesn't show the toolbar when passed nil" do
screen = HomeScreen.new modal: true, nav_bar: true, toolbar: true
screen = HomeScreen.new(nav_bar: true, modal: true, toolbar: true)
screen.on_load
screen.set_toolbar_button(nil, false)
screen.navigationController.toolbarHidden?.should == true
end

it "doesn't show the toolbar when passed false" do
screen = HomeScreen.new modal: true, nav_bar: true, toolbar: true
screen = HomeScreen.new(nav_bar: true, modal: true, toolbar: true)
screen.on_load
screen.set_toolbar_button(false, false)
screen.navigationController.toolbarHidden?.should == true
end

it "hides the toolbar when passed nil" do
screen = HomeScreen.new modal: true, nav_bar: true, toolbar: true
screen = HomeScreen.new(nav_bar: true, modal: true, toolbar: true)
screen.on_load
screen.set_toolbar_button([{title: "Testing Toolbar"}], false)
screen.navigationController.toolbarHidden?.should == false
Expand All @@ -381,9 +381,9 @@

end

describe 'toolbar tinted buttons' do
describe "toolbar tinted buttons" do
before do
@screen = HomeScreen.new modal: true, nav_bar: true, toolbar: true
@screen = HomeScreen.new(nav_bar: true, modal: true, toolbar: true)
@screen.on_load
end

Expand All @@ -403,3 +403,28 @@
end

end

describe "child screen management" do
before do
@screen = HomeScreen.new
@child = BasicScreen.new
end

it "#add_child_screen" do
@screen.add_child_screen @child
@screen.childViewControllers.should.include(@child)
@screen.childViewControllers.length.should == 1
@child.parent_screen.should == @screen
end

it "#remove_child_screen" do
@screen.add_child_screen @child
@screen.childViewControllers.should.include(@child)
@screen.remove_child_screen @child
@screen.childViewControllers.length.should == 0
@child.parent_screen.should == nil
end

end


File renamed without changes.