private async void Login()
        {
            var connection = await this.internet.CheckConnection();

            if (!connection.IsSuccess)
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Error",
                    connection.Message,
                    "Accept");

                await Application.Current.MainPage.Navigation.PopAsync();

                return;
            }
            if (this.IsRemembered)
            {
                this.Email = ActivityMaps.Utils.Settings.LastUsedEmail;
            }



            if (string.IsNullOrEmpty(this.Email))
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Error",
                    "You must enter an email.",
                    "Accept");

                return;
            }

            if (string.IsNullOrEmpty(this.Password))
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Error",
                    "You must enter an Password.",
                    "Accept");

                return;
            }

            this.IsRunning = true;
            this.IsEnabled = false;
            var userQuerry = await App.MobileService.GetTable <User>().Where(p => p.Email == Email).ToListAsync();

            if (this.isRegistered)
            {
                User_LogType.userLogTypesAsync(userQuerry[0].Id, logType);                //falta logica aqui
            }


            var result = string.Empty;

            if (userQuerry.Count > 0)
            {
                var passwordQuerry = await App.MobileService.GetTable <User_Password>().Where(p => p.User_Id_FK == userQuerry[0].Id).ToListAsync();

                if (passwordQuerry.Count > 0)
                {
                    byte[] decryted = System.Convert.FromBase64String(passwordQuerry[0].Password);

                    result = System.Text.Encoding.Unicode.GetString(decryted);
                }
            }
            else
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(
                    "Error",
                    "Email or Password is incorrect.",
                    "Accept");

                this.Password = string.Empty;
                return;
            }


            if (this.Email != userQuerry[0].Email || this.Password != result)
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(
                    "Error",
                    "Email or Password is incorrect.",
                    "Accept");

                this.Password = string.Empty;
                return;
            }

            this.IsRunning = false;
            this.IsEnabled = true;

            //this.Email = string.Empty;
            this.Password = string.Empty;


            MainViewModel.GetInstance().Activity_Child = new ActivityViewModel(userQuerry);

            await Application.Current.MainPage.Navigation.PushAsync(new ActivityPage());
        }
Exemplo n.º 2
0
        private async void CreateActivity()
        {
            CheckConnectionInternet.checkConnectivity();

            //if ((this.StartDay.Date + this.StartHour) < DateTime.Now)
            //{
            //    await Application.Current.MainPage.DisplayAlert(
            //        "Message",
            //        "Past date is not allowed.",
            //        "Ok");
            //    return;
            //}

            //if (this.StartDay.Date + this.StartHour > this.FinishDay.Date + this.FinishHour)
            //{
            //    await Application.Current.MainPage.DisplayAlert(
            //        "Message",
            //        "The Start Date is greater than Finish Date",
            //        "Ok");
            //    this.FinishDay = this.StartDay;
            //    this.FinishHour = this.StartHour;
            //    return;

            //}
            ValidateDatetime();

            if (string.IsNullOrEmpty(this.ActivityName))
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Error",
                    "You must enter an Activity Name.",
                    "Accept");

                return;
            }
            if (string.IsNullOrEmpty(this.Description))
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Error",
                    "You must enter a description.",
                    "Accept");

                return;
            }
            if (SelectedCategory == null)
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Error",
                    "You must select a Category.",
                    "Accept");

                return;
            }


            if (loc == null)
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Error",
                    "You must select a Location.",
                    "Accept");

                return;
            }

            if ((this.StartDay.Date + this.StartHour) < DateTime.Now)
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Message",
                    "Past date is not allowed.",
                    "Ok");

                this.StartHour = DateTime.Now.TimeOfDay;
                return;
            }


            string[] addrSplit = this.Placename.Split(',');
            for (int i = 0; i < addrSplit.Length; i++)
            {
                addrSplit[i] = addrSplit[i].Trim();
            }
            int len = RandomId.length.Next(5, 10);
            Activity_Location activity_location = new Activity_Location()
            {
                Id        = RandomId.RandomString(len),
                Nameplace = addrSplit[0],
                City      = addrSplit.Length == 5 ? addrSplit[2] : addrSplit[1],
                State     = "PR",//Cambiarlo luego con IF dinamico
                Country   = addrSplit.Length == 5 ? addrSplit[4] : addrSplit[3],
                ZipCode   = addrSplit.Length == 5 ? addrSplit[3] : addrSplit[2],
                IsSecure  = false,
                Latitude  = (decimal)this.loc.Latitude,
                Longitude = (decimal)this.loc.Longitude,
                CreatorOriginalPinLatitude  = (decimal)this.origLoc.Latitude,
                CreatorOriginalPinLongitude = (decimal)this.origLoc.Longitude
            };

            try
            {
                //find duplicate
                var query = await App.MobileService.GetTable <Activity_Location>().Where(
                    p => p.Nameplace == activity_location.Nameplace &&
                    p.City == activity_location.City &&
                    p.ZipCode == activity_location.ZipCode
                    ).ToListAsync();

                if (query.Count == 0)
                {
                    await App.MobileService.GetTable <Activity_Location>().InsertAsync(activity_location);
                }
                else
                {
                    var location = query?.FirstOrDefault();
                    activity_location.Id = location.Id;
                }
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Ok");
            }

            Activity activity = new Activity()
            {
                Id                 = RandomId.RandomString(len),
                Name               = this.ActivityName,
                Created_Date       = DateTime.Now,
                IsPrivate          = false,//todo
                Start_Act_Datetime = this.StartDay.Date + this.StartHour,
                End_Act_Datetime   = this.FinishDay.Date + this.FinishHour,
                Description        = this.Description.TrimEnd().TrimStart(),
                Status             = 1,//check
                IsService          = this.IsService,
                Activity_Cat_Code  = SelectedCategory.Id,
                Activity_Loc_Id    = activity_location.Id
            };

            //ctivity history
            Activity_History activityHistory = new Activity_History()
            {
                Id = RandomId.RandomString(len),
                Activity_Code_Id   = activity.Id,
                Name               = this.ActivityName,
                Created_Date       = DateTime.Now,
                IsPrivate          = false,//todo
                Start_Act_Date     = this.StartDay.Date + this.StartHour,
                End_Act_Date       = this.FinishDay.Date + this.FinishHour,
                Description        = this.Description,
                Status             = 1,//check
                IsService          = this.IsService,
                Activity_Cat_code  = SelectedCategory.Id,
                Activity_Loc_Id_FK = activity_location.Id
            };

            userCreating = User_LogType.userLogTypesAsync(userQuery[0].Id, usLog);
            User_Entered entry = new User_Entered()
            {
                Id                = RandomId.RandomString(len),
                Status            = "in",
                IsCreator         = true,
                User_Log_Id_FK1   = userCreating.Id,
                Activity_Code_FK2 = activityHistory.Activity_Code_Id
            };
            Entered_History entryHistory = new Entered_History()
            {
                Id                = entry.Id,
                Status            = "in",
                IsCreator         = true,
                Activity_Code_FK2 = activityHistory.Activity_Code_Id,
                UserJoin          = userQuery[0].Id,
                UserCreator       = userQuery[0].Id
            };

            try
            {
                await App.MobileService.GetTable <Activity>().InsertAsync(activity);

                await App.MobileService.GetTable <Activity_History>().InsertAsync(activityHistory);

                await App.MobileService.GetTable <User_Log>().InsertAsync(userCreating);

                await App.MobileService.GetTable <User_Entered>().InsertAsync(entry);

                await App.MobileService.GetTable <Entered_History>().InsertAsync(entryHistory);
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Ok");
            }

            MainViewModel.GetInstance().Activity_Child = new ActivityViewModel(userQuery, entry);
            await Application.Current.MainPage.Navigation.PushAsync(new ActivityPage());
        }