From cba4d0dd34a7156f81ef55d2701ecdb75b6b5b1a Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Mon, 2 Mar 2015 19:05:37 -0600 Subject: [PATCH] Adds UIActivityIndicatorView stylers and actions With tests! --- motion/ruby_motion_query/actions.rb | 16 ++++++ .../stylers/ui_activity_indicator_view.rb | 50 ++++++++++++++++++ motion/ruby_motion_query/stylesheet.rb | 37 +++++++------- motion/ruby_motion_query/subviews.rb | 5 ++ .../ui_activity_indicator_view_styler.rb | 51 +++++++++++++++++++ 5 files changed, 141 insertions(+), 18 deletions(-) create mode 100644 motion/ruby_motion_query/stylers/ui_activity_indicator_view.rb create mode 100644 spec/stylers/ui_activity_indicator_view_styler.rb diff --git a/motion/ruby_motion_query/actions.rb b/motion/ruby_motion_query/actions.rb index 21cd824..df86cce 100644 --- a/motion/ruby_motion_query/actions.rb +++ b/motion/ruby_motion_query/actions.rb @@ -155,5 +155,21 @@ module RubyMotionQuery self end + # For UIActivityIndicatorViews + def start_animating + selected.each do |view| + view.startAnimating if view.respond_to?(:startAnimating) + end + self + end + + # For UIActivityIndicatorViews + def stop_animating + selected.each do |view| + view.stopAnimating if view.respond_to?(:startAnimating) + end + self + end + end end diff --git a/motion/ruby_motion_query/stylers/ui_activity_indicator_view.rb b/motion/ruby_motion_query/stylers/ui_activity_indicator_view.rb new file mode 100644 index 0000000..c452ed2 --- /dev/null +++ b/motion/ruby_motion_query/stylers/ui_activity_indicator_view.rb @@ -0,0 +1,50 @@ +module RubyMotionQuery + module Stylers + class UIActivityIndicatorViewStyler < UIViewStyler + + UI_ACTIVITY_INDICATOR_VIEW_STYLES = { + large: UIActivityIndicatorViewStyleWhiteLarge, + white_large: UIActivityIndicatorViewStyleWhiteLarge, + default: UIActivityIndicatorViewStyleWhite, + white: UIActivityIndicatorViewStyleWhite, + gray: UIActivityIndicatorViewStyleGray + } + + def start_animating + @view.startAnimating + end + alias :start :start_animating + + def stop_animating + @view.stopAnimating + end + alias :stop :stop_animating + + def is_animating? + @view.isAnimating + end + alias :animating? :is_animating? + + def hides_when_stopped=(value) + @view.hidesWhenStopped = value + end + def hides_when_stopped + @view.hidesWhenStopped + end + + def activity_indicator_style=(value) + @view.activityIndicatorViewStyle = UI_ACTIVITY_INDICATOR_VIEW_STYLES[value] || value + end + def activity_indicator_style + @view.activityIndicatorViewStyle + end + + def color=(value) + @view.color = value + end + def color + @view.color + end + end + end +end diff --git a/motion/ruby_motion_query/stylesheet.rb b/motion/ruby_motion_query/stylesheet.rb index 9ea6cc5..9e57f46 100644 --- a/motion/ruby_motion_query/stylesheet.rb +++ b/motion/ruby_motion_query/stylesheet.rb @@ -97,24 +97,25 @@ module RubyMotionQuery # memoize this, however if you do that, make sure the dev doesn't retain them in a var custom_stylers(view) || begin case view - when UILabel then Stylers::UILabelStyler.new(view) - when UIButton then Stylers::UIButtonStyler.new(view) - when UIImageView then Stylers::UIImageViewStyler.new(view) - when UITableView then Stylers::UITableViewStyler.new(view) - when UISwitch then Stylers::UISwitchStyler.new(view) - when UIDatePicker then Stylers::UIDatePickerStyler.new(view) - when UISegmentedControl then Stylers::UISegmentedControlStyler.new(view) - when UIRefreshControl then Stylers::UIRefreshControlStyler.new(view) - when UIPageControl then Stylers::UIPageControlStyler.new(view) - when UIProgressView then Stylers::UIProgressViewStyler.new(view) - when UISlider then Stylers::UISliderStyler.new(view) - when UIStepper then Stylers::UIStepperStyler.new(view) - when UITabBar then Stylers::UITabBarStyler.new(view) - when UITableViewCell then Stylers::UITableViewCellStyler.new(view) - when UITextView then Stylers::UITextViewStyler.new(view) - when UITextField then Stylers::UITextFieldStyler.new(view) - when UINavigationBar then Stylers::UINavigationBarStyler.new(view) - when UIScrollView then Stylers::UIScrollViewStyler.new(view) + when UILabel then Stylers::UILabelStyler.new(view) + when UIButton then Stylers::UIButtonStyler.new(view) + when UIActivityIndicatorView then Stylers::UIActivityIndicatorViewStyler.new(view) + when UIImageView then Stylers::UIImageViewStyler.new(view) + when UITableView then Stylers::UITableViewStyler.new(view) + when UISwitch then Stylers::UISwitchStyler.new(view) + when UIDatePicker then Stylers::UIDatePickerStyler.new(view) + when UISegmentedControl then Stylers::UISegmentedControlStyler.new(view) + when UIRefreshControl then Stylers::UIRefreshControlStyler.new(view) + when UIPageControl then Stylers::UIPageControlStyler.new(view) + when UIProgressView then Stylers::UIProgressViewStyler.new(view) + when UISlider then Stylers::UISliderStyler.new(view) + when UIStepper then Stylers::UIStepperStyler.new(view) + when UITabBar then Stylers::UITabBarStyler.new(view) + when UITableViewCell then Stylers::UITableViewCellStyler.new(view) + when UITextView then Stylers::UITextViewStyler.new(view) + when UITextField then Stylers::UITextFieldStyler.new(view) + when UINavigationBar then Stylers::UINavigationBarStyler.new(view) + when UIScrollView then Stylers::UIScrollViewStyler.new(view) # TODO, all the controls are done, but missing some views, add else if view.respond_to?(:rmq_styler) diff --git a/motion/ruby_motion_query/subviews.rb b/motion/ruby_motion_query/subviews.rb index 0362f52..ccbf4df 100644 --- a/motion/ruby_motion_query/subviews.rb +++ b/motion/ruby_motion_query/subviews.rb @@ -199,6 +199,11 @@ module RubyMotionQuery #o.setTitle('Ok', forState: UIControlStateNormal) o.opaque = true end + elsif (klass == UIActivityIndicatorView) || klass < UIActivityIndicatorView + klass.alloc.initWithActivityIndicatorStyle(UIActivityIndicatorViewStyleWhite).tap do |o| + o.hidden = false + o.opaque = true + end elsif reuse_identifier = opts[:reuse_identifier] style = opts[:cell_style] || UITableViewCellStyleDefault klass.alloc.initWithStyle(style, reuseIdentifier: reuse_identifier).tap do |o| diff --git a/spec/stylers/ui_activity_indicator_view_styler.rb b/spec/stylers/ui_activity_indicator_view_styler.rb new file mode 100644 index 0000000..517c3ce --- /dev/null +++ b/spec/stylers/ui_activity_indicator_view_styler.rb @@ -0,0 +1,51 @@ +class StyleSheetForUIViewStylerTests < RubyMotionQuery::Stylesheet + + def ui_activity_indicator_view_kitchen_sink(st) + st.hides_when_stopped = true + st.activity_indicator_style = :gray + st.color = color.blue + end + +end + +describe 'stylers/ui_activity_indicator_view' do + before do + @vc = UIViewController.alloc.init + @vc.rmq.stylesheet = StyleSheetForUIViewStylerTests + @view_klass = UIActivityIndicatorView + end + + behaves_like "styler" + + it 'should apply a style with every UIImageViewStyler wrapper method' do + view = @vc.rmq.append(@view_klass, :ui_activity_indicator_view_kitchen_sink).get + + view.hidesWhenStopped.should == true + view.activityIndicatorViewStyle.should == UIActivityIndicatorViewStyleGray + view.color.should == rmq.color.blue + end + + it 'should start and stop animations from the styler' do + view = @vc.rmq.append(@view_klass, :ui_activity_indicator_view_kitchen_sink) + + view.style do |st| + st.is_animating?.should == false + st.start_animating + st.is_animating?.should == true + st.stop_animating + st.is_animating?.should == false + + end + end + + it 'should start and stop animations from a direct action' do + view = @vc.rmq.append(@view_klass, :ui_activity_indicator_view_kitchen_sink) + + view.get.isAnimating.should == false + view.start_animating + view.get.isAnimating.should == true + view.stop_animating + view.get.isAnimating.should == false + end + +end