Exemplo n.º 1
0
        /// <summary>
        /// Saves a contribution.
        /// </summary>
        async Task SaveContribution()
        {
            try
            {
                if (!await VerifyInternetConnection())
                {
                    return;
                }

                if (!Contribution.IsValid())
                {
                    IsContributionValid = false;
                    return;
                }

                IsContributionValid = true;

                State = LayoutState.Saving;

                if (IsEditing)
                {
                    var result = await MvpApiService.UpdateContributionAsync(Contribution.ToContribution());

                    if (!result)
                    {
                        await DialogService.AlertAsync(Translations.error_couldntsavecontribution, Translations.error_title, Translations.ok).ConfigureAwait(false);

                        return;
                    }

                    MainThread.BeginInvokeOnMainThread(() => HapticFeedback.Perform(HapticFeedbackType.LongPress));
                    AnalyticsService.Track("Contribution Added");
                    await CloseModalAsync().ConfigureAwait(false);;
                    await BackAsync().ConfigureAwait(false);

                    MessagingService.Current.SendMessage(MessageKeys.RefreshNeeded);
                }
                else
                {
                    var result = await MvpApiService.SubmitContributionAsync(Contribution.ToContribution());

                    if (result == null)
                    {
                        await DialogService.AlertAsync(Translations.error_couldntsavecontribution, Translations.error_title, Translations.ok).ConfigureAwait(false);

                        return;
                    }

                    AnalyticsService.Track("Contribution Edited");
                    MainThread.BeginInvokeOnMainThread(() => HapticFeedback.Perform(HapticFeedbackType.LongPress));
                    await CloseModalAsync().ConfigureAwait(false);

                    MessagingService.Current.SendMessage(MessageKeys.RefreshNeeded);
                }
            }
            catch (Exception ex)
            {
                AnalyticsService.Report(ex);

                await DialogService.AlertAsync(Translations.error_couldntsavecontribution, Translations.error_title, Translations.ok).ConfigureAwait(false);
            }
            finally
            {
                State = LayoutState.None;
            }
        }