android_query 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2cac681d57bb85063a53184fa2da5889f2c5b6cf
4
- data.tar.gz: 0a02022cd528fe128f5d4e4b0c8796a5c9091556
3
+ metadata.gz: 1fae24679345a4398a3d4c74bfec81e04a949473
4
+ data.tar.gz: 562ab6af7cd492f6a4ae51bfbe9dff7a06074ea3
5
5
  SHA512:
6
- metadata.gz: 96a173ed0a4dae949e3ad96ecc0faac8324424c3f6327604aad7f106b8875618d8de9abeaa04caa1391279299ca45f405ab0373c4eda531bfec359039c2355f2
7
- data.tar.gz: d63d96e315e7dc598aab325e691cb53b3227093e8766598b711d9c6c6f536c956b0c9d25a0b35bd120e1bc191e5b9b5681f3bb68ad770cec8ccd98ec08abe1ab
6
+ metadata.gz: 02dc384ec6f0cbbd858e04bed96e1baaac1ed05e04f7fffbc50753f18f4a92c364ce707943ae0c133e0ff8919e1f60d2818f20d984b54e064dc9e135f162014c
7
+ data.tar.gz: 106b1946d4989ff99f1e64f560731e072f00ccbb5a96ccbabb48cfdc963a59593ae96236b18c29c69e5ae6d6424e0da44607d7f49897c6bf72110f035db1070e
data/README.md CHANGED
@@ -15,7 +15,7 @@ It's intended for developers who prefer to code their UIs rather than use a GUI
15
15
  Add this line to your application's Gemfile:
16
16
 
17
17
  ```ruby
18
- gem 'android_query', '~> 0.0.5'
18
+ gem 'android_query', '~> 0.0.6'
19
19
  ```
20
20
 
21
21
  And then execute:
@@ -33,90 +33,108 @@ Each view should be passed a style method.
33
33
 
34
34
  ```ruby
35
35
  class MainActivity < Android::App::Activity
36
- attr_accessor :aq, :counter
36
+ attr_accessor :aq
37
37
 
38
38
  def onCreate(savedInstanceState)
39
39
  super
40
- self.counter = 0
41
40
  self.aq = AndroidQuery.new(self, HomeStyle)
42
- self.aq.linear_layout(:top_layout) do |top|
43
- top.text_view(:phone_field)
44
- top.edit_text(:email_field)
45
- top.button(:submit_button)
46
- top.linear_layout(:counter_layout) do |counter_layout|
47
- counter_layout.button(:increment)
48
- counter_layout.button(:decrement)
41
+ aq.linear_layout(:top_layout) do |top|
42
+ top.image_button(:plumbing_button)
43
+ top.image_button(:electricity_button)
44
+ top.linear_layout(:directions) do |direction_layout|
45
+ direction_layout.text_view(:left_text)
46
+ direction_layout.button(:right_button)
49
47
  end
50
48
  end
51
49
  end
52
50
 
53
- def show_message(view)
54
- # toast options can be:
55
- # for gravity: :bottom, :right, :left, :center, :top, and their combinations
56
- # for length: :short, :long
57
- self.aq.toast("The counter is set at #{self.counter}", gravity: :bottom_right, length: :short)
51
+ def coffee_message(view)
52
+ aq.toast('This is a message for COFFEE LOVERS :)', gravity: :center)
58
53
  end
59
54
 
60
- def increment_counter(view)
61
- self.counter += 1
55
+ def random_thing(view)
56
+ puts "This should be printed when I click the button"
62
57
  end
63
58
 
64
- def decrement_counter(view)
65
- self.counter -= 1
59
+ def another_toast(view)
60
+ aq.toast("This is a purple", gravity: :top_right, length: :long)
66
61
  end
67
62
  end
68
63
  ```
64
+
69
65
  The previous code produces the following app:
66
+
70
67
  ![Sample Screenshot](screenshot.png)
71
68
 
72
69
 
73
70
  The following is the `HomeStyle` class that styles the screen:
74
71
  ```ruby
75
72
  class HomeStyle < AndroidMotionQuery::Stylesheet
76
- def top_layout(v)
77
- v.width = :mp # <-- :mp, :wc, or a number
78
- v.height = :mp
79
- v.orientation = :vertical # <-- or :horizontal
80
- v.weight_sum = 10
73
+ def top_layout(st)
74
+ st.width = :mp
75
+ st.height = 0
76
+ st.weight_sum = 4
77
+ st.orientation = :vertical
78
+ st.background_color = '#A87E54'
81
79
  end
82
80
 
83
- def phone_field(v)
84
- v.text = 'Hello My Style!'
85
- v.weight = 4
81
+ def plumbing_button(st)
82
+ st.width = :mp
83
+ st.margin_top = 10
84
+ st.margin_bottom = 10
85
+ st.background_image = 'bench'
86
+ st.click = :coffee_message
87
+ shared_button_styles(st)
86
88
  end
87
89
 
88
- def email_field(v)
89
- v.text = 'This is my email'
90
- v.weight = 1
90
+ def electricity_button(st)
91
+ st.width = :mp
92
+ st.background_image = 'flower'
93
+ st.click = :random_thing
94
+ shared_button_styles(st)
91
95
  end
92
96
 
93
- def submit_button(v)
94
- v.text = 'Click Me To See'
95
- v.weight = 5
96
- v.click = :show_message # <-- this would call the show_message(view) method on the activity
97
+ def shared_button_styles(st)
98
+ st.height = 0
99
+ st.padding = 0
100
+ st.margin_left = 10
101
+ st.margin_right = 10
102
+ st.scale_type = :fit_xy
103
+ st.weight = 1.5
97
104
  end
98
105
 
99
- def counter_layout(v)
100
- v.orientation = :horizontal
101
- v.width = :mp
102
- v.height = :wc
103
- v.weight_sum = 2
106
+ def directions(st)
107
+ st.orientation = :horizontal
108
+ st.weight = 1
109
+ st.weight_sum = 2
110
+ st.width = :mp
111
+ st.height = 0
112
+ st.margin_top = 10
104
113
  end
105
114
 
106
- def increment(v)
107
- v.text = '+ Increment'
108
- v.click = :increment_counter
109
- v.weight = 1
115
+ def left_text(st)
116
+ st.weight = 1
117
+ st.text = 'android_query is AWESOME!'
118
+ st.text_alignment = :center
110
119
  end
111
120
 
112
- def decrement(v)
113
- v.text = '- Decrement'
114
- v.click = :decrement_counter
115
- v.weight = 1
121
+ def right_button(st)
122
+ st.weight = 1
123
+ st.text = 'Click Me'
124
+ st.click = :another_toast
125
+ st.background_color = '#927FD5'
126
+ st.text_color = :white
116
127
  end
117
128
  end
118
129
  ```
119
130
 
131
+ ## Todo List
132
+ - [ ] Set automatic IDs for views
133
+ - [ ] Support all built-in android widgets (currently android_query supports 4 widgets)
134
+ - [ ] Support @string
135
+ - [x] Support @drawable
136
+ - [ ] Support easy animations
137
+
120
138
  ## Contributing
121
139
 
122
140
  Bug reports and pull requests are welcome on GitHub at https://github.com/aesmail/android_query.
@@ -1,3 +1,3 @@
1
1
  module AndroidQuery
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  module AndroidMotionQuery
2
2
  class View
3
- attr_accessor :view, :activity, :stylesheet, :style_name, :layout_params, :options
3
+ attr_accessor :view, :activity, :stylesheet, :style_name, :layout_params, :options, :extra
4
4
 
5
5
  def initialize(view, activity, stylesheet, style_name, layout_params, options = {})
6
6
  self.view = view
@@ -12,14 +12,22 @@ module AndroidMotionQuery
12
12
  parent: nil,
13
13
  }.merge(options)
14
14
  end
15
-
16
- def get
17
- self.view
18
- end
15
+
16
+ # convenience methods
17
+ def get; self.view end
18
+ def left; get.getLeft end
19
+ def right; get.getRight end
20
+ def bottom; get.getBottom end
21
+ def top; get.getTop end
22
+ def width; get.getWidth end
23
+ def height; get.getHeight end
24
+ def text; get.text end
25
+ def text=(t); get.text = t end
19
26
 
20
27
  def create_android_query_view(view, style_method, layout_params, options = {}, &block)
21
28
  aqv = View.new(view, self.activity, self.stylesheet, style_method, layout_params, options)
22
29
  self.stylesheet.apply_style_for(aqv, style_method, layout_params)
30
+ puts "Adding #{aqv.get} to #{self.get}"
23
31
  self.get.addView(aqv.get)
24
32
  block.call(aqv) if block_given?
25
33
  aqv
@@ -45,6 +53,20 @@ module AndroidMotionQuery
45
53
  view = Android::Widget::Button.new(self.activity)
46
54
  create_android_query_view(view, style_method, self.layout_params, {}, &block)
47
55
  end
56
+
57
+ def image_view(style_method, &block)
58
+ view = Android::Widget::ImageView.new(self.activity)
59
+ create_android_query_view(view, style_method, self.layout_params, {}, &block)
60
+ end
61
+
62
+ def image_button(style_method, &block)
63
+ view = Android::Widget::ImageButton.new(self.activity)
64
+ create_android_query_view(view, style_method, self.layout_params, {}, &block)
65
+ end
66
+
67
+ def new_view(view, style_method, &block)
68
+ create_android_query_view(view, style_method, self.layout_params, {}, &block)
69
+ end
48
70
  end
49
71
 
50
72
  class Stylesheet
@@ -52,12 +74,13 @@ module AndroidMotionQuery
52
74
  style_view = StylesheetElement.new(view, layout_params)
53
75
  self.send(style_name.to_s, style_view)
54
76
  view.get.setLayoutParams(style_view.params)
77
+ view.get.tag = view
55
78
  view
56
79
  end
57
80
  end
58
81
 
59
82
  class StylesheetElement
60
- attr_accessor :view, :params
83
+ attr_accessor :view, :params, :radius
61
84
 
62
85
  LAYOUT_SIZE_OPTIONS = {
63
86
  mp: Android::View::ViewGroup::LayoutParams::MATCH_PARENT,
@@ -74,7 +97,12 @@ module AndroidMotionQuery
74
97
  self.params = layout_params.new(LAYOUT_SIZE_OPTIONS[:mp], LAYOUT_SIZE_OPTIONS[:wc])
75
98
  self
76
99
  end
77
-
100
+
101
+ def id=(number)
102
+
103
+ self.view.get.id = number
104
+ end
105
+
78
106
  def text=(t)
79
107
  self.view.get.text = t
80
108
  end
@@ -109,9 +137,169 @@ module AndroidMotionQuery
109
137
  self.params.weight = number
110
138
  end
111
139
 
140
+ def padding_left=(number)
141
+ left, top, right, bottom = get_padding
142
+ self.padding = [number, top, right, bottom]
143
+ end
144
+
145
+ def padding_right=(number)
146
+ left, top, right, bottom = get_padding
147
+ self.padding = [left, top, number, bottom]
148
+ end
149
+
150
+ def padding_top=(number)
151
+ left, top, right, bottom = get_padding
152
+ self.padding = [left, number, right, bottom]
153
+ end
154
+
155
+ def padding_bottom=(number)
156
+ left, top, right, bottom = get_padding
157
+ self.padding = [left, top, right, number]
158
+ end
159
+
160
+ def padding=(number)
161
+ if number.class == Array && number.count == 4
162
+ left, top, right, bottom = number
163
+ self.view.get.setPadding(left, top, right, bottom)
164
+ elsif number.class == Fixnum
165
+ self.view.get.setPadding(number, number, number, number)
166
+ else
167
+ raise "Invalid value (#{number}) set as padding for #{self.view.get}"
168
+ end
169
+ end
170
+
171
+ def get_padding
172
+ v = self.view.get
173
+ [v.getPaddingLeft, v.getPaddingTop, v.getPaddingRight, v.getPaddingBottom]
174
+ end
175
+
176
+ def margin_left=(number)
177
+ left, top, right, bottom = get_margins
178
+ self.margin = [number, top, right, bottom]
179
+ end
180
+
181
+ def margin_right=(number)
182
+ left, top, right, bottom = get_margins
183
+ self.margin = [left, top, number, bottom]
184
+ end
185
+
186
+ def margin_top=(number)
187
+ left, top, right, bottom = get_margins
188
+ self.margin = [left, number, right, bottom]
189
+ end
190
+
191
+ def margin_bottom=(number)
192
+ left, top, right, bottom = get_margins
193
+ self.margin = [left, top, right, number]
194
+ end
195
+
196
+ def get_margins
197
+ lp = self.params
198
+ [lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin]
199
+ end
200
+
201
+ def margin=(number)
202
+ if number.class == Array && number.count == 4
203
+ left, top, right, bottom = number
204
+ self.params.setMargins(left, top, right, bottom)
205
+ elsif number.class == Fixnum
206
+ self.params.setMargins(number, number, number, number)
207
+ else
208
+ raise "Invalid value (#{number}) set as margin for #{self.view.get}"
209
+ end
210
+ end
211
+
212
+ def extra=(something)
213
+ self.view.extra = something
214
+ end
215
+
112
216
  def click=(method_name)
113
217
  self.view.get.onClickListener = AQClickListener.new(self.view.activity, method_name)
114
218
  end
219
+
220
+ # TODO find a solution for rounded corners (with or without images)
221
+ # def corner_radius=(radius)
222
+ # self.radius ||= radius
223
+ # drawable = self.view.get.getDrawable
224
+ # if drawable
225
+ # self.draw_image_with_radius(drawable, self.radius)
226
+ # else
227
+ # shape = Android::Graphics::GradientDrawable.new
228
+ # shape.cornerRadius = self.radius
229
+ # self.view.get.background = shape
230
+ # end
231
+ # end
232
+ # TODO find a solution for rounded corners (with or without images)
233
+ # def draw_image_with_radius(image, radius)
234
+ # self.radius ||= radius
235
+ # width = image.getWidth
236
+ # height = image.getHeight
237
+ # result = Android::Graphics::Bitmap.createBitmap(width, height, Android::Graphics::Bitmap::Config::ARGB_8888)
238
+ # canvas = Android::Graphics::Canvas.new(result)
239
+ # canvas.drawARGB(0, 0, 0, 0)
240
+ # paint = Android::Graphics::Paint.new
241
+ # paint.antiAlias = true
242
+ # paint.color = AQColor.parse_color('#000000')
243
+ # rect = Android::Graphics::Rect.new(0, 0, width, height)
244
+ # rect_f = Android::Graphics::RectF.new(rect)
245
+ # canvas.drawRoundRect(rect_f, self.radius, self.radius, paint)
246
+ # paint.xfermode = Android::Graphics::PorterDuffXfermode.new(Android::Graphics::PorterDuff::Mode::SRC_IN)
247
+ # canvas.drawBitmap(raw, rect, rect, paint)
248
+ # result
249
+ # end
250
+
251
+ def background_color=(color)
252
+ self.view.get.backgroundColor = AQColor.parse_color(color.to_s)
253
+ end
254
+
255
+ def background_image=(image_name)
256
+ context = self.view.get.getContext
257
+ resource_id = context.getResources.getIdentifier(image_name, "drawable", context.getPackageName)
258
+ self.view.get.setImageResource(resource_id)
259
+ end
260
+
261
+ def scale_type=(option)
262
+ scale_types = {
263
+ center: Android::Widget::ImageView::ScaleType::CENTER,
264
+ center_crop: Android::Widget::ImageView::ScaleType::CENTER_CROP,
265
+ center_inside: Android::Widget::ImageView::ScaleType::CENTER_INSIDE,
266
+ fit_center: Android::Widget::ImageView::ScaleType::FIT_CENTER,
267
+ fit_end: Android::Widget::ImageView::ScaleType::FIT_END,
268
+ fit_start: Android::Widget::ImageView::ScaleType::FIT_START,
269
+ fit_xy: Android::Widget::ImageView::ScaleType::FIT_XY,
270
+ matrix: Android::Widget::ImageView::ScaleType::MATRIX,
271
+ }
272
+ self.view.get.scaleType = scale_types[option]
273
+ end
274
+
275
+ def text_alignment=(alignment)
276
+ self.gravity = alignment
277
+ end
278
+
279
+ def text_color=(color)
280
+ self.view.get.textColor = AQColor.parse_color(color.to_s)
281
+ end
282
+
283
+ def gravity=(alignment)
284
+ gravity_options = {
285
+ top: Android::View::Gravity::TOP,
286
+ left: Android::View::Gravity::LEFT,
287
+ right: Android::View::Gravity::RIGHT,
288
+ bottom: Android::View::Gravity::BOTTOM,
289
+ center: Android::View::Gravity::CENTER,
290
+ bottom_right: Android::View::Gravity::BOTTOM | Android::View::Gravity::RIGHT,
291
+ bottom_left: Android::View::Gravity::BOTTOM | Android::View::Gravity::LEFT,
292
+ center_right: Android::View::Gravity::CENTER | Android::View::Gravity::RIGHT,
293
+ center_left: Android::View::Gravity::CENTER | Android::View::Gravity::LEFT,
294
+ top_right: Android::View::Gravity::TOP | Android::View::Gravity::RIGHT,
295
+ top_left: Android::View::Gravity::TOP | Android::View::Gravity::LEFT,
296
+ }
297
+ self.view.get.gravity = gravity_options[alignment]
298
+ end
299
+ end
300
+
301
+ class AQColor
302
+ def self.parse_color(hex); Android::Graphics::Color.parseColor(hex) end
115
303
  end
116
304
 
117
305
  class AQClickListener
@@ -129,7 +317,7 @@ end
129
317
 
130
318
 
131
319
  class AndroidQuery
132
- attr_accessor :activity, :stylesheet
320
+ attr_accessor :activity, :stylesheet, :root, :view_ids
133
321
 
134
322
  def initialize(activity, stylesheet)
135
323
  self.activity = activity
@@ -137,17 +325,27 @@ class AndroidQuery
137
325
  self
138
326
  end
139
327
 
140
- def create_android_query_view(view, style_method, layout_params, options = {})
141
- AndroidMotionQuery::View.new(view, self.activity, self.stylesheet, style_method, layout_params, options)
328
+ def create_android_query_view(view, style_method, layout_params, options = {}, &block)
329
+ self.root = AndroidMotionQuery::View.new(view, self.activity, self.stylesheet, style_method, layout_params, options)
330
+ self.stylesheet.apply_style_for(self.root, style_method, layout_params)
331
+ block.call(self.root) if block_given?
332
+ self.activity.setContentView(self.root.get)
333
+ end
334
+
335
+ def find(id)
336
+ self.root.get.findViewById(id).tag
142
337
  end
143
338
 
144
339
  def linear_layout(style_method, &block)
145
340
  view = Android::Widget::LinearLayout.new(self.activity)
146
341
  layout_params = Android::Widget::LinearLayout::LayoutParams
147
- aqv = create_android_query_view(view, style_method, layout_params, {})
148
- self.stylesheet.apply_style_for(aqv, style_method, layout_params)
149
- block.call(aqv) if block_given?
150
- self.activity.setContentView(aqv.get)
342
+ create_android_query_view(view, style_method, layout_params, {}, &block)
343
+ end
344
+
345
+ def relative_layout(style_method, &block)
346
+ view = Android::Widget::RelativeLayout.new(self.activity)
347
+ layout_params = Android::Widget::RelativeLayout::Params
348
+ create_android_query_view(view, style_method, layout_params, {}, &block)
151
349
  end
152
350
 
153
351
  def toast(text, options = {})
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: android_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdullah Esmail
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-05 00:00:00.000000000 Z
11
+ date: 2017-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake