private void InitDependencies() { Application.Current.MainWindow.Activated += WeakEventHandler.Create(this, (@this, o, args) => Task.Run(@this.UpdateAppsAndUrls)); this.WhenAnyValue(vm => vm.OtpSecret).Where(x => !string.IsNullOrWhiteSpace(x)) .Subscribe(o => ErrorOtpSecret = (ValidateBase32String(OtpSecret) ? null : TranslationSource.Instance["PasswordManager.Error.OtpSecretInvalid"])); this.WhenAnyValue(vm => vm.ErrorAccountName, vm => vm.ErrorAccountLogin, vm => vm.ErrorOtpSecret) .Subscribe(observer => HasError = !(ErrorAccountName == null && ErrorAccountLogin == null && ErrorOtpSecret == null)); this.WhenAnyValue(vm => vm.Name, vm => vm.Login, vm => vm.IsPasswordChanged, vm => vm.OtpSecret, vm => vm.HasOpt, vm => vm.Apps, vm => vm.Urls) .Subscribe(o => VerifyHasChanged()); // Some issues arise when using WhenAnyValue is 8 or more arguments this.WhenAnyValue(vm => vm.EditOtp).Subscribe(o => VerifyHasChanged()); this.WhenAnyValue(vm => vm.SelectedApp).Subscribe(OnAppSelected); this.WhenAnyValue(vm => vm.SelectedUrl).Subscribe(OnUrlSelected); Observable.FromEventPattern <NotifyCollectionChangedEventArgs>(AppsAndUrls, nameof(ObservableCollection <string> .CollectionChanged)) .Subscribe(change => AppsOrUrlsCollectonChanges()); OpenedApps.Add(loadingAppInfo); OpenedForegroundUrls.Add(addUrlAppInfo); OpenedForegroundUrls.Add(loadingAppInfo); Task.Run(UpdateAppsAndUrls).ContinueWith(_ => { Application.Current.Dispatcher.Invoke(() => { OpenedApps.Remove(loadingAppInfo); OpenedForegroundUrls.Remove(loadingAppInfo); }); }); }
private void UpdateAppsAndUrls() { if (isUpdateAppsUrls) { return; } try { isUpdateAppsUrls = true; var allApps = AppInfoFactory.GetVisibleAppsInfo(); var appInfoComparer = new AppInfoComparer(); var apps = allApps .Where(a => string.IsNullOrEmpty(a.Domain)) .Except(OpenedApps, appInfoComparer) .ToArray(); var urls = AddMainDomain(allApps.Where(a => !string.IsNullOrEmpty(a.Domain))) .Distinct(appInfoComparer) .Except(OpenedForegroundUrls, appInfoComparer) .ToArray(); Application.Current.Dispatcher.Invoke(() => { OpenedApps.Add(apps); OpenedForegroundUrls.Add(urls); }); } finally { isUpdateAppsUrls = false; } }