public static void ExecuteIfCan(this IReactiveCommand @this, object o)
 {
     if (@this.CanExecute(o))
     {
         @this.Execute(o);
     }
 }
Example #2
0
 /// <summary>
 /// A utility method that will pipe an Observable to an ICommand (i.e.
 /// it will first call its CanExecute with the provided value, then if
 /// the command can be executed, Execute() will be called)
 /// </summary>
 /// <param name="command">The command to be executed.</param>
 /// <returns>An object that when disposes, disconnects the Observable
 /// from the command.</returns>
 public static IDisposable InvokeCommand <T>(this IObservable <T> This, IReactiveCommand command)
 {
     return(This.Throttle(x => command.CanExecuteObservable.StartWith(command.CanExecute(x)).Where(b => b))
            .Subscribe(x => {
         command.Execute(x);
     }));
 }
Example #3
0
        public RemoteSongsActivity()
        {
            this.WhenActivated(() =>
            {
                var disposable = new CompositeDisposable();

                var adapter = new ReactiveListAdapter <RemoteSongViewModel>(new ReactiveList <RemoteSongViewModel>(this.ViewModel.Songs),
                                                                            (vm, parent) => new RemoteSongView(this, vm, parent));
                this.SongsList.Adapter = adapter;

                this.SongsList.Events().ItemClick.Select(x => x.Position)
                .Subscribe(x =>
                {
                    this.ViewModel.SelectedSong = this.ViewModel.Songs[x];

                    var items = new List <Tuple <string, IReactiveCommand> >();

                    if (this.ViewModel.IsAdmin)
                    {
                        items.Add(Tuple.Create(Resources.GetString(Resource.String.play), (IReactiveCommand)this.ViewModel.PlaySongsCommand));
                        items.Add(Tuple.Create(Resources.GetString(Resource.String.add_to_playlist), (IReactiveCommand)this.ViewModel.AddToPlaylistCommand));
                    }

                    else if (this.ViewModel.RemainingVotes > 0)
                    {
                        string voteString = string.Format(Resources.GetString(Resource.String.uses_vote), this.ViewModel.RemainingVotes);
                        items.Add(Tuple.Create(string.Format("{0} \n({1})", Resources.GetString(Resource.String.add_to_playlist), voteString),
                                               (IReactiveCommand)this.ViewModel.AddToPlaylistCommand));
                    }

                    else
                    {
                        items.Add(Tuple.Create(Resources.GetString(Resource.String.no_votes_left), (IReactiveCommand)null));
                    }

                    var builder = new AlertDialog.Builder(this);
                    builder.SetItems(items.Select(y => y.Item1).ToArray(), (o, eventArgs) =>
                    {
                        IReactiveCommand command = items[eventArgs.Which].Item2;

                        if (command != null)
                        {
                            command.Execute(null);
                        }
                    });
                    builder.Create().Show();
                })
                .DisposeWith(disposable);

                Observable.Merge(this.ViewModel.PlaySongsCommand.Select(_ => Resource.String.playing_songs),
                                 this.ViewModel.AddToPlaylistCommand.Select(_ => Resource.String.added_to_playlist),
                                 this.ViewModel.PlaySongsCommand.ThrownExceptions.Merge(this.ViewModel.AddToPlaylistCommand.ThrownExceptions).Select(_ => Resource.String.error_adding_song))
                .ObserveOn(RxApp.MainThreadScheduler)
                .Subscribe(x => Toast.MakeText(this, x, ToastLength.Short).Show())
                .DisposeWith(disposable);

                return(disposable);
            });
        }
Example #4
0
        private void SongDoubleClick(object sender, MouseButtonEventArgs e)
        {
            IReactiveCommand command = this.ViewModel.DefaultPlaybackCommand;

            if (e.LeftButton == MouseButtonState.Pressed && command.CanExecute(null))
            {
                command.Execute(null);
            }
        }
Example #5
0
 public static IDisposable Bind <T>(
     this IObservable <T> source,
     IReactiveCommand <T> target,
     int frameThrottle = 1)
 {
     return(source.
            BatchPlayerTiming(frameThrottle, PlayerLoopTiming.LastPostLateUpdate).
            Where(x => target.CanExecute.Value).
            Subscribe(x => target.Execute(x)));
 }
Example #6
0
        private void SongKeyPressed(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                IReactiveCommand command = this.ViewModel.DefaultPlaybackCommand;

                if (command.CanExecute(null))
                {
                    command.Execute(null);
                }

                e.Handled = true;
            }
        }
Example #7
0
 public IDisposable HandleErrors(IReactiveCommand command)
 {
     return command.ThrownExceptions.SelectMany(ex => UserError.Throw(ex.Message, ex.InnerException))
         .Subscribe(result =>
             {
                 switch (result)
                 {
                     case RecoveryOptionResult.RetryOperation:
                         command.Execute(null);
                         break;
                     case RecoveryOptionResult.FailOperation:
                         Application.Current.Shutdown();
                         break;
                 }
             });
 }
Example #8
0
        public IDisposable HandleErrors(IReactiveCommand command)
        {
            return(command.ThrownExceptions.SelectMany(ex => UserError.Throw(ex.Message, ex.InnerException))
                   .Subscribe(result =>
            {
                switch (result)
                {
                case RecoveryOptionResult.RetryOperation:
                    command.Execute(null);
                    break;

                case RecoveryOptionResult.FailOperation:
                    Application.Current.Shutdown();
                    break;
                }
            }));
        }
Example #9
0
        public static void DisplayAddToPlaylistDialog <T, TSong>(this IViewFor <T> activity, Context context, int songPosition) where T : SongsViewModelBase <TSong>
        {
            activity.ViewModel.SelectedSong = activity.ViewModel.Songs[songPosition];

            var items = new List <Tuple <string, IReactiveCommand> >();

            if (activity.ViewModel.IsAdmin)
            {
                items.Add(Tuple.Create(context.Resources.GetString(Resource.String.add_to_playlist), (IReactiveCommand)activity.ViewModel.AddToPlaylistCommand));
            }

            else if (activity.ViewModel.RemainingVotes > 0)
            {
                string voteString = string.Format(context.Resources.GetString(Resource.String.uses_vote), activity.ViewModel.RemainingVotes);
                items.Add(Tuple.Create(string.Format("{0} \n({1})", context.Resources.GetString(Resource.String.add_to_playlist), voteString),
                                       (IReactiveCommand)activity.ViewModel.AddToPlaylistCommand));
            }

            else
            {
                items.Add(Tuple.Create(context.Resources.GetString(Resource.String.no_votes_left), (IReactiveCommand)null));
            }

            var builder = new AlertDialog.Builder(context);

            builder.SetItems(items.Select(y => y.Item1).ToArray(), (o, eventArgs) =>
            {
                IReactiveCommand command = items[eventArgs.Which].Item2;

                if (command != null)
                {
                    command.Execute(null);
                }
            });
            builder.Create().Show();
        }