Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------------------------------------------------
- --
- -- main.lua
- --
- -----------------------------------------------------------------------------------------
- -- Your code here
- local composer = require("composer");
- local widget = require("widget");
- local json = require("json");
- http = require("socket.http")
- local appodeal = require( "plugin.appodeal" )
- local scene = composer.newScene();
- local function adListener( event )
- if ( event.phase == "init" ) then -- Successful initialization
- print( event.isError )
- elseif ( event.phase == "failed" ) then -- The ad failed to load
- print( event.type )
- print( event.isError )
- print( event.response )
- end
- end
- local jsonRawData = http.request("http://ezmarketing.ru/fitness1/test.json")
- local jsonData = json.decode(jsonRawData)
- function saveSettings(t, filename)
- local path = system.pathForFile(filename, system.ResourceDirectory);
- local file = io.open(path, "w");
- if (file) then
- local contents = json.encode(t);
- file:write(contents);
- io.close(file);
- return true
- else
- return false
- end
- end
- saveSettings(jsonData, "settings.json");
- settings = jsonData
- function scene:show(event)
- local sceneGroup = self.view;
- -- создаём группу для ввода веса
- local contentGroup = display.newGroup();
- local function scrollListener(event)
- local phase = event.phase
- local direction = event.direction
- -- если долистает до конца - дойдет до лимита
- if event.limitReached then
- if "up" == direction then
- print("Reached Top Limit")
- elseif "down" == direction then
- print("Reached Bottom Limit")
- end
- end
- return true
- end
- -- создаем скролвью
- local scrollView = widget.newScrollView
- {
- left = 0,
- top = 0,
- width = display.contentWidth,
- height = display.contentHeight,
- topPadding = 50,
- bottomPadding = 50,
- horizontalScrollDisabled = true,
- verticalScrollDisabled = false,
- listener = scrollListener,
- }
- for i = 1,5 do
- local function networkListener( event, urlOfImage )
- if ( event.isError ) then
- print( "Network error - download failed: ", event.response )
- elseif ( event.phase == "began" ) then
- print( "Progress Phase: began" )
- elseif ( event.phase == "ended" ) then
- print( "Displaying response image file" )
- -- myImage = display.newImage( event.response.filename, event.response.baseDirectory, 60, 40 )
- local imageContent = display.newImageRect(contentGroup, event.response.filename,event.response.baseDirectory, 100, 100);
- imageContent.x = -display.contentWidth/3
- imageContent.y = 5+(110+10)*(i-1)
- contentGroup.x = display.contentCenterX
- -- contentGroup.y = (110+10)*(i-1)
- -- scrollView:insert(contentGroup)
- end
- end
- local function doesFileExist( fname, path )
- local results = false
- -- Path for the file
- local filePath = system.pathForFile( fname, path )
- if ( filePath ) then
- local file, errorString = io.open( filePath, "r" )
- if not file then
- -- Error occurred; output the cause
- print( "File error: " .. errorString )
- else
- -- File exists!
- print( "File found: " .. fname )
- results = true
- -- Close the file handle
- file:close()
- end
- end
- return results
- end
- local contentGroup = display.newGroup()
- local backgrRect = display.newRect(contentGroup, 0, 5, display.contentWidth, 110 ):setFillColor(0.4)
- local textTitle = jsonData[i].name
- local textTitleObject = display.newText(contentGroup, textTitle, 110, -20, 300, 50, "Helvetica", 20)
- local textDescr = jsonData[i].description
- local textDecsrObject = display.newText(contentGroup, textDescr, 60, 25, 200, 50, "Helvetica", 14)
- local params = {}
- params.progress = true
- local urlOfImage = jsonData[i].img
- print(urlOfImage)
- print("check")
- contentGroup.x = display.contentCenterX
- contentGroup.y = (110+10)*(i-1)
- scrollView:insert(contentGroup)
- params =
- {
- }
- params[i] = urlOfImage
- print("Param"..i,"=",params[i])
- local results = doesFileExist( i..".jpg", system.DocumentsDirectory )
- print("Results=", results)
- if results == true then
- local imageContent = display.newImageRect(contentGroup, i..".jpg", system.DocumentsDirectory, 100, 100);
- imageContent.x = -display.contentWidth/3
- imageContent.y = 5+(110+10)*(i-1)
- print("i=",i)
- print(i..".jpg")
- contentGroup.x = display.contentCenterX
- -- scrollView:insert(contentGroup)
- else
- network.download(
- urlOfImage,
- "GET",
- networkListener,
- params,
- i..".jpg",
- system.DocumentsDirectory
- )
- end
- local options =
- {
- effect = "fade",
- time = 400,
- params = {
- iImage = i,
- textT = textTitle,
- textD = textDescr,
- jsonD = jsonData
- }
- }
- contentGroup:addEventListener("touch",
- function(event)
- -- composer.showOverlay("scenes.activity")
- composer.gotoScene("scenes.activity", options)
- -- Initialize the Appodeal plugin
- -- appodeal.show("interstitial")
- print("hello")
- end
- );
- end
- end
- scene:addEventListener("show", scene)
- return scene
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement