android_query 0.0.1
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 +7 -0
- data/README.md +78 -0
- data/lib/android_query/android_query.rb +178 -0
- data/lib/android_query/version.rb +3 -0
- data/lib/android_query.rb +11 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 41d095320389479597e37525949a8bc4ce358f41
|
4
|
+
data.tar.gz: 557d9fb428ae43b7f9fc28e94a6c37f70c658b10
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4bfeeea7d9eccc1d1eb62072d5cf89aa66ad6d8b9aadac3381230a15f0e151748f05d25a774365b92e281ec942fb552c4f3e47b532dad9a7c84295d07fbe2e73
|
7
|
+
data.tar.gz: 497d4d4f6c90c9d3122b090d16a2aeaa784b1667793bd7e4dccf8a856b89aed7fa48fd6e738ba0ae605a2583c3603f19ac3799c79699380ae5918d459096e0cc
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# Android Query
|
2
|
+
|
3
|
+
This gem is in its very early development. Please don't use in production.
|
4
|
+
|
5
|
+
Currently, `android_query` only supports the `LinearLayout`, `EditText`, `TextView`, and `Button` views.
|
6
|
+
|
7
|
+
The goal of `android_query` is to make android development on RubyMotion fun, easy, and quick.
|
8
|
+
It's intended for developers who prefer to code their UIs rather than use a GUI editor.
|
9
|
+
|
10
|
+
`android_query` was inspired by the wonderful [rmq](http://github.com/infinitered/rmq/) gem.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'android_query', '~> 0.0.1'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install android_query
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
```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)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
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
|
+
|
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 = ""
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+

|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/aesmail/android_query.
|
75
|
+
|
76
|
+
## License
|
77
|
+
|
78
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -0,0 +1,178 @@
|
|
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
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
unless defined?(Motion::Project::Config)
|
4
|
+
raise "This gem works only within a RubyMotion android project."
|
5
|
+
end
|
6
|
+
|
7
|
+
lib_dir_path = File.dirname(File.expands_path(__FILE__))
|
8
|
+
|
9
|
+
Motion::Project::App.setup do |app|
|
10
|
+
app.files.unshift(Dir.glob(File.join(lib_dir_path, "android_query/**/*.rb")))
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: android_query
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Abdullah Esmail
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: This gem was inspired by the rmq gem for iOS
|
42
|
+
email:
|
43
|
+
- abdullah.esmail@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- README.md
|
49
|
+
- lib/android_query.rb
|
50
|
+
- lib/android_query/android_query.rb
|
51
|
+
- lib/android_query/version.rb
|
52
|
+
homepage: https://github.com/aesmail/android-query
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.6.11
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Making android development easy and enjoyable
|
76
|
+
test_files: []
|