Ejemplo n.º 1
0
        /// <summary>
        /// Initialize instance.
        /// </summary>
        /// <param name="navigationService"></param>
        /// <param name="editExpense"></param>
        public AddExpensePageViewModel(INavigationService navigationService, IEditExpense editExpense)
        {
            _navigationService = navigationService;
            _editExpense       = editExpense;
            _editExpense.AddTo(Disposable);

            Amount   = _editExpense.ToReactivePropertyAsSynchronized(x => x.Amount).AddTo(Disposable);
            Date     = _editExpense.ToReactivePropertyAsSynchronized(x => x.Date).AddTo(Disposable);
            Location = _editExpense.ToReactivePropertyAsSynchronized(x => x.Location).AddTo(Disposable);
            Note     = _editExpense.ToReactivePropertyAsSynchronized(x => x.Note).AddTo(Disposable);

            // Convert, because picker supports only string.
            Categories = _editExpense.ObserveProperty(x => x.Categories)
                         .Where(x => x != null)
                         .Select(x => x.OrderBy(category => category.SortOrder).Select(category => category.Name))
                         .ToReadOnlyReactiveProperty().AddTo(Disposable);
            // Convert, because picker supports only string.
            SelectedCategoryIndex = _editExpense.ObserveProperty(x => x.SelectedCategory)
                                    .Select(x =>
            {
                if (x == null)
                {
                    return(-1);
                }
                else
                {
                    // Elements obtained from Azure's IEnumerable return different instances each time.
                    // For this reason we compare by ID.
                    foreach (var item in _editExpense.Categories.Select((value, index) => new { value, index }))
                    {
                        if (item.value.Id == x.Id)
                        {
                            return(item.index);
                        }
                    }
                    return(-1);
                }
            })
                                    .ToReactiveProperty().AddTo(Disposable);
            // When you select into the Category name.
            SelectedCategoryIndex.Subscribe(x =>
            {
                if (0 <= x)
                {
                    _editExpense.SelectedCategory = _editExpense.Categories.ToList()[x];
                }
            });

            SaveAsyncCommand =
                Location.Select(x => !string.IsNullOrWhiteSpace(x))
                .ToAsyncReactiveCommand().AddTo(Disposable);
            SaveAsyncCommand.Subscribe(SaveAsync);

            NavigateReceiptPageCommand = new ReactiveCommand();
            NavigateReceiptPageCommand.Subscribe(_ => _navigationService.NavigateAsync("ReceiptPage"));
        }
Ejemplo n.º 2
0
 public ExpenseController(
     IAddExpense addExpense,
     IGetExpensesByPage getExpensesByPage,
     IGetExpenseById getExpenseById,
     IEditExpense editExpense
     )
 {
     this.addExpense        = addExpense;
     this.getExpensesByPage = getExpensesByPage;
     this.getExpenseById    = getExpenseById;
     this.editExpense       = editExpense;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Initialize Instance.
        /// </summary>
        /// <param name="editExpense"></param>
        public ReceiptPageViewModel(IEditExpense editExpense)
        {
            _editExpense = editExpense;

            Receipt = _editExpense.ObserveProperty(x => x.Receipt)
                      .Where(x => x != null)
                      .Select(x => ImageSource.FromStream(x.GetStream))
                      .ToReadOnlyReactiveProperty().AddTo(Disposable);

            PickPhotoAsyncCommand = _editExpense.ObserveProperty(m => m.IsPickPhotoSupported).ToAsyncReactiveCommand().AddTo(Disposable);
            PickPhotoAsyncCommand.Subscribe(async _ => await _editExpense.PickPhotoAsync());

            TakePhotoAsyncCommand = _editExpense.ObserveProperty(m => m.IsTakePhotoSupported).ToAsyncReactiveCommand().AddTo(Disposable);
            TakePhotoAsyncCommand.Subscribe(async _ => await _editExpense.TakePhotoAsync());
        }