public SettingsViewModel( ITranslationsService translationsService, IFilePickerService filePickerService, IPackagingService packagingService, ISettingsService settingsService, IPlatformService platformService, IDialogService dialogService, IOpmlService opmlService) { Theme = string.Empty; Version = packagingService.Version; (LoadImages, NeedBanners) = (true, true); (FontSize, NotifyPeriod, MaxArticlesPerFeed) = (0, 0, 0); LeaveFeedback = new ObservableCommand(packagingService.LeaveFeedback); LeaveReview = new ObservableCommand(packagingService.LeaveReview); ImportOpml = new ObservableCommand(async() => { var stream = await filePickerService.PickFileForReadAsync(); await opmlService.ImportOpmlFeedsAsync(stream); }); ExportOpml = new ObservableCommand(async() => { var stream = await filePickerService.PickFileForWriteAsync(); await opmlService.ExportOpmlFeedsAsync(stream); }); Reset = new ObservableCommand(async() => { var response = await dialogService.ShowDialogForConfirmation( translationsService.Resolve("ResetAppNoRestore"), translationsService.Resolve("Notification")); if (response) { await platformService.ResetApp(); } }); Load = new ObservableCommand(async() => { await Task.WhenAll( StartTracking(NotifyPeriod, "NotifyPeriod", platformService.RegisterBackgroundTask), StartTracking(MaxArticlesPerFeed, "MaxArticlesPerFeed", o => Task.CompletedTask), StartTracking(NeedBanners, "NeedBanners", o => Task.CompletedTask), StartTracking(LoadImages, "LoadImages", o => Task.CompletedTask), StartTracking(FontSize, "FontSize", o => Task.CompletedTask), StartTracking(Theme, "Theme", platformService.RegisterTheme) ); }); async Task StartTracking <T>(ObservableProperty <T> property, string key, Func <T, Task> callback) where T : IConvertible { property.Value = await settingsService.GetAsync <T>(key); property.PropertyChanged += async(o, args) => { var value = property.Value; await callback.Invoke(value); await settingsService.SetAsync(key, value); }; } }
public SettingViewModel( IFilePickerService filePickerService, IPackagingService packagingService, IPlatformService platformService, ISettingManager settingManager, IOpmlService opmlService) { _filePickerService = filePickerService; _packagingService = packagingService; _platformService = platformService; _settingManager = settingManager; _opmlService = opmlService; Version = _packagingService.Version; Feedback = ReactiveCommand.CreateFromTask(packagingService.LeaveFeedback); Review = ReactiveCommand.CreateFromTask(packagingService.LeaveReview); ImportSuccess = new Interaction <Unit, bool>(); ExportSuccess = new Interaction <Unit, bool>(); Import = ReactiveCommand.CreateFromTask(DoImport); Export = ReactiveCommand.CreateFromTask(DoExport); ResetConfirm = new Interaction <Unit, bool>(); Reset = ReactiveCommand.CreateFromTask(DoReset); _settingManager.Read().ToObservable() .Do(x => Banners = x.Banners) .Do(x => Images = x.Images) .Do(x => Period = x.Period) .Do(x => Theme = x.Theme) .Do(x => Read = x.Read) .Do(x => Font = x.Font) .Do(x => Max = x.Max) .Subscribe(); this.WhenAnyValue(x => x.Period).Skip(1) .SelectMany(platformService.RegisterBackgroundTask) .Subscribe(); this.WhenAnyValue(x => x.Theme).Skip(1) .SelectMany(platformService.RegisterTheme) .Subscribe(); this.WhenAnyValue( x => x.Period, x => x.Theme, x => x.Banners, x => x.Images, x => x.Read, x => x.Font, x => x.Max) .Skip(1).Select(x => new Settings { Period = x.Item1, Theme = x.Item2, Banners = x.Item3, Images = x.Item4, Read = x.Item5, Font = x.Item6, Max = x.Item7 }) .SelectMany(settingManager.Write) .Subscribe(); }
public SettingViewModel( IFilePickerService filePickerService, IPackagingService packagingService, IPlatformService platformService, ISettingManager settingManager, IOpmlService opmlService) { Version = packagingService.Version; LeaveFeedback = ReactiveCommand.CreateFromTask(packagingService.LeaveFeedback); Review = ReactiveCommand.CreateFromTask(packagingService.LeaveReview); ImportSuccess = new Interaction <Unit, bool>(); Import = ReactiveCommand.CreateFromTask(async() => { var stream = await filePickerService.PickFileForReadAsync(); var success = await opmlService.ImportOpmlFeedsAsync(stream); if (success) { await ImportSuccess.Handle(Unit.Default); } }); ExportSuccess = new Interaction <Unit, bool>(); Export = ReactiveCommand.CreateFromTask(async() => { var stream = await filePickerService.PickFileForWriteAsync(); var success = await opmlService.ExportOpmlFeedsAsync(stream); if (success) { await ExportSuccess.Handle(Unit.Default); } }); ResetConfirm = new Interaction <Unit, bool>(); Reset = ReactiveCommand.CreateFromTask(async() => { var response = await ResetConfirm.Handle(Unit.Default); if (response) { await platformService.ResetApp(); } }); Load = ReactiveCommand.CreateFromTask(async() => { var settings = await settingManager.Read(); Track(x => x.Period, (s, x) => s.Period = x, settings.Period, platformService.RegisterBackgroundTask); Track(x => x.Theme, (s, x) => s.Theme = x, settings.Theme, platformService.RegisterTheme); Track(x => x.Banners, (s, x) => s.Banners = x, settings.Banners); Track(x => x.Images, (s, x) => s.Images = x, settings.Images); Track(x => x.Read, (s, x) => s.Read = x, settings.Read); Track(x => x.Font, (s, x) => s.Font = x, settings.Font); Track(x => x.Max, (s, x) => s.Max = x, settings.Max); void Track <T>( Expression <Func <SettingViewModel, T> > bind, Action <Settings, T> assign, T initial, Func <T, Task> callback = null) { var memberExpression = (MemberExpression)bind.Body; var property = (PropertyInfo)memberExpression.Member; property.SetValue(this, initial); this.WhenAnyValue(bind) .Do(x => assign.Invoke(settings, x)) .Do(async x => await settingManager.Write(settings)) .Where(x => callback != null) .Subscribe(async x => await callback(x)); } }); }