public ProjectFilterPageViewModel(
            INavigationService navigationService,
            IProjectDataStore projectDataStore) : base(navigationService)
        {
            Title = "Filter";

            Apply = ReactiveCommand.CreateFromTask(async() =>
            {
                _filter.StartDate           = _selectedStartDate;
                _filter.StartDateComparison = _selectedStartDateComparisonType;

                _filter.Trades.Clear();
                _filter.Trades.AddRange(_selectedTrades);

                await NavigationService.GoBackAsync(new NavigationParameters
                {
                    { "filter", _filter }
                },
                                                    useModalNavigation: true).ConfigureAwait(false);
            });

            Cancel = ReactiveCommand.CreateFromTask(() => NavigationService.GoBackAsync(useModalNavigation: true));

            NavigatedTo
            .Where(args => args.ContainsKey("filter"))
            .Take(1)
            .Select(args => (ProjectFilter)args["filter"])
            .Subscribe(filter => _filter = filter);

            NavigatedTo
            .Where(args => args.ContainsKey("selected_items"))
            .Select(args => (IEnumerable <SelectionViewModel>)args["selected_items"])
            .Subscribe(selections => _selectedTrades = selections.Select(x => x.Item).Cast <Trade>());

            // begin loading the trade data
            var trades = projectDataStore.GetTradesAsync();

            SelectTrades = ReactiveCommand.CreateFromTask(async() =>
            {
                var args = new NavigationParameters();
                args.Add(
                    "items",
                    (await trades).Select(specialty => new SelectionViewModel <Trade>(specialty)
                {
                    DisplayValue = specialty.Name,
                    IsSelected   = _selectedTrades?.Contains(specialty) == true
                }));

                await NavigationService.NavigateAsync(nameof(MultiSelectListViewPage), args).ConfigureAwait(false);
            });
        }
        public EditableProfilePageViewModel(
            INavigationService navigationService,
            IUserDialogs dialogService,
            IProjectDataStore projectDataStore,
            IPermissionsService permissionsService) : base(navigationService)
        {
            Title = "Edit Profile";

            AddValidationRules();

            NavigatedTo
            .Take(1)
            .Select(args => args["user"] as Account)
            .Subscribe(user =>
            {
                // TODO: Handle null user, handle editing...
            });

            ChangePhoto = ReactiveCommand.CreateFromTask(async() =>
            {
                try
                {
                    // check if permission has been granted to the photos
                    var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Photos);
                    if (status != PermissionStatus.Granted)
                    {
                        status = await permissionsService.CheckPermissionsAsync(Permission.Photos, dialogService);
                    }

                    // if permission was granted, open the photo picker
                    if (status == PermissionStatus.Granted)
                    {
                        using (var file = await CrossMedia.Current.PickPhotoAsync(_pickOptions))
                        {
                            if (file != null)
                            {
                                Photo = file.GetStream();
                            }
                        }
                    }
                    else
                    {
                        // permission was not granted. Let the user know they can't pick a photo
                        // without permissions
                        await dialogService.AlertAsync(
                            new AlertConfig
                        {
                            Title   = "Photos Not Supported",
                            Message = ":( Permission not granted to photos.",
                            OkText  = "OK"
                        }).ConfigureAwait(false);

                        return;
                    }
                }
                catch (MediaPermissionException mediaException)
                {
                    await dialogService.AlertAsync(
                        new AlertConfig
                    {
                        Title   = "Permission Error",
                        Message = $"Permissions not granted: {string.Join(", ", mediaException.Permissions)}",
                        OkText  = "OK"
                    }).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    // TODO: log the exception...
                    await dialogService.AlertAsync(
                        new AlertConfig
                    {
                        Title   = "Error",
                        Message = "An error has occurred.",
                        OkText  = "OK"
                    }).ConfigureAwait(false);
                }
            });

            Save = ReactiveCommand.CreateFromTask(async() =>
            {
                // TODO:...
            });

            Cancel = ReactiveCommand.CreateFromTask(async() =>
            {
                await NavigationService.GoBackAsync(useModalNavigation: true).ConfigureAwait(false);
            });

            SelectCommunities = ReactiveCommand.CreateFromTask(async() =>
            {
                var args = new NavigationParameters();
                args.Add(
                    "items",
                    (await projectDataStore.GetTradesAsync()).Select(specialty => new SelectionViewModel <Trade>(specialty)
                {
                    DisplayValue = specialty.Name
                }));

                await NavigationService.NavigateAsync(nameof(MultiSelectListViewPage), args).ConfigureAwait(false);
            });
        }
예제 #3
0
        public TradeSpecialtiesPageViewModel(
            INavigationService navigationService,
            IUserDialogs dialogService,
            IContainerRegistry containerRegistry,
            IProjectDataStore projectDataStore) : base(navigationService)
        {
            Title = "Trade Specialties";

            SelectAll = ReactiveCommand.Create(() =>
            {
                foreach (var item in Items)
                {
                    item.IsSelected = true;
                }
            });

            SelectNone = ReactiveCommand.Create(() =>
            {
                foreach (var item in Items)
                {
                    item.IsSelected = false;
                }
            });

            Next = ReactiveCommand.CreateFromTask(async() =>
            {
                // if there are no items selected, they can't move on. Must select at least one specialty
                var selectedItems = Items.Where(item => item.IsSelected).Select(item => item.Item);
                if (!selectedItems.Any())
                {
                    await dialogService.AlertAsync(
                        new AlertConfig
                    {
                        Title   = "Error",
                        Message = "You must select at least one trade specialty",
                        OkText  = "OK"
                    }).ConfigureAwait(false);

                    return;
                }

                if (_account is ContractorAccount contractor)
                {
                    contractor.AccountTrades.Clear();
                    contractor.AccountTrades.AddRange(
                        selectedItems.Select(item => new AccountTrade
                    {
                        AccountId = contractor.AccountId,
                        TradeId   = item.TradeId
                    }));
                }

                // TODO: Save the account...
                containerRegistry.RegisterInstance <IUserService>(new UserService(_account));
                await NavigationService.NavigateHomeAsync().ConfigureAwait(false);
            });

            projectDataStore
            .GetTradesAsync()
            .ContinueWith(t =>
            {
                // TODO: Handle failure?
                Items.AddRange(
                    t.Result.Select(
                        specialty => new SelectionViewModel <Trade>(specialty)
                {
                    DisplayValue = specialty.Name
                }));
            });

            NavigatingTo
            .Select(args => (Account)args["account"])
            .Subscribe(user =>
            {
                _account = user;
            });
        }