android_query 0.0.4 → 0.0.5

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: 17844db6657c5dc25029a6ed1c13797a3b054bc0
4
- data.tar.gz: a6459ae1d7bdec869b05f952e3bf874c56680eef
3
+ metadata.gz: 2cac681d57bb85063a53184fa2da5889f2c5b6cf
4
+ data.tar.gz: 0a02022cd528fe128f5d4e4b0c8796a5c9091556
5
5
  SHA512:
6
- metadata.gz: 155b2c8cc1f22e5b94c4d924d2ffa44d3dd54afac25467840c429dbef642ea6d44ed6d9a0ab5cfc0419e7205eca4f263795ae6e05ac4acde04eee7e03e193230
7
- data.tar.gz: f53af793423a174ac1d0a1eda864d19ca5f07177f34e060c9b4da737a4044099fa1e64ccd71a526dfba6402c651bde2e59049cbb48d685e428d7cd557a88b59b
6
+ metadata.gz: 96a173ed0a4dae949e3ad96ecc0faac8324424c3f6327604aad7f106b8875618d8de9abeaa04caa1391279299ca45f405ab0373c4eda531bfec359039c2355f2
7
+ data.tar.gz: d63d96e315e7dc598aab325e691cb53b3227093e8766598b711d9c6c6f536c956b0c9d25a0b35bd120e1bc191e5b9b5681f3bb68ad770cec8ccd98ec08abe1ab
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.4'
18
+ gem 'android_query', '~> 0.0.5'
19
19
  ```
20
20
 
21
21
  And then execute:
@@ -28,47 +28,95 @@ Or install it yourself as:
28
28
 
29
29
  ## Usage
30
30
 
31
+ The general rule is to create a top-level layout and add views to it.
32
+ Each view should be passed a style method.
33
+
31
34
  ```ruby
32
- def onCreate(savedInstanceState)
33
- super
34
- @counter = 0
35
- self.aq = AndroidQuery.new(self)
36
- aq.layout(:linear, weight_sum: 10, w: :mp, h: :mp) do |linear|
37
- aq.edit_text(linear, id: 10, w: :mp, h: :wc)
38
- aq.text_view(linear, id: 11, text: "Hello Android Query!", w: :mp, h: :wc, weight: 8)
39
- aq.layout(:linear, parent: linear, weight_sum: 2, weight: 1, orientation: :h) do |buttons_layout|
40
- aq.button(buttons_layout, text: "+ (plus)", w: :wc, h: :mp, weight: 1, click: :increase_counter)
41
- aq.button(buttons_layout, text: "- (minus)", w: :wc, h: :mp, weight: 1, click: :decrease_counter)
42
- end
43
- aq.layout(:linear, parent: linear, weight_sum: 2, weight: 1) do |sweet|
44
- aq.button(sweet, id: 12, text: "This is SO SWEET!", w: :mp, h: :mp, click: :toast_me, weight: 1)
45
- aq.button(sweet, id: 13, text: "Change Label", w: :mp, h: :mp, click: :show_message, weight: 1)
35
+ class MainActivity < Android::App::Activity
36
+ attr_accessor :aq, :counter
37
+
38
+ def onCreate(savedInstanceState)
39
+ super
40
+ self.counter = 0
41
+ 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)
49
+ end
46
50
  end
47
51
  end
52
+
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)
58
+ end
59
+
60
+ def increment_counter(view)
61
+ self.counter += 1
62
+ end
63
+
64
+ def decrement_counter(view)
65
+ self.counter -= 1
66
+ end
48
67
  end
68
+ ```
69
+ The previous code produces the following app:
70
+ ![Sample Screenshot](screenshot.png)
49
71
 
50
- def toast_me(view)
51
- aq.toast("You've been toasted #{@counter} times!", gravity: :center)
52
- end
53
-
54
- def increase_counter(view)
55
- @counter += 1
56
- end
57
-
58
- def decrease_counter(view)
59
- @counter -= 1
60
- end
61
72
 
62
- def show_message(view)
63
- my_text = aq.find(10)
64
- my_label = aq.find(11)
65
- my_label.text = my_text.text
66
- my_text.text = ""
73
+ The following is the `HomeStyle` class that styles the screen:
74
+ ```ruby
75
+ 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
81
+ end
82
+
83
+ def phone_field(v)
84
+ v.text = 'Hello My Style!'
85
+ v.weight = 4
86
+ end
87
+
88
+ def email_field(v)
89
+ v.text = 'This is my email'
90
+ v.weight = 1
91
+ end
92
+
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
+ end
98
+
99
+ def counter_layout(v)
100
+ v.orientation = :horizontal
101
+ v.width = :mp
102
+ v.height = :wc
103
+ v.weight_sum = 2
104
+ end
105
+
106
+ def increment(v)
107
+ v.text = '+ Increment'
108
+ v.click = :increment_counter
109
+ v.weight = 1
110
+ end
111
+
112
+ def decrement(v)
113
+ v.text = '- Decrement'
114
+ v.click = :decrement_counter
115
+ v.weight = 1
116
+ end
67
117
  end
68
118
  ```
69
119
 
70
- ![Sample Screenshot](screenshot.png)
71
-
72
120
  ## Contributing
73
121
 
74
122
  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.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,182 @@
1
+ module AndroidMotionQuery
2
+ class View
3
+ attr_accessor :view, :activity, :stylesheet, :style_name, :layout_params, :options
4
+
5
+ def initialize(view, activity, stylesheet, style_name, layout_params, options = {})
6
+ self.view = view
7
+ self.activity = activity
8
+ self.stylesheet = stylesheet
9
+ self.style_name = style_name
10
+ self.layout_params = layout_params
11
+ self.options = {
12
+ parent: nil,
13
+ }.merge(options)
14
+ end
15
+
16
+ def get
17
+ self.view
18
+ end
19
+
20
+ def create_android_query_view(view, style_method, layout_params, options = {}, &block)
21
+ aqv = View.new(view, self.activity, self.stylesheet, style_method, layout_params, options)
22
+ self.stylesheet.apply_style_for(aqv, style_method, layout_params)
23
+ self.get.addView(aqv.get)
24
+ block.call(aqv) if block_given?
25
+ aqv
26
+ end
27
+
28
+ def linear_layout(style_method, &block)
29
+ view = Android::Widget::LinearLayout.new(self.activity)
30
+ lp = Android::Widget::LinearLayout::LayoutParams
31
+ create_android_query_view(view, style_method, lp, {}, &block)
32
+ end
33
+
34
+ def text_view(style_method, &block)
35
+ view = Android::Widget::TextView.new(self.activity)
36
+ create_android_query_view(view, style_method, self.layout_params, {}, &block)
37
+ end
38
+
39
+ def edit_text(style_method, &block)
40
+ view = Android::Widget::EditText.new(self.activity)
41
+ create_android_query_view(view, style_method, self.layout_params, {}, &block)
42
+ end
43
+
44
+ def button(style_method, &block)
45
+ view = Android::Widget::Button.new(self.activity)
46
+ create_android_query_view(view, style_method, self.layout_params, {}, &block)
47
+ end
48
+ end
49
+
50
+ class Stylesheet
51
+ def apply_style_for(view, style_name, layout_params)
52
+ style_view = StylesheetElement.new(view, layout_params)
53
+ self.send(style_name.to_s, style_view)
54
+ view.get.setLayoutParams(style_view.params)
55
+ view
56
+ end
57
+ end
58
+
59
+ class StylesheetElement
60
+ attr_accessor :view, :params
61
+
62
+ LAYOUT_SIZE_OPTIONS = {
63
+ mp: Android::View::ViewGroup::LayoutParams::MATCH_PARENT,
64
+ wc: Android::View::ViewGroup::LayoutParams::WRAP_CONTENT,
65
+ }
66
+
67
+ ORIENTATION_OPTIONS = {
68
+ vertical: Android::Widget::LinearLayout::VERTICAL,
69
+ horizontal: Android::Widget::LinearLayout::HORIZONTAL,
70
+ }
71
+
72
+ def initialize(view, layout_params)
73
+ self.view = view
74
+ self.params = layout_params.new(LAYOUT_SIZE_OPTIONS[:mp], LAYOUT_SIZE_OPTIONS[:wc])
75
+ self
76
+ end
77
+
78
+ def text=(t)
79
+ self.view.get.text = t
80
+ end
81
+
82
+ def width=(w)
83
+ if w == :mp || w == :wc
84
+ self.params.width = LAYOUT_SIZE_OPTIONS[w]
85
+ else
86
+ self.params.width = w
87
+ end
88
+ end
89
+
90
+ def height=(h)
91
+ if h == :mp || h == :wc
92
+ self.params.height = LAYOUT_SIZE_OPTIONS[h]
93
+ else
94
+ self.params.height = h
95
+ end
96
+ end
97
+
98
+ def orientation=(o)
99
+ if o == :vertical || o == :horizontal
100
+ self.view.get.orientation = ORIENTATION_OPTIONS[o]
101
+ end
102
+ end
103
+
104
+ def weight_sum=(number)
105
+ self.view.get.weightSum = number
106
+ end
107
+
108
+ def weight=(number)
109
+ self.params.weight = number
110
+ end
111
+
112
+ def click=(method_name)
113
+ self.view.get.onClickListener = AQClickListener.new(self.view.activity, method_name)
114
+ end
115
+ end
116
+
117
+ class AQClickListener
118
+ attr_accessor :activity, :method_name
119
+ def initialize(activity, method_name)
120
+ self.activity = activity
121
+ self.method_name = method_name
122
+ end
123
+
124
+ def onClick(view)
125
+ self.activity.send(self.method_name.to_s, view)
126
+ end
127
+ end
128
+ end
129
+
130
+
131
+ class AndroidQuery
132
+ attr_accessor :activity, :stylesheet
133
+
134
+ def initialize(activity, stylesheet)
135
+ self.activity = activity
136
+ self.stylesheet = stylesheet.new
137
+ self
138
+ end
139
+
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)
142
+ end
143
+
144
+ def linear_layout(style_method, &block)
145
+ view = Android::Widget::LinearLayout.new(self.activity)
146
+ 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)
151
+ end
152
+
153
+ def toast(text, options = {})
154
+ options = {
155
+ gravity: :bottom,
156
+ length: :short
157
+ }.merge(options)
158
+
159
+ gravity_options = {
160
+ top: Android::View::Gravity::TOP,
161
+ left: Android::View::Gravity::LEFT,
162
+ right: Android::View::Gravity::RIGHT,
163
+ bottom: Android::View::Gravity::BOTTOM,
164
+ center: Android::View::Gravity::CENTER,
165
+ bottom_right: Android::View::Gravity::BOTTOM | Android::View::Gravity::RIGHT,
166
+ bottom_left: Android::View::Gravity::BOTTOM | Android::View::Gravity::LEFT,
167
+ center_right: Android::View::Gravity::CENTER | Android::View::Gravity::RIGHT,
168
+ center_left: Android::View::Gravity::CENTER | Android::View::Gravity::LEFT,
169
+ top_right: Android::View::Gravity::TOP | Android::View::Gravity::RIGHT,
170
+ top_left: Android::View::Gravity::TOP | Android::View::Gravity::LEFT,
171
+ }
172
+
173
+ length_options = {
174
+ short: Android::Widget::Toast::LENGTH_SHORT,
175
+ long: Android::Widget::Toast::LENGTH_LONG,
176
+ }
177
+
178
+ the_toast = Android::Widget::Toast.makeText(self.activity, text, length_options[options[:length]])
179
+ the_toast.setGravity(gravity_options[options[:gravity]], 0, 0)
180
+ the_toast.show
181
+ end
182
+ end
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.4
4
+ version: 0.0.5
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-03 00:00:00.000000000 Z
11
+ date: 2017-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -34,8 +34,8 @@ files:
34
34
  - LICENSE.txt
35
35
  - README.md
36
36
  - lib/android_query.rb
37
- - lib/android_query/android_query.rb
38
37
  - lib/android_query/version.rb
38
+ - lib/android_query/views.rb
39
39
  homepage: https://github.com/aesmail/android-query
40
40
  licenses:
41
41
  - MIT
@@ -1,178 +0,0 @@
1
- class AndroidQuery
2
- attr_accessor :activity
3
-
4
- def initialize(source)
5
- self.activity = source
6
- end
7
-
8
- def layout(layout_sym, options = {}, &block)
9
- layout_info = get_layout(layout_sym, options)
10
- my_layout = create_layout_with_params(layout_info, options)
11
- block_layout = {
12
- layout: my_layout,
13
- layout_class: layout_info[:layout_class],
14
- params_class: layout_info[:params_class],
15
- layout_sym: layout_sym,
16
- parent: layout_info[:parent],
17
- }
18
- block.call(block_layout)
19
- block_layout[:parent][:layout].addView(my_layout) if block_layout[:parent]
20
- set_content(block_layout) if block_layout[:parent].nil?
21
- block_layout
22
- end
23
-
24
- def text_view(layout, options = {})
25
- tv = Android::Widget::TextView.new(self.activity)
26
- create_view_layout_params(tv, layout, options)
27
- end
28
-
29
- def edit_text(layout, options = {})
30
- et = Android::Widget::EditText.new(self.activity)
31
- create_view_layout_params(et, layout, options)
32
- end
33
-
34
- def button(layout, options = {})
35
- btn = Android::Widget::Button.new(self.activity)
36
- create_view_layout_params(btn, layout, options)
37
- end
38
-
39
- def set_content(layout, params = {})
40
- self.activity.setContentView(layout[:layout])
41
- end
42
-
43
- def get_layout(layout, options)
44
- options = merge_options(options)
45
- layout_and_params = case layout
46
- when :linear
47
- [Android::Widget::LinearLayout, Android::Widget::LinearLayout::LayoutParams]
48
- when :relative
49
- [Android::Widget::RelativeLayout, Android::Widget::RelativeLayout::LayoutParams]
50
- when :absolute
51
- [Android::Widget::AbsoluteLayout, Android::Widget::AbsoluteLayout::LayoutParams]
52
- else
53
- layout
54
- end
55
- {
56
- layout: nil,
57
- layout_class: layout_and_params[0],
58
- params_class: layout_and_params[1],
59
- layout_sym: layout,
60
- parent: options[:parent],
61
- }
62
- end
63
-
64
- def merge_options(options)
65
- {
66
- w: :mp,
67
- h: :wc,
68
- orientation: :vertical,
69
- weight: 0,
70
- weight_sum: 0,
71
- parent: nil,
72
- click: nil,
73
- id: nil,
74
- }.merge(options)
75
- end
76
-
77
- def get_width(options, layout)
78
- case options[:w]
79
- when :mp, :match_parent
80
- layout[:params_class]::MATCH_PARENT
81
- when :wc, :wrap_content
82
- layout[:params_class]::WRAP_CONTENT
83
- else
84
- options[:w]
85
- end
86
- end
87
-
88
- def get_height(options, layout)
89
- case options[:h]
90
- when :mp, :match_parent
91
- layout[:params_class]::MATCH_PARENT
92
- when :wc, :wrap_content
93
- layout[:params_class]::WRAP_CONTENT
94
- else
95
- options[:h]
96
- end
97
- end
98
-
99
- def get_orientation(options, layout)
100
- case options[:orientation]
101
- when :vertical, :v
102
- layout[:layout_class]::VERTICAL
103
- when :horizontal, :h
104
- layout[:layout_class]::HORIZONTAL
105
- else
106
- raise 'Please set either :horizontal or :vertical for the orientation'
107
- end
108
- end
109
-
110
- def create_layout_with_params(layout, options)
111
- options = merge_options(options)
112
- width = get_width(options, layout)
113
- height = get_height(options, layout)
114
- orientation = get_orientation(options, layout)
115
- my_layout = layout[:layout_class].new(self.activity)
116
- layout_params = layout[:params_class].new(width, height)
117
- layout_params.weight = options[:weight] if layout[:layout_sym] == :linear
118
- my_layout.setLayoutParams(layout_params)
119
- my_layout.setOrientation(orientation) if layout[:layout_sym] == :linear
120
- my_layout.setWeightSum(options[:weight_sum]) if layout[:layout_sym] == :linear
121
- my_layout
122
- end
123
-
124
- def create_view_layout_params(view, layout, options)
125
- options = merge_options(options)
126
- width = get_width(options, layout)
127
- height = get_height(options, layout)
128
- layout_params = layout[:params_class].new(width, height)
129
- layout_params.weight = options[:weight]
130
- view.setText(options[:text]) unless options[:text].nil?
131
- view.setLayoutParams(layout_params)
132
- view.onClickListener = AQClickListener.new(self.activity, options) if options[:click]
133
- view.setId(options[:id]) if options[:id]
134
- layout[:layout].addView(view)
135
- view
136
- end
137
-
138
- def find(view_id)
139
- self.activity.findViewById(view_id)
140
- end
141
-
142
- def toast(text, options = {})
143
- options = {
144
- gravity: :bottom,
145
- length: :short
146
- }.merge(options)
147
-
148
- gravity_options = {
149
- top: Android::View::Gravity::TOP,
150
- left: Android::View::Gravity::LEFT,
151
- right: Android::View::Gravity::RIGHT,
152
- bottom: Android::View::Gravity::BOTTOM,
153
- center: Android::View::Gravity::CENTER,
154
- }
155
-
156
- length_options = {
157
- short: Android::Widget::Toast::LENGTH_SHORT,
158
- long: Android::Widget::Toast::LENGTH_LONG,
159
- }
160
-
161
- the_toast = Android::Widget::Toast.makeText(self.activity, text, length_options[options[:length]])
162
- the_toast.setGravity(gravity_options[options[:gravity]], 0, 0)
163
- the_toast.show
164
- end
165
- end
166
-
167
- class AQClickListener
168
- attr_accessor :activity, :options
169
-
170
- def initialize(activity, options)
171
- self.activity = activity
172
- self.options = options
173
- end
174
-
175
- def onClick(view)
176
- self.activity.send(self.options[:click], view)
177
- end
178
- end