private void UpdateExisting(DateTime now) { // Custom Values edit for (var i = 0; i < CustomFields.Count(); i++) { // has value // is same // delete if no value but previously existed // add if has value but not existing // edit value if it changed var jsonValue = @""; var hasValue = false; switch (CustomFields[i].FieldType) { case @"text": var textValue = ((CustomFieldStringValueViewModel)CustomFieldControls[i].BindingContext).EntryValue; if (textValue != null && !textValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonText(textValue); } break; case @"textarea": var textAreaValue = ((CustomFieldStringValueViewModel)CustomFieldControls[i].BindingContext).EntryValue; if (textAreaValue != null && !textAreaValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonTextArea(textAreaValue); } break; case @"check_box": var checkBoxValues = ((CustomFieldSwitchArrayView)CustomFieldControls[i]).GetSelectedValuesAsList(); if (checkBoxValues.Any()) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonCheckBox(checkBoxValues); } break; case @"radio_button": var radioButtonValue = ((CustomFieldPickerViewModel)CustomFieldControls[i].BindingContext).SelectedBindableOption; if (radioButtonValue != null && !radioButtonValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonRadioButton(radioButtonValue); } break; case @"select": var selectValue = ((CustomFieldPickerViewModel)CustomFieldControls[i].BindingContext).SelectedBindableOption; if (selectValue != null && !selectValue.Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonSelect(selectValue); } break; case @"number": var numberValue = ((CustomFieldDoubleValueViewModel)CustomFieldControls[i].BindingContext).GetDoubleValue(); if (numberValue != null) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonNumber((double)numberValue); } break; case @"date": var dateValue = ((CustomFieldDateTimeValueViewModel)CustomFieldControls[i].BindingContext).DateValue; if (dateValue != null && !dateValue.ToString().Equals(string.Empty)) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonDate((DateTime)dateValue); } break; case @"rank_list": var rankedValues = ((CustomFieldRankListViewModel)CustomFieldControls[i].BindingContext).GetRankedValues(); if (!rankedValues.Equals(@"")) { hasValue = true; jsonValue = Helpers.CustomValueConverter.ConvertCustomValueToJsonRankList(((CustomFieldRankListViewModel)CustomFieldControls[i].BindingContext).Entries.ToList()); } break; default: break; } var existingValueQuery = ApplicationInstanceData.Data.CustomHouseholdValues .Where(a => a.Household.InternalId == Household.InternalId && a.CustomField.InternalId == CustomFields[i].InternalId); if (hasValue) { // check to see if new needed if (!existingValueQuery.Any()) { // add a record var newCustomValue = new CustomHouseholdValue { CreatedAt = now, LastUpdatedAt = now, SoftDeleted = false, CustomField = CustomFields[i], Value = jsonValue, Household = Household, InternalParentId = Household.InternalId }; ApplicationInstanceData.Data.CustomHouseholdValues.Add(newCustomValue); } else { // if existing compare and update if necesary if (existingValueQuery.First().Value != jsonValue) { existingValueQuery.First().Value = jsonValue; } } } else { // delete value if it exists if (existingValueQuery.Any()) { ApplicationInstanceData.Data.CustomHouseholdValues.Remove(existingValueQuery.First()); } } } ApplicationInstanceData.Data.SaveChanges(); }
private void SaveNew(DateTime now) { // GPS location Household.GpsLatitude = GPSPosition?.Latitude; Household.GpsLongitude = GPSPosition?.Longitude; Household.GpsPositionAccuracy = GPSPosition?.Accuracy; Household.GpsAltitude = GPSPosition?.Altitude; Household.GpsAltitudeAccuracy = GPSPosition?.AltitudeAccuracy; Household.GpsHeading = GPSPosition?.Heading; Household.GpsSpeed = GPSPosition?.Speed; Household.GpsPositionTime = now; ApplicationInstanceData.Data.Households.Add(Household); ApplicationInstanceData.Data.SaveChanges(); // Custom Values for (var i = 0; i < CustomFields.Count; i++) { var newCustomValue = new CustomHouseholdValue { CreatedAt = DateTime.UtcNow, LastUpdatedAt = DateTime.UtcNow, SoftDeleted = false, CustomField = CustomFields[i], Value = "", Household = Household, InternalParentId = Household.InternalId }; switch (CustomFields[i].FieldType) { case @"text": var textValue = ((CustomFieldStringValueViewModel)CustomFieldControls[i].BindingContext).EntryValue; if (textValue != null && !textValue.Equals(string.Empty)) { newCustomValue.Value = Helpers.CustomValueConverter.ConvertCustomValueToJsonText(textValue); ApplicationInstanceData.Data.CustomHouseholdValues.Add(newCustomValue); } break; case @"textarea": var textAreaValue = ((CustomFieldStringValueViewModel)CustomFieldControls[i].BindingContext).EntryValue; if (textAreaValue != null && !textAreaValue.Equals(string.Empty)) { newCustomValue.Value = Helpers.CustomValueConverter.ConvertCustomValueToJsonTextArea(textAreaValue); ApplicationInstanceData.Data.CustomHouseholdValues.Add(newCustomValue); } break; case @"check_box": var checkBoxValues = ((CustomFieldSwitchArrayView)CustomFieldControls[i]).GetSelectedValuesAsList(); if (checkBoxValues.Any()) { newCustomValue.Value = Helpers.CustomValueConverter.ConvertCustomValueToJsonCheckBox(checkBoxValues); ApplicationInstanceData.Data.CustomHouseholdValues.Add(newCustomValue); } break; case @"radio_button": var radioButtonValue = ((CustomFieldPickerViewModel)CustomFieldControls[i].BindingContext).SelectedBindableOption; if (radioButtonValue != null && !radioButtonValue.Equals(string.Empty)) { newCustomValue.Value = Helpers.CustomValueConverter.ConvertCustomValueToJsonRadioButton(radioButtonValue); ApplicationInstanceData.Data.CustomHouseholdValues.Add(newCustomValue); } break; case @"select": var selectValue = ((CustomFieldPickerViewModel)CustomFieldControls[i].BindingContext).SelectedBindableOption; if (selectValue != null && !selectValue.Equals(string.Empty)) { newCustomValue.Value = Helpers.CustomValueConverter.ConvertCustomValueToJsonSelect(selectValue); ApplicationInstanceData.Data.CustomHouseholdValues.Add(newCustomValue); } break; case @"number": var numberValue = ((CustomFieldDoubleValueViewModel)CustomFieldControls[i].BindingContext).GetDoubleValue(); if (numberValue != null) { newCustomValue.Value = Helpers.CustomValueConverter.ConvertCustomValueToJsonNumber((double)numberValue); ApplicationInstanceData.Data.CustomHouseholdValues.Add(newCustomValue); } break; case @"date": var dateValue = ((CustomFieldDateTimeValueViewModel)CustomFieldControls[i].BindingContext).DateValue; if (dateValue != null && !dateValue.ToString().Equals(string.Empty)) { newCustomValue.Value = Helpers.CustomValueConverter.ConvertCustomValueToJsonDate((DateTime)dateValue); ApplicationInstanceData.Data.CustomHouseholdValues.Add(newCustomValue); } break; case @"rank_list": var rankedValues = ((CustomFieldRankListViewModel)CustomFieldControls[i].BindingContext).GetRankedValues(); if (!rankedValues.Equals(@"")) { newCustomValue.Value = Helpers.CustomValueConverter.ConvertCustomValueToJsonRankList(((CustomFieldRankListViewModel)CustomFieldControls[i].BindingContext).Entries.ToList()); ApplicationInstanceData.Data.CustomHouseholdValues.Add(newCustomValue); } break; default: break; } } ApplicationInstanceData.Data.SaveChanges(); }