/// <summary>
        /// Login button clicked method.
        /// Network Connectivity is checked.
        /// Authentication
        /// DB version is checked with the server and db updation happens according
        /// </summary>
        /// <param name="sender">Sender.</param>

        async partial void btnLogin_TouchUpInside(UIButton sender)
        {
            this.ResignFirstResponder();

            this.txtUserName.ResignFirstResponder();
            this.txtPassword.ResignFirstResponder();

            try
            {
                this.ResignFirstResponder();

                if (CrossConnectivity.Current.IsConnected == false)
                {
                    if (CrossConnectivity.Current.IsConnected == false)
                    {
                        UIAlertView alert = new UIAlertView(@"Alert", @"You are not connected to the network", null, NSBundle.MainBundle.LocalizedString("OK", "OK"));
                        alert.Show();
                        return;
                    }
                }

                if (txtUserName.Text.Length <= 0 || txtPassword.Text.Length <= 0)
                {
                    UIAlertView alert = new UIAlertView(@"Alert", @"Please fill in both username and password", null, NSBundle.MainBundle.LocalizedString("OK", "OK"));
                    alert.Show();
                    return;
                }

                if (txtUserName.Text.ToUpper() == Constants.AdminUserID && txtPassword.Text.ToUpper() == Constants.AdminPassword)
                {
                    config con = this.Storyboard.InstantiateViewController("config") as config;
                    this.NavigationController.PushViewController(con, true);
                }
                else if (txtUserName.Text.ToUpper() == Constants.AdminUserID && txtPassword.Text.ToUpper() != Constants.AdminPassword)
                {
                    UIAlertView alert = new UIAlertView(@"Alert", @"Invalid Username Or Password", null, NSBundle.MainBundle.LocalizedString("OK", "OK"));
                    alert.Show();
                }
                else
                {
                    if (AppDelegate.DatabaseContext != null)
                    {
                        //AppDelegate del=new AppDelegate();
                        loginScrollView.ContentOffset = new CGPoint(0, 0);


                        LoadOverLayPopup();
                        //getter method should return if we should allow going to dashboard or not

                        string username = txtUserName.Text;
                        string password = txtPassword.Text;
                        authenticatedUser = await Task.Run(() => LoginToService(username, password));

                        HideOverLay();


                        if (authenticatedUser.Result != null && (authenticatedUser.Result.code == 0 || authenticatedUser.Result.code == 1) && authenticatedUser.UserDetails != null && authenticatedUser.IsTokenActive)
                        {
                            AppDelegate.user = authenticatedUser;
                            using (MasterDataUpdateService masterDataUpdateService = new MasterDataUpdateService(AppDelegate.DatabaseContext))
                            {
                                string dbVersion = "1.0";
                                using (UserSettingService usrSetting = new UserSettingService(AppDelegate.DatabaseContext))
                                {
                                    var dbVersions = usrSetting.GetUserSettings().Where(u => u.SettingName == "DBVersion");
                                    if (dbVersions != null && dbVersions.Count() > 0)
                                    {
                                        dbVersion = dbVersions.FirstOrDefault().SettingValue;
                                    }
                                }
                                var AppDbVersion    = string.IsNullOrEmpty(dbVersion) ?1:float.Parse(dbVersion);
                                var serverDBVersion = string.IsNullOrEmpty(authenticatedUser.DBVersion) ?1:float.Parse(authenticatedUser.DBVersion);

                                if (AppDbVersion < serverDBVersion)
                                {
                                    UIAlertView alert1 = new UIAlertView(@"Alert", @"Master Data update available,DB update is going to start.", null, NSBundle.MainBundle.LocalizedString("Ok", "Ok"));
                                    alert1.Show();

                                    alert1.Clicked += async(DBsender, buttonArgs) => {
                                        masterDataUpdateService.progressDelegate = this.UpdateProgress;
                                        this.ResignFirstResponder();
                                        this.View.UserInteractionEnabled = false;
                                        progressView.Hidden      = false;
                                        dataProgressLabel.Hidden = false;
                                        CGAffineTransform transform = CGAffineTransform.MakeScale(1.0f, 6.0f);
                                        progressView.Transform = transform;
                                        MasterDataUpdateStatus resultMasterData = await Task.Run(() => masterDataUpdateService.UpdateDatabaseService(authenticatedUser.UserDetails.Token));

                                        progressView.Hidden      = true;
                                        dataProgressLabel.Hidden = true;
                                        if (resultMasterData != null)
                                        {
                                            UIAlertView alert = new UIAlertView(@"Alert", resultMasterData.ErrorMessage, null, NSBundle.MainBundle.LocalizedString("OK", "OK"));
                                            alert.Show();
                                            if (resultMasterData.Result == false)
                                            {
                                                this.View.UserInteractionEnabled = true;
                                                AppDelegate.deleteSession();
                                            }
                                            else
                                            {
                                                this.View.UserInteractionEnabled = true;
                                                MoveToDashBoard(authenticatedUser);
                                            }
                                        }
                                        else
                                        {
                                            this.View.UserInteractionEnabled = true;
                                        }
                                    };
                                }
                                else
                                {
                                    MoveToDashBoard(authenticatedUser);
                                }
                            }
                        }
                        else if (authenticatedUser.Result != null && (authenticatedUser.Result.code == 0 || authenticatedUser.Result.code == 1) && authenticatedUser.UserDetails != null && !authenticatedUser.IsTokenActive)
                        {
                            UIAlertView alert1 = new UIAlertView(@"Invalid Token", @"Token Expired", null, NSBundle.MainBundle.LocalizedString("OK", "OK"));
                            alert1.Show();
                        }
                        else
                        {
                            this.ShowErrorResponse(authenticatedUser.Result.code);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception Occured in btnLogin_TouchUpInside  method due to " + ex.Message);
            }
            finally
            {
                progressView.Hidden              = true;
                dataProgressLabel.Hidden         = true;
                this.View.UserInteractionEnabled = true;
            }
        }
Beispiel #2
0
        //
        // This method is invoked when the application has loaded and is ready to run. In this
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            try
            {
                UIApplication.SharedApplication.IdleTimerDisabled = false;


                databaseContext = CreateDB();
                app             = this;
                dataSync        = new Sync(databaseContext);
                //Your View Controller Identifiers defined in Interface Builder
                String firstViewControllerIdentifier  = @"LoginViewController";
                String secondViewControllerIdentifier = @"DashBoardViewController";

                //check if the key exists and its value
                LoginBAL loginBal = new LoginBAL(AppDelegate.DatabaseContext);
                Model.AuthenticatedUser authenticatedUser = loginBal.GetUserDetailsFromDatabase(AppDelegate.DatabaseContext);
                AppDelegate.user = authenticatedUser;


                bool isUserDetailsAvailableInDB = false;
                if (authenticatedUser != null && authenticatedUser.UserDetails != null && !string.IsNullOrEmpty(authenticatedUser.UserDetails.Token))
                {
                    authenticatedUser.IsTokenActive = CheckIfTokenIsActive(authenticatedUser.UserDetails.ExpiryDate);
                    isUserDetailsAvailableInDB      = true;
                }

                //check which view controller identifier should be used
                String viewControllerIdentifier = (isUserDetailsAvailableInDB && authenticatedUser.IsTokenActive)? secondViewControllerIdentifier : firstViewControllerIdentifier;

                //IF THE STORYBOARD EXISTS IN YOUR INFO.PLIST FILE AND YOU USE A SINGLE STORYBOARD
                UIStoryboard storyboard = Window.RootViewController.Storyboard;

                if (isUserDetailsAvailableInDB && authenticatedUser.IsTokenActive)
                {
                    startAutoSync();
                    viewControllerIdentifier = secondViewControllerIdentifier;
                    DashBoardViewController dashBoardViewController = storyboard.InstantiateViewController("DashBoardViewController") as DashBoardViewController;
                    dashBoardViewController.IsFirstLogin = authenticatedUser.IsFirstTimeLoggedIn;
                    DashBoardViewController.Token        = authenticatedUser.UserDetails.Token;
                    DashBoardViewController.UserName     = string.Concat(authenticatedUser.UserDetails.FirstName, " ", authenticatedUser.UserDetails.LastName);
                }
                else if (isUserDetailsAvailableInDB && !authenticatedUser.IsTokenActive)
                {
                    viewControllerIdentifier = firstViewControllerIdentifier;
                    deleteSession();
                    UIAlertView alert = new UIAlertView(@"Alert", @"Token Expired", null, NSBundle.MainBundle.LocalizedString("OK", "OK"));
                    alert.Show();
                }
                else
                {
                    viewControllerIdentifier = firstViewControllerIdentifier;
                    deleteSession();
                }
                //instantiate the view controller
                UIViewController presentedViewController = storyboard.InstantiateViewController(viewControllerIdentifier);
                if (presentedViewController is DashBoardViewController)
                {
                    (presentedViewController as DashBoardViewController).IsIntiatedFromAppDelegate = true;
                }
                //UIApplication.SharedApplication.IdleTimerDisabled = true;
                UINavigationController navController = (UINavigationController)Window.RootViewController;
                navController.PushViewController(presentedViewController, false);

                return(true);
            }
            catch (Exception ex)
            {
                return(true);
            }
        }