Skip to content

Commit 551b77f

Browse files
committed
fix reactive resets
1 parent 7932583 commit 551b77f

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

K8sFileBrowser/ViewModels/MainWindowViewModel.cs

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ namespace K8sFileBrowser.ViewModels;
1414

1515
public class MainWindowViewModel : ViewModelBase
1616
{
17-
private ObservableAsPropertyHelper<IEnumerable<ClusterContext>> _clusterContexts = null!;
18-
public IEnumerable<ClusterContext> ClusterContexts => _clusterContexts.Value;
17+
[Reactive]
18+
public IEnumerable<ClusterContext> ClusterContexts { get; set; } = null!;
1919

2020
[Reactive]
2121
public ClusterContext? SelectedClusterContext { get; set; }
2222

2323
[Reactive]
24-
public IEnumerable<Namespace> Namespaces { get; set; }
24+
public IEnumerable<Namespace> Namespaces { get; set; } = null!;
2525

2626
[Reactive]
2727
public Namespace? SelectedNamespace { get; set; }
2828

29-
private ObservableAsPropertyHelper<IEnumerable<Pod>> _pods = null!;
30-
public IEnumerable<Pod> Pods => _pods.Value;
29+
[Reactive]
30+
public IEnumerable<Pod> Pods { get; set; } = null!;
3131

3232
[Reactive]
3333
public Pod? SelectedPod { get; set; }
@@ -38,8 +38,8 @@ public class MainWindowViewModel : ViewModelBase
3838
[Reactive]
3939
public Container? SelectedContainer { get; set; }
4040

41-
private ObservableAsPropertyHelper<IEnumerable<FileInformation>> _fileInformation = null!;
42-
public IEnumerable<FileInformation> FileInformation => _fileInformation.Value;
41+
[Reactive]
42+
public IEnumerable<FileInformation> FileInformation { get; set; } = null!;
4343

4444
[Reactive]
4545
public FileInformation? SelectedFile { get; set; }
@@ -48,7 +48,7 @@ public class MainWindowViewModel : ViewModelBase
4848
public string? SelectedPath { get; set; }
4949

5050
[Reactive]
51-
public Message Message { get; set; }
51+
public Message Message { get; set; } = null!;
5252

5353
public ReactiveCommand<Unit, Unit> DownloadCommand { get; private set; } = null!;
5454
public ReactiveCommand<Unit, Unit> DownloadLogCommand { get; private set; } = null!;
@@ -86,18 +86,22 @@ private void InitiallyLoadContexts(IKubernetesService kubernetesService)
8686
// load the cluster contexts when the view model is created
8787
var loadContexts = ReactiveCommand
8888
.Create<Unit, IEnumerable<ClusterContext>>(_ => kubernetesService.GetClusterContexts());
89-
_clusterContexts = loadContexts.Execute().ToProperty(
90-
this, x => x.ClusterContexts, scheduler: RxApp.MainThreadScheduler);
91-
92-
// select the current cluster context
93-
SelectedClusterContext = ClusterContexts
94-
.FirstOrDefault(x => x.Name == kubernetesService.GetCurrentContext());
89+
loadContexts.Execute()
90+
.Throttle(new TimeSpan(10))
91+
.ObserveOn(RxApp.MainThreadScheduler)
92+
.Subscribe(x =>
93+
{
94+
ClusterContexts = x;
95+
// select the current cluster context
96+
SelectedClusterContext = ClusterContexts
97+
.FirstOrDefault(x => x.Name == kubernetesService.GetCurrentContext());
98+
});
9599
}
96100

97101
private void RegisterResetPath()
98102
{
99103
// reset the path when the pod or namespace changes
100-
this.WhenAnyValue(c => c.SelectedPod, c => c.SelectedNamespace)
104+
this.WhenAnyValue(c => c.SelectedContainer)
101105
.Throttle(new TimeSpan(10))
102106
.ObserveOn(RxApp.TaskpoolScheduler)
103107
.Subscribe(_ => SelectedPath = "/");
@@ -107,13 +111,17 @@ private void RegisterReadContainers()
107111
{
108112
// read the file information when the path changes
109113
this
110-
.WhenAnyValue(c => c.SelectedPod, c => c.SelectedNamespace)
114+
.WhenAnyValue(c => c.SelectedPod)
111115
.Throttle(new TimeSpan(10))
112-
.Select(x => x.Item2 == null || x.Item1 == null
116+
.Select(x => x == null
113117
? new List<Container>()
114-
: x.Item1.Containers.Select(c => new Container {Name = c}))
118+
: x.Containers.Select(c => new Container {Name = c}))
115119
.ObserveOn(RxApp.MainThreadScheduler)
116-
.Subscribe( x => Containers = x);
120+
.Subscribe( x =>
121+
{
122+
Containers = x;
123+
FileInformation = new List<FileInformation>();
124+
});
117125

118126
this.WhenAnyValue(x => x.Containers)
119127
.Throttle(new TimeSpan(10))
@@ -124,25 +132,30 @@ private void RegisterReadContainers()
124132
private void RegisterReadFiles(IKubernetesService kubernetesService)
125133
{
126134
// read the file information when the path changes
127-
_fileInformation = this
128-
.WhenAnyValue(c => c.SelectedPath, c => c.SelectedPod, c => c.SelectedNamespace, c => c.SelectedContainer)
135+
this
136+
.WhenAnyValue(c => c.SelectedContainer)
129137
.Throttle(new TimeSpan(10))
130-
.Select(x => x.Item3 == null || x.Item2 == null || x.Item1 == null || x.Item4 == null
138+
.Select(x => x == null
131139
? new List<FileInformation>()
132-
: GetFileInformation(kubernetesService, x.Item1, x.Item2, x.Item3, x.Item4))
140+
: GetFileInformation(kubernetesService, SelectedPath!, SelectedPod!, SelectedNamespace!, x))
133141
.ObserveOn(RxApp.MainThreadScheduler)
134-
.ToProperty(this, x => x.FileInformation);
142+
.Subscribe(x => FileInformation = x);
135143
}
136144

137145
private void RegisterReadPods()
138146
{
139147
// read the pods when the namespace changes
140-
_pods = this
148+
this
141149
.WhenAnyValue(c => c.SelectedNamespace)
142150
.Throttle(new TimeSpan(10))
143151
.SelectMany(ns => GetPodsForNamespace.Execute(ns!))
144152
.ObserveOn(RxApp.MainThreadScheduler)
145-
.ToProperty(this, x => x.Pods);
153+
.Subscribe(x =>
154+
{
155+
Pods = x;
156+
Containers = null;
157+
FileInformation = new List<FileInformation>();
158+
});
146159
}
147160

148161
private void RegisterReadNamespaces(IKubernetesService kubernetesService)
@@ -153,7 +166,13 @@ private void RegisterReadNamespaces(IKubernetesService kubernetesService)
153166
.Throttle(new TimeSpan(10))
154167
.SelectMany(context => GetClusterContextAsync(context, kubernetesService))
155168
.ObserveOn(RxApp.MainThreadScheduler)
156-
.Subscribe(ns => Namespaces = ns);
169+
.Subscribe(ns =>
170+
{
171+
Namespaces = ns;
172+
Pods = new List<Pod>();
173+
Containers = null;
174+
FileInformation = new List<FileInformation>();
175+
});
157176
}
158177

159178
private void ConfigureGetPodsForNamespaceCommand(IKubernetesService kubernetesService)

0 commit comments

Comments
 (0)