forked from coronalabs/framework-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tableView.lua
372 lines (303 loc) · 9.3 KB
/
tableView.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
-- Copyright (C) 2013 Corona Inc. All Rights Reserved.
-- File: newTableView unit test.
local widget = require( "widget" )
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
--Forward reference for test function timer
local testTimer = nil
local USE_ANDROID_THEME = false
local USE_IOS7_THEME = true
--widget.setTheme( "widget_theme_ios" )
local isGraphicsV1 = ( 1 == display.getDefault( "graphicsCompatibility" ) )
local tableSeparatorColor = { 0.86, 0.86, 0.86, 1 }
if isGraphicsV1 then
widget._convertColorToV1( tableSeparatorColor )
end
local tableView
function scene:createScene( event )
local group = self.view
-- Test android theme
if USE_ANDROID_THEME then
widget.setTheme( "widget_theme_android" )
end
--Display an iOS style background
local background
local xAnchor, yAnchor
if not isGraphicsV1 then
xAnchor = display.contentCenterX
yAnchor = display.contentCenterY
else
xAnchor = 0
yAnchor = 0
end
if USE_IOS7_THEME then
background = display.newRect( xAnchor, yAnchor, display.contentWidth, display.contentHeight )
else
background = display.newImage( "unitTestAssets/background.png" )
background.x, background.y = xAnchor, yAnchor
end
group:insert( background )
if USE_IOS7_THEME then
-- create a white background, 40px tall, to mask / hide the scrollView
local topMask = display.newRect( 0, 0, display.contentWidth, 40 )
topMask:setFillColor( 235, 235, 235, 255 )
group:insert( topMask )
end
local backButtonPosition = 5
local backButtonSize = 52
local fontUsed = native.systemFont
local textFontUsed = native.systemFontBold
if USE_IOS7_THEME then
backButtonPosition = 0
backButtonSize = 40
fontUsed = "HelveticaNeue-Light"
textFontUsed = "HelveticaNeue-Light"
end
-- Button to return to unit test listing
local returnToListing = widget.newButton
{
id = "returnToListing",
left = 0,
top = backButtonPosition,
label = "Exit",
labelAlign = "center",
fontSize = 18,
width = 200, height = backButtonSize,
cornerRadius = 8,
onRelease = function() storyboard.gotoScene( "unitTestListing" ) end;
}
returnToListing.x = display.contentCenterX
group:insert( returnToListing )
----------------------------------------------------------------------------------------------------------------
-- START OF UNIT TEST
----------------------------------------------------------------------------------------------------------------
--Toggle these defines to execute tests. NOTE: It is recommended to only enable one of these tests at a time
local TEST_SCROLL_TO_Y = false
local TEST_SCROLL_TO_INDEX = false
local TEST_DELETE_SINGLE_ROW = false
local TEST_DELETE_ALL_ROWS = false
local TEST_GET_CONTENT_POSITION = false
local TEST_REMOVE_AND_RECREATE = false
-- Listen for tableView events
local function tableViewListener( event )
local phase = event.phase
local direction = event.direction
if "began" == phase then
--print( "Began" )
elseif "moved" == phase then
--print( "Moved" )
elseif "ended" == phase then
--print( "Ended" )
end
if event.limitReached then
if "up" == direction then
print( "Reached Top Limit" )
elseif "down" == direction then
print( "Reached Bottom Limit" )
elseif "left" == direction then
print( "Reached Left Limit" )
elseif "right" == direction then
print( "Reached Right Limit" )
end
end
return true
end
local noCategories = 0
-- Handle row rendering
local function onRowRender( event )
local phase = event.phase
local row = event.row
--print( "Rendering row with id:", row.id )
--print( #tableView._view._rows )
local rowTitleText = "Row " .. row.index
if row.isCategory then
noCategories = noCategories + 1
rowTitleText = "Category"
end
local rowTitle
if USE_IOS7_THEME and not row.isCategory then
rowTitle = display.newText( row, rowTitleText, 0, 0, "HelveticaNeue-Light", 17 )
elseif USE_IOS7_THEME and row.isCategory then
rowTitle = display.newText( row, rowTitleText, 0, 0, "HelveticaNeue", 14 )
else
rowTitle = display.newText( row, rowTitleText, 0, 0, nil, 14 )
end
rowTitle.x = ( rowTitle.contentWidth * 0.5 + 15 )
rowTitle.y = row.contentHeight * 0.5
rowTitle.anchorY = 0.8
rowTitle:setFillColor( 0, 0, 0 )
if not row.isCategory then
--print( row.index )
local spinner = widget.newSpinner{}
spinner.x = row.x + ( row.contentWidth * 0.5 ) - ( spinner.contentWidth * 0.5 )
spinner.y = row.contentHeight * 0.5
spinner:scale( 0.5, 0.5 )
row:insert( spinner )
--spinner:start()
end
end
-- Handle touches on the row
local function onRowTouch( event )
local phase = event.phase
local row = event.target
if "swipeRight" == phase then
print( "Swiped right on row with index: ", row.index )
elseif "swipeLeft" == phase then
print( "Swiped left on row with id: ", row.id )
elseif "tap" == phase then
print( "Tapped on row with id:", row.id )
elseif "press" == phase then
print( "Pressed row with id: ", row.id )
-- Set the row's default/over color's
--row:setRowColor( { default = { 255, 0, 0 }, over = { 0, 0, 255 } } )
print( row.params.rowIdentifier )
elseif "cancelled" == phase then
print( "Cancelled event on row with index: ", row.index )
elseif "release" == phase then
print( "Released row with index: ", row.index )
-- Test removing all rows and re-adding 20 more
if TEST_REMOVE_AND_RECREATE then
timer.performWithDelay( 500, function()
tableView:deleteAllRows()
-- Create 100 rows
for i = 1, 20 do
local isCategory = false
local rowHeight = 40
--local rowColor =
--{
-- default = { 255, 255, 255 }
--}
local lineColor = { 0.86, 0.86, 0.86, 1 }
-- Make some rows categories
if i == 8 or i == 25 or i == 50 or i == 75 then
isCategory = true
rowHeight = 24
--rowColor =
--{
-- default = { 150, 160, 180, 200 },
--}
end
-- Insert the row into the tableView
tableView:insertRow
{
id = "row:" .. i,
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
}
end
end)
end
end
end
-- Create a tableView
tableView = widget.newTableView
{
top = 40,
left = 0,
x = 160,
y = 260,
width = display.contentWidth,
height = display.contentHeight - 40,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
}
group:insert( tableView )
-- Create 200 rows
for i = 1, 200 do
local isCategory = false
local rowHeight = 40
local rowColor = nil
if not USE_IOS7_THEME then
--rowColor = {
-- default = { 255, 255, 255 },
-- over = { 217, 217, 217 },
--}
end
local lineColor = tableSeparatorColor;
-- Make some rows categories
---[[
if i == 1 or i == 4 or i == 8 or i == 22 or i == 28 or i == 35 or i == 45 then
isCategory = true
rowHeight = 24
--rowHeight = 47
if not USE_IOS7_THEME then
--rowColor =
--{
-- default = { 150, 160, 180, 200 },
--}
end
end
--]]
local rowParams = {
rowIdentifier = "testing"..i,
}
-- Insert the row into the tableView
tableView:insertRow
{
id = "row:" .. i,
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
params = rowParams
}
end
----------------------------------------------------------------------------------------------------------------
-- TESTS
----------------------------------------------------------------------------------------------------------------
-- Test to scroll list to Y position
if TEST_SCROLL_TO_Y then
testTimer = timer.performWithDelay( 1000, function()
tableView:scrollToY{ y = -300, time = 6000 }
end, 1 )
end
-- Test getting the content position
if TEST_GET_CONTENT_POSITION then
local function getPosition()
print( "tableView content position is: ", tableView:getContentPosition() )
end
tableView:scrollToY{ y = - 300, time = 600, onComplete = getPosition }
end
-- Test to scroll list to index
if TEST_SCROLL_TO_INDEX then
local currentIndex = 1
local testIndexes = { 31, 1, 7, 168, 4, 14, 2, 8 }
local function scrollToNextIndex()
print( "Scrolled to row index:", testIndexes[currentIndex] )
if currentIndex < #testIndexes then
currentIndex = currentIndex + 1
end
timer.performWithDelay( 1500, function()
tableView:scrollToIndex( testIndexes[currentIndex], 1000, scrollToNextIndex )
end)
end
testTimer = timer.performWithDelay( 1000, function()
tableView:scrollToIndex( testIndexes[currentIndex], 1000, scrollToNextIndex )
end)
end
-- Test deleting single row
if TEST_DELETE_SINGLE_ROW then
testTimer = timer.performWithDelay( 1000, function()
tableView:deleteRow( 5 ) --Delete Row 5
end, 1 )
end
-- Test delete all rows
if TEST_DELETE_ALL_ROWS then
testTimer = timer.performWithDelay( 1000, function()
tableView:deleteAllRows() --No rows after execution
end, 1 )
end
end
function scene:didExitScene( event )
--Cancel test timer if active
if testTimer ~= nil then
timer.cancel( testTimer )
testTimer = nil
end
storyboard.removeAll()
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "didExitScene", scene )
return scene