Ejemplo n.º 1
0
        private async void OnLoadMoreClicked(object sender, EventArgs e)
        {
            int start = MyProfileViewModel.Instance.List.Count;

            var contributionInfo = await MvpHelper.MvpService.GetContributions(start, 5, LogOnViewModel.StoredToken);

            if (contributionInfo != null && contributionInfo.Contributions != null && contributionInfo.Contributions.Count > 0)
            {
                var contributors = contributionInfo.Contributions.Select(c =>
                {
                    MvpHelper.SetLabelTextOfContribution(c);
                    return(c);
                });
                MyProfileViewModel.Instance.List.AddRange(contributors);

                MyProfileViewModel.Instance.TotalOfData = contributionInfo.TotalContributions;
            }
        }
        public async void ButtonSaveClicked(object sender, EventArgs e)
        {
            IProgressDialog progress = null;

            try
            {
                bool isValid = CheckData();
                if (!isValid)
                {
                    return;
                }

                if (!CrossConnectivity.Current.IsConnected)
                {
                    await UserDialogs.Instance.AlertAsync("Please check connectivity to submit activity.", "Check Connectivity", "OK");

                    return;
                }

                IsBusy   = true;
                progress = UserDialogs.Instance.Loading("Saving...", maskType: MaskType.Clear);
                progress.Show();


                if (ViewModel.MyContribution == null)
                {
                    var model = new ContributionModel()
                    {
                        ContributionId         = "0",
                        ContributionType       = ViewModel.ContributionTypeNames[contributionTypeSelector.SelectedIndex],
                        ContributionTechnology = ViewModel.ContributionAreas[ContributionAreaSelector.SelectedIndex],
                        Visibility             = ViewModel.PersonGroups[PersonGroupSelector.SelectedIndex],
                        StartDate            = ContributionDateSelector.Date.ToUniversalTime(),
                        Title                = entryTitle.Text,
                        ReferenceUrl         = entryURL.Text,
                        Description          = entryDescription.Text,
                        AnnualQuantity       = Convert.ToInt32(entryAnnualQuantity.Text, System.Globalization.CultureInfo.InvariantCulture),
                        AnnualReach          = Convert.ToInt32(entryAnnualReach.Text, System.Globalization.CultureInfo.InvariantCulture),
                        SecondAnnualQuantity = Convert.ToInt32(entrySecondAnnualQuantity.Text, System.Globalization.CultureInfo.InvariantCulture)
                    };
                    var result = await MvpHelper.MvpService.AddContributionModel(model, LogOnViewModel.StoredToken);

                    if (result != null && result.ContributionId != "0")
                    {
                        Logger.Log("Activity-Added");
                        MvpHelper.SetLabelTextOfContribution(result);
                        MyProfileViewModel.Instance.List.Insert(0, result);
                        MyProfileViewModel.Instance.TotalOfData += 1;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    ViewModel.MyContribution.ContributionType       = ViewModel.ContributionTypeNames[contributionTypeSelector.SelectedIndex];
                    ViewModel.MyContribution.ContributionTechnology = ViewModel.ContributionAreas[ContributionAreaSelector.SelectedIndex];
                    ViewModel.MyContribution.Visibility             = ViewModel.PersonGroups[PersonGroupSelector.SelectedIndex];
                    ViewModel.MyContribution.StartDate            = ContributionDateSelector.Date.ToUniversalTime();
                    ViewModel.MyContribution.AnnualQuantity       = Convert.ToInt32(entryAnnualQuantity.Text, System.Globalization.CultureInfo.InvariantCulture);
                    ViewModel.MyContribution.AnnualReach          = Convert.ToInt32(entryAnnualReach.Text, System.Globalization.CultureInfo.InvariantCulture);
                    ViewModel.MyContribution.SecondAnnualQuantity = Convert.ToInt32(entrySecondAnnualQuantity.Text, System.Globalization.CultureInfo.InvariantCulture);
                    string result = await MvpHelper.MvpService.EditContributionModel(ViewModel.MyContribution, LogOnViewModel.StoredToken);

                    if (result == CommonConstants.OkResult)
                    {
                        Logger.Log("Activity-Edit");
                        MyProfileViewModel.Instance.List = new ObservableRangeCollection <ContributionModel>(MyProfileViewModel.Instance.List);
                    }
                    else
                    {
                        var currentContribution = MyProfileViewModel.Instance.List.Where(item => item.ContributionId == ViewModel.MyContribution.ContributionId).FirstOrDefault();
                        int index = MyProfileViewModel.Instance.List.IndexOf(currentContribution);
                        MyProfileViewModel.Instance.List.Remove(currentContribution);
                        MyProfileViewModel.Instance.List.Insert(index, ViewModel.MyContributionBackup);
                        return;
                    }
                }

                ViewModel.MyContribution = null;

#if DEBUG
                await Task.Delay(3000);
#endif

                progress?.Hide();

                await UserDialogs.Instance.AlertAsync("MVP activity has been saved successfully. Thank you for your contribution.", "Saved!", "OK");

                await Navigation.PopModalAsync();
            }
            catch (Exception ex)
            {
                progress?.Hide();
                ViewModel.ErrorMessage = ex.Message;
                await UserDialogs.Instance.AlertAsync("Looks like something went wrong. Please check your connection and submit again. Error: " + ex.Message, "Unable to save", "OK");
            }
            finally
            {
                if (progress?.IsShowing ?? false)
                {
                    progress?.Hide();
                }
                IsBusy = false;
            }
        }