From 551b77f5a1af46cb002cd8127f7ec552d83b40be Mon Sep 17 00:00:00 2001 From: Andreas Billmann Date: Fri, 11 Aug 2023 13:09:27 +0200 Subject: [PATCH] fix reactive resets --- .../ViewModels/MainWindowViewModel.cs | 73 ++++++++++++------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/K8sFileBrowser/ViewModels/MainWindowViewModel.cs b/K8sFileBrowser/ViewModels/MainWindowViewModel.cs index 8ac5747..c17951a 100644 --- a/K8sFileBrowser/ViewModels/MainWindowViewModel.cs +++ b/K8sFileBrowser/ViewModels/MainWindowViewModel.cs @@ -14,20 +14,20 @@ namespace K8sFileBrowser.ViewModels; public class MainWindowViewModel : ViewModelBase { - private ObservableAsPropertyHelper> _clusterContexts = null!; - public IEnumerable ClusterContexts => _clusterContexts.Value; + [Reactive] + public IEnumerable ClusterContexts { get; set; } = null!; [Reactive] public ClusterContext? SelectedClusterContext { get; set; } [Reactive] - public IEnumerable Namespaces { get; set; } + public IEnumerable Namespaces { get; set; } = null!; [Reactive] public Namespace? SelectedNamespace { get; set; } - private ObservableAsPropertyHelper> _pods = null!; - public IEnumerable Pods => _pods.Value; + [Reactive] + public IEnumerable Pods { get; set; } = null!; [Reactive] public Pod? SelectedPod { get; set; } @@ -38,8 +38,8 @@ public class MainWindowViewModel : ViewModelBase [Reactive] public Container? SelectedContainer { get; set; } - private ObservableAsPropertyHelper> _fileInformation = null!; - public IEnumerable FileInformation => _fileInformation.Value; + [Reactive] + public IEnumerable FileInformation { get; set; } = null!; [Reactive] public FileInformation? SelectedFile { get; set; } @@ -48,7 +48,7 @@ public class MainWindowViewModel : ViewModelBase public string? SelectedPath { get; set; } [Reactive] - public Message Message { get; set; } + public Message Message { get; set; } = null!; public ReactiveCommand DownloadCommand { get; private set; } = null!; public ReactiveCommand DownloadLogCommand { get; private set; } = null!; @@ -86,18 +86,22 @@ public class MainWindowViewModel : ViewModelBase // load the cluster contexts when the view model is created var loadContexts = ReactiveCommand .Create>(_ => kubernetesService.GetClusterContexts()); - _clusterContexts = loadContexts.Execute().ToProperty( - this, x => x.ClusterContexts, scheduler: RxApp.MainThreadScheduler); - - // select the current cluster context - SelectedClusterContext = ClusterContexts - .FirstOrDefault(x => x.Name == kubernetesService.GetCurrentContext()); + loadContexts.Execute() + .Throttle(new TimeSpan(10)) + .ObserveOn(RxApp.MainThreadScheduler) + .Subscribe(x => + { + ClusterContexts = x; + // select the current cluster context + SelectedClusterContext = ClusterContexts + .FirstOrDefault(x => x.Name == kubernetesService.GetCurrentContext()); + }); } private void RegisterResetPath() { // reset the path when the pod or namespace changes - this.WhenAnyValue(c => c.SelectedPod, c => c.SelectedNamespace) + this.WhenAnyValue(c => c.SelectedContainer) .Throttle(new TimeSpan(10)) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(_ => SelectedPath = "/"); @@ -107,13 +111,17 @@ public class MainWindowViewModel : ViewModelBase { // read the file information when the path changes this - .WhenAnyValue(c => c.SelectedPod, c => c.SelectedNamespace) + .WhenAnyValue(c => c.SelectedPod) .Throttle(new TimeSpan(10)) - .Select(x => x.Item2 == null || x.Item1 == null + .Select(x => x == null ? new List() - : x.Item1.Containers.Select(c => new Container {Name = c})) + : x.Containers.Select(c => new Container {Name = c})) .ObserveOn(RxApp.MainThreadScheduler) - .Subscribe( x => Containers = x); + .Subscribe( x => + { + Containers = x; + FileInformation = new List(); + }); this.WhenAnyValue(x => x.Containers) .Throttle(new TimeSpan(10)) @@ -124,25 +132,30 @@ public class MainWindowViewModel : ViewModelBase private void RegisterReadFiles(IKubernetesService kubernetesService) { // read the file information when the path changes - _fileInformation = this - .WhenAnyValue(c => c.SelectedPath, c => c.SelectedPod, c => c.SelectedNamespace, c => c.SelectedContainer) + this + .WhenAnyValue(c => c.SelectedContainer) .Throttle(new TimeSpan(10)) - .Select(x => x.Item3 == null || x.Item2 == null || x.Item1 == null || x.Item4 == null + .Select(x => x == null ? new List() - : GetFileInformation(kubernetesService, x.Item1, x.Item2, x.Item3, x.Item4)) + : GetFileInformation(kubernetesService, SelectedPath!, SelectedPod!, SelectedNamespace!, x)) .ObserveOn(RxApp.MainThreadScheduler) - .ToProperty(this, x => x.FileInformation); + .Subscribe(x => FileInformation = x); } private void RegisterReadPods() { // read the pods when the namespace changes - _pods = this + this .WhenAnyValue(c => c.SelectedNamespace) .Throttle(new TimeSpan(10)) .SelectMany(ns => GetPodsForNamespace.Execute(ns!)) .ObserveOn(RxApp.MainThreadScheduler) - .ToProperty(this, x => x.Pods); + .Subscribe(x => + { + Pods = x; + Containers = null; + FileInformation = new List(); + }); } private void RegisterReadNamespaces(IKubernetesService kubernetesService) @@ -153,7 +166,13 @@ public class MainWindowViewModel : ViewModelBase .Throttle(new TimeSpan(10)) .SelectMany(context => GetClusterContextAsync(context, kubernetesService)) .ObserveOn(RxApp.MainThreadScheduler) - .Subscribe(ns => Namespaces = ns); + .Subscribe(ns => + { + Namespaces = ns; + Pods = new List(); + Containers = null; + FileInformation = new List(); + }); } private void ConfigureGetPodsForNamespaceCommand(IKubernetesService kubernetesService)