-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasyfb.lua
More file actions
211 lines (170 loc) · 6.62 KB
/
easyfb.lua
File metadata and controls
211 lines (170 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
module (..., package.seeall)
-- external modules ( remember to put json.lua in the same folder as this file )
local json = require( "json" )
local facebook = require( "facebook" )
-- facebook setup
local appId = "125744637524981"
-- Facebook Commands
local fbCommand
local LOGOUT = 1
local SHOW_DIALOG = 2
local POST_MSG = 3
local POST_PHOTO = 4
local GET_USER_INFO = 5
local GET_PLATFORM_INFO = 6
local statusMessage
local messageToPost
-- facebook handlers
local function createStatusMessage( message, x, y )
-- Show text, using default bold font of device (Helvetica on iPhone)
local textObject = display.newText( message, 0, 0, native.systemFontBold, 24 )
textObject:setTextColor( 255,255,255 )
-- A trick to get text to be centered
local group = display.newGroup()
group.x = x
group.y = y
group:insert( textObject, true )
-- Insert rounded rect behind textObject
local r = 10
local roundedRect = display.newRoundedRect( 0, 0, textObject.contentWidth + 2*r, textObject.contentHeight + 2*r, r )
roundedRect:setFillColor( 55, 55, 55, 190 )
group:insert( 1, roundedRect, true )
group.textObject = textObject
return group
end
local function getInfo_onRelease( event )
-- call the login method of the FB session object, passing in a handler
-- to be called upon successful login.
fbCommand = GET_USER_INFO
facebook.login( appId, facebookListener, {"publish_stream"} )
end
local function postMsg_onRelease( event )
-- call the login method of the FB session object, passing in a handler
-- to be called upon successful login.
fbCommand = POST_MSG
facebook.login( appId, facebookListener, {"publish_stream"} )
end
local function showDialog_onRelease( event )
-- call the login method of the FB session object, passing in a handler
-- to be called upon successful login.
fbCommand = SHOW_DIALOG
facebook.login( appId, facebookListener, {"publish_stream"} )
end
local function logOut_onRelease( event )
-- call the login method of the FB session object, passing in a handler
-- to be called upon successful login.
fbCommand = LOGOUT
facebook.logout()
end
function removeStatusMessage()
statusMessage.isVisible = false
statusMessage:removeSelf()
end
local function facebookListener( event )
print( "Facebook Listener events:" )
local maxStr = 20 -- set maximum string length
local endStr
for k,v in pairs( event ) do
local valueString = tostring(v)
if string.len(valueString) > maxStr then
endStr = " ... #" .. tostring(string.len(valueString)) .. ")"
else
endStr = ")"
end
print( " " .. tostring( k ) .. "(" .. tostring( string.sub(valueString, 1, maxStr ) ) .. endStr )
end
--- End of debug Event routine -------------------------------------------------------
print( "event.name", event.name ) -- "fbconnect"
print( "event.type:", event.type ) -- type is either "session" or "request" or "dialog"
print( "isError: " .. tostring( event.isError ) )
print( "didComplete: " .. tostring( event.didComplete) )
-----------------------------------------------------------------------------------------
-- After a successful login event, send the FB command
-- Note: If the app is already logged in, we will still get a "login" phase
--
if ( "session" == event.type ) then
-- event.phase is one of: "login", "loginFailed", "loginCancelled", "logout"
statusMessage.textObject.text = event.phase -- tjn Added
print( "Session Status: " .. event.phase )
if event.phase ~= "login" then
statusMessage.textObject.text = "login error"
-- Exit if login error
return
end
-- The following displays a Facebook dialog box for posting to your Facebook Wall
if fbCommand == SHOW_DIALOG then
statusMessage.textObject.text = "show dialog"
facebook.showDialog( {action="stream.publish"} )
end
-- Request the Platform information (FB information)
if fbCommand == GET_PLATFORM_INFO then
statusMessage.textObject.text = "platform info"
facebook.request( "platform" ) -- **tjn Displays info about Facebook platform
end
-- Request the current logged in user's info
if fbCommand == GET_USER_INFO then
statusMessage.textObject.text = "get user info"
facebook.request( "me" )
-- facebook.request( "me/friends" ) -- Alternate request
end
-- This code posts a photo image to your Facebook Wall
--
if fbCommand == POST_PHOTO then
statusMessage.textObject.text = "post photo"
local attachment = {
name = "Developing a Facebook Connect app using the Corona SDK!",
link = "http://developer.anscamobile.com/forum",
caption = "Link caption",
description = "Corona SDK for developing iOS and Android apps with the same code base.",
picture = "http://developer.anscamobile.com/demo/Corona90x90.png",
actions = json.encode( { { name = "Learn More", link = "http://anscamobile.com" } } )
}
facebook.request( "me/feed", "POST", attachment ) -- posting the photo
end
-- This code posts a message to your Facebook Wall
if fbCommand == POST_MSG then
statusMessage.textObject.text = "posting message"
local postMsg = {
message = messageToPost
}
facebook.request( "me/feed", "POST", postMsg ) -- posting the message
end
-----------------------------------------------------------------------------------------
end
if ( "request" == event.type ) then
-- event.response is a JSON object from the FB server
local response = event.response
statusMessage.textObject.text = "response"
timer.performWithDelay(1000, removeStatusMessage)
if ( not event.isError ) then
response = json.decode( event.response )
statusMessage.textObject.text = "response not error"
if fbCommand == GET_USER_INFO then
statusMessage.textObject.text = response.name
print( "name", response.name )
elseif fbCommand == POST_PHOTO then
statusMessage.textObject.text = "Photo Posted"
elseif fbCommand == POST_MSG then
statusMessage.textObject.text = "Message Posted"
else
-- Unknown command response
print( "Unknown command response" )
statusMessage.textObject.text = "Post failed, please try again later!"
end
else
-- Post Failed
statusMessage.textObject.text = "Post failed, please try again later!"
end
elseif ( "dialog" == event.type ) then
-- showDialog response
--
print( "dialog response:", event.response )
statusMessage.textObject.text = "event.response"
end
end
function publishOnFacebook( _messageToPost )
messageToPost = _messageToPost
statusMessage = createStatusMessage( "Connecting to Facebook", 0.5*display.contentWidth, 30 )
fbCommand = POST_MSG
facebook.login( appId, facebookListener, {"publish_stream"} )
end