//protected override bool OnBackButtonPressedAsync() //{ // this.Navigation.PopAsync(); // return base.OnBackButtonPressed(); //} private async void UploadEvent_Clicked(object sender, EventArgs e) { if (string.IsNullOrEmpty(Event_Name.Text) || string.IsNullOrEmpty(Event_Description.Text) || string.IsNullOrEmpty(Event_Link.Text) || string.IsNullOrEmpty(Event_Contact.Text) || string.IsNullOrEmpty(Event_Date.ToString()) || string.IsNullOrEmpty(Event_Place.Text)) { await DisplayAlert("Empty Field", "Please Fill The Fields", "Ok"); return; } MyActivityIndicator.IsVisible = true; insertEvent.event_name = Event_Name.Text; insertEvent.event_description = Event_Description.Text; insertEvent.event_link = Event_Link.Text; insertEvent.event_cordinator_number = Event_Contact.Text.Split(',').ToList(); insertEvent.event_date = Event_Date.Date.ToString("MM d, yyyy"); insertEvent.event_location = Event_Place.Text; insertEvent.event_organizer = LoggedInUser.userID; insertEvent.event_status = "OPEN"; if (string.IsNullOrEmpty(IssueID)) { insertEvent.event_issue_id = ""; } else { insertEvent.event_issue_id = IssueID; } try { this.newEventResponse = await aPIService.InsertNewEvent(Constants.mongoDBBName, Constants.mongoDBCollectionEvents, Constants.mongoDBKey, insertEvent); MyActivityIndicator.IsVisible = false; if (!string.IsNullOrEmpty(this.newEventResponse._id.oid)) { await DisplayAlert("Success", "Successsfully Inserted The Record.", "ok"); await this.Navigation.PopAsync(); } else { await DisplayAlert("Failure", "Failed To Insert The Record.", "ok"); } } catch (Exception apiException) { this.newEventResponse = null; MongoCache.WriteOfflineEvent(insertEvent); await DisplayAlert("Failure", "Failed To Insert The Record. Will Try again when the internet is back.", "ok"); await this.Navigation.PopAsync(); } }
protected override async void OnStart() { // Handle when your app starts issuesToBeUploaded = MongoCache.ReadOfflineIssue(); eventsToBeUploaded = MongoCache.ReadOfflineEvent(); if (issuesToBeUploaded != null) { newRecordResponse = new NewRecordResponse(); aPIService = RestService.For <IAPIService>(Constants.mongoDBBaseUrl); int currentIssue = 0; if (issuesToBeUploaded.Count > 0) { try { foreach (var issue in issuesToBeUploaded) { newRecordResponse = await aPIService.InsertNewIssue(Constants.mongoDBBName, Constants.mongoDBCollectionIssues, Constants.mongoDBKey, issue); if (!string.IsNullOrEmpty(newRecordResponse._id.oid)) { // This is Success. /* * First, we get the issue list from application settings and then we iterate over each and every saved instance. * Now, when we have success, we remove that item, so that it doesn't appear again in the list. * Now, we need to increment the issue count. */ issuesToBeUploaded.RemoveAt(currentIssue); currentIssue++; } else { // This is Failure. Console.WriteLine("Failure Occurred. Moving To Next One."); } } foreach (var item in issuesToBeUploaded) { MongoCache.WriteOfflineIssue(item); } } catch (Exception ex) { // Move On. Console.WriteLine("Error While Uploading Issue Backlog : " + ex.Message); } } } // Issue Uploaded if (eventsToBeUploaded != null) { newEventResponse = new Event(); aPIService = RestService.For <IAPIService>(Constants.mongoDBBaseUrl); int currentEvent = 0; if (eventsToBeUploaded.Count > 0) { try { foreach (var eventVal in eventsToBeUploaded) { newEventResponse = await aPIService.InsertNewEvent(Constants.mongoDBBName, Constants.mongoDBCollectionEvents, Constants.mongoDBKey, eventVal); if (!string.IsNullOrEmpty(newEventResponse._id.oid)) { // This is Success. /* * First, we get the event list from application settings and then we iterate over each and every saved instance. * Now, when we have success, we remove that item, so that it doesn't appear again in the list. * Now, we need to increment the issue count. */ eventsToBeUploaded.RemoveAt(currentEvent); currentEvent++; } else { // This is Failure. Console.WriteLine("Failure Occurred. Moving To Next One."); } } foreach (var item in eventsToBeUploaded) { MongoCache.WriteOfflineEvent(item); } } catch (Exception ex) { // Move On. Console.WriteLine("Error While Uploading Event Backlog : " + ex.Message); } } } // Event Uploaded }