Commit 8432328

mo khan <mo@mokhan.ca>
2013-03-31 20:01:01
extract button builder
1 parent 2195319
Changed files (2)
app
app/controllers/controls/build.rb
@@ -2,4 +2,47 @@ class Build
   def self.textbox(coordinates)
     TextBoxBuilder.new(coordinates)
   end
+  def self.button(name)
+    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/restaurant_controller.rb
@@ -7,29 +7,56 @@ 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)
-    self.view.addSubview @add
-    @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
+    #@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")
+      .centered(CGPointMake(self.view.frame.size.width / 2, @textbox.center.y + 40))
+      .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
+  end
+end