Created
October 5, 2016 15:17
-
-
Save Shchvova/11bd3314bf546483eb7932620d4feb39 to your computer and use it in GitHub Desktop.
iCloud sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
settings = | |
{ | |
orientation = | |
{ | |
-- default = "landscapeLeft", | |
default = "portrait", | |
supported = { "landscapeLeft", "landscapeRight", "portrait" }, | |
}, | |
plugins = | |
{ | |
["plugin.iCloud"] = | |
{ | |
publisherId = "com.coronalabs" | |
}, | |
}, | |
iphone = | |
{ | |
iCloud = true, | |
plist = | |
{ | |
CFBundleIconFiles = { | |
"Icon.png", | |
"[email protected]", | |
"Icon-60.png", | |
"[email protected]", | |
"[email protected]", | |
"Icon-72.png", | |
"[email protected]", | |
"Icon-76.png", | |
"[email protected]", | |
"Icon-167.png", | |
"Icon-Small-40.png", | |
"[email protected]", | |
"[email protected]", | |
"Icon-Small-50.png", | |
"[email protected]", | |
"Icon-Small.png", | |
"[email protected]", | |
"[email protected]" | |
}, | |
}, | |
}, | |
tvos = | |
{ | |
-- Enabling iCloud | |
iCloud = true, | |
-- tvOS app icons require multiple layers, and must provide both a small and a large size. | |
icon = | |
{ | |
-- A collection of 400x240 pngs, in order of top to bottom. | |
small = | |
{ | |
"Icon-tvOS-Small-Pew.png", | |
"Icon-tvOS-Small-LogoA.png", | |
"Icon-tvOS-Small-LogoB.png", | |
"Icon-tvOS-Small-LogoC.png", | |
"Icon-tvOS-Small-Background.png", | |
}, | |
-- A collection of 1280x768 pngs, in order of top to bottom. | |
large = | |
{ | |
"Icon-tvOS-Large-Pew.png", | |
"Icon-tvOS-Large-LogoA.png", | |
"Icon-tvOS-Large-LogoB.png", | |
"Icon-tvOS-Large-LogoC.png", | |
"Icon-tvOS-Large-Background.png", | |
} | |
}, | |
-- A 1920x720 png file, displayed when your app is on the "top shelf". | |
topShelfImage = "Icon-tvOS-TopShelf.png", | |
-- A 1920x1080 png file, displayed briefly as your app loads. | |
launchImage = "Icon-tvOS-Launch.png", | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local iCloud = require "plugin.iCloud" | |
local json = require "json" | |
local composer = require( "composer" ) | |
local widget = require( "widget" ) | |
local scene = composer.newScene() | |
local FocusEngine = require( "FocusEngine" ) | |
local focusEngineObjects = {} | |
local function WriteTestRecord( ) | |
local recordData = { | |
name = {type="string", string="Basil S."}, | |
when = {type="date", time=-1449201382}, | |
where = {type="location", latitude=37.453139, longitude=122.113451 }, | |
amount = {type="number", number=1987}, | |
} | |
local record = iCloud.recordCreate{ | |
recordName = "Basil 3", | |
type = "people", | |
table = recordData, | |
} | |
if record then | |
record:save{ | |
onComplete=function( event ) | |
print("New record saved?!") | |
print(json.prettify(event)) | |
if event.record then | |
print(json.prettify(event.record:table())) | |
end | |
timer.performWithDelay( 1, function ( ) | |
collectgarbage( ) | |
end) | |
end | |
} | |
end | |
end | |
local function TestRecordAccessebility( ) | |
iCloud.recordGetAccountStatus{ | |
onComplete = function( event ) | |
print("Account status!") | |
print(json.prettify(event)) | |
print("---") | |
end, | |
} | |
end | |
local function DeleteTestRecord( ) | |
iCloud.recordDelete{ | |
onComplete = function( event ) | |
print("Record is deleted!") | |
print(json.prettify(event)) | |
print("---") | |
end, | |
recordName = "Boris 2", | |
} | |
end | |
local function FetchTestRecord( ) | |
iCloud.recordFetch{ | |
onComplete = function( event ) | |
print("Record is fetched!") | |
print(json.prettify(event)) | |
if event.record then | |
print("Record is: ", json.prettify(event.record:table())) | |
else | |
print("Record is nil") | |
end | |
print("---") | |
end, | |
recordName = "Basil 3", | |
} | |
end | |
local function FetchAndSaveTestRecord( ) | |
iCloud.recordFetch{ | |
onComplete = function( event ) | |
print("Record is fetched!") | |
print(json.prettify(event)) | |
print("---") | |
local record = event.record | |
if record then | |
print("Here Is data We Got") | |
print(json.prettify(record:table())) | |
local num = record:get("amount").number | |
print("Num is ", num) | |
record:set("amount", num+1) | |
record:save{ | |
onComplete=function( event ) | |
print("Record saved?!") | |
print(json.prettify(event)) | |
timer.performWithDelay( 1, function ( ) | |
collectgarbage( ) | |
end) | |
end | |
} | |
end | |
end, | |
recordName = "Vlad 1", | |
} | |
end | |
local function QueryRecords( ) | |
iCloud.recordQuery{ | |
onComplete = function( event ) | |
print("Records are queries!") | |
print(json.prettify(event)) | |
if event.recordArray and #event.recordArray then | |
print("Records are: ", json.prettify(event.recordArray[1]:table())) | |
else | |
print("Records is nil") | |
end | |
print("---") | |
end, | |
type="people", | |
query="TRUEPREDICATE", | |
} | |
end | |
local text = nil | |
local function FetchAndIncrement( event ) | |
if ( event and "ended" ~= event.phase ) then | |
return | |
end | |
native.setKeyboardFocus( nil ) | |
text.text = "Fetching record" | |
iCloud.recordFetch{ | |
onComplete = function( event ) | |
print("Tapper is fetched!") | |
print(json.prettify(event)) | |
if event.isError and event.errorCode == 11 then | |
text.text = event.error .. "\nAttempting to create new counter" | |
print("Attempting to create new record") | |
local record = iCloud.recordCreate{ | |
type="ClickerCounter", | |
recordName="Common Tapper", | |
} | |
record:set("taps", {type="number", number=1}) | |
record:set("name", "Cloud Stored Text" ) | |
record:save{ | |
database="public", | |
onComplete=function(event) | |
print("Attempted to create tapper!") | |
print(json.prettify(event)) | |
if not event.isError then | |
text.text = tostring(event.record:get("taps").number) | |
else | |
text.text = event.error | |
end | |
end | |
} | |
elseif event.isError then | |
text.text = event.error .. "errorCode " .. tostring(event.errorCode) .."\nTry tapping again..." | |
else | |
local record = event.record | |
if record then | |
print("Here Is data We Got") | |
print(json.prettify(record:table())) | |
local num = record:get("taps").number | |
print("Num is ", num) | |
text.text = "Received: " .. tostring(num) | |
if record:get("name") then | |
text.text = text.text .. '\n' .. record:get('name').string | |
else | |
text.text = text.text .. '\n' .. '<no message>' | |
end | |
record:set("taps", num+1) | |
record:save{ | |
database="public", | |
onComplete=function( event ) | |
print("Record saved?!") | |
print(json.prettify(event)) | |
if not event.isError and event.record then | |
text.text = "Saved: " .. record:get("taps").number | |
if record:get("name") then | |
text.text = text.text .. '\n' .. record:get('name').string | |
else | |
text.text = text.text .. '\n' .. '<no message>' | |
end | |
else | |
text.text = "Error saving increment\n"..event.error | |
end | |
timer.performWithDelay( 1, function ( ) | |
collectgarbage( ) | |
end) | |
end | |
} | |
end | |
end | |
end, | |
database = "public", | |
recordName = "Common Tapper", | |
} | |
end | |
local function FetchOnly( event ) | |
if ( event and "ended" ~= event.phase ) then | |
return | |
end | |
native.setKeyboardFocus( nil ) | |
text.text = "Fetching record" | |
iCloud.recordFetch{ | |
onComplete = function( event ) | |
print("Tapper is fetched!") | |
print(json.prettify(event)) | |
if event.isError then | |
text.text = event.error .. "errorCode " .. tostring(event.errorCode) .."\nTry tapping again..." | |
else | |
local record = event.record | |
if record then | |
print("Here Is data We Got") | |
print(json.prettify(record:table())) | |
local num = record:get("taps").number | |
print("Num is ", num) | |
text.text = "Received: " .. tostring(num) | |
if record:get("name") then | |
text.text = text.text .. '\n' .. record:get('name').string | |
else | |
text.text = text.text .. '\n' .. '<no message>' | |
end | |
print("metadata ", json.prettify(record:metadata())) | |
end | |
end | |
end, | |
database = "public", | |
recordName = "Common Tapper", | |
} | |
end | |
function scene:create( event ) | |
local sceneGroup = self.view | |
CreateBackButton(sceneGroup) | |
local btn = widget.newButton | |
{ | |
x = display.contentCenterX, | |
y = display.contentHeight*0.15, | |
label = "Increment Counter", | |
onEvent = FetchAndIncrement, | |
} | |
sceneGroup:insert(btn) | |
focusEngineObjects[#focusEngineObjects+1] = btn | |
btn = widget.newButton | |
{ | |
parent = sceneGroup, | |
x = display.contentCenterX, | |
y = display.contentHeight*0.25, | |
label = "Fetch Only", | |
onEvent = FetchOnly, | |
} | |
sceneGroup:insert(btn) | |
focusEngineObjects[#focusEngineObjects+1] = btn | |
text = display.newText{ | |
parent = sceneGroup, | |
text = "Tap button to participate.", | |
x = display.contentCenterX, | |
y = display.contentCenterY, | |
width = display.contentWidth, | |
fontSize = 18, | |
align = "center" | |
} | |
end | |
function scene:show( event ) | |
local sceneGroup = self.view | |
if ( event.phase == "will" ) then | |
FocusEngine.setObjects(focusEngineObjects) | |
elseif ( event.phase == "did" ) then | |
end | |
end | |
scene:addEventListener( "create", scene ) | |
scene:addEventListener( "show", scene ) | |
return scene |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
application = | |
{ | |
content = | |
{ | |
width = 320, | |
height = 480, | |
-- scale = "letterbox", | |
-- scale = "zoomEven", | |
-- scale = "none", | |
scale = "zoomStretch", | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local iCloud = require "plugin.iCloud" | |
local json = require "json" | |
local composer = require( "composer" ) | |
local widget = require( "widget" ) | |
local scene = composer.newScene() | |
local text | |
local function ListFiles(event) | |
if ( "ended" ~= event.phase ) then | |
return | |
end | |
json.prettify(iCloud.docList{ | |
onComplete = function(event) | |
print(json.prettify(event)); | |
if event.files then | |
text.text = "Files: " .. json.prettify(event.files) | |
elseif event.isError then | |
text.text = "List failed, error: " .. tostring(event.error) | |
else | |
text.text = "No files" | |
end | |
end | |
}) | |
end | |
local function WriteFile(event) | |
if ( "ended" ~= event.phase ) then | |
return | |
end | |
iCloud.docWrite{ | |
filename = "test.txt", | |
contents = "1", | |
onComplete = function(event) | |
print(json.prettify(event)); | |
if event.isError then | |
text.text = "Write failed, error: " .. tostring(event.error) | |
else | |
text.text = "Wrote 1." | |
end | |
end | |
} | |
end | |
local function ReadFile(event) | |
if ( "ended" ~= event.phase ) then | |
return | |
end | |
iCloud.docRead{ | |
filename = "test.txt", | |
onComplete = function(event) | |
print(json.prettify(event)); | |
if event.isError then | |
text.text = "Read failed, error: " .. tostring(event.error) | |
else | |
text.text = "Read file:" .. tostring(event.contents) | |
end | |
end | |
} | |
end | |
local function ReadAndIncrementFile(event) | |
if ( "ended" ~= event.phase ) then | |
return | |
end | |
iCloud.docRead{ | |
filename = "test.txt", | |
onComplete = function(event) | |
print(json.prettify(event)); | |
if event.isError then | |
text.text = "Read failed, error: " .. tostring(event.error) | |
else | |
text.text = "Read file:" .. tostring(event.contents) | |
end | |
if not event.isError and event.contents then | |
iCloud.docWrite{ | |
filename = "test.txt", | |
contents = tostring( tonumber( event.contents ) + 1 ), | |
onComplete = function(event) | |
print(json.prettify(event)); | |
if event.isError then | |
text.text = text.text .. "\nError incrementing: " .. tostring(event.error) | |
else | |
text.text = text.text .. "\nAnd incremented" | |
end | |
end | |
} | |
end | |
end | |
} | |
end | |
local function DeleteFile(event) | |
if ( "ended" ~= event.phase ) then | |
return | |
end | |
iCloud.docDelete{ | |
filename = "test.txt", | |
onComplete = function(event) | |
print(json.prettify(event)); | |
if event.isError then | |
text.text = "Delete failed, error: " .. tostring(event.error) | |
else | |
text.text = "File deleted" | |
end | |
end | |
} | |
end | |
local function EvictFile(event) | |
if ( "ended" ~= event.phase ) then | |
return | |
end | |
local evicted, err = iCloud.docEvict{ | |
filename = "test.txt", | |
} | |
print("Evict File results: ", evicted, err) | |
if evicted then | |
text.text = "Evicted" | |
else | |
text.text = "Eviciton error: " .. tostring(err) | |
end | |
end | |
local function DocConflicts(event) | |
if ( "ended" ~= event.phase ) then | |
return | |
end | |
local conflicts = iCloud.docConflicts{ | |
filename = "test.txt", | |
} | |
print("Conflicts: ", json.prettify(conflicts or {"<no conflicts>"})) | |
text.text = "#Conflicts: " .. tostring(#(conflicts or {})) | |
end | |
local function DocResolve(event) | |
if ( "ended" ~= event.phase ) then | |
return | |
end | |
local resolved, err = iCloud.docResolve{ | |
filename = "test.txt", | |
} | |
print("Evict File results: ", resolved, err) | |
if resolved then | |
text.text = "Reslolved" | |
else | |
text.text = "Resolve error: " .. tostring(err) | |
end | |
end | |
function scene:create( event ) | |
local sceneGroup = self.view | |
CreateBackButton(sceneGroup) | |
if system.getInfo( "platformName" ) == "tvOS" then | |
display.newText( sceneGroup, "tvOS does not support Documents", display.contentCenterX, display.contentCenterY, nil, 20 ) | |
return | |
end | |
local btn = {y=20, height=25} | |
btn = widget.newButton | |
{ | |
x = display.contentCenterX, | |
y = btn.y+btn.height, | |
height = btn.height, | |
label = "List Files", | |
onEvent = ListFiles, | |
} | |
sceneGroup:insert(btn) | |
btn = widget.newButton | |
{ | |
x = display.contentCenterX, | |
y = btn.y+btn.height, | |
height = btn.height, | |
label = "Write 1", | |
onEvent = WriteFile, | |
} | |
sceneGroup:insert(btn) | |
btn = widget.newButton | |
{ | |
x = display.contentCenterX, | |
y = btn.y+btn.height, | |
height = btn.height, | |
label = "Read File", | |
onEvent = ReadFile, | |
} | |
sceneGroup:insert(btn) | |
btn = widget.newButton | |
{ | |
x = display.contentCenterX, | |
y = btn.y+btn.height, | |
height = btn.height, | |
label = "Read And Increment", | |
onEvent = ReadAndIncrementFile, | |
} | |
sceneGroup:insert(btn) | |
btn = widget.newButton | |
{ | |
x = display.contentCenterX, | |
y = btn.y+btn.height, | |
height = btn.height, | |
label = "Delete File", | |
onEvent = DeleteFile, | |
} | |
sceneGroup:insert(btn) | |
btn = widget.newButton | |
{ | |
x = display.contentCenterX, | |
y = btn.y+btn.height, | |
height = btn.height, | |
label = "Evict File", | |
onEvent = EvictFile, | |
} | |
sceneGroup:insert(btn) | |
btn = widget.newButton | |
{ | |
x = display.contentCenterX, | |
y = btn.y+btn.height, | |
height = btn.height, | |
label = "Conflicts", | |
onEvent = DocConflicts, | |
} | |
sceneGroup:insert(btn) | |
btn = widget.newButton | |
{ | |
x = display.contentCenterX, | |
y = btn.y+btn.height, | |
height = btn.height, | |
label = "Resolve", | |
onEvent = DocResolve, | |
} | |
sceneGroup:insert(btn) | |
text = display.newText{ | |
parent = sceneGroup, | |
text = "Tap a button.", | |
x = display.contentCenterX, | |
y = (btn.y+btn.height + display.contentHeight)*0.5, | |
width = display.contentWidth, | |
fontSize = 18, | |
align = "center" | |
} | |
end | |
scene:addEventListener( "create", scene ) | |
return scene |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local json = require "json" | |
local enabled = false | |
local FocusEngine = {} | |
local objects = {} | |
local focusedObject = 0 | |
local rect = nil | |
function FocusEngine.setObjects( objs ) | |
if not enabled then | |
return | |
end | |
for i, obj in pairs(objs) do | |
objects[i] = obj | |
obj:setEnabled( false ) | |
end | |
end | |
local function createFocusedRect(obj) | |
if rect then | |
rect:removeSelf( ) | |
end | |
local rectBorder = 4 | |
rect = display.newRect( obj.x, obj.y, obj.width+rectBorder*2, obj.height+rectBorder*2 ) | |
rect:setFillColor( 0,0,0,0 ) | |
rect.stroke = { 1, 0, 0.5 } | |
rect.strokeWidth = rectBorder | |
end | |
local shiftedThisEvent = false | |
local function onTouch(event) | |
if #objects < 1 then | |
return | |
end | |
if not rect then | |
focusedObject = 1 | |
createFocusedRect(objects[focusedObject]) | |
end | |
if event.phase == "ended" then | |
shiftedThisEvent = false | |
rect.y = objects[focusedObject].y | |
else | |
if not shiftedThisEvent then | |
local dy = (event.y - event.yStart)/display.contentCenterY | |
rect.y = objects[focusedObject].y + dy*rect.height*0.3 | |
if math.abs(dy) > 0.5 then | |
if dy > 0 and focusedObject < #objects then | |
shiftedThisEvent = true | |
focusedObject = focusedObject + 1 | |
createFocusedRect(objects[focusedObject]) | |
end | |
if dy < 0 and focusedObject > 1 then | |
shiftedThisEvent = true | |
focusedObject = focusedObject - 1 | |
createFocusedRect(objects[focusedObject]) | |
end | |
end | |
end | |
end | |
end | |
local resetAt0 = true | |
local function onAxis(event) | |
if #objects < 1 or not event.axis or event.axis.type~='y' then | |
return | |
end | |
if resetAt0 and event.normalizedValue ~= 0 then | |
return | |
end | |
resetAt0 = false | |
if not rect then | |
focusedObject = 1 | |
createFocusedRect(objects[focusedObject]) | |
end | |
local dy = event.normalizedValue | |
rect.y = objects[focusedObject].y + dy*rect.height*0.3 | |
if math.abs(dy) > 0.5 then | |
if dy > 0 and focusedObject < #objects then | |
shiftedThisEvent = true | |
focusedObject = focusedObject + 1 | |
createFocusedRect(objects[focusedObject]) | |
end | |
if dy < 0 and focusedObject > 1 then | |
shiftedThisEvent = true | |
focusedObject = focusedObject - 1 | |
createFocusedRect(objects[focusedObject]) | |
end | |
resetAt0 = true | |
end | |
end | |
local function onTap( event ) | |
local obj = objects[focusedObject] | |
if #objects < 1 or not obj or not obj._view._onEvent then | |
return | |
end | |
obj._view._onEvent({phase="ended", target=obj}) | |
end | |
local function onKey( event ) | |
if event.keyName == "buttonA" and event.phase == "up" then | |
onTap() | |
end | |
end | |
local initialized = false | |
function FocusEngine.initialize(composer) | |
enabled = true | |
if initialized then | |
return | |
end | |
initialized = true | |
if composer then | |
local oldGoToScene = composer.gotoScene | |
composer.gotoScene = function( ... ) | |
objects = {} | |
if rect then | |
focusedObject = 0 | |
rect:removeSelf( ) | |
rect = nil | |
end | |
oldGoToScene( ... ) | |
end | |
end | |
Runtime:addEventListener( "key", onKey ) | |
Runtime:addEventListener( "touch", onTouch ) | |
-- Runtime:addEventListener( "tap", onTap ) | |
Runtime:addEventListener( "axis", onAxis ) | |
end | |
return FocusEngine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local composer = require( "composer" ) | |
local widget = require( "widget" ) | |
local iCloud = require "plugin.iCloud" | |
local json = require "json" | |
local scene = composer.newScene() | |
local text = nil | |
local function setText() | |
text.text = tostring( iCloud.get("touches") ) | |
end | |
local function listener( event ) | |
print( "KVS SYNC EVENT ", json.prettify( event ) ) | |
setText() | |
end | |
local function touched( event ) | |
local t = 1 + (iCloud.get("touches") or 0) | |
iCloud.set("touches", t) | |
iCloud.synchronize() | |
setText() | |
end | |
local function onKey( event ) | |
if event.keyName == "buttonA" and event.phase == "up" then | |
touched() | |
end | |
end | |
function scene:create( event ) | |
local sceneGroup = self.view | |
CreateBackButton(sceneGroup) | |
text = display.newText( sceneGroup, "iCloud", display.contentCenterX, display.contentCenterY, nil, 20 ) | |
iCloud.setKVSListener( listener ) | |
iCloud.synchronize() | |
setText() | |
end | |
function scene:show( event ) | |
local sceneGroup = self.view | |
if ( event.phase == "will" ) then | |
elseif ( event.phase == "did" ) then | |
Runtime:addEventListener( "tap", touched ) | |
Runtime:addEventListener( "key", onKey ) | |
end | |
end | |
function scene:hide( event ) | |
local sceneGroup = self.view | |
local phase = event.phase | |
if ( phase == "will" ) then | |
Runtime:removeEventListener( "tap", touched ) | |
Runtime:removeEventListener( "key", onKey ) | |
elseif ( phase == "did" ) then | |
end | |
end | |
scene:addEventListener( "create", scene ) | |
scene:addEventListener( "show", scene ) | |
scene:addEventListener( "hide", scene ) | |
return scene |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local composer = require( "composer" ) | |
local FocusEngine = require "FocusEngine" | |
if system.getInfo( "platformName" ) == "tvOS" then | |
FocusEngine.initialize(composer) | |
end | |
composer.gotoScene( "mainMenu" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment