Class: Formotion::RowType::MapRow

Inherits:
Base show all
Includes:
BW::KVO
Defined in:
lib/formotion/row_type/map_row.rb

Constant Summary collapse

MAP_VIEW_TAG =
1100

Instance Attribute Summary

Attributes inherited from Base

#row, #tableView

Instance Method Summary collapse

Methods inherited from Base

#_on_select, #after_build, #after_delete, #break_with_semaphore, #button?, #cellEditingStyle, #cell_style, #delete_row, #done_editing, field_buffer, #indentWhileEditing?, #initialize, #input_accessory_view, #on_delete, #update_cell, #with_semaphore

Constructor Details

This class inherits a constructor from Formotion::RowType::Base

Instance Method Details

#annotationsObject



81
82
83
# File 'lib/formotion/row_type/map_row.rb', line 81

def annotations
  @map_view.annotations
end

#build_cell(cell) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/formotion/row_type/map_row.rb', line 89

def build_cell(cell)
  cell.selectionStyle = self.row.selection_style || UITableViewCellSelectionStyleBlue

  @map_view = MKMapView.alloc.init
  @map_view.delegate = self

  set_pin

  observe(self.row, "value") do |old_value, new_value|
    break_with_semaphore do
      set_pin
    end
  end

  @map_view.tag = MAP_VIEW_TAG
  @map_view.contentMode = UIViewContentModeScaleAspectFit
  @map_view.backgroundColor = UIColor.clearColor
  cell.addSubview(@map_view)

  cell.swizzle(:layoutSubviews) do
    def layoutSubviews
      old_layoutSubviews

      # viewWithTag is terrible, but I think it's ok to use here...
      formotion_field = self.viewWithTag(MAP_VIEW_TAG)

      field_frame = formotion_field.frame
      field_frame.origin.y = 10
      field_frame.origin.x = self.textLabel.frame.origin.x + self.textLabel.frame.size.width + Formotion::RowType::Base.field_buffer
      field_frame.size.width  = self.frame.size.width - field_frame.origin.x - Formotion::RowType::Base.field_buffer
      field_frame.size.height = self.frame.size.height - Formotion::RowType::Base.field_buffer
      formotion_field.frame = field_frame
    end
  end

  nil
end

#mapObject



85
86
87
# File 'lib/formotion/row_type/map_row.rb', line 85

def map
  @map_view
end

#on_select(tableView, tableViewDelegate) ⇒ Object



127
128
129
130
131
# File 'lib/formotion/row_type/map_row.rb', line 127

def on_select(tableView, tableViewDelegate)
  if !row.editable?
    return
  end
end

#set_pinObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/formotion/row_type/map_row.rb', line 40

def set_pin
  return unless row.value

  unless row.value.is_a?(Hash)
    coord = (row.value.is_a?(Array) and row.value.size==2) ? CLLocationCoordinate2D.new(row.value[0], row.value[1]) : row.value
    row.value = {coord: coord, pin: {coord:coord}}
  end

  # Set Defaults
  row.value = {
    animated: true,
    type: MKMapTypeStandard,
    enabled: true
  }.merge(row.value)

  if row.value[:coord].is_a?(CLLocationCoordinate2D)
    region = MKCoordinateRegionMakeWithDistance(row.value[:coord], 400.0, 480.0)
  elsif row.value[:coord].is_a?(CLCircularRegion)
    region = MKCoordinateRegionMakeWithDistance(
      row.value[:coord].center,
      row.value[:coord].radius * 2,
      row.value[:coord].radius * 2
    )
  else
    return
  end

  if row.value[:pin]
    row.value[:pin] = {title: nil, subtitle:nil}.merge(row.value[:pin]) #Defaults

    @map_row_data = MapRowData.new(row.value[:pin])
    @map_view.removeAnnotations(@map_view.annotations)
    @map_view.addAnnotation(@map_row_data)
    @map_view.selectAnnotation(@map_row_data, animated:row.value[:animated]) if row.value[:pin][:title]
  end

  @map_view.setUserInteractionEnabled(row.value[:enabled])
  @map_view.setMapType(row.value[:type])
  @map_view.setRegion(region, animated:row.value[:animated])
end