/// <summary>
        ///     Creates a new empty <see cref="BudgetCollection" /> but does not save it.
        /// </summary>
        public BudgetCollection CreateNew()
        {
            var budget = new BudgetModel();

            this.currentBudgetCollection = new BudgetCollection(budget);
            this.budgetBucketRepository.Initialise(new List <BudgetBucketDto>());
            return(this.currentBudgetCollection);
        }
        public async Task <BudgetCollection> LoadAsync(string storageKey, bool isEncrypted)
        {
            if (storageKey.IsNothing())
            {
                throw new ArgumentNullException(nameof(storageKey));
            }

            this.isEncryptedAtLastAccess = isEncrypted;
            var reader = this.readerWriterSelector.SelectReaderWriter(isEncrypted);

            if (!reader.FileExists(storageKey))
            {
                throw new KeyNotFoundException("File not found. " + storageKey);
            }

            object serialised;

            try
            {
                var xaml = await reader.LoadFromDiskAsync(storageKey); // May return null for some errors.

                serialised = Deserialise(xaml);
            }
            catch (XamlObjectWriterException ex)
            {
                throw new DataFormatException($"The budget file '{storageKey}' is an invalid format. This is probably due to changes in the code, most likely namespace changes.", ex);
            }
            catch (EncryptionKeyIncorrectException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DataFormatException("Deserialisation the Budget file failed, an exception was thrown by the Xaml deserialiser, the file format is invalid.", ex);
            }

            var correctDataFormat = serialised as BudgetCollectionDto;

            if (correctDataFormat == null)
            {
                throw new DataFormatException(
                          string.Format(CultureInfo.InvariantCulture,
                                        "The file used to store application state ({0}) is not in the correct format. It may have been tampered with.",
                                        storageKey));
            }

            // Bucket Repository must be initialised first, the budget model incomes/expenses are dependent on the bucket repository.
            this.budgetBucketRepository.Initialise(correctDataFormat.Buckets);

            var budgetCollection = this.mapper.ToModel(correctDataFormat);

            budgetCollection.StorageKey  = storageKey;
            this.currentBudgetCollection = budgetCollection;
            return(budgetCollection);
        }
示例#3
0
        /// <summary>
        ///     Creates a new instance of the <see cref="BudgetCurrencyContext" /> class.
        /// </summary>
        /// <param name="budgets">The collection of available budgets loaded.</param>
        /// <param name="budget">
        ///     The currently selected budget. This isn't necessarily the current one compared with today's date.
        ///     Can be any in the <paramref name="budgets" /> collection.
        /// </param>
        public BudgetCurrencyContext([NotNull] BudgetCollection budgets, [NotNull] BudgetModel budget)
        {
            if (budgets == null)
            {
                throw new ArgumentNullException(nameof(budgets));
            }

            if (budget == null)
            {
                throw new ArgumentNullException(nameof(budget));
            }

            BudgetCollection = budgets;
            Model            = budget;

            if (budgets.IndexOf(budget) < 0)
            {
                throw new KeyNotFoundException("The given budget is not found in the given collection.");
            }
        }
        public async Task <BudgetCollection> CreateNewAndSaveAsync(string storageKey)
        {
            if (storageKey.IsNothing())
            {
                throw new ArgumentNullException(nameof(storageKey));
            }

            var newBudget = new BudgetModel
            {
                EffectiveFrom = DateTime.Today,
                Name          = Path.GetFileNameWithoutExtension(storageKey).Replace('.', ' ')
            };

            this.currentBudgetCollection = new BudgetCollection(newBudget)
            {
                StorageKey = storageKey
            };

            this.budgetBucketRepository.Initialise(new List <BudgetBucketDto>());

            await SaveAsync(storageKey, false);

            return(this.currentBudgetCollection);
        }