Hello,
I’m learning in lua and corona and have created my first module.
This module allow you to storage data of any type in memory or disk and is easy to use.
Please help to improve this code, and show my mistakes…
Json library is required, get it here:
http://www.chipmunkav.com/downloads/Json.lua
storage.lua
--
-- Storage Box Module
-- 2010-04
-- Pedro Valentini
-- tecnalta.net
--
-- USAGE
--
-- local storage = require("storage")
--
---- DEFINE DATA, ANY TYPE
-- local user = {}
-- user.name = "User Name"
--
---- SET DATA (in memory)
-- storage:set("user", user)
--
---- WRITE DATA (in disk)
-- storage:write() -- save on disk! -- you can call on exit too
--
-- READ DATA (from disk)
-- called one time before first get or set (you dont need to call it)
-- storage:read()
--
---- GET DATA (from disk if not loaded yet, other way, from memory)
-- local user = storage:get("user")
--
module(..., package.seeall)
-- http://www.chipmunkav.com/downloads/Json.lua
local Json = require("Json")
local storage = {}
local data = {}
local loadedFromDisk = false
local needToWrite = false
local fileName = "storagebox"
local filePath = system.pathForFile( fileName, system.DocumentsDirectory )
-- SET
function storage:set ( name, value )
if not loadedFromDisk then
storage:read()
end
needToWrite = true
data[name] = value
end
-- GET
function storage:get ( name, defaultValue )
if not loadedFromDisk then
storage:read()
end
if data[name] == nil and defaultValue then
storage:set( name, defaultValue )
end
return data[name]
end
-- CLEAN
function storage:clean ( name )
storage:set( name, nil )
-- TODO: testar, fazer remover a chave...
end
-- PRINT / DEBUG
function storage:debug ()
if not loadedFromDisk then
storage:read()
end
local alert = native.showAlert("DEBUG:\n " .. Json.Encode(data), "OK")
-- print(Json.Encode(data))
end
-- WRITE TO DISK
function storage:write()
if needToWrite then
local file = io.open( filePath, "w" )
if file then
file:write( Json.Encode(data) )
io.close( file )
needToWrite = false
end
end
end
-- READ FROM DISK
function storage:read()
local file = io.open( filePath, "r" )
if file then
local json = file:read( "\*a" )
data = Json.Decode(json)
io.close( file )
loadedFromDisk = true
end
end
-- CHECK IF FILE EXISTS
function storage:fileExists ( path )
local file = io.open ( path )
if file == nil then
return false
else
io.close( file )
return true
end
end
--function applicationStart (event)
-- storage:read()
--end
--Runtime:addEventListener( "applicationStart", applicationStart )
--
--function applicationExit (event)
-- storage:write()
--end
--Runtime:addEventListener( "applicationExit", applicationExit )
--
--function applicationResume (event)
-- storage:read()
--end
--Runtime:addEventListener( "applicationResume", applicationResume )
--
--function applicationSuspend (event)
-- storage:write()
--end
--Runtime:addEventListener( "applicationSuspend", applicationSuspend )
return storage
My question:
I want to add this block but it not work, this event is not called on simulator?
function applicationExit (event)
storage:write()
end
Runtime:addEventListener( "applicationExit", applicationExit )
[import]uid: 6732 topic_id: 8822 reply_id: 308822[/import]