Esempio n. 1
0
        public async void DeleteWorkoutAsync()
        {
            IsBusy = true;
            var cWorkout = _context.Workouts.SingleOrDefault(w => w.Id.Equals(Workout.Id));
            var appMode  = (int)GetValueFromProperties(StoreKeys.AppMode);

            if (appMode == 0 && DoIHaveInternet())
            {
                var nS = new NetworkService
                {
                    AuthCookie = GetValueFromProperties(StoreKeys.AuthCookie) as string,
                };
                try
                {
                    var response = await nS.DeleteWorkout(cWorkout.ServerId);

                    if (response.IsSuccessStatusCode)
                    {
                        var connectedDWorkout = _context.DoneWorkouts.Where(w => w.WorkoutId.Equals(cWorkout.Id));
                        if (connectedDWorkout == null)
                        {
                            _context.Workouts.Remove(cWorkout);
                            _context.SaveChanges();
                            await Page.DisplayAlert("", AppResources.SuccessDelete, AppResources.Ok);
                        }
                        else
                        {
                            await Page.DisplayAlert("", AppResources.NotDeleteableWorkout, AppResources.Ok);
                        }
                    }
                    else
                    {
                        DeleteFromClientSideAsync(cWorkout);
                    }
                }
                catch (Exception e)
                {
                    await Page.DisplayAlert(AppResources.Error, e.Message, AppResources.Ok);

                    return;
                }
            }
            else
            {
                DeleteFromClientSideAsync(cWorkout);
            }

            NavigateAsync(nameof(MainPage));
            IsBusy = false;
        }