Exemplo n.º 1
0
        private async Task <GetClientsByUserIdResponse> TryGetClientsFromCache()
        {
            var clients = new GetClientsByUserIdResponse();

            try
            {
                clients.Clients = await CacheAccess.Get <List <Client> >(CacheKeys.CLIENTS);
            }
            catch
            {
                var user = await CacheAccess.GetSecure <User>(CacheKeys.USER_KEY);

                clients = await _clientAppService.GetClientsByUserId(user.UsuarioId.ToString());

                await CacheAccess.Insert <List <Client> >(CacheKeys.CLIENTS, clients.Clients);
            }
            return(clients);
        }
Exemplo n.º 2
0
        public override async void OnNavigatedTo(NavigationParameters parameters)
        {
            base.OnNavigatedTo(parameters);
            try
            {
                var user = await CacheAccess.GetSecure <User>(CacheKeys.USER_KEY);

                var token = await CacheAccess.GetSecure <Guid>(CacheKeys.ACCESS_TOKEN);

                var currentSeasonReponse = await _seasonAppService.CurrentSeason();

                var currentClients = await _clientsAppService.GetClientsByUserId(user.UsuarioId.ToString());

                var myTrophies = await _trophyAppService.GetCurrentTrophies(user.UsuarioId.ToString());

                await CacheAccess.Insert <List <Client> >(CacheKeys.CLIENTS, currentClients.Clients);

                var currentChallenges = await _challengesAppService.GetCurrentChallenges(currentSeasonReponse.Entity.TemporadaId.ToString());

                localBirthdays = await CheckAnniversary();

                Settings.AccessToken = token.ToString();
                Settings.UserId      = user.UsuarioId.ToString();
                await CheckInviteChallengeCompleted(myTrophies.Trophies, user.UsuarioId.ToString(), currentChallenges.Challenges, currentSeasonReponse.Entity);

                await _navigationService.NavigateAsync(NavigationSettings.MenuPrincipal);
            }
            catch
            {
                Settings.AccessToken = string.Empty;
                Settings.UserId      = string.Empty;
                await _navigationService.NavigateAsync(NavigationSettings.AbsoluteLogin);
            }
            finally
            {
            }
        }
        private async void RegisterNewClient()
        {
            if (!string.IsNullOrEmpty(ClientSelectedForEdition.Name) && !string.IsNullOrEmpty(ClientSelectedForEdition.Email) && !string.IsNullOrEmpty(ClientSelectedForEdition.Telephone) && !string.IsNullOrEmpty(ClientSelectedForEdition.Birthdate.ToString()))
            {
                try
                {
                    await NavigationHelper.ShowLoading();

                    var user = await CacheAccess.GetSecure <User>(CacheKeys.USER_KEY);

                    var userGuid = user.UsuarioId;

                    if (RegisterOrEditText.Contains("CADASTRAR"))
                    {
                        await _clientsAppService.RegisterClient(new RegisterClientRequest()
                        {
                            UsuarioId = userGuid,
                            Birthdate = ClientSelectedForEdition.Birthdate,
                            Cep       = ClientSelectedForEdition.Cep,
                            Email     = ClientSelectedForEdition.Email,
                            Name      = ClientSelectedForEdition.Name,
                            Telephone = ClientSelectedForEdition.Telephone
                        });

                        var myClients = await _clientsAppService.GetClientsByUserId(user.UsuarioId.ToString());

                        await CacheAccess.Insert <List <Client> >(CacheKeys.CLIENTS, myClients.Clients);

                        var points = new GetGamificationPointsResponse();
                        points = await _gamificationPointsAppService.GetCurrentGamificationPoints();

                        await CacheAccess.InsertSecure <Points>(CacheKeys.POINTS, points.Entity);

                        user.RegisterClientsPoints += (int)points.Entity.RegisterNewClients;

                        await _userAppService.UpdateUserPoints(new UpdateUserPointsRequest()
                        {
                            UsuarioId = userGuid,
                            AverageItensPerSalePoints = user.AverageItensPerSalePoints,
                            AverageTicketPoints       = user.AverageTicketPoints,
                            RegisterClientsPoints     = user.RegisterClientsPoints,
                            InviteAllyFlowersPoints   = user.InviteAllyFlowersPoints,
                            SalesNumberPoints         = user.SalesNumberPoints
                        });

                        await CacheAccess.InsertSecure <User>(CacheKeys.USER_KEY, user);
                        await GetParametersForChallenge();

                        TimeSpan duration = new TimeSpan(0, 0, 3);
                        if (!_hasWonTrophy)
                        {
                            UserDialogs.Instance.Toast("Parabéns! Você ganhou " + points.Entity.RegisterNewClients + " Sementes com esse Cadastro!", duration);
                        }
                        try
                        {
                            Analytics.TrackEvent(InsightsTypeEvents.ActionView, new Dictionary <string, string>
                            {
                                { InsightsPagesNames.RegisterClientsPage, InsightsActionNames.AddNewClient }
                            });
                        }
                        catch { }
                    }
                    else
                    {
                        try
                        {
                            await _clientsAppService.UpdateClientInfo(new UpdateClientInfoRequest()
                            {
                                ClienteId = ClientSelectedForEdition.ClienteId,
                                Birthdate = ClientSelectedForEdition.Birthdate,
                                Cep       = ClientSelectedForEdition.Cep,
                                Email     = ClientSelectedForEdition.Email,
                                Name      = ClientSelectedForEdition.Name,
                                Telephone = ClientSelectedForEdition.Telephone
                            });

                            var myClients = await _clientsAppService.GetClientsByUserId(user.UsuarioId.ToString());

                            await CacheAccess.Insert <List <Client> >(CacheKeys.CLIENTS, myClients.Clients);
                        }
                        catch
                        {
                            UserDialogs.Instance.Toast("Não foi possível editar sua cliente, verifique sua conexão!");
                        }
                        UserDialogs.Instance.Toast("Colecionadora editada com sucesso!");
                        try
                        {
                            Analytics.TrackEvent(InsightsTypeEvents.ActionView, new Dictionary <string, string>
                            {
                                { InsightsPagesNames.RegisterClientsPage, InsightsActionNames.EditClient }
                            });
                        }
                        catch { }
                    }
                }
                catch (Exception ex)
                {
                    if (RegisterOrEditText.Contains("CADASTRAR"))
                    {
                        UserDialogs.Instance.Toast(ex.Message);
                    }
                    else
                    {
                        UserDialogs.Instance.Toast("Não foi possível editar a Colecionadora.");
                    }
                }
                finally
                {
                    await NavigationHelper.PopLoading();

                    if (!_cameFromClientsForSale)
                    {
                        await _navigationService.NavigateAsync(NavigationSettings.MenuPrincipal);
                    }
                    else
                    {
                        await _navigationService.GoBackAsync();
                    }
                }
            }
            else
            {
                TimeSpan duration = new TimeSpan(0, 0, 2);
                _userDialogs.Toast("Insira todos os dados obrigatórios.", duration);
            }
        }