Class: Formotion::RowType::ImageRow

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

Constant Summary collapse

TAKE =
BW.localized_string("Take", nil)
DELETE =
BW.localized_string("Delete", nil)
CHOOSE =
BW.localized_string("Choose", nil)
CANCEL =
BW.localized_string("Cancel", nil)
IMAGE_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, field_buffer, #indentWhileEditing?, #initialize, #on_delete, #update_cell, #with_semaphore

Constructor Details

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

Instance Method Details

#actionSheet(actionSheet, clickedButtonAtIndex: index) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/formotion/row_type/image_row.rb', line 72

def actionSheet actionSheet, clickedButtonAtIndex: index
  source = nil

  if index == actionSheet.destructiveButtonIndex
    row.value = nil
    return
  end

  case actionSheet.buttonTitleAtIndex(index)
  when TAKE
    source = :camera
  when CHOOSE
    source = :photo_library
  when CANCEL
  else
    p "Unrecognized button title #{actionSheet.buttonTitleAtIndex(index)}"
  end

  if source
    @camera = BW::Device.camera.send((source == :camera) ? :rear : :any)
    @camera.picture(source_type: source, media_types: [:image]) do |result|
      if result[:original_image]
        #-Resize image when requested (no image upscale)
        if result[:original_image].respond_to?(:resize_image_to_size) and row.max_image_size
          result[:original_image]=result[:original_image].resize_image_to_size(row.max_image_size, false)
        end
        row.value = result[:original_image]
      end
    end
  end
end

#add_plus_accessory(cell) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/formotion/row_type/image_row.rb', line 104

def add_plus_accessory(cell)
  @add_button ||= begin
    button = UIButton.buttonWithType(UIButtonTypeContactAdd)
    button.accessibilityLabel = BW.localized_string("add image", nil)
    button.when(UIControlEventTouchUpInside) do
      self._on_select(nil, nil)
    end
    button
  end
  cell.accessoryView = cell.editingAccessoryView = @add_button
end

#build_cell(cell) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/formotion/row_type/image_row.rb', line 15

def build_cell(cell)
  cell.selectionStyle = self.row.selection_style || UITableViewCellSelectionStyleBlue
  # only show the "plus" when editable
  add_plus_accessory(cell) if row.editable? && (row.value == nil)

  observe(self.row, "value") do |old_value, new_value|
    @image_view.image = new_value
    if new_value
      self.row.row_height = 200
      cell.accessoryView = cell.editingAccessoryView = nil
    else
      self.row.row_height = 44
      # only show the "plus" when editable
      add_plus_accessory(cell) if row.editable? && (row.value == nil)
    end
    row.form.reload_data
  end

  @image_view = UIImageView.alloc.init
  @image_view.image = row.value if row.value
  @image_view.tag = IMAGE_VIEW_TAG
  @image_view.contentMode = UIViewContentModeScaleAspectFit
  @image_view.backgroundColor = UIColor.clearColor
  cell.addSubview(@image_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(IMAGE_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
end

#on_select(tableView, tableViewDelegate) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/formotion/row_type/image_row.rb', line 57

def on_select(tableView, tableViewDelegate)
  if !row.editable?
    return
  end
  @action_sheet = UIActionSheet.alloc.init
  @action_sheet.delegate = self

  @action_sheet.destructiveButtonIndex = (@action_sheet.addButtonWithTitle DELETE) if row.value
  @action_sheet.addButtonWithTitle TAKE if BW::Device.camera.front? or BW::Device.camera.rear?
  @action_sheet.addButtonWithTitle CHOOSE
  @action_sheet.cancelButtonIndex = (@action_sheet.addButtonWithTitle CANCEL)

  @action_sheet.showInView @image_view
end