<commit_before>module Formotion
  module RowType
    class ImageRow < Base
      include BW::KVO

      IMAGE_VIEW_TAG=1100

      def build_cell(cell)
        add_plus_accessory(cell)

        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
            add_plus_accessory(cell)
          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

      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

      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|<commit_msg>Localization for the camera function<commit_after>module Formotion
  module RowType
    class ImageRow < Base
      include BW::KVO

      IMAGE_VIEW_TAG=1100

      def build_cell(cell)
        add_plus_accessory(cell)

        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
            add_plus_accessory(cell)
          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

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

        #-added some localisation functionality
        @action_sheet.destructiveButtonIndex = (@action_sheet.addButtonWithTitle BW.localized_string("Delete", nil)) if row.value
        @action_sheet_index={}
        if BW::Device.camera.front? or BW::Device.camera.rear?
          idx=@action_sheet.addButtonWithTitle BW.localized_string("Take", nil)
          @action_sheet_index[idx]="Take"
        end
        idx=@action_sheet.addButtonWithTitle BW.localized_string("Choose")
        @action_sheet_index[idx]="Choose"
        idx=@action_sheet.cancelButtonIndex = (@action_sheet.addButtonWithTitle BW.localized_string("Cancel"))
        @action_sheet_index[idx]="Cancel"

        @action_sheet.showInView @image_view
      end

      def actionSheet actionSheet, clickedButtonAtIndex: index
        source = nil

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

        #-for localisation, as there is only a string comparison
        case @action_sheet_index[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|
{'fraction_non_alphanumeric': 0.08357534688609229, 'fraction_numerical': 0.0035495321071313327, 'mean_word_length': 2.3149732620320855, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3934407', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1743, 'n_words': 345}