Commit e2b5768

mo khan <mo@mokhan.ca>
2013-04-01 01:56:07
extract button builder
1 parent 8432328
app/controllers/commands/add_restaurant_command.rb
@@ -0,0 +1,15 @@
+class AddRestaurantCommand
+  def initialize(restaurant_name_textbox)
+    @textbox = restaurant_name_textbox
+  end
+  def run(button)
+    puts "RUNNING COMMAND"
+    #button.disable
+    #@textbox.disable
+    BW::Location.get do |result|
+      BW::Location.stop
+      location = Restaurant.new(@textbox.text, result[:to].latitude, result[:to].longitude)
+      location.save
+    end
+  end
+end
app/controllers/controls/build.rb
@@ -6,43 +6,3 @@ class Build
     ButtonBuilder.new(name)
   end
 end
-
-class ButtonBuilder
-  def initialize(name)
-    @button = Button.new(name)
-  end
-  def centered(coordinates)
-    @button.control.center = coordinates
-    self
-  end
-  def when_clicked(command)
-    @button.bind_to(command)
-    self
-  end
-  def build
-    @button
-  end
-end
-
-class Button
-  def initialize(name)
-    @button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
-    @button.setTitle(name, forState:UIControlStateNormal)
-    @button.setTitle("Loading", forState:UIControlStateDisabled)
-    @button.sizeToFit
-  end
-  def bind_to(command)
-    @button.when(UIControlEventTouchUpInside) do
-      command.run(@button)
-    end
-  end
-  def disable
-    @button.enabled=false
-  end
-  def add_to(view)
-    view.addSubview(@button)
-  end
-  def control
-    @button
-  end
-end
app/controllers/controls/button.rb
@@ -0,0 +1,22 @@
+class Button
+  def initialize(name)
+    @button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
+    @button.setTitle(name, forState:UIControlStateNormal)
+    @button.setTitle("Loading", forState:UIControlStateDisabled)
+    @button.sizeToFit
+  end
+  def bind_to(command)
+    @button.when(UIControlEventTouchUpInside) do
+      command.run(@button)
+    end
+  end
+  def disable
+    @button.enabled=false
+  end
+  def add_to(view)
+    view.addSubview(@button)
+  end
+  def control
+    @button
+  end
+end
app/controllers/controls/button_builder.rb
@@ -0,0 +1,16 @@
+class ButtonBuilder
+  def initialize(name)
+    @button = Button.new(name)
+  end
+  def centered(coordinates)
+    @button.control.center = coordinates
+    self
+  end
+  def when_clicked(command)
+    @button.bind_to(command)
+    self
+  end
+  def build
+    @button
+  end
+end
app/controllers/restaurant_controller.rb
@@ -7,27 +7,6 @@ class RestaurantController < UIViewController
     @textbox = Build.textbox([[0,0], [160, 26]]).centered_within(self.view).build
     self.add_control(@textbox)
 
-    #@add = UIButton.buttonWithType(UIButtonTypeRoundedRect)
-    #@add.setTitle("Add Restaurant", forState:UIControlStateNormal)
-    #@add.setTitle("Loading", forState:UIControlStateDisabled)
-    #@add.sizeToFit
-    #@add.center = CGPointMake(self.view.frame.size.width / 2, @textbox.center.y + 40)
-    #@add.when(UIControlEventTouchUpInside) do
-      #begin
-        #@add.enabled = false
-        #@textbox.disable
-        #BW::Location.get do |result|
-          #BW::Location.stop
-          #location = Restaurant.new(@textbox.text, result[:to].latitude, result[:to].longitude)
-          #location.save
-        #end
-      #rescue Exception => e
-        #puts e.message
-        #puts e.backtrace.inspect
-      #end
-    #end
-    #self.view.addSubview @add
-
     command = AddRestaurantCommand.new(@textbox)
     button = Build
       .button("Add Restaurant")
@@ -35,28 +14,14 @@ class RestaurantController < UIViewController
       .when_clicked(command)
       .build
 
-    #button.bind_to(command)
-
     add_control(button)
   end
 
   def add_control(view)
-    p 'adding to view'
     view.add_to(self.view)
   end
-end
-class AddRestaurantCommand
-  def initialize(restaurant_name_textbox)
-    @textbox = restaurant_name_textbox
-  end
-  def run(button)
-    puts "RUNNING COMMAND"
-    #button.disable
-    #@textbox.disable
-    BW::Location.get do |result|
-      BW::Location.stop
-      location = Restaurant.new(@textbox.text, result[:to].latitude, result[:to].longitude)
-      location.save
-    end
+
+  def bind_to(controller_factory)
+    @controller_factory = controller_factory
   end
 end
app/controllers/search_controller.rb
@@ -7,6 +7,19 @@ class SearchController < UIViewController
   end
 
   def push
-    self.navigationController.pushViewController(RestaurantController.alloc.initWithNibName(nil, bundle: nil), animated: true)
+    controller = @controller_factory.create(RestaurantController)
+    self.navigationController.pushViewController(controller, animated: true)
+  end
+
+  def bind_to(controller_factory)
+    @controller_factory = controller_factory
+  end
+end
+
+class ControllerFactory
+  def create(controller)
+    instance = controller.alloc
+    instance.bind_to(self)
+    instance.initWithNibName(nil, bundle: nil)
   end
 end
app/views/welcome_view.rb
@@ -0,0 +1,8 @@
+class WelcomeView < UIViewController
+  def viewDidLoad
+    super
+  end
+  def bind_to(factory)
+    
+  end
+end
app/app_delegate.rb
@@ -1,7 +1,8 @@
 class AppDelegate
   def application(application, didFinishLaunchingWithOptions:launchOptions)
     @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
-    @window.rootViewController = UINavigationController.alloc.initWithRootViewController(SearchController.alloc.initWithNibName(nil, bundle:nil))
+    controller_factory = ControllerFactory.new
+    @window.rootViewController = UINavigationController.alloc.initWithRootViewController(controller_factory.create(SearchController))
     @window.makeKeyAndVisible
     true
   end