private async void Download() { try { //TODO: check if connectivity if (!App.IsConnected) { await App.CurrentApp.MainPage.DisplayAlert(AppResources.NoInternetText, "", AppResources.OKButton); } else { //TODO: if any locally saved data, then uplo _hudProvider.DisplayProgress(AppResources.WorkingText); var observations = _observationRepository.All(mn => mn.ModifiedLocally == true); if (observations != null && observations.Count > 0) { var userinfo = _userInfoService.GetSavedInfo(); foreach (var ob in observations) { ob.Begin_Date = OffSetDateTimeZone((DateTime)ob.Begin_Date); ob.End_Date = OffSetDateTimeZone((DateTime)ob.End_Date); var obsResponse = await _observationService.ObservationSave(userinfo.Email, ob); _observationRepository.Upsert(obsResponse.observation); } } var user = _userInfoService.GetSavedInfo(); var response = await _observationService.GetAllData(user.Email, LanguageId); if (response != null) { //Need to determine if observations need to be uploaded _siteRepository.DeleteAll(); _activityRepository.DeleteAll(); _indicatorRepository.DeleteAll(); _siteIndicatorRepository.DeleteAll(); _observationRepository.DeleteAll(); _observationEntryRepository.DeleteAll(); _observationChangeRepository.DeleteAll(); _observationCommentRepository.DeleteAll(); _observationAttachmentRepository.DeleteAll(); _indicatorAgeRepository.DeleteAll(); //have response object so need to insert into database if (response.Sites != null) { foreach (var item in response.Sites) { _siteRepository.Upsert(item); } } if (response.Activities != null) { foreach (var activity in response.Activities) { _activityRepository.Upsert(activity); } } if (response.Indicators != null) { foreach (var indicator in response.Indicators) { _indicatorRepository.Upsert(indicator); } } if (response.SiteIndicators != null) { foreach (var si in response.SiteIndicators) { _siteIndicatorRepository.Upsert(si); } } if (response.Observations != null) { foreach (var obs in response.Observations) { //changes var changes = response.ObservationChanges.ToList().Where(m => m.Observation_id == obs.Observation_id).ToList(); obs.Changes = new ObservableCollection <ObservationChange>(changes); // comments var comments = response.ObservationComments.ToList().Where(m => m.Observation_Id == obs.Observation_id).ToList(); obs.Comments = new ObservableCollection <ObservationComment>(comments); // attachments var attachments = response.ObservationAttachments.ToList().Where(m => m.Observation_Id == obs.Observation_id).ToList(); obs.Attachments = new ObservableCollection <ObservationAttachment>(attachments); // Entries var entries = response.ObservationEntries.ToList().Where(m => m.Observation_id == obs.Observation_id).ToList(); obs.ObservationEntries = new ObservableCollection <ObservationEntry>(entries); //Need to get entries, changes, comments, and attachments _observationRepository.Upsert(obs); } } if (response.IndicatorAgePeriods != null) { foreach (var age in response.IndicatorAgePeriods) { _indicatorAgeRepository.Upsert(age); } } ////save to database //LastInformationText = AppResources.LastInformationText; //var ci = DependencyService.Get<ILocale>().GetCurrentCultureInfo(); //var date = string.Format(ci, "{0}", DateTime.Now); //LastInformationText = String.Format(AppResources.LastInformationText, date); user.LastDownload = DateTime.Now.ToString(); _userInfoService.SaveUserInfo(user); var last = DateTime.Parse(user.LastDownload); var ci = DependencyService.Get <ILocale>().GetCurrentCultureInfo(); LastInformationTextDate = user.LastDownload; //string.Format(ci, "{0}", last); LastInformationText = AppResources.LastInformationText; } //} } //return RateOptionsSubmissionResult.Failure; } catch (Exception ex) { var x = ex.ToString(); //return RateOptionsSubmissionResult.Failure; //tell view to pop alert //display error } finally { _hudProvider.Dismiss(); } }
private async void Save() { _hudProvider.DisplayProgress("Saving"); Observation.ModifiedLocally = true; Observation.Changes = Changes == null ? new ObservableCollection <ObservationChange>() : Changes; Observation.Comments = Comments == null ? new ObservableCollection <ObservationComment>() : Comments; Observation.Attachments = Attachments == null ? new ObservableCollection <ObservationAttachment>() : Attachments; //saving observation data var obsId = SelectedPeriod.ObservationID; Observation.Observation_id = SelectedPeriod.ObservationID; if (Observation.ObservationEntries == null) { Observation.ObservationEntries = new ObservableCollection <ObservationEntry>(); } //checking to see if they toggled //bool sameCount = Observation.ObservationEntries.Count == Obs.Count; if (obsId > 0) { //need to load up Observation with entries foreach (ObsViewModel ob in Obs) { //find entry for this vm ObservationEntry entry = Observation.ObservationEntries.Where(m => m.ObservationEntryId == ob.ObservationEntryId).First(); if (entry != null) { entry.LocalObservationID = SelectedPeriod.LocalObservationID; entry.Observation_id = obsId; entry.ModifiedLocally = true; entry.Count = (int)ob.IndicatorCount; entry.Numerator = ob.NumeratorValue; entry.Denominator = ob.DenominatorValue; entry.Yes_No = ob.IndicatorYesNo; entry.Indicator_Gender = ob.IndicatorGender; entry.Indicator_Age_Range_Id = ob.IndicatorAgeRangeId; } else { //new entry ObservationEntry newEntry = new ObservationEntry(); newEntry.Observation_id = obsId; newEntry.LocalObservationID = SelectedPeriod.LocalObservationID; newEntry.ModifiedLocally = true; newEntry.Count = (int)ob.IndicatorCount; newEntry.Numerator = ob.NumeratorValue; newEntry.Denominator = ob.DenominatorValue; newEntry.Yes_No = ob.IndicatorYesNo; entry.Indicator_Gender = ob.IndicatorGender; entry.Indicator_Age_Range_Id = ob.IndicatorAgeRangeId; Observation.ObservationEntries.Add(newEntry); } } } else { foreach (ObsViewModel ob in Obs) { //new entry ObservationEntry newEntry = new ObservationEntry(); newEntry.Observation_id = obsId; newEntry.LocalObservationID = SelectedPeriod.LocalObservationID; newEntry.ModifiedLocally = true; newEntry.Count = (int)ob.IndicatorCount; newEntry.Numerator = ob.NumeratorValue; newEntry.Denominator = ob.DenominatorValue; newEntry.Yes_No = ob.IndicatorYesNo; newEntry.Indicator_Age_Range_Id = ob.IndicatorAgeRangeId; newEntry.Indicator_Gender = ob.IndicatorGender; Observation.ObservationEntries.Add(newEntry); } } //need to check if dissagregated then add total row if (DisaggregatedSelected) { ObservationEntry newEntry = new ObservationEntry(); newEntry.Observation_id = obsId; newEntry.LocalObservationID = SelectedPeriod.LocalObservationID; newEntry.ModifiedLocally = true; newEntry.Count = (int)IndicatorCount; newEntry.Numerator = (int)IndicatorNumCount; newEntry.Denominator = (int)IndicatorDenCount; //newEntry.Yes_No = ob.IndicatorYesNo; //newEntry.Indicator_Age_Range_Id = ob.IndicatorAgeRangeId; //newEntry.Indicator_Gender = ob.IndicatorGender; Observation.ObservationEntries.Add(newEntry); } var userinfo = _userInfoService.GetSavedInfo(); //saving locally _observationRep.Upsert(Observation); //if connected try to save up to server if (App.IsConnected) { Observation.Begin_Date = OffSetDateTimeZone((DateTime)Observation.Begin_Date); Observation.End_Date = OffSetDateTimeZone((DateTime)Observation.End_Date); var response = await _observationService.ObservationSave(userinfo.Email, Observation); _observationRep.Upsert(response.observation); } _hudProvider.Dismiss(); await _Navigation.PopAsync(); }