Beispiel #1
0
		public mpGoalsViewModel(User selectedUser)
		{
			SelectedUser = selectedUser;
			//get the Member Image
			if(SelectedUser.ImageURL == null || SelectedUser.ImageURL == ""){
				MemberImage = "memberpic.png";

			}else{

				MemberImage = SelectedUser.ImageURL;
			}
			AvailableGoalsList = new ObservableCollection<GoalsAvailable> ();
			MemberGoalsList = new ObservableCollection<MemberGoals> ();
			if (CrossConnectivity.Current.IsConnected) {
				Task.Run (async() => {
					var availableGoalsListAPI = await CoachServices.GetAvailableGoals ();
					var memberGoalsListAPI = await CoachServices.GetMemberGoals(SelectedUser.ProfileID);

					//If available goals returned from the API delete all goals from local database
					if(availableGoalsListAPI.Count() > 0)
					{
						var goalsAvailableToDeleteDB = await GoalsAvailableDAL.GetGoalsAvailable();
						foreach(var goalToDelete in goalsAvailableToDeleteDB)
						{
							await GoalsAvailableDAL.DeleteGoalsAvailable(goalToDelete);
						}
					}

					//If member goals returned from the API delete all goals from local database
					if(memberGoalsListAPI.Count() > 0)
					{
						var memberGoalsToDeleteDB = await MemberGoalsDAL.GetMemberGoals(SelectedUser.ProfileID);
						foreach(var goalToDelete in memberGoalsToDeleteDB)
						{
							if(!goalToDelete.IsSoftDeleted)
							{
								await MemberGoalsDAL.DeleteMemberGoals(goalToDelete);
							}
						}
					}
					foreach(var availableGoalDTO in availableGoalsListAPI)
					{
						GoalsAvailable availableGoal = new GoalsAvailable();
						availableGoal.GoalID = availableGoalDTO.GoalID;
						availableGoal.GoalName = availableGoalDTO.GoalName;

						await GoalsAvailableDAL.SaveGoalsAvailable(availableGoal);
					}

					var goalsAvailableDB = await GoalsAvailableDAL.GetGoalsAvailable();
					AvailableGoalsList = new ObservableCollection<GoalsAvailable> (goalsAvailableDB);

					foreach(var memberGoalDTO in memberGoalsListAPI)
					{
						MemberGoals memberGoal = new MemberGoals();
						memberGoal.GoalID = memberGoalDTO.GoalID;
						memberGoal.GoalSetDate = memberGoalDTO.GoalSetDate;
						memberGoal.GoalValue = memberGoalDTO.GoalValue;
						memberGoal.MemberGoalValueID = memberGoalDTO.MemberGoalValueID;
						memberGoal.ProfileID = memberGoalDTO.ProfileID;

						string goalName = AvailableGoalsList.First(row => row.GoalID == memberGoal.GoalID).GoalName;
						memberGoal.GoalDisplayValue = memberGoal.GoalValue.ToString() + " " + goalName;
				
						await MemberGoalsDAL.SaveMemberGoals(memberGoal);
					}



					var memberGoalsDB = await MemberGoalsDAL.GetMemberGoals(SelectedUser.ProfileID);
					MemberGoalsList = new ObservableCollection<MemberGoals> (memberGoalsDB);
				});
			} else {
				DependencyService.Get<ICustomDialog>().Display("You are not connected to a network. The displayed data might not be up to date.", "OK");
				Task.Run (async() => {
					var goalsAvailableDB = await GoalsAvailableDAL.GetGoalsAvailable();
					AvailableGoalsList = new ObservableCollection<GoalsAvailable> (goalsAvailableDB);

					var memberGoalsDB = await MemberGoalsDAL.GetMemberGoals(SelectedUser.ProfileID);
					MemberGoalsList = new ObservableCollection<MemberGoals> (memberGoalsDB);
				});
			}

		}
Beispiel #2
0
		private async void ExecuteDeleteMemberGoalCommand (MemberGoals memberGoal)
		{

			if (IsProcessing)
				return;

			IsProcessing = true;

			//first try deleting through the api if there is a network connection

			if (CrossConnectivity.Current.IsConnected) {
				//Delete member goal in API
				int memberGoalValueID = memberGoal.MemberGoalValueID;
				//int memberGoalValueID = 123;
				var result = await CoachServices.DeleteMemberGoal(memberGoalValueID);

				//Hard delete assuming deletion was successful on the server
				if (result == string.Empty) {
					await MemberGoalsDAL.DeleteMemberGoals (memberGoal);
					var memberGoalsDB = await MemberGoalsDAL.GetMemberGoals(SelectedUser.ProfileID);
					MemberGoalsList = new ObservableCollection<MemberGoals> (memberGoalsDB);
				}
			} else {
				//Soft delete because deletion will not be in sync with the API
				memberGoal.IsSoftDeleted = true;
				await MemberGoalsDAL.SaveMemberGoals(memberGoal);
				var memberGoalsDB = await MemberGoalsDAL.GetMemberGoals(SelectedUser.ProfileID);
				MemberGoalsList = new ObservableCollection<MemberGoals> (memberGoalsDB);
			}
				
			IsProcessing = false;

			MessagingCenter.Send<mpGoalsPage> (Application.Current.MainPage.Navigation.NavigationStack.Last() as mpGoalsPage, "GoalsModified");

		}
Beispiel #3
0
		public async void AddGoal(GoalsAvailable goalAvailable, int goalValue)
		{
			
			List<MemberGoals> memberGoalsDTOList = new List<MemberGoals> ();

			MemberGoals memberGoalsDTO = new MemberGoals ();
			memberGoalsDTO.GoalID = goalAvailable.GoalID;
			memberGoalsDTO.GoalSetDate = DateTime.Now.ToString ();
			memberGoalsDTO.GoalValue = goalValue;

			//Insert new goal by assigning value of 0
			memberGoalsDTO.MemberGoalValueID = 0;
			memberGoalsDTO.ProfileID = SelectedUser.ProfileID;

			memberGoalsDTOList.Add (memberGoalsDTO);

			if (CrossConnectivity.Current.IsConnected) {
				var resultList = await CoachServices.SaveMemberGoalsToServer (memberGoalsDTOList);
				memberGoalsDTO.MemberGoalValueID = resultList [0].MemberGoalValueID;
			}

			MemberGoals memberGoal = new MemberGoals ();
			memberGoal.GoalDisplayValue = goalValue.ToString() + " " + goalAvailable.GoalName;
			memberGoal.GoalID = memberGoalsDTO.GoalID;
			memberGoal.GoalSetDate = memberGoalsDTO.GoalSetDate;
			memberGoal.GoalValue = memberGoalsDTO.GoalValue;
			memberGoal.MemberGoalValueID = memberGoalsDTO.MemberGoalValueID;
			memberGoal.ProfileID = memberGoalsDTO.ProfileID;

			await MemberGoalsDAL.SaveMemberGoals (memberGoal);

			var memberGoalsDB = await MemberGoalsDAL.GetMemberGoals(SelectedUser.ProfileID);
			MemberGoalsList = new ObservableCollection<MemberGoals> (memberGoalsDB);

			MessagingCenter.Send<mpGoalsPage> (Application.Current.MainPage.Navigation.NavigationStack.Last() as mpGoalsPage, "GoalsModified");
		}
Beispiel #4
0
		static async Task<bool> SyncMemberGoals (int gymID, bool returnValue)
		{
			
			var memberGoalsNoServerID = await MemberGoalsDAL.GetMemberGoalsNoMemberGoalValueID ();
			foreach (var goal in memberGoalsNoServerID) {
				try {
					List<MemberGoals> memberGoalsDTOList = new List<MemberGoals> ();
					MemberGoals memberGoalsDTO = new MemberGoals ();
					memberGoalsDTO.GoalID = goal.GoalID;
					memberGoalsDTO.GoalSetDate = goal.GoalSetDate;
					memberGoalsDTO.GoalValue = goal.GoalValue;
					memberGoalsDTO.MemberGoalValueID = goal.MemberGoalValueID;
					memberGoalsDTO.ProfileID = goal.ProfileID;
					memberGoalsDTOList.Add (memberGoalsDTO);
					var resultList = await CoachServices.SaveMemberGoalsToServer (memberGoalsDTOList);
					goal.MemberGoalValueID = resultList [0].MemberGoalValueID;
					if (goal.MemberGoalValueID == 0) {
						await MemberGoalsDAL.SaveMemberGoalsAndIncrementFailedCount (goal);
					} else {
						await MemberGoalsDAL.SaveMemberGoals (goal);
					}
				} catch (Exception e) {
					await MemberGoalsDAL.SaveMemberGoalsAndIncrementFailedCount (goal);
					returnValue = false;
					Insights.Report(e);
				}
			}
			return returnValue;
		}