Exemplo n.º 1
0
		public async Task<Folder> AddFolderAsync()
		{
			Folder newFolder = null;

			bool isOk = await RunFunctionIfOpenAsyncTB(async delegate
			{
				newFolder = new Folder(_dbManager, RuntimeData.GetText("NewFolder"), DateTime.Now);
				// folder.ParentId = Id; // folders may not have ParentId because they can be exported or imported

				if (await _dbManager.InsertIntoFoldersAsync(newFolder))
				{
					// Add the same categories as the last folder, which was added. 
					// This is an automatism to streamline usage, it has no special reason to be.
					await newFolder.OpenAsync().ConfigureAwait(false);
					Folder lastAddedFolder = null;
					if (_folders.Any())
					{
						var maxCreateDate = _folders.Max(fol => fol.DateCreated);
						if (maxCreateDate != default(DateTime))
						{
							lastAddedFolder = _folders.FirstOrDefault(fol => fol.DateCreated == maxCreateDate);
						}
					}
					if (lastAddedFolder != null)
					{
						foreach (var cat in lastAddedFolder.DynamicCategories)
						{
							await newFolder.AddDynamicCategoryAsync(cat?.CategoryId).ConfigureAwait(false);
						}
					}

					await RunInUiThreadAsync(() => _folders.Add(newFolder)).ConfigureAwait(false);
					return true;
				}
				return false;
			});

			if (isOk) return newFolder; else return null;
		}