Esempio n. 1
0
		public Task<bool> TrySetFieldValueAsync(DynamicField dynFld, string newValue)
		{
			return RunFunctionIfOpenAsyncTB(() => dynFld?.TrySetFieldValueAsync(newValue));
		}
Esempio n. 2
0
		/// <summary>
		/// Removes those fields that have been removed from the categories since the last save,
		/// and adds those fields that have been added to the categories since the last save.
		/// To be sure, we assume the metadata has been changed since the folder was last saved.
		/// </summary>
		/// <returns></returns>
		private static async Task RefreshDynamicFieldsAsync(DBManager dbM, ICollection<DynamicCategory> dynamicCategories, ICollection<DynamicField> dynamicFields, string folderId)
		{
			if (dbM == null) return;

			try
			{
				var shouldBeFldDscs = new List<FieldDescription>(); // HashSet may be tricky, a List is easier
				foreach (var dynCat in dynamicCategories)
				{
					if (dynCat.Category != null)
					{
						foreach (var fldDsc in dynCat.Category.FieldDescriptions)
						{
							if (shouldBeFldDscs.All(fd => fd.Id != fldDsc.Id)) shouldBeFldDscs.Add(fldDsc);
						}
					}
					else
					{
						await Logger.AddAsync("DynamicCategory " + dynCat.Id + " has Category = null: this must never happen", Logger.ForegroundLogFilename).ConfigureAwait(false);
						Debugger.Break(); // this must never happen
					}
				}
				// remove obsolete fields
				// LOLLO I create a List, instead of using the IEnumerable, otherwise the following Remove will break the ienumerable and dump!
				var obsoleteDynFlds = dynamicFields.Where(dynFld => shouldBeFldDscs.All(fldDsc => fldDsc.Id != dynFld.FieldDescriptionId)).ToList();
				foreach (var obsoleteDynFld in obsoleteDynFlds)
				{
					if (await dbM.DeleteFromDynamicFieldsAsync(obsoleteDynFld).ConfigureAwait(false) && obsoleteDynFld != null)
					{
						dynamicFields.Remove(obsoleteDynFld);
						await obsoleteDynFld.CloseAsync().ConfigureAwait(false);
					}
				}

				var newFldDscs = shouldBeFldDscs.Where(fldDsc => dynamicFields.All(dynFld => dynFld.FieldDescriptionId != fldDsc.Id));
				foreach (var newFldDsc in newFldDscs)
				{
					var dynFld = new DynamicField(dbM, folderId, newFldDsc.Id);
					if (await dbM.InsertIntoDynamicFieldsAsync(dynFld).ConfigureAwait(false) && dynFld != null)
					{
						dynamicFields.Add(dynFld);
						await dynFld.OpenAsync().ConfigureAwait(false);
					}
				}
			}
			catch (Exception ex)
			{
				await Logger.AddAsync(ex.ToString(), Logger.ForegroundLogFilename).ConfigureAwait(false);
				Debugger.Break();
			}
		}