-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainview.lua
More file actions
254 lines (193 loc) · 6.83 KB
/
mainview.lua
File metadata and controls
254 lines (193 loc) · 6.83 KB
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
module(..., package.seeall)
-- Main function - MUST return a display.newGroup()
function new()
local mime = require("mime")
local json = require("json")
local sign = require("sign")
local menu = require("menu")
local localGroup = display.newGroup()
local signs = {};
local loadTimer = nil;
-- API-key:4ecb1b6cbef818285e382fdf363177f1
local back = display.newImageRect("assets/back.jpg",610,420);
localGroup:insert(back);
back.x = display.contentCenterX;
back.y = display.contentCenterY;
back.alpha = .5;
local lblStation = display.newText("T - centralen",0,0,"Helvetica-Bold",20)
lblStation:setTextColor(255,245,71)
lblStation.y = screen.top + 12 ;
lblStation:setReferencePoint(display.CenterLeftReferencePoint)
lblStation.x = screen.left + 5;
lblStation.isVisible=false
lblStation.alpha = 1;
-- lblStation.blendMode = "add"
--lblStation.y = screen.bottom - 20
localGroup:insert(lblStation)
local loader = display.newImageRect("assets/loader.png",20,20);
loader.x = screen.right - 15;
loader.y = screen.bottom - 15;
loader:setFillColor(255,241,71)
local topMenu = menu.new();
localGroup:insert(topMenu)
local function flagForReload()
needsUpdate = true;
end
local function loginCallback(event)
loader.isVisible = false;
loadTimer = timer.performWithDelay(10000,flagForReload)
local count = 0;
if ( event.isError ) then
print( "Network error!")
needsUpdate = true;
count = 0;
else
lblStation.text = currentStation.name;
lblStation.isVisible = true;
local data = json.decode(event.response)
-- do with data what you want...
if(data == nil) then lblStation.text = event.response; lblStation:setReferencePoint(display.CenterLeftReferencePoint)
lblStation.x = screen.left + 5; return
end
if(#data.Departure.Metros.Metro == 0) then
for key,value in pairs(data.Departure.Metros) do
print(value);
if(signs[1] == nil and count < 2 ) then
signs[1] = sign.new();
signs[1].update(value.DisplayRow1,value.DisplayRow2)
localGroup:insert(signs[1])
elseif(signs[1] ~= nil and count < 2) then
signs[1].update(value.DisplayRow1,value.DisplayRow2)
end
--signs[key].isVisible = false;
count = count +1;
end
end
if(#data.Departure.Metros.Metro > 1) then
for key,value in pairs(data.Departure.Metros.Metro) do
print(value);
if(signs[key] == nil and count < 2 ) then
signs[key] = sign.new();
signs[key].update(value.DisplayRow1,value.DisplayRow2)
localGroup:insert(signs[key])
elseif(signs[key] ~= nil and count < 3) then
signs[key].update(value.DisplayRow1,value.DisplayRow2)
end
--signs[key].isVisible = false;
count = count +1;
end
end
--count = 2
end
print("COUNT: " .. count)
if(count == 1) then
signs[1].x = screen.left + 20;
signs[1].y = display.contentCenterY;
signs[1].isVisible = true;
if(signs[2]) then
signs[2].isVisible = false;
end
end
if(count > 1) then
signs[1].x = screen.left + 20;
signs[1].y = display.contentCenterY - 60 ;
signs[2].x = screen.left + 20;
signs[2].y = display.contentCenterY + 60 ;
signs[1].isVisible = true;
signs[2].isVisible = true;
end
return true
end
local lastLoadedStation = nil;
local function loadData(station)
if(loadTimer) then
timer.cancel(loadTimer)
end
if(lastLoadedStation ~= station) then
if(signs[1]) then signs[1].isVisible = false; end;
if(signs[2]) then signs[2].isVisible = false; end;
end
lastLoadedStation = station;
loader.isVisible = true;
local URL = "https://api.trafiklab.se/sl/realtid/GetDepartures.json?siteId=".. station.id .. "&key=4ecb1b6cbef818285e382fdf363177f1"
network.request( URL, "GET", loginCallback );
print("loading")
end
local function getDist(p1,p2)
local dx = p1.x-p2.x;
local dy = p1.y-p2.y;
return math.sqrt(dx * dx + dy * dy);
end
local function findClosestStation(pos)
if(needsUpdate == false) then return end;
needsUpdate = false;
-- local pos = {x=59.2,y=17.2};
local minDist = 10000;
local station = nil;
for key,value in pairs(stations) do
local dist = getDist(pos,{x=value["lat"],y=value["long"]});
value.dist = dist;
value.selected = false;
if(dist < minDist) then
minDist = dist;
station = value;
end
end
local function comp(a,b)
if(a.dist < b.dist) then
return a.dist
end
end
table.sort(stations , comp)
print(station.name)
if(currentStation ~= station) then topMenu.show(); end
currentStation = station;
loadData(station)
station.selected = true;
topMenu.update(stations[1],stations[2],stations[3],stations[4],1)
print(stations[1].name)
print(stations[2].name)
print(stations[3].name)
print(stations[4].name)
print(stations[5].name)
end
local locationHandler = function( event )
-- Check for error (user may have turned off Location Services)
if event.errorCode then
native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} )
print( "Location error: " .. tostring( event.errorMessage ) )
else
if(needsUpdate == true) then
-- native.showAlert("GPS","LAT:" .. event.latitude .. " LONG:" .. event.longitude ,{"OK"} )
end
findClosestStation({x=59.2984995379,y=17.99126573538});
-- findClosestStation({x=event.latitude,y=event.longitude});
end
end
local function menuSelectStation(station)
if(station) then
userStation = station;
end
if(userStation and userStation ~= currentStation) then
currentStation.selected = false;
currentStation = userStation;
userStation.selected = true;
loadData(userStation);
end
end
-- loadData();
unloadMe = function()
end
local function update()
for key,value in pairs(signs) do
value.animate()
end
loader.rotation = loader.rotation +5;
end
topMenu.selectCallback = menuSelectStation;
Runtime:addEventListener("touch",topMenu.show)
Runtime:addEventListener("enterFrame",update)
Runtime:addEventListener( "location", locationHandler )
-- MUST return a display.newGroup()
return localGroup
end