Advertisement
reshetnikoff

Untitled

Aug 1st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.04 KB | None | 0 0
  1. local composer = require("composer")
  2. local widget = require("widget")
  3. local json = require("json");
  4. http = require("socket.http")
  5.  
  6. local jsonRawData = http.request("http://ezmarketing.ru/fitness1/test.json")
  7. local jsonData = json.decode(jsonRawData)
  8.  
  9. function saveSettings(t, filename)
  10.   local path = system.pathForFile(filename, system.ResourceDirectory);
  11.   local file = io.open(path, "w");
  12.   if (file) then
  13.     local contents = json.encode(t);
  14.     file:write(contents);
  15.     io.close(file);
  16.     return true
  17.   else
  18.     return false
  19.   end
  20. end
  21.  
  22.  
  23.  
  24. saveSettings(jsonData, "settings.json")
  25.  
  26. settings = jsonData
  27.  
  28. local scene = composer.newScene()
  29.  
  30. -- -----------------------------------------------------------------------------------
  31. -- Code outside of the scene event functions below will only be executed ONCE unless
  32. -- the scene is removed entirely (not recycled) via "composer.removeScene()"
  33. -- -----------------------------------------------------------------------------------
  34.  
  35. display.setDefault( "anchorX", 0 )
  36. display.setDefault( "anchorY", 0 )
  37.  
  38.  
  39.  
  40. -- -----------------------------------------------------------------------------------
  41. -- Scene event functions
  42. -- -----------------------------------------------------------------------------------
  43.  
  44. -- create()
  45. function scene:show(event)
  46.     local sceneGroup = self.view
  47.     local phase = event.phase;
  48.  
  49.     if phase == "will" then
  50.     -- Code here runs when the scene is first created but has not yet appeared on screen
  51.  
  52.    
  53.  
  54.        
  55.  
  56.  
  57.     elseif phase == "did" then
  58.     -- Create the widget
  59.  
  60.     hideGroup = display.newGroup() -- ГРУППА ДЛЯ ХАЙД
  61.  
  62.     local scrollView =
  63.         widget.newScrollView(
  64.         {
  65.             top = 0,
  66.             left = 0,
  67.             width = display.contentWidth,
  68.             height = display.contentHeight,
  69.         }
  70.     )
  71.     sceneGroup:insert(scrollView)
  72.  
  73.  
  74.     local y = 50
  75.     for i = 1, #jsonData do
  76.         local contentGroup = display.newGroup()
  77.  
  78.        
  79.  
  80.         local function networkListener( event, urlOfImage )
  81.                 if ( event.isError ) then
  82.                     print( "Network error - download failed: ", event.response )
  83.  
  84.                 elseif ( event.phase == "began" ) then
  85.                     print( "Progress Phase: began" )
  86.                 elseif ( event.phase == "ended" ) then
  87.                     print( "Displaying response image file" )
  88.                     -- myImage = display.newImage( event.response.filename, event.response.baseDirectory, 60, 40 )
  89.                        
  90.                     local imageContent = display.newImageRect(contentGroup, event.response.filename,event.response.baseDirectory, 100, 100);
  91.                     imageContent.x = 20
  92.                     imageContent.y = 25+(110+10)*(i-1)
  93.  
  94.                
  95.                
  96.                 end
  97.         end
  98.  
  99.  
  100.  
  101.  
  102.         local textTitle = jsonData[i].name
  103.         local urlOfImage = jsonData[i].img
  104.         local textDescr = jsonData[i].description
  105.  
  106.         local backgrRect = display.newRect(contentGroup, 5, y-30, display.contentWidth-10, 110 ):setFillColor(125/255, 255/255, 136/255)
  107.         local backgrRectWhite = display.newRect(contentGroup, 10, y-25, display.contentWidth-20, 100 ):setFillColor(1)
  108.         local highScoresButton = display.newText(contentGroup, textTitle, 125, y-20, 200, 100, native.systemFont, 20):setFillColor(0.48, 0.6, 0.38)
  109.        
  110.  
  111.         local params = {}
  112.         params.progress = true
  113.  
  114.  
  115.         local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000
  116.         print(textMem)
  117.  
  118.         local function doesFileExist( fname, path )
  119.  
  120.                 local results = false
  121.  
  122.                 -- Path for the file
  123.                 local filePath = system.pathForFile( fname, path )
  124.              
  125.                 if ( filePath ) then
  126.                     local file, errorString = io.open( filePath, "r" )
  127.              
  128.                     if not file then
  129.                         -- Error occurred; output the cause
  130.                         print( "File error: " .. errorString )
  131.                         network.download(
  132.                                 urlOfImage,
  133.                                 "GET",
  134.                                 networkListener,
  135.                                 params,
  136.                                 i..".jpg",
  137.                                 system.DocumentsDirectory
  138.                               )
  139.                   else
  140.                   -- File exists!
  141.                   print( "File found: " .. fname )
  142.                        
  143.                   local imageContent = display.newImageRect(contentGroup, i..".jpg", system.DocumentsDirectory, 100, 100);
  144.                   imageContent.x = 20
  145.                   imageContent.y = 25+(110+10)*(i-1)
  146.  
  147.                   results = true
  148.                   -- Close the file handle
  149.                   file:close()
  150.                   end
  151.               end
  152.              
  153.             return results
  154.         end      
  155.  
  156.         local results = doesFileExist( i..".jpg", system.DocumentsDirectory )
  157.            
  158.  
  159.         scrollView:insert(contentGroup)
  160.  
  161.         local options =
  162.             {
  163.                 effect = "fade",
  164.                 time = 400,
  165.                 params = {
  166.                     iImage = i,
  167.                     textT = textTitle,
  168.                     textD = textDescr,
  169.                     jsonD = jsonData
  170.                 }
  171.         }
  172.  
  173.         contentGroup:addEventListener(
  174.             "tap",
  175.             function()
  176.                 composer.gotoScene("scenes.activity", options)
  177.             end
  178.         )
  179.  
  180.         y = y + 120
  181.     end
  182.  
  183.     hideGroup:insert(sceneGroup)
  184. end
  185. end
  186.  
  187. -- -- show()
  188. -- function scene:show(event)
  189. --     local sceneGroup = self.view
  190. --     local phase = event.phase
  191.  
  192. --     if (phase == "will") then
  193. --         -- Code here runs when the scene is still off screen (but is about to come on screen)
  194. --     elseif (phase == "did") then
  195. --     -- Code here runs when the scene is entirely on screen
  196. --     end
  197. -- end
  198.  
  199. -- -- hide()
  200. function scene:hide(event)
  201.     local sceneGroup = self.view
  202.     local phase = event.phase
  203.  
  204.     if (phase == "will") then
  205.         -- Code here runs when the scene is on screen (but is about to go off screen)
  206.     elseif (phase == "did") then
  207.     -- Code here runs immediately after the scene goes entirely off screen
  208.    
  209.     hideGroup:removeSelf() -- УДАЛЯЕМ ГРУППУ
  210.     end
  211. end
  212.  
  213. -- -- destroy()
  214. -- function scene:destroy(event)
  215. --     local sceneGroup = self.view
  216. --     -- Code here runs prior to the removal of scene's view
  217. -- end
  218.  
  219. -- -----------------------------------------------------------------------------------
  220. -- Scene event function listeners
  221. -- -----------------------------------------------------------------------------------
  222. -- scene:addEventListener("create", scene)
  223. scene:addEventListener("show", scene)
  224. scene:addEventListener("hide", scene)
  225. -- scene:addEventListener("destroy", scene)
  226. -- -----------------------------------------------------------------------------------
  227.  
  228. return scene
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement