Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using NavigationMVVM.Models;
- using NavigationMVVM.Services;
- using NavigationMVVM.Stores;
- using NavigationMVVM.ViewModels;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows;
- namespace NavigationMVVM.Commands
- {
- public class LoginCommand : CommandBase
- {
- private readonly LoginViewModel _viewModel;
- private readonly AccountStore _accountStore;
- private readonly INavigationService _navigationService;
- public LoginCommand(LoginViewModel viewModel, AccountStore accountStore, INavigationService navigationService)
- {
- _viewModel = viewModel;
- _accountStore = accountStore;
- _navigationService = navigationService;
- }
- public override void Execute(object parameter)
- {
- // Account account = new Account()
- // {
- // Email = $"{_viewModel.Username}@test.com",
- // Username = _viewModel.Username
- // };
- //
- //
- // _accountStore.CurrentAccount = account;
- //
- // _navigationService.Navigate();
- CreateFluentExecute(() =>
- {
- Account account = new Account()
- {
- Email = $"{_viewModel.Username}@test.com",
- Username = _viewModel.Username
- };
- _accountStore.CurrentAccount = account;
- _navigationService.Navigate();
- }).LogOnException().WhenParameterHasValue(parameter);
- // var execute = new LogOnException(() => { })
- // .LogOnException()
- // .WhenParameterHasValue(parameter);
- }
- private static IGuard CreateFluentExecute(Action action) => new AlwaysExecute(action);
- }
- public static class CommandExtensions
- {
- public static IGuard WhenParameterHasValue(this IGuard guard, object parameter)
- {
- return new ParamNotNull(parameter, guard);
- }
- public static IGuard LogOnException(this IGuard guard) => LogOnException(guard);
- }
- public class AlwaysExecute : IGuard
- {
- private readonly Action _action;
- public AlwaysExecute(Action action)
- {
- _action = action;
- }
- public void Execute() => _action();
- }
- public class ParamNotNull : IGuard
- {
- private readonly object _parameter;
- private readonly IGuard _other;
- public ParamNotNull(object parameter, IGuard other)
- {
- _parameter = parameter;
- _other = other;
- }
- public void Execute()
- {
- if (_parameter is not null)
- {
- _other.Execute();
- }
- }
- }
- public class LogOnException : IGuard
- {
- private readonly Action _action;
- public LogOnException(Action action)
- {
- _action = action;
- }
- public void Execute()
- {
- try
- {
- _action.Invoke();
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- }
- public void Execute(object parameter)
- {
- }
- }
- public interface IGuard
- {
- void Execute();
- }
- }
- Author: https://github.com/ivandrofly
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement