File size: 3,745 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Windows.Input;

namespace VersOne.Epub.WpfDemo.Utils
{
    /// <summary>
    /// A custom command which uses an action and an optional predicate supplied through one of its constructors.
    /// The predicate is used to determine if the command can be executed and the action is called with the command is executed.
    /// </summary>
    public class Command : ICommand
    {
        private readonly Predicate<object> canExecute = null;
        private readonly Action<object> executeAction = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="Command" /> class with a specified execute action.
        /// </summary>
        /// <param name="executeAction">An action that needs to be executed when the <see cref="Execute" /> method is called.</param>
        public Command(Action executeAction)
            : this(param => true, param => executeAction())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Command" /> class with a specified execute action.
        /// </summary>
        /// <param name="executeAction">An action that needs to be executed when the <see cref="Execute" /> method is called.</param>
        public Command(Action<object> executeAction)
            : this(param => true, executeAction)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Command" /> class with a specified predicate that determines whether the action can be executed or not
        /// and a specified execute action.
        /// </summary>
        /// <param name="canExecute">A predicate that is executed when the <see cref="CanExecute" /> method is called.</param>
        /// <param name="executeAction">An action that needs to be executed when the <see cref="Execute" /> method is called.</param>
        public Command(Predicate<object> canExecute, Action<object> executeAction)
        {
            this.canExecute = canExecute;
            this.executeAction = executeAction;
        }

        /// <summary>
        /// Occurs after the execution of the <see cref="Execute" /> method, so the consumer of this command can check whether the command can still be executed or not.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// Determines whether the command can execute in its current state.
        /// </summary>
        /// <param name="parameter">
        /// Data used by the predicate supplied through the constructor of this command.
        /// If the predicate does not require data to be passed, this object can be set to <c>null</c>.
        /// </param>
        /// <returns><c>true</c> if this command can be executed; otherwise, <c>false</c>.</returns>
        public bool CanExecute(object parameter)
        {
            if (canExecute != null)
            {
                return canExecute(parameter);
            }
            return true;
        }

        /// <summary>
        /// Method called when the command is invoked.
        /// </summary>
        /// <param name="parameter">
        /// Data used by the action supplied through the constructor of this command.
        /// If the action does not require data to be passed, this object can be set to <c>null</c>.
        /// </param>
        public void Execute(object parameter)
        {
            executeAction?.Invoke(parameter);
            UpdateCanExecuteState();
        }

        /// <summary>
        /// Triggers the <see cref="CanExecuteChanged" /> event.
        /// </summary>
        public void UpdateCanExecuteState()
        {
            CanExecuteChanged?.Invoke(this, new EventArgs());
        }
    }
}