Exemplo n.º 1
0
        public ConnectionViewModel(UserSettings userSettings, Func <string> ipAddress, IInstallationDateFetcher installationDateFetcher = null, IClock clock = null)
        {
            if (ipAddress == null)
            {
                throw new ArgumentNullException("ipAddress");
            }

            if (userSettings == null)
            {
                throw new ArgumentNullException("userSettings");
            }

            this.userSettings            = userSettings;
            this.installationDateFetcher = installationDateFetcher;
            this.clock = clock;

            this.Activator = new ViewModelActivator();

            this.WhenActivated(() =>
            {
                var disposable = new CompositeDisposable();

                this.isConnected = NetworkMessenger.Instance.WhenAnyValue(x => x.IsConnected)
                                   .ObserveOn(RxApp.MainThreadScheduler)
                                   .ToProperty(this, x => x.IsConnected)
                                   .DisposeWith(disposable);

                var canConnect      = this.WhenAnyValue(x => x.IsConnected, x => !x);
                this.ConnectCommand = ReactiveCommand.CreateAsyncObservable(canConnect, _ => ConnectAsync(ipAddress(), this.userSettings.Port)
                                                                            .Timeout(ConnectCommandTimeout, RxApp.TaskpoolScheduler)
                                                                            .Catch <ConnectionResultContainer, TimeoutException>(ex => Observable.Return(new ConnectionResultContainer(ConnectionResult.Timeout)))
                                                                            .Catch <ConnectionResultContainer, NetworkException>(ex => Observable.Return(new ConnectionResultContainer(ConnectionResult.Failed)))
                                                                            .Catch <ConnectionResultContainer, NetworkRequestException>(ex => Observable.Return(new ConnectionResultContainer(ConnectionResult.Failed)))
                                                                            .TakeUntil(disposable));

                this.DisconnectCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.IsConnected));
                this.DisconnectCommand.Subscribe(x => NetworkMessenger.Instance.Disconnect());

                // We don't simply use InvokeCommand here, it results in a weird infinite loop where
                // DisconnectCommand is executed infinitely
                this.ConnectCommand.Where(x => x.ConnectionResult != ConnectionResult.Successful)
                .SelectMany(x => this.DisconnectCommand.CanExecute(null) ? this.DisconnectCommand.ExecuteAsync() : Observable.Return((object)null))
                .Subscribe()
                .DisposeWith(disposable);

                return(disposable);
            });
        }
Exemplo n.º 2
0
 public static bool IsInTrialPeriod(TimeSpan trialTime, IClock clock = null, IInstallationDateFetcher installationDateFetcher = null)
 {
     return(GetRemainingTrialTime(trialTime, clock, installationDateFetcher) > TimeSpan.Zero);
 }
Exemplo n.º 3
0
        public static TimeSpan GetRemainingTrialTime(TimeSpan trialTime, IClock clock = null, IInstallationDateFetcher installationDateFetcher = null)
        {
            clock = clock ?? new Clock();
            installationDateFetcher = installationDateFetcher ?? Locator.Current.GetService <IInstallationDateFetcher>();

            return(installationDateFetcher.GetInstallationDate() + trialTime - clock.Now);
        }