Exemplo n.º 1
0
        public RootViewModel(IEventAggregator eventAggregator, IViewModelFactory viewModelFactory,
                             SettingsService settingsService, UpdateService updateService,
                             CalculationService calculationService, LocationService locationService,
                             WinApiService winApiService)
        {
            _settingsService    = settingsService;
            _calculationService = calculationService;
            _winApiService      = winApiService;

            // Handle messages
            eventAggregator.Subscribe(this);

            // Initialize view models
            GeneralSettings  = viewModelFactory.CreateGeneralSettingsViewModel();
            LocationSettings = viewModelFactory.CreateLocationSettingsViewModel();
            AdvancedSettings = viewModelFactory.CreateAdvancedSettingsViewModel();

            // When IsEnabled switches to 'true' - cancel 'disable temporarily'
            this.Bind(o => o.IsEnabled, (sender, args) =>
            {
                if (IsEnabled)
                {
                    _enableAfterDelayTimer.Stop();
                }
            });

            // Initialize timers
            _updateTimer = new AutoResetTimer(() =>
            {
                UpdateIsPaused();
                UpdateInstant();
                UpdateGamma();
            });

            _settingsAutoSaveTimer = new AutoResetTimer(() => _settingsService.SaveIfNeeded());

            _internetSyncTimer = new AutoResetTimer(async() =>
            {
                if (!_settingsService.IsInternetSyncEnabled)
                {
                    return;
                }

                // TODO: rework later
                var location = await locationService.GetLocationAsync();

                if (_settingsService.IsInternetSyncEnabled)
                {
                    _settingsService.Location = location;

                    var date = DateTimeOffset.Now;
                    _settingsService.SunriseTime = _calculationService.CalculateSunrise(location, date).TimeOfDay;
                    _settingsService.SunsetTime  = _calculationService.CalculateSunset(location, date).TimeOfDay;
                }
            });

            _checkForUpdatesTimer = new AutoResetTimer(async() =>
            {
                IsUpdateAvailable = await updateService.CheckForUpdatesAsync();
            });

            _enableAfterDelayTimer = new ManualResetTimer(Enable);
        }