conceptdev / xamarin-forms-samples

Samples that use Xamarin.Forms (http://xamarin.com/forms)
MIT License
471 stars 657 forks source link

LoginDemo in Xamarin.Forms 1.3 #7

Closed tkowalczyk closed 9 years ago

tkowalczyk commented 9 years ago

Hi Craig, Is tere any chance to port this sample to XF 1.3? Best, TK

tkowalczyk commented 9 years ago

@conceptdev any suggestions? How to replace SetPage in MainActivity and implement correctly ILoginManager? I am not able to use there LoadApplication().

tkowalczyk commented 9 years ago

Here is my workaround to do this, @conceptdev please tell me if it is correct.

`public class App : Application
{
    static ILoginManager loginManager;

    public App()
    {
    }

    public App(ILoginManager ilm)
    {
        loginManager = ilm;
        MainPage = new LoginModalPage (ilm);
    }

    public void GetLoginPage (ILoginManager ilm)
    {   
        MainPage = new LoginModalPage (ilm);
    }

    public void GetMainPage ()
    {   
        MainPage = new LoginPattern.MainPage ();
    }

    public static void Logout ()
    {
        loginManager.Logout();
    }
}`

and MainActivity:

`public class MainActivity : FormsApplicationActivity, ILoginManager
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        Xamarin.Forms.Forms.Init (this, bundle);

        LoadApplication (new App (this));
    }

    #region ILoginManager implementation

    public void ShowMainPage ()
    {
        var ap = (App)Xamarin.Forms.Application.Current;
        ap.GetMainPage ();
    }

    public void Logout() 
    {
        var ap = (App)Xamarin.Forms.Application.Current;
        ap.GetLoginPage (this);
    }
    #endregion
}`
conceptdev commented 9 years ago

It's actually a lot easier than that - I've just updated the code - but you can now do everything in the App.cs common code, no need for stuff in the iOS and Android projects

    public class App : Application, ILoginManager
    {
        static ILoginManager loginManager;
        public static App Current;

        public App ()
        {   
            Current = this;

            var isLoggedIn = Properties.ContainsKey("IsLoggedIn")?(bool)Properties ["IsLoggedIn"]:false;

            // we remember if they're logged in, and only display the login page if they're not
            if (isLoggedIn)
                MainPage = new LoginPattern.MainPage ();
            else
                MainPage = new LoginModalPage (this);
        }

        public void ShowMainPage ()
        {   
            MainPage = new MainPage ();
        }

        public void Logout ()
        {
            Properties ["IsLoggedIn"] = false; // only gets set to 'true' on the LoginPage
            MainPage = new LoginModalPage (this);
        }
    }
tkowalczyk commented 9 years ago

@conceptdev cool!, big thanks will check this out!