Advertisement
reshetnikoff

Untitled

Jul 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.87 KB | None | 0 0
  1. -----------------------------------------------------------------------------------------
  2. --
  3. -- main.lua
  4. --
  5. -----------------------------------------------------------------------------------------
  6.  
  7. -- Your code here
  8.  
  9. local composer = require("composer");
  10. local widget = require("widget");
  11. local json = require("json");
  12. http = require("socket.http")
  13. local appodeal = require( "plugin.appodeal" )
  14.  
  15.  
  16.  
  17. local scene = composer.newScene();
  18.  
  19. local function adListener( event )
  20.  
  21.     if ( event.phase == "init" ) then  -- Successful initialization
  22.        
  23.         print( event.isError )
  24.     elseif ( event.phase == "failed" ) then  -- The ad failed to load
  25.         print( event.type )
  26.         print( event.isError )
  27.         print( event.response )
  28.     end
  29. end
  30.  
  31.  
  32.  
  33.  
  34.  
  35. local jsonRawData = http.request("http://ezmarketing.ru/fitness1/test.json")
  36.  
  37. local jsonData = json.decode(jsonRawData)
  38.  
  39.  
  40.  
  41. function saveSettings(t, filename)
  42.     local path = system.pathForFile(filename, system.ResourceDirectory);
  43.     local file = io.open(path, "w");
  44.     if (file) then
  45.         local contents = json.encode(t);
  46.         file:write(contents);
  47.         io.close(file);
  48.         return true
  49.     else
  50.         return false
  51.     end
  52. end
  53.  
  54. saveSettings(jsonData, "settings.json");
  55.  
  56.  
  57. settings = jsonData
  58.  
  59.  
  60.  
  61. function scene:show(event)
  62.     local sceneGroup = self.view;
  63.  
  64.     -- создаём группу для ввода веса
  65.     local contentGroup = display.newGroup();
  66.  
  67.  
  68.     local function scrollListener(event)
  69.         local phase = event.phase
  70.         local direction = event.direction
  71.  
  72.         -- если долистает до конца - дойдет до лимита
  73.         if event.limitReached then
  74.             if "up" == direction then
  75.                 print("Reached Top Limit")
  76.             elseif "down" == direction then
  77.                 print("Reached Bottom Limit")
  78.             end
  79.         end
  80.  
  81.         return true
  82.     end
  83.  
  84.     -- создаем скролвью
  85.     local scrollView = widget.newScrollView
  86.         {
  87.             left = 0,
  88.             top = 0,
  89.             width = display.contentWidth,
  90.             height = display.contentHeight,
  91.             topPadding = 50,
  92.             bottomPadding = 50,
  93.             horizontalScrollDisabled = true,
  94.             verticalScrollDisabled = false,
  95.             listener = scrollListener,
  96.         }
  97.  
  98.  
  99.  
  100.  
  101.    
  102.  
  103.    
  104.  
  105.      
  106.         for i = 1,5 do
  107.  
  108.  
  109.             local function networkListener( event, urlOfImage )
  110.                 if ( event.isError ) then
  111.                     print( "Network error - download failed: ", event.response )
  112.                 elseif ( event.phase == "began" ) then
  113.                     print( "Progress Phase: began" )
  114.                 elseif ( event.phase == "ended" ) then
  115.                     print( "Displaying response image file" )
  116.                     -- myImage = display.newImage( event.response.filename, event.response.baseDirectory, 60, 40 )
  117.                        
  118.                     local imageContent = display.newImageRect(contentGroup, event.response.filename,event.response.baseDirectory, 100, 100);
  119.                     imageContent.x = -display.contentWidth/3
  120.                     imageContent.y = 5+(110+10)*(i-1)
  121.  
  122.                     contentGroup.x = display.contentCenterX
  123.                     -- contentGroup.y = (110+10)*(i-1)
  124.                     -- scrollView:insert(contentGroup)
  125.  
  126.                
  127.  
  128.                 end
  129.             end
  130.  
  131.  
  132.             local function doesFileExist( fname, path )
  133.  
  134.                 local results = false
  135.  
  136.                 -- Path for the file
  137.                 local filePath = system.pathForFile( fname, path )
  138.              
  139.                 if ( filePath ) then
  140.                     local file, errorString = io.open( filePath, "r" )
  141.              
  142.                     if not file then
  143.                         -- Error occurred; output the cause
  144.                         print( "File error: " .. errorString )
  145.                     else
  146.                         -- File exists!
  147.                         print( "File found: " .. fname )
  148.                         results = true
  149.                         -- Close the file handle
  150.                         file:close()
  151.                     end
  152.                 end
  153.              
  154.                 return results
  155.             end
  156.  
  157.  
  158.             local contentGroup = display.newGroup()
  159.            
  160.             local backgrRect = display.newRect(contentGroup, 0, 5, display.contentWidth, 110 ):setFillColor(0.4)
  161.  
  162.             local textTitle = jsonData[i].name
  163.             local textTitleObject = display.newText(contentGroup, textTitle, 110, -20, 300, 50, "Helvetica", 20)
  164.  
  165.             local textDescr = jsonData[i].description
  166.             local textDecsrObject = display.newText(contentGroup, textDescr, 60, 25, 200, 50, "Helvetica", 14)
  167.            
  168.  
  169.  
  170.            
  171.  
  172.             local params = {}
  173.             params.progress = true
  174.             local urlOfImage = jsonData[i].img
  175.  
  176.  
  177.             print(urlOfImage)
  178.             print("check")
  179.  
  180.             contentGroup.x = display.contentCenterX
  181.             contentGroup.y = (110+10)*(i-1)
  182.             scrollView:insert(contentGroup)
  183.  
  184.  
  185.  
  186.             params =
  187.             {
  188.             }
  189.  
  190.             params[i] = urlOfImage
  191.             print("Param"..i,"=",params[i])
  192.  
  193.             local results = doesFileExist( i..".jpg", system.DocumentsDirectory )
  194.            
  195.             print("Results=", results)
  196.  
  197.  
  198.             if results == true then
  199.                 local imageContent = display.newImageRect(contentGroup, i..".jpg", system.DocumentsDirectory, 100, 100);
  200.                 imageContent.x = -display.contentWidth/3
  201.                 imageContent.y = 5+(110+10)*(i-1)
  202.  
  203.                 print("i=",i)
  204.                 print(i..".jpg")
  205.  
  206.                 contentGroup.x = display.contentCenterX
  207.                
  208.                 -- scrollView:insert(contentGroup)
  209.  
  210.             else
  211.                 network.download(
  212.                     urlOfImage,
  213.                     "GET",
  214.                     networkListener,
  215.                     params,
  216.                     i..".jpg",
  217.                     system.DocumentsDirectory
  218.                 )
  219.             end
  220.  
  221.  
  222.  
  223.  
  224.        
  225.  
  226.            
  227.  
  228.             local options =
  229.             {
  230.                 effect = "fade",
  231.                 time = 400,
  232.                 params = {
  233.                     iImage = i,
  234.                     textT = textTitle,
  235.                     textD = textDescr,
  236.                     jsonD = jsonData
  237.                 }
  238.             }
  239.  
  240.  
  241.  
  242.  
  243.             contentGroup:addEventListener("touch",  
  244.             function(event)
  245.                 -- composer.showOverlay("scenes.activity")
  246.                 composer.gotoScene("scenes.activity", options)
  247.  
  248.                 -- Initialize the Appodeal plugin
  249.                
  250.                 -- appodeal.show("interstitial")
  251.                 print("hello")
  252.             end
  253.             );
  254.            
  255.  
  256.            
  257.         end
  258.  
  259.  
  260.  
  261.  
  262. end
  263.  
  264.  
  265.  
  266. scene:addEventListener("show", scene)
  267.  
  268.  
  269.  
  270. return scene
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement