Native Question

1 reply. Last post: October 2, 2014 4:20 AM by Hans Kristian
Tags :
  • (None)
  • New Discussion
  • New Question
  • New Product Feedback
Ryan HermanMember

I am unable to get the Crosslight Social to work correctly so I am using a Xamarin Component.  The only problem is, how do I send the Json object back up to the core when its done authenticating?  I tried using MobileCompnentServiceBase but it doesn't allow me to StartActivity to show the ui.  Is there a way to hook this into a Binding Property?

Here is the code for Native:

using System.Net.Mime;
using Android.App;
using Android.Graphics;
using Android.Renderscripts;
using Android.Widget;
using IncroudRadio.Android.Custom;
using IncroudRadio.BindingProviders;
using IncroudRadio.Custom;
using IncroudRadio.ViewModels;
using Intersoft.Crosslight;
using Intersoft.Crosslight.Android;
using System.Threading.Tasks;
using Xamarin.Auth;
using System.Json;
using System.Threading.Tasks;
using Android.App;
using Android.Widget;
using Android.OS;
using System;


namespace IncroudRadio.Android.Activities
{
    [Activity(Theme = "@style/Theme.Home")]
    [ImportBinding(typeof(LoginBindingProvider))]
    [RegisterNavigation(IsRootView = true)]
    public class HomeActivity : Activity<HomeViewModel>
    {
        #region Constructors

        public HomeActivity()
            : base(Resource.Layout.home_activity)
        {
		
        }

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            //SetContentView(Resource.Layout.home_activity);
            var facebook = FindViewById<Button>(Resource.Id.fb);
            facebook.Click += delegate { LoginToFacebook(false); };
        }

        public void LoginToFacebook(bool allowCancel)
        {
            var auth = new OAuth2Authenticator(
                clientId: "Client ID",
                scope: "email",
                authorizeUrl: new Uri("https://m.facebook.com/dialog/oauth/"),
                redirectUrl: new Uri("http://www.facebook.com/connect/login_success.html"));

            auth.AllowCancel = allowCancel;

            // If authorization succeeds or is canceled, .Completed will be fired.
            auth.Completed += (s, ee) =>
            {
                if (!ee.IsAuthenticated)
                {
                    var builder = new AlertDialog.Builder(this);
                    builder.SetMessage("Not Authenticated");
                    builder.SetPositiveButton("Ok", (o, e) => { });
                    builder.Create().Show();
                    return;
                }

                // Now that we're logged in, make a OAuth2 request to get the user's info.
                var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me"), null, ee.Account);
                request.GetResponseAsync().ContinueWith(t =>
                {
                    var builder = new AlertDialog.Builder(this);
                    if (t.IsFaulted)
                    {
                        builder.SetTitle("Error");
                        builder.SetMessage(t.Exception.Flatten().InnerException.ToString());
                    }
                    else if (t.IsCanceled)
                        builder.SetTitle("Task Canceled");
                    else
                    {
                        var obj = JsonValue.Parse(t.Result.GetResponseText());

                        builder.SetTitle("Logged in");
                        builder.SetMessage("Name: " + obj["name"] + " " + obj["email"]);
                    }

                    builder.SetPositiveButton("Ok", (o, e) => { });
                    builder.Create().Show();
                }, UIScheduler);

            };

            var intent = auth.GetUI(this);
            StartActivity(intent);
            IncroudRadioFacebook test = new IncroudRadioFacebookAndroid();
            
            
        }
        private static readonly TaskScheduler UIScheduler = TaskScheduler.FromCurrentSynchronizationContext();
        #endregion
    }
}

 

All times are GMT -5. The time now is 7:47 PM.
Previous Next