ProMotion 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -3,3 +3,18 @@ require 'motion/project'
3
3
  require 'bundler/gem_tasks'
4
4
  Bundler.setup
5
5
  Bundler.require
6
+ require 'motion-table'
7
+
8
+ Motion::Project::App.setup do |app|
9
+ # Use `rake config' to see complete project settings.
10
+ app.name = 'ProMotionTest'
11
+ app.version = "0.1.0"
12
+
13
+
14
+ # Devices
15
+ app.deployment_target = "4.3"
16
+ app.device_family = [:iphone, :ipad]
17
+
18
+ # Preload screens
19
+ # app.files = Dir.glob(File.join(app.project_dir, 'lib/**/*.rb')) | app.files
20
+ end
@@ -0,0 +1,5 @@
1
+ class AppDelegate < ProMotion::AppDelegateParent
2
+ def on_load(options)
3
+ open_screen TestScreen, nav_bar: true
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ class HomeScreen < ProMotion::Screen
2
+ title "Home Screen"
3
+
4
+ def on_load
5
+ @label = add_view UILabel.alloc.initWithFrame(CGRectMake(5, 5, 20, 20)), {
6
+ text: "This is awesome!",
7
+ font: UIFont.UIFont.systemFontOfSize(18)
8
+ }
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class TestScreen < ProMotion::TableScreen
2
+ title "Test Screen"
3
+
4
+ def on_load
5
+ @label = add_view UILabel.alloc.initWithFrame(CGRectMake(5, 5, 20, 20)), {
6
+ text: "This is awesome!",
7
+ font: UIFont.UIFont.systemFontOfSize(18)
8
+ }
9
+ end
10
+ end
data/lib/ProMotion.rb CHANGED
@@ -5,7 +5,13 @@ end
5
5
  require "ProMotion/version"
6
6
 
7
7
  Motion::Project::App.setup do |app|
8
- Dir.glob(File.join(File.dirname(__FILE__), "ProMotion/*.rb")).each do |file|
9
- app.files.unshift(file)
10
- end
8
+
9
+ # Dir.glob(File.join(File.dirname(__FILE__), "ProMotion/**/*.rb")).each do |file|
10
+ # app.files.unshift(file)
11
+ # end
12
+ app.files = Dir.glob(File.join(File.dirname(__FILE__), 'ProMotion/**/*.rb')) | app.files
13
+ app.files = Dir.glob(File.join(File.dirname(__FILE__), 'ProMotion/helpers/**/*.rb')) | app.files
14
+ app.files = Dir.glob(File.join(File.dirname(__FILE__), 'ProMotion/modules/**/*.rb')) | app.files
15
+ app.files = Dir.glob(File.join(File.dirname(__FILE__), 'ProMotion/ext/**/*.rb')) | app.files
16
+
11
17
  end
@@ -1,7 +1,60 @@
1
1
  module ProMotion
2
- class AppDelegate
3
- class << self
2
+ class AppDelegateParent
3
+ attr_accessor :window
4
+
5
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
6
+ return true if RUBYMOTION_ENV == "test"
7
+
8
+ Console.log(" Your AppDelegate (usually in app_delegate.rb) needs an on_load(options) method.", withColor: Console::RED_COLOR) unless self.respond_to? :on_load
4
9
 
10
+ on_load launchOptions
11
+
12
+ open_home_screen if has_home_screen
13
+
14
+ get_home_screen.on_opened if has_home_screen && get_home_screen.respond_to?(:on_opened)
15
+
16
+ true
17
+ end
18
+
19
+ def app_delegate
20
+ UIApplication.sharedApplication.delegate
21
+ end
22
+
23
+ def app_window
24
+ self.app_delegate.window
25
+ end
26
+
27
+ def load_root_view(new_view)
28
+ self.window ||= UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
29
+ self.window.rootViewController = new_view
30
+ self.window.makeKeyAndVisible
31
+ end
32
+
33
+ def open_screen(screen)
34
+ home(screen)
35
+ end
36
+
37
+ def home(screen)
38
+ screen = screen.new if screen.respond_to? :new
39
+ @home_screen = screen
40
+ end
41
+
42
+ def fresh_start(new_screen)
43
+ home(new_screen)
44
+ open_home_screen
45
+ end
46
+
47
+ def open_home_screen
48
+ root = get_home_screen.main_controller
49
+ load_root_view root
50
+ end
51
+
52
+ def get_home_screen
53
+ @home_screen
54
+ end
55
+
56
+ def has_home_screen
57
+ @home_screen.nil? == false
5
58
  end
6
59
  end
7
60
  end
@@ -0,0 +1,19 @@
1
+ module ProMotion
2
+ class Console
3
+ NAME = "RubyMotion::Console"
4
+ DEFAULT_COLOR = [ '', '' ]
5
+ RED_COLOR = [ "\e[0;31m", "\e[0m" ] # Must be in double quotes
6
+ GREEN_COLOR = [ "\e[0;32m", "\e[0m" ]
7
+ PURPLE_COLOR = [ "\e[0;35m", "\e[0m" ]
8
+
9
+ class << self
10
+ def log(log, withColor:color)
11
+ puts color[0] + NAME + log + color[1]
12
+ end
13
+
14
+ def log(log)
15
+ log(log, withColor: DEFAULT_COLOR)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Promotion
2
+ end
3
+ ::PM = ProMotion unless defined?(::PM)
@@ -0,0 +1,14 @@
1
+ module ProMotion
2
+ class Element
3
+ attr_accessor :view
4
+
5
+ def initialize(with_view)
6
+ self.view = with_view
7
+ self
8
+ end
9
+
10
+ def remove
11
+ self.view.removeFromSuperview
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,239 @@
1
+ module ProMotion
2
+ module ScreenElements
3
+ def add_element(view, attrs = {})
4
+ if attrs.length > 0
5
+ set_attributes(view, attrs)
6
+ end
7
+ self.view_controller.view.addSubview(view)
8
+ view
9
+ end
10
+
11
+ def remove_element(view)
12
+ view.removeFromSuperview
13
+ view = nil
14
+ nil
15
+ end
16
+
17
+ def bounds
18
+ return self.view_controller.view.bounds
19
+ end
20
+
21
+ def frame
22
+ return self.view_controller.view.frame
23
+ end
24
+
25
+ def view
26
+ return self.view_controller.view
27
+ end
28
+
29
+ def set_attributes(element, args = {})
30
+ args.each do |k, v|
31
+ element.send("#{k}=", v) if element.respond_to? "#{k}="
32
+ end
33
+ element
34
+ end
35
+ end
36
+
37
+ module ScreenNavigation
38
+ def open_screen(screen, args = {})
39
+ # Instantiate screen if given a class instead
40
+ screen = screen.new if screen.respond_to? :new
41
+ screen.add_nav_bar if args[:nav_bar]
42
+ screen.parent_screen = self
43
+
44
+ screen.main_controller.hidesBottomBarWhenPushed = true if args[:hide_tab_bar]
45
+
46
+ if args[:close_all]
47
+ fresh_start(screen)
48
+ elsif self.navigation_controller
49
+ screen.navigation_controller = self.navigation_controller
50
+ push_view_controller screen.view_controller
51
+ else
52
+ open_view_controller screen.main_controller
53
+ end
54
+
55
+ screen.on_opened if screen.respond_to? :on_opened
56
+ end
57
+
58
+ def fresh_start(screen)
59
+ app_delegate.fresh_start(screen)
60
+ end
61
+
62
+ def app_delegate
63
+ UIApplication.sharedApplication.delegate
64
+ end
65
+
66
+ def close_screen(args = {})
67
+ # Pop current view, maybe with arguments, if in navigation controller
68
+ if self.navigation_controller
69
+ self.navigation_controller.popViewControllerAnimated(true)
70
+ else
71
+ # What do we do now? Nothing to "pop"
72
+ end
73
+ end
74
+
75
+ def tab_bar_controller(*screens)
76
+ tab_bar_controller = UITabBarController.alloc.init
77
+
78
+ view_controllers = []
79
+ screens.each do |s|
80
+ if s.is_a? Screen
81
+ s = s.new if s.respond_to? :new
82
+ view_controllers << s.main_controller
83
+ else
84
+ Console.log("Non-Screen passed into tab_bar_controller: #{s.to_s}", withColor: Console::RED_COLOR)
85
+ end
86
+ end
87
+
88
+ tab_bar_controller.viewControllers = view_controllers
89
+ tab_bar_controller
90
+ end
91
+
92
+ def open_tab_bar(*screens)
93
+ tab_bar = tab_bar_controller(*screens)
94
+ open_view_controller tab_bar
95
+ screens.each do |s|
96
+ s.on_opened if s.respond_to? :on_opened
97
+ s.parent_screen = self if s.respond_to? "parent_screen="
98
+ end
99
+ tab_bar
100
+ end
101
+
102
+ def push_tab_bar(*screens)
103
+ tab_bar = tab_bar_controller(*screens)
104
+ push_view_controller tab_bar
105
+ screens.each do |s|
106
+ s.on_opened if s.respond_to? :on_opened
107
+ s.parent_screen = self if s.respond_to? "parent_screen="
108
+ end
109
+ tab_bar
110
+ end
111
+
112
+ def open_view_controller(vc)
113
+ UIApplication.sharedApplication.delegate.load_root_view vc
114
+ end
115
+
116
+ def push_view_controller(vc)
117
+ # vc.hidesBottomBarWhenPushed = true if args[:hide_tab_bar]
118
+ Console.log(" You need a nav_bar if you are going to push #{vc.to_s} onto it.", withColor: Console::RED_COLOR) unless self.navigation_controller
119
+ self.navigation_controller.pushViewController(vc, animated: true)
120
+ end
121
+ end
122
+ end
123
+
124
+ module ProMotion
125
+ # Instance methods
126
+ class Screen
127
+ include ProMotion::ScreenNavigation
128
+ include ProMotion::ScreenElements
129
+
130
+ attr_accessor :view_controller
131
+ attr_accessor :navigation_controller
132
+ attr_accessor :parent_screen
133
+ attr_accessor :first_screen
134
+ attr_accessor :tab_bar_item
135
+
136
+ def initialize(attrs = {})
137
+ attrs.each do |k, v|
138
+ self.send "#{k}=", v if self.respond_to? "#{k}="
139
+ end
140
+
141
+ self.load_view_controller
142
+
143
+ self.view_controller.title = self.title
144
+
145
+ self.add_nav_bar if attrs[:nav_bar]
146
+
147
+ self.on_load if self.respond_to? :on_load
148
+
149
+ self
150
+ end
151
+
152
+ def load_view_controller
153
+ self.view_controller ||= ViewController
154
+ end
155
+
156
+ def set_tab_bar_item(args = {})
157
+ self.tab_bar_item = args
158
+ refresh_tab_bar_item
159
+ end
160
+
161
+ def refresh_tab_bar_item
162
+ self.main_controller.tabBarItem = ProMotion::TabBar.tab_bar_item(self.tab_bar_item) if self.tab_bar_item
163
+ end
164
+
165
+ def add_nav_bar
166
+ unless self.navigation_controller
167
+ self.navigation_controller = NavigationController.alloc.initWithRootViewController(self.view_controller)
168
+ self.first_screen = true
169
+ end
170
+ end
171
+
172
+ def set_nav_bar_right_button(title, args={})
173
+ args[:style] ||= UIBarButtonItemStyleBordered
174
+ args[:target] ||= self
175
+ args[:action] ||= nil
176
+
177
+ right_button = UIBarButtonItem.alloc.initWithTitle(title, style: args[:style], target: args[:target], action: args[:action])
178
+ self.view_controller.navigationItem.rightBarButtonItem = right_button
179
+ end
180
+
181
+ def view_controller=(vc)
182
+ vc = vc.alloc.initWithNibName(nil, bundle:nil) if vc.respond_to? :alloc
183
+ if self.navigation_controller && self.first_screen?
184
+ @view_controller = vc
185
+ self.navigation_controller = NavigationController.alloc.initWithRootViewController(self.view_controller)
186
+ else
187
+ @view_controller = vc
188
+ end
189
+ @view_controller.screen = self if @view_controller.respond_to? "screen="
190
+
191
+ refresh_tab_bar_item
192
+ end
193
+
194
+ def first_screen?
195
+ self.first_screen == true
196
+ end
197
+
198
+ def set_view_controller(vc)
199
+ self.view_controller = vc
200
+ end
201
+
202
+ def view_will_appear(animated)
203
+ end
204
+
205
+ def view_did_appear(animated)
206
+ self.on_appear if self.respond_to? :on_appear
207
+ end
208
+
209
+ def title
210
+ self.class.send :get_title
211
+ end
212
+
213
+ def title=(new_title)
214
+ self.class.title = new_title
215
+ self.view_controller.title = new_title
216
+ end
217
+
218
+ def main_controller
219
+ return self.navigation_controller if self.navigation_controller
220
+ self.view_controller
221
+ end
222
+
223
+ end
224
+
225
+ # Class methods
226
+ class Screen
227
+ class << self
228
+ def title(t)
229
+ @title = t
230
+ end
231
+ def title=(t)
232
+ @title = t
233
+ end
234
+ def get_title
235
+ @title ||= self.to_s
236
+ end
237
+ end
238
+ end
239
+ end
@@ -0,0 +1,47 @@
1
+ module ProMotion
2
+ # Instance methods
3
+ class TableScreen < Screen
4
+ include MotionTable::PlainTable
5
+ include MotionTable::SearchableTable
6
+
7
+ def view
8
+ return self.view_controller.view
9
+ end
10
+
11
+ def load_view_controller
12
+ Console.log("- table_data method needed in table view screen.", withColor: Console::RED_COLOR) unless self.respond_to? :table_data
13
+ self.view_controller ||= TableViewController
14
+ self.view_controller.view = self.createTableViewFromData(self.table_data)
15
+ if self.class.get_searchable
16
+ self.makeSearchable(contentController: self.view_controller, searchBar: self.class.get_searchable_params)
17
+ end
18
+ end
19
+
20
+ def update_table_data
21
+ self.updateTableViewData(table_data)
22
+ end
23
+
24
+ class << self
25
+ def searchable(params={})
26
+ @searchable_params = params
27
+ @searchable = true
28
+ end
29
+
30
+ def get_searchable_params
31
+ @searchable_params ||= nil
32
+ end
33
+
34
+ def get_searchable
35
+ @searchable ||= false
36
+ end
37
+ end
38
+ end
39
+
40
+ class GroupedTableScreen < TableScreen
41
+ include MotionTable::GroupedTable
42
+ end
43
+
44
+ class SectionedTableScreen < TableScreen
45
+ include MotionTable::SectionedTable
46
+ end
47
+ end