Class: PMBaseAdapter

Inherits:
Android::Widget::BaseAdapter
  • Object
show all
Defined in:
lib/project/pro_motion/adapters/pm_base_adapter.rb

Direct Known Subclasses

PMCursorAdapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PMBaseAdapter

Returns a new instance of PMBaseAdapter.


4
5
6
7
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 4

def initialize(opts={})
  super()
  self.data = opts.fetch(:data, [])
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.


2
3
4
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 2

def data
  @data
end

Instance Method Details

#action_arguments(data, position) ⇒ Object

configure what to pass back when we tap that action


88
89
90
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 88

def action_arguments(data, position)
  data[:arguments]
end

#are_all_items_enabled?Boolean

Returns:

  • (Boolean)

17
18
19
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 17

def are_all_items_enabled?
  true
end

#areAllItemsEnabledObject


16
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 16

def areAllItemsEnabled(); are_all_items_enabled?; end

#countObject


59
60
61
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 59

def count()
  data.length
end

#getCountObject


58
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 58

def getCount(); count(); end

#getItem(position) ⇒ Object


63
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 63

def getItem(position); item_data(position); end

#getItemId(position) ⇒ Object


68
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 68

def getItemId(position); item_id(position); end

#getItemViewType(position) ⇒ Object


42
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 42

def getItemViewType(position); item_view_type_id(position); end

#getView(position, convert_view, parent) ⇒ Object


73
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 73

def getView(position, convert_view, parent); view(position, convert_view, parent); end

#getViewTypeCountObject


36
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 36

def getViewTypeCount(); view_type_count; end

#has_stable_ids?Boolean

Returns:

  • (Boolean)

32
33
34
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 32

def has_stable_ids?
  true
end

#hasStableIdsObject


31
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 31

def hasStableIds(); has_stable_ids?; end

#inflate_row(xml_resource) ⇒ Object


141
142
143
144
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 141

def inflate_row(xml_resource)
  inflater = Potion::LayoutInflater.from(find.activity)
  row_view = inflater.inflate(xml_resource, nil, true)
end

#is_empty?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 27

def is_empty?
  data.blank?
end

#is_enabled?(position) ⇒ Boolean

Returns:

  • (Boolean)

22
23
24
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 22

def is_enabled?(position)
  true
end

#isEmptyObject


26
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 26

def isEmpty(); is_empty?; end

#isEnabled(position) ⇒ Object


21
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 21

def isEnabled(position); is_enabled?(position); end

#item_data(position) ⇒ Object


64
65
66
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 64

def item_data(position)
  data[position]
end

#item_id(position) ⇒ Object


69
70
71
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 69

def item_id(position)
  position
end

#item_view_type_id(position) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 43

def item_view_type_id(position)
  data_item = self.item_data(position)
  idx = nil
  if data_item[:prevent_reuse]
    idx = Android::Widget::Adapter::IGNORE_ITEM_VIEW_TYPE
  else
    # get custom cell index
    idx = view_types.index(data_item[:cell_xml] || data_item[:cell_class])
    # Shift custom cells up 1, no custom == index 0
    idx = idx ? (idx + 1) : 0
  end

  idx
end

#screenObject


9
10
11
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 9

def screen
  @screen ||= rmq.screen
end

#screen=(value) ⇒ Object


12
13
14
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 12

def screen=(value)
  @screen
end

#selected_view(cv, data) ⇒ Object


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 123

def selected_view(cv, data)
  row_view = cv
  unless row_view
    if data[:cell_class]
      row_view = rmq.create!(data[:cell_class])
    elsif data[:cell_xml]
      row_view = inflate_row(data[:cell_xml])
      rmq.tag_all_from_resource_entry_name(row_view)
    else
      # Default is Sipmle List Item 1
      # TODO:  Possibly use Android::R::Layout::Simple_list_item_2 which has subtitle
      #https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/simple_list_item_2.xml
      row_view = inflate_row(Android::R::Layout::Simple_list_item_1)
    end
  end
  row_view
end

#update_view(view, data) ⇒ Object


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
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 92

def update_view(view, data)
  update = data[:update]
  if update.is_a?(Proc)
    update.call(out, data)
  elsif update.is_a?(Symbol) || update.is_a?(String)
    if find.screen.respond_to?(update)
      find.screen.send(update, view, data)
    else
      mp "Warning: #{find.screen.class} does not respond to #{update}"
    end
  elsif data[:properties]
    data[:properties].each do |k, v|
      if view.respond_to?("#{k}=")
        view.send("#{k}=", v)
      else
        mp "Warning: #{view.class} does not respond to #{k}="
      end
    end
  elsif view.is_a?(Potion::TextView)
    # Specific to use of Simple list item 1
    view.text = data[:title]
  elsif update
    mp "We don't know how to update your cell"
  end
end

#view(position, convert_view, parent) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 74

def view(position, convert_view, parent)
  data = item_data(position)
  out = selected_view(convert_view, data)
  update_view(out, data)
  if data[:action]
    find(out).on(:tap) do
      arguments = action_arguments data, position
      find.screen.send(data[:action], arguments, position) 
    end
  end
  out
end

#view_type_countObject


37
38
39
40
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 37

def view_type_count()
  # all custom items added up (+1 for non-custom)
  view_types.length + 1
end

#view_typesObject


118
119
120
121
# File 'lib/project/pro_motion/adapters/pm_base_adapter.rb', line 118

def view_types
  # unique cell_xmls and cell_classes
  data.map{ |i| i[:cell_xml] || i[:cell_class]}.compact.uniq
end