/
RMShare.cs
374 lines (293 loc) · 10 KB
/
RMShare.cs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
using System;
using System.Drawing;
using System.Collections.Generic;
using Mono.Data.Sqlite;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.Twitter;
using MonoTouch.FacebookConnect;
using MonoTouch.Dialog;
using System.Json;
namespace onermlog
{
public class RMShare
{
private UIViewController _vc;
// Facebook
public string _fbAppId;
public Facebook _facebook;
public FBDialogDelegate _wallDialogHandler;
// social
private string _socialMessage;
public RMShare (UIViewController viewC)
{
//this._canSendTweets = TWTweetComposeViewController.CanSendTweet;
// FB - change appid
this._fbAppId = "435387349843657";
var sessionDelegate = new SessionDelegate (this);
this._facebook = new Facebook (this._fbAppId, sessionDelegate);
var defaults = NSUserDefaults.StandardUserDefaults;
if (defaults ["FBAccessTokenKey"] != null && defaults ["FBExpirationDateKey"] != null){
this._facebook.AccessToken = defaults ["FBAccessTokenKey"] as NSString;
this._facebook.ExpirationDate = defaults ["FBExpirationDateKey"] as NSDate;
}
this._vc = viewC;
this._socialMessage = "";
ValidateSetup();
}
public string SocialMessage {
get { return this._socialMessage; }
set { this._socialMessage = value; }
}
public void ShowTwitterView () {
NetworkStatus status = Reachability.InternetConnectionStatus();
if(status == NetworkStatus.NotReachable) {
// Put alternative content/message here
ShowMessage ("Network Error", "Sharing requires an Internet connection. Please check your network settings and try again.");
}
else
{
var tvc = new TWTweetComposeViewController();
tvc.SetInitialText("I just got a new one rep max " + this.SocialMessage + " #onermlog");
this._vc.PresentModalViewController(tvc, true);
}
}
public void ShowFacebookView ()
{
NetworkStatus status = Reachability.InternetConnectionStatus();
if(status == NetworkStatus.NotReachable) {
// Put alternative content/message here
ShowMessage ("Network Error", "Sharing requires an Internet connection. Please check your network settings and try again.");
}
else
{
if (this._facebook.IsSessionValid)
PostToWall("test");
else
Login ();
}
}
/** Facebook Methods **/
public void Login ()
{
if (!this._facebook.IsSessionValid)
this._facebook.Authorize (new string [] { "offline_access" });
else
PostToWall("test");
}
public void UninstallFromFacebook ()
{
this._facebook.GraphRequest ("me/permissions", new NSMutableDictionary (), "DELETE", Handler (delegate {
ShowMessage ("Success", "The application has been uninstalled from Facebook");
// Clear out authentication values
this._facebook.AccessToken = null;
this._facebook.ExpirationDate = null;
ShowLoggedOut ();
}));
}
public void ShowLoggedIn () {
// implement for a config page
PostToWall("test");
}
public void ShowLoggedOut () {
// implement for a config page
}
/** For later
public Section FacebookConfig (UILabel label)
{
Section facebookConfig = new Section(label);
if (this._facebook.IsSessionValid) {
facebookConfig.Add(new StringElement ("Uninstall app", this.UninstallFromFacebook));
} else {
facebookConfig.Add(new StringElement ("Login to Facebook", this.Login));
}
return facebookConfig;
}
**/
public void PostToWall (string wallPost)
{
Console.WriteLine("post to wall launched");
var json = new JsonObject ();
json.Add ("name", new JsonPrimitive ("About One RM Log"));
json.Add ("link", new JsonPrimitive ("http://pennyfarthingapps.com")); // TODO: update with one rm website
// TODO: update the picture and URL for the facebook wall post
var parameters = NSMutableDictionary.FromObjectsAndKeys (
new object [] { "Hit a new one rep max", "one rm log app", "I just got a new one rep max " + this.SocialMessage,
"http://pennyfarthingapps.com", "http://www.wodti.me/img/facebook_icon_large.png", json.ToString ()},
new object [] { "name", "caption", "description", "link", "picture", "actions"});
_wallDialogHandler = DialogCallback (url => {
if (url.Query == null)
return;
var pars = System.Web.HttpUtility.ParseQueryString (url.Query);
if (pars ["post_id"] != null)
ShowMessage ("Success", "Posted to your wall");
});
this._facebook.Dialog ("feed", parameters, _wallDialogHandler);
}
public void SaveAuthorization () {
Console.WriteLine("authorizing");
var defaults = NSUserDefaults.StandardUserDefaults;
defaults ["FBAccessTokenKey"] = new NSString (this._facebook.AccessToken);
defaults ["FBExpirationDateKey"] = this._facebook.ExpirationDate;
defaults.Synchronize ();
}
public void ClearAuthorization () {
var defaults = NSUserDefaults.StandardUserDefaults;
defaults.RemoveObject ("FBAccessTokenKey");
defaults.RemoveObject ("FBExpirationDateKey");
defaults.Synchronize ();
}
public void ShowMessage (string caption, string message, NSAction callback = null)
{
var alert = new UIAlertView (caption, message, null, "Ok");
if (callback != null)
alert.Dismissed += delegate { callback (); };
alert.Show ();
}
public void SetupError (string msg)
{
ShowMessage ("Setup Error", msg, ()=>{Environment.Exit (1); });
}
//
// This method merely validates that the basic components of a Facebook
// app are complete, it should not be needed in a complete application,
// but will help in the first stages of debugging your Facebook
// interop
//
public void ValidateSetup ()
{
if (this._fbAppId == null)
SetupError ("Missing AppId, You can not run the app until this is setup");
// Validate the callback "fb[APPID]://authorize is in the Info.plist
// which is what facebook uses to call us back
bool urlFound = false;
try {
var arr = NSArray.FromArray<NSObject> (NSBundle.MainBundle.ObjectForInfoDictionary ("CFBundleURLTypes") as NSArray);
if (arr != null && arr.Length > 0){
var dict = arr [0] as NSDictionary;
arr = NSArray.FromArray<NSString> (dict [(NSString) "CFBundleURLSchemes"] as NSArray);
if (arr != null && arr.Length > 0){
var fbvalue = arr [0].ToString ();
if (fbvalue.StartsWith ("fb" + this._fbAppId))
urlFound = true;
}
} else
SetupError ("Missing fbXXXX URL handler in Info.plist's CFBundleURLTypes section");
} catch {
SetupError ("Invalid contents of Info.plist file");
}
if (!urlFound)
SetupError ("Missing correct fbXXXX i the Info.plist's setup");
// Check if the authorization callback will work
if (!UIApplication.SharedApplication.CanOpenUrl (new NSUrl ("fb" + this._fbAppId + "://authorize")))
SetupError ("Invalid or missing URL scheme. You cannot run the app until you set up a valid URL scheme in your .plist.");
}
// Pre-4.2 callback
// This method is called back when Facebook has authenticated us in the Web UI
public bool HandleOpenURL (UIApplication application, NSUrl url)
{
return this._facebook.HandleOpenURL (url);
}
// Post 4.2 callback
public bool OpenUrl (UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
Console.WriteLine ("Got: {0} and {1}", url, this._facebook.Handle);
try
{
this._facebook.HandleOpenURL (url);
return true;
}
catch (Exception ex)
{
Console.WriteLine ("A very bad thing happened : " + ex.Message );
return false;
}
}
public FBRequestDelegate Handler (Action<FBRequest,NSObject> handler)
{
return new RequestHandler (handler);
}
public FBDialogDelegate DialogCallback (Action<NSUrl> callback)
{
return new DialogHandler (callback);
}
// Method called back by all of the various Requests* methods
public void RequestsCallback (NSUrl url)
{
var collection = System.Web.HttpUtility.ParseQueryString (url.Query);
int count = 0;
foreach (var k in collection.AllKeys){
if (k.StartsWith ("request_ids"))
count++;
}
ShowMessage ("Request Result", "Successfully sent " + count + " requests");
}
}
class SessionDelegate : FBSessionDelegate
{
RMShare container;
public SessionDelegate (RMShare container)
{
this.container = container;
}
public override void DidLogin ()
{
//Console.WriteLine("did log in");
container.ShowLoggedIn ();
container.SaveAuthorization ();
}
public override void DidLogout ()
{
container.ClearAuthorization ();
container.ShowLoggedOut ();
}
public override void DidNotLogin (bool cancelled)
{
// TODO: Implement - see: http://go-mono.com/docs/index.aspx?link=T%3aMonoTouch.Foundation.ModelAttribute
}
public override void SessionInvalidated ()
{
// TODO: Implement - see: http://go-mono.com/docs/index.aspx?link=T%3aMonoTouch.Foundation.ModelAttribute
}
}
//
// Proxy class that turns method invocations into delegate calls
//
class RequestHandler : FBRequestDelegate {
static List<RequestHandler> handlers = new List<RequestHandler> ();
Action<FBRequest, NSObject> loadedHandler;
public RequestHandler (Action<FBRequest, NSObject> loadedHandler)
{
handlers.Add (this);
this.loadedHandler = loadedHandler;
}
public override void FailedWithError (FBRequest request, NSError error)
{
var u = new UIAlertView ("Request Error", "Failed with " + error.ToString (), null, "ok");
u.Dismissed += delegate {
handlers.Remove (this);
};
Console.WriteLine(error.Description.ToString());
u.Show ();
}
public override void RequestLoaded (FBRequest request, NSObject result)
{
loadedHandler (request, result);
handlers.Remove (this);
}
}
//
// Proxy call that turns method invocation into delegate calls
//
class DialogHandler : FBDialogDelegate {
Action<NSUrl> callback;
public DialogHandler (Action<NSUrl> callback)
{
this.callback = callback;
}
public override void CompletedWithUrl (NSUrl url)
{
callback (url);
}
}
}