Skip to content

Instantly share code, notes, and snippets.

@Shchvova
Last active July 13, 2016 17:08
Show Gist options
  • Save Shchvova/8b15e5afe0a64357e1c8b6a3eaa95e7e to your computer and use it in GitHub Desktop.
Save Shchvova/8b15e5afe0a64357e1c8b6a3eaa95e7e to your computer and use it in GitHub Desktop.
local messages = require "plugin.messages"
local function listener( event )
timer.performWithDelay(1, function( )
native.showAlert( "Message sending done", "with status: " .. tostring( event.status ), { "OK" } )
end)
end
messages.init( listener )
timer.performWithDelay( 1000, function()
messages.show( "corona" )
end )
#import "PluginMessages.h"
#include "CoronaRuntime.h"
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
class PluginMessages;
@interface MyMessageDelegate : NSObject <MFMessageComposeViewControllerDelegate> {
lua_State*_L;
PluginMessages* plugin;
}
@end
class PluginMessages
{
public:
typedef PluginMessages Self;
public:
static const char kName[];
static const char kEvent[];
protected:
PluginMessages();
public:
bool Initialize( CoronaLuaRef listener );
public:
CoronaLuaRef GetListener() const { return fListener; }
public:
static int Open( lua_State *L );
protected:
static int Finalizer( lua_State *L );
public:
static Self *ToPlugin( lua_State *L );
public:
static int init( lua_State *L );
static int show( lua_State *L );
private:
CoronaLuaRef fListener;
MyMessageDelegate* fDelegate;
};
@implementation MyMessageDelegate
- (instancetype)initWithLuaState:(lua_State*)luaState andPlugin:(PluginMessages*)pluginInstance
{
self = [super init];
if (self) {
self->_L = luaState;
self->plugin = pluginInstance;
}
return self;
}
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
lua_State* L = self->_L;
CoronaLuaRef listener = self->plugin->GetListener();
if(listener)
{
CoronaLuaNewEvent( L, PluginMessages::kEvent );
switch (result) {
case MessageComposeResultCancelled:
lua_pushstring( L, "cancelled" );
break;
case MessageComposeResultSent:
lua_pushstring( L, "sent" );
break;
case MessageComposeResultFailed:
lua_pushstring( L, "failed" );
break;
default:
lua_pushnil( L );
break;
}
lua_setfield( L, -2, "status" );
CoronaLuaDispatchEvent( L, listener, 0 );
}
// Documentation requires it!
[controller dismissViewControllerAnimated:YES completion:nil];
}
@end
// ----------------------------------------------------------------------------
// This corresponds to the name of the library, e.g. [Lua] require "plugin.library"
const char PluginMessages::kName[] = "plugin.messages";
// This corresponds to the event name, e.g. [Lua] event.name
const char PluginMessages::kEvent[] = "PluginMessagesevent";
PluginMessages::PluginMessages()
: fListener( NULL )
, fDelegate( nil )
{
}
bool
PluginMessages::Initialize( CoronaLuaRef listener )
{
// Can only initialize listener once
bool result = ( NULL == fListener );
if ( result )
{
fListener = listener;
}
return result;
}
int
PluginMessages::Open( lua_State *L )
{
// Register __gc callback
const char kMetatableName[] = __FILE__; // Globally unique string to prevent collision
CoronaLuaInitializeGCMetatable( L, kMetatableName, Finalizer );
// Functions in a plugin
const luaL_Reg kVTable[] =
{
{ "init", init },
{ "show", show },
{ NULL, NULL }
};
// Set plugin as upvalue for each plugin function
Self *plugin = new Self;
CoronaLuaPushUserdata( L, plugin, kMetatableName );
luaL_openlib( L, kName, kVTable, 1 ); // leave "plugin" on top of stack
return 1;
}
int
PluginMessages::Finalizer( lua_State *L )
{
Self *plugin = (Self *)CoronaLuaToUserdata( L, 1 );
CoronaLuaDeleteRef( L, plugin->GetListener() );
if (plugin->fDelegate) {
[plugin->fDelegate release];
}
delete plugin;
return 0;
}
PluginMessages *
PluginMessages::ToPlugin( lua_State *L )
{
// plugin is pushed as part of the closure
Self *plugin = (Self *)CoronaLuaToUserdata( L, lua_upvalueindex( 1 ) );
return plugin;
}
// [Lua] messages.init( listener )
int
PluginMessages::init( lua_State *L )
{
int listenerIndex = 1;
if ( CoronaLuaIsListener( L, listenerIndex, kEvent ) )
{
Self *plugin = ToPlugin( L );
CoronaLuaRef listener = CoronaLuaNewRef( L, listenerIndex );
plugin->Initialize( listener );
}
return 0;
}
int
PluginMessages::show( lua_State *L )
{
PluginMessages *plugin = ToPlugin(L);
if (plugin->fDelegate == nil) {
plugin->fDelegate = [[MyMessageDelegate alloc] initWithLuaState:L andPlugin:plugin];
}
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
controller.messageComposeDelegate = plugin->fDelegate;
controller.body = @"Hello!";
id<CoronaRuntime> runtime = (id<CoronaRuntime>)CoronaLuaGetContext( L );
[runtime.appViewController presentViewController:controller animated:YES completion:nil];
return 0;
}
CORONA_EXPORT int luaopen_plugin_messages( lua_State *L )
{
return PluginMessages::Open( L );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment