Ejemplo n.º 1
0
        private async void ApplicationBarIconButton_OnClick(object sender, EventArgs e)
        {
            if (IsBusy)
            {
                IsBusy = true;
            }
            StateService.ProgressIndicatorService.Show("Logging in...");


            ApiResponse <List <Surveyor> > login = null;

            if (!Helpers.IsNetworkAvailable && StateService.Login == tbLogin.Text && StateService.Password == tbPassword.Password)
            {
                login = new ApiResponse <List <Surveyor> >()
                {
                    Data = new List <Surveyor>()
                    {
                        new Surveyor()
                        {
                            id = StateService.CurrentUserId
                        }
                    }, IsSuccess = true
                };
                MessageBox.Show("Working in Offline mode. Data will be synched when internet connection becomes available");
            }
            else
            {
                login = await new MyNetmeraClient().Login(tbLogin.Text, tbPassword.Password);
            }


            if (ApiResponseProcessor.Execute(login))
            {
                var userId = login.Data.First().id;

                if (StateService.PreviousUserId != userId)
                {
                    if (SyncEngine.IsSyncing)
                    {
                        MessageBox.Show("Syncing now, wait");
                        StateService.ProgressIndicatorService.Hide();
                        IsBusy = false;
                        return;
                    }

                    PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;


                    StateService.ProgressIndicatorService.Hide();
                    StateService.ProgressIndicatorService.Show("Syncing previous user data");

                    await SyncEngine.Sync(true);

                    PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;

                    StateService.ProgressIndicatorService.Hide();
                    StateService.ProgressIndicatorService.Show("Clearing database");

                    await new DbService().ClearDb();


                    StateService.ProgressIndicatorService.Hide();
                    StateService.ProgressIndicatorService.Show("Loading data");


                    var r = await new MyNetmeraClient().SyncUserData(userId);

                    StateService.ProgressIndicatorService.Hide();
                    PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;

                    if (!r.IsSuccess)
                    {
                        MessageBox.Show(string.IsNullOrEmpty(r.Message) ? "Error while loading data. Try again" : r.Message);
                        return;
                    }

                    StateService.CurrentUserId  = userId;
                    StateService.PreviousUserId = userId;

                    StateService.Login    = tbLogin.Text;
                    StateService.Password = tbPassword.Password;
                }
                else
                {
                    SyncEngine.Sync();
                }

                SyncEngine.InitializeTimer();
                ExNavigationService.Navigate <SelectCustomersPage>();
            }
            StateService.ProgressIndicatorService.Hide();
            IsBusy = false;
        }
Ejemplo n.º 2
0
        private async void ApplicationBarIconButton_OnClick(object sender, EventArgs e)
        {
            if (IsBusy)
            {
                IsBusy = true;
            }
            StateService.ProgressIndicatorService.Show("Logging in...");

            var offLineMode = false;

            LoginReply login = null;

            if (!Helpers.IsNetworkAvailable && StateService.Login == tbLogin.Text && StateService.Password == tbPassword.Password)
            {
                login = new LoginReply()
                {
                    UserId = StateService.CurrentUserId, IsSuccess = true
                };
                offLineMode = true;
                Helpers.LogEvent("Login", new Dictionary <string, string> {
                    { "UserId", StateService.CurrentUserId.ToString() }, { "AppVersion", Helpers.GetAppVersion() }
                });
                MessageBox.Show("Working in Offline mode. Data will be synched when internet connection becomes available");
            }
            else
            {
                login = await new MyNetmeraClient().Login(tbLogin.Text, tbPassword.Password);
            }


            if (ApiResponseProcessor.Execute(login))
            {
                StateService.CurrentUserType = (ESurveyorType)login.Type;

                Guid userId = login.UserId.Value;

                Helpers.LogEvent("Login", new Dictionary <string, string> {
                    { "UserId", userId.ToString() }, { "AppVersion", Helpers.GetAppVersion() }
                });

                if (StateService.PreviousUserId != userId)
                {
                    if (SyncEngine.IsSyncing)
                    {
                        MessageBox.Show("Syncing now, wait");
                        StateService.ProgressIndicatorService.Hide();
                        IsBusy = false;
                        return;
                    }

                    if (StateService.CurrentUserId == Guid.Empty)
                    {
                        StateService.CurrentUserId = userId;
                    }

                    PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;


                    StateService.ProgressIndicatorService.Hide();
                    StateService.ProgressIndicatorService.Show("Syncing previous user data");

                    await SyncEngine.Sync(true);

                    PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;

                    StateService.ProgressIndicatorService.Hide();
                    StateService.ProgressIndicatorService.Show("Clearing database");

                    await new DbService().ClearDb();

                    if (StateService.IsQA)
                    {
                        var myQaAddresses =
                            DataLoaderService.Convert <QAAddress, ServiceReference.QAAddress>(login.QaAddresses);

                        await new DbService().Save(myQaAddresses);

                        var myQaAddressesComments =
                            DataLoaderService.Convert <QAAddressComment, ServiceReference.QAAddressComment>(login.QaAddressComments);

                        await new DbService().Save(myQaAddressesComments);
                    }


                    StateService.ProgressIndicatorService.Hide();
                    StateService.ProgressIndicatorService.Show("Loading data");

                    bool isError = false;
                    StateService.CurrentUserTimeStamp = login.Timestamp;
                    StateService.CurrentUserId        = userId;
                    try
                    {
                        await new MyNetmeraClient().DownloadUserData();
                    }
                    catch (Exception exc)
                    {
                        isError = true;
                        Helpers.LogException(exc, "DownloadUserData");
                        MessageBox.Show(string.IsNullOrEmpty(exc.Message) ? "Error while loading data. Try again" : exc.Message);
                    }

                    if (isError)
                    {
                        await new MyNetmeraClient().ClearDb();
                    }

                    StateService.ProgressIndicatorService.Hide();
                    PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;

                    if (isError)
                    {
                        return;
                    }

                    StateService.PreviousUserId = userId;

                    StateService.Login    = tbLogin.Text;
                    StateService.Password = tbPassword.Password;
                }
                else
                {
                    bool isError = false;
                    //Process timestamp
                    if (!offLineMode && StateService.CurrentUserTimeStamp != login.Timestamp)
                    {
                        StateService.ProgressIndicatorService.Hide();
                        StateService.ProgressIndicatorService.Show("Refreshing user data");

                        try
                        {
                            await new MyNetmeraClient().RefreshUserData();
                            StateService.CurrentUserTimeStamp = login.Timestamp;
                        }
                        catch (Exception exc)
                        {
                            isError = true;
                            Helpers.LogException(exc, "RefreshUserData");
                            MessageBox.Show(string.IsNullOrEmpty(exc.Message) ? "Error while refreshing data. Try again" : exc.Message);
                        }
                    }

                    try
                    {
                        if (!isError)
                        {
                            StateService.ProgressIndicatorService.Hide();
                            StateService.ProgressIndicatorService.Show("Processing address move");
                            await new MyNetmeraClient().RefreshAddressMoves(login.AddressMoves);
                        }
                    }
                    catch (Exception exc)
                    {
                        isError = true;
                        Helpers.LogException(exc, "RefreshAddressMoves");
                        MessageBox.Show(string.IsNullOrEmpty(exc.Message) ? "Error while processing address moves. Try again" : exc.Message);
                    }

                    //update address properties
                    try
                    {
                        StateService.ProgressIndicatorService.Hide();
                        StateService.ProgressIndicatorService.Show("Refreshing addresses properties");

                        if (!offLineMode)
                        {
                            await new MyNetmeraClient().RefreshAddressesProperties();
                        }
                    }
                    catch (Exception exc)
                    {
                        isError = true;
                        Helpers.LogException(exc, "RefreshAddressesProperties");
                        MessageBox.Show(string.IsNullOrEmpty(exc.Message) ? "Error while refreshing data. Try again" : exc.Message);
                    }
                    //

                    if (isError)
                    {
                        StateService.ProgressIndicatorService.Hide();
                        IsBusy = false;
                        return;
                    }



                    SyncEngine.Sync();
                }

                SyncEngine.InitializeTimer();
                ExNavigationService.Navigate <SelectCustomersPage>();
            }
            StateService.ProgressIndicatorService.Hide();
            IsBusy = false;
        }
        private async void ApplicationBarIconButton_OnClick(object sender, EventArgs e)
        {
            await SyncEngine.Sync();

            RefreshSyncStatus(SynTextBlock);
        }