public static async Task<bool> AddGoal(GoalDetails goalDetails) { if (!CrossConnectivity.Current.IsConnected) { return false; } try { string result = String.Empty; var client = new HttpClient(); client.Timeout = new TimeSpan(0, 15, 0); client.BaseAddress = new Uri(Constants.SERVICE_BASE_URL); client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data"); var url = "api.php?action=goalinsert1"; MultipartFormDataContent content = new MultipartFormDataContent(); if (App.MediaArray != null && App.MediaArray.Count > 0) { for (int index = 0; index < App.MediaArray.Count; index++) { int imgIndex = index + 1; MediaItem media = App.MediaArray[index]; content.Add(new StringContent(media.MediaString, Encoding.UTF8), "goal_media" + imgIndex.ToString()); content.Add(new StringContent(App.ExtentionArray[index], Encoding.UTF8), "file_type" + imgIndex.ToString()); if( media.MediaType != null && media.MediaType == Constants.MediaType.Video ) { content.Add(new StringContent(media.MediaThumbString, Encoding.UTF8), "video_thumb" + imgIndex.ToString()); } } content.Add(new StringContent(App.MediaArray.Count.ToString(), Encoding.UTF8), "media_count"); } if (App.ContactsArray != null && App.ContactsArray.Count > 0) { for (int index = 0; index < App.ContactsArray.Count; index++) { int contactsindex = index + 1; content.Add(new StringContent(App.ContactsArray[index], Encoding.UTF8), "contact_name" + contactsindex.ToString()); } content.Add(new StringContent(App.ContactsArray.Count.ToString(), Encoding.UTF8), "contact_count"); } if (!string.IsNullOrEmpty(goalDetails.start_date)) { content.Add(new StringContent(goalDetails.start_date, Encoding.UTF8), "start_date"); content.Add(new StringContent(goalDetails.end_date, Encoding.UTF8), "end_date"); } if (!string.IsNullOrEmpty(goalDetails.location_latitude)) content.Add(new StringContent(goalDetails.location_latitude, Encoding.UTF8), "location_latitude"); if (!string.IsNullOrEmpty(goalDetails.location_longitude)) content.Add(new StringContent(goalDetails.location_longitude, Encoding.UTF8), "location_longitude"); if (!string.IsNullOrEmpty(goalDetails.location_address)) content.Add(new StringContent(goalDetails.location_address, Encoding.UTF8), "location_address"); if (goalDetails.goal_title != null && goalDetails.goal_details != null && goalDetails.user_id != null) { content.Add(new StringContent(goalDetails.goal_title, Encoding.UTF8), "goal_title"); content.Add(new StringContent(goalDetails.goal_details, Encoding.UTF8), "goal_details"); content.Add(new StringContent(goalDetails.user_id, Encoding.UTF8), "user_id"); } if (!string.IsNullOrEmpty(goalDetails.goal_id)) { content.Add(new StringContent(goalDetails.goal_id, Encoding.UTF8), "goal_id"); } content.Add(new StringContent("1", Encoding.UTF8), "actionstatus_id");// to be confirmed - status id of new goal // for testing only // test content.Add(new StringContent("1", Encoding.UTF8), "category_id"); // category_id = 1 for testing only // test HttpResponseMessage response = await client.PostAsync(url, content); if (response != null && response.StatusCode == HttpStatusCode.OK) { var eventsJson = response.Content.ReadAsStringAsync().Result; var rootobject = JsonConvert.DeserializeObject<ResultJSon>(eventsJson); if (rootobject.code == "200") { client.Dispose(); App.ExtentionArray.Clear(); App.MediaArray.Clear(); return true; } } client.Dispose(); return false; } catch (Exception ex) { var test = ex.Message; return false; } }
async void NextButtonTapRecognizer_Tapped(object sender, System.EventArgs e) { User user = null; try { user = App.Settings.GetUser(); } catch (Exception ex) { } IProgressBar progress = DependencyService.Get<IProgressBar>(); try { if (string.IsNullOrWhiteSpace(eventDescription.Text) || string.IsNullOrWhiteSpace(eventTitle.Text)) { await DisplayAlert(Constants.ALERT_TITLE, "Value cannot be empty", Constants.ALERT_OK); } else { string input = pageTitle; CustomListViewItem item = new CustomListViewItem { Name = eventDescription.Text }; bool serviceResultOK = false; if (input == Constants.ADD_ACTIONS || input == Constants.EDIT_ACTIONS) { #region ADD || EDIT ACTIONS try { GoalDetails newGoal = new GoalDetails(); ActionModel details = new ActionModel(); if (!isUpdatePage) { progress.ShowProgressbar("Creating new action.."); } else { progress.ShowProgressbar("Updating the action.."); details.action_id = currentGemId; } details.action_title = eventTitle.Text; details.action_details = eventDescription.Text; details.user_id = user.UserId; details.location_latitude = lattitude; details.location_longitude = longitude; if(!string.IsNullOrEmpty(currentAddress)) { details.location_address = currentAddress; } //details.start_date = DateTime.Now.ToString("yyyy/MM/dd"); // for testing only //details.end_date = DateTime.Now.AddDays(1).ToString("yyyy/MM/dd"); // for testing only //details.start_time = DateTime.Now.AddHours(1).ToString("HH:mm"); //for testing only //details.end_time = DateTime.Now.AddHours(2).ToString("HH:mm"); //for testing only if (!string.IsNullOrEmpty(App.SelectedActionStartDate)) { DateTime myDate = DateTime.Now;//DateTime.ParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture); myDate = DateTime.Parse(App.SelectedActionStartDate); details.start_date = App.SelectedActionStartDate; details.end_date = App.SelectedActionEndDate; details.start_time = myDate.ToString("HH:mm"); myDate = DateTime.Parse(App.SelectedActionEndDate); details.end_time = myDate.ToString("HH:mm"); details.action_repeat = "0"; details.action_alert = App.SelectedActionReminderValue.ToString(); } if (!string.IsNullOrEmpty(currentAddress)) { details.location_address = currentAddress; } if (!await ServiceHelper.AddAction(details)) { await DisplayAlert(Constants.ALERT_TITLE, Constants.NETWORK_ERROR_MSG, Constants.ALERT_OK); } else { try { var suportingActions = await ServiceHelper.GetAllSpportingActions(); //for testing only if (suportingActions != null) { App.actionsListSource = null; App.actionsListSource = new List<CustomListViewItem>(); foreach (var action in suportingActions) { App.actionsListSource.Add(action); } } CustomListViewItem newEmotionItem = new CustomListViewItem(); newEmotionItem.EventID = App.actionsListSource.First().EventID; newEmotionItem.Name = App.actionsListSource.First().Name; newEmotionItem.SliderValue = App.actionsListSource.First().SliderValue; newEmotionItem.Source = Device.OnPlatform("tick_box.png", "tick_box.png", "//Assets//tick_box.png"); SelectedItemChangedEventArgs newEmotionEvent = new SelectedItemChangedEventArgs( newEmotionItem ); feelingSecondPage.OnActionPickerItemSelected( this, newEmotionEvent ); serviceResultOK = true; } catch (System.Exception) { DisplayAlert(Constants.ALERT_TITLE, "Error in retrieving goals list, Please try again", Constants.ALERT_OK); } } // ILocalNotification notfiy = DependencyService.Get<ILocalNotification>(); // if (!isUpdatePage) // { // notfiy.ShowNotification("Purpose Color - Action Created", "", eventTitle.Text, false); // } // else // { // notfiy.ShowNotification("Purpose Color - Action Updated","", eventTitle.Text, false); // } } catch (Exception ex) { var test = ex.Message; } progress.HideProgressbar(); #endregion } else if (input == Constants.ADD_EVENTS || input == Constants.EDIT_EVENTS) { #region ADD || EDIT EVENTS try { EventDetails details = new EventDetails(); details.event_title = eventTitle.Text; details.event_details = eventDescription.Text; details.user_id = user.UserId; details.location_latitude = lattitude; details.location_longitude = longitude; if (!string.IsNullOrEmpty(currentAddress)) { details.location_address = currentAddress; } if (!isUpdatePage) { progress.ShowProgressbar("Creating new event.."); } else { progress.ShowProgressbar("Updating the event.."); details.event_id = currentGemId; } if (!await ServiceHelper.AddEvent(details)) { await DisplayAlert(Constants.ALERT_TITLE, Constants.NETWORK_ERROR_MSG, Constants.ALERT_OK); } else { await FeelingNowPage.DownloadAllEvents(); serviceResultOK = true; CustomListViewItem newEmotionItem = new CustomListViewItem(); newEmotionItem.EventID = App.eventsListSource.First().EventID; newEmotionItem.Name = App.eventsListSource.First().Name; newEmotionItem.SliderValue = App.eventsListSource.First().SliderValue; SelectedItemChangedEventArgs newEmotionEvent = new SelectedItemChangedEventArgs( newEmotionItem ); feelingsPage.OnEventPickerItemSelected( this, newEmotionEvent ); // if(!isUpdatePage) // { // await Navigation.PopModalAsync(); // } // else { // await Navigation.PopModalAsync(); // } } } catch (Exception ex) { var test = ex.Message; } progress.HideProgressbar(); #endregion } else if (input == Constants.ADD_GOALS || input == Constants.EDIT_GOALS) { #region ADD || EDIT GOALS try { GoalDetails newGoal = new GoalDetails(); //EventDetails newGoal = new EventDetails(); newGoal.goal_title = eventTitle.Text; newGoal.goal_details = eventDescription.Text; newGoal.user_id = user.UserId; newGoal.location_latitude = lattitude; newGoal.location_longitude = longitude; newGoal.category_id = "1"; if (!string.IsNullOrEmpty(App.SelectedActionStartDate)) { newGoal.start_date = App.SelectedActionStartDate; //DateTime.Now.ToString("yyyy/MM/dd"); // for testing only newGoal.end_date = App.SelectedActionEndDate; //DateTime.Now.AddDays(1).ToString("yyyy/MM/dd"); // for testing onl } if (!string.IsNullOrEmpty(currentAddress)) { newGoal.location_address = currentAddress; } if (!isUpdatePage) { progress.ShowProgressbar("Creating new goal.."); } else { progress.ShowProgressbar("Updating the goal.."); newGoal.goal_id = currentGemId; } if (!await ServiceHelper.AddGoal(newGoal)) { await DisplayAlert(Constants.ALERT_TITLE, Constants.NETWORK_ERROR_MSG, Constants.ALERT_OK); } else { try { var goals = await ServiceHelper.GetAllGoals( user.UserId ); if (goals != null) { App.goalsListSource = null; App.goalsListSource = new List<CustomListViewItem>(); foreach (var goal in goals) { App.goalsListSource.Add(goal); } } CustomListViewItem newEmotionItem = new CustomListViewItem(); newEmotionItem.EventID = App.goalsListSource.First().EventID; newEmotionItem.Name = App.goalsListSource.First().Name; newEmotionItem.SliderValue = App.goalsListSource.First().SliderValue; SelectedItemChangedEventArgs newGoalsEvent = new SelectedItemChangedEventArgs( newEmotionItem ); feelingSecondPage.OnGoalsPickerItemSelected( this, newGoalsEvent ); serviceResultOK = true; } catch (System.Exception) { DisplayAlert(Constants.ALERT_TITLE, "Error in retrieving goals list, Please try again", Constants.ALERT_OK); } } } catch (Exception ex) { DisplayAlert(Constants.ALERT_TITLE, ex.Message, Constants.ALERT_OK); } progress.HideProgressbar(); #endregion } if (serviceResultOK) { if (!isUpdatePage) { await Navigation.PopAsync(); } else { if(Device.OS != TargetPlatform.iOS) { await Navigation.PopToRootAsync(); } else { await Navigation.PopToRootAsync(); } #region MyRegionNavigate back to GEM details page //progress.ShowProgressbar("Loading"); // // if (App.isEmotionsListing) { // try { // SelectedEventDetails eventDetails = await ServiceHelper.GetSelectedEventDetails(currentGemId); // if (eventDetails != null) // { // List<string> listToDownload = new List<string>(); // // foreach (var eventi in eventDetails.event_media) // { // if(string.IsNullOrEmpty(eventi.event_media)) // { // continue; // } // // if (eventi.media_type == "png" || eventi.media_type == "jpg" || eventi.media_type == "jpeg") // { // // listToDownload.Add(Constants.SERVICE_BASE_URL+eventi.event_media); // string fileName = System.IO.Path.GetFileName(eventi.event_media); // eventi.event_media = App.DownloadsPath + fileName; // } // else // { // eventi.event_media = Constants.SERVICE_BASE_URL + eventi.event_media ; // } // } // // if (listToDownload != null && listToDownload.Count > 0) { // IDownload downloader = DependencyService.Get<IDownload>(); // //progressBar.ShowProgressbar("loading details.."); // await downloader.DownloadFiles(listToDownload); // // } // // DetailsPageModel model = new DetailsPageModel(); // model.actionMediaArray = null; // model.eventMediaArray = eventDetails.event_media; // model.goal_media = null; // model.Media = null; // model.NoMedia = null; // model.pageTitleVal = "Event Details"; // model.titleVal = eventDetails.event_title; // model.description = eventDetails.event_details; // model.gemType = GemType.Event; // model.gemId = currentGemId; // progress.HideProgressbar(); // // await Navigation.PushAsync(new GemsDetailsPage(model)); // eventDetails = null; // } // } catch (Exception ) { // progress.HideProgressbar(); // Navigation.PopAsync(); // } // } // else // { // //-- call service for Action details // try { // // SelectedActionDetails actionDetails = await ServiceHelper.GetSelectedActionDetails(currentGemId); // // List<string> listToDownload = new List<string>(); // // foreach (var action in actionDetails.action_media) // { // if( string.IsNullOrEmpty(action.action_media)) // { // continue; // } // // if (action.media_type == "png" || action.media_type == "jpg" || action.media_type == "jpeg") // { // // listToDownload.Add(Constants.SERVICE_BASE_URL+action.action_media); // string fileName = System.IO.Path.GetFileName(action.action_media); // action.action_media = App.DownloadsPath + fileName; // } // else // { // action.action_media = Constants.SERVICE_BASE_URL + action.action_media; // } // } // // if (listToDownload != null && listToDownload.Count > 0) { // IDownload downloader = DependencyService.Get<IDownload>(); // //progressBar.ShowProgressbar("loading details.."); // await downloader.DownloadFiles(listToDownload); // //progressBar.HideProgressbar(); // } // // if (actionDetails != null) { // DetailsPageModel model = new DetailsPageModel(); // model.actionMediaArray = actionDetails.action_media; // model.eventMediaArray = null; // model.goal_media = null; // model.Media = null; // model.NoMedia = null; // model.pageTitleVal = "Action Details"; // model.titleVal = actionDetails.action_title; // model.description = actionDetails.action_details; // model.gemType = GemType.Action; // model.gemId = currentGemId; // // progress.HideProgressbar(); // await Navigation.PushAsync(new GemsDetailsPage(model)); // // // // // actionDetails = null; // } // } catch (Exception ) { // progress.HideProgressbar(); // Navigation.PopAsync(); // } // // #endregion } } } } catch (Exception ex) { var test = ex.Message; } }