/
ValueDisplayViewModels.cs
116 lines (103 loc) · 3.64 KB
/
ValueDisplayViewModels.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Akavache;
using Newtonsoft.Json;
using ReactiveUI;
namespace AkavacheExplorer.ViewModels
{
public interface ICacheValueViewModel : IReactiveNotifyPropertyChanged
{
byte[] Model { get; set; }
}
public class TextValueViewModel : ReactiveObject, ICacheValueViewModel
{
byte[] _Model;
public byte[] Model {
get { return _Model; }
set { this.RaiseAndSetIfChanged(x => x.Model, value); }
}
ObservableAsPropertyHelper<string> _TextToDisplay;
public string TextToDisplay {
get { return _TextToDisplay.Value; }
}
public TextValueViewModel()
{
this.WhenAny(x => x.Model, x => x.Value)
.Where(x => x != null)
.Select(x => {
var ret = Encoding.UTF8.GetString(x);
return ret;
})
.ToProperty(this, x => x.TextToDisplay);
}
}
public class JsonValueViewModel : ReactiveObject, ICacheValueViewModel
{
byte[] _Model;
public byte[] Model {
get { return _Model; }
set { this.RaiseAndSetIfChanged(x => x.Model, value); }
}
ObservableAsPropertyHelper<string> _TextToDisplay;
public string TextToDisplay {
get { return _TextToDisplay.Value; }
}
public JsonValueViewModel()
{
this.WhenAny(x => x.Model, x => x.Value)
.Where(x => x != null)
.Select(x => {
var ret = Encoding.UTF8.GetString(x);
return ret;
})
.Select<string, string>(x => {
try {
dynamic ret = JsonConvert.DeserializeObject(x);
return JsonConvert.SerializeObject(ret, Formatting.Indented);
} catch (Exception ex) {
return ex.ToString();
}
})
.ToProperty(this, x => x.TextToDisplay);
}
}
public class ImageValueViewModel : ReactiveObject, ICacheValueViewModel
{
byte[] _Model;
public byte[] Model
{
get { return _Model; }
set { this.RaiseAndSetIfChanged(x => x.Model, value); }
}
ObservableAsPropertyHelper<BitmapImage> _Image;
public BitmapImage Image {
get { return _Image.Value; }
}
ObservableAsPropertyHelper<Visibility> _ImageVisibility;
public Visibility ImageVisibility {
get { return _ImageVisibility.Value; }
}
ObservableAsPropertyHelper<Visibility> _ErrorVisibility;
public Visibility ErrorVisibility {
get { return _ErrorVisibility.Value; }
}
public ImageValueViewModel()
{
this.WhenAny(x => x.Model, x => x.Value)
.Where(x => x != null)
.SelectMany(BitmapImageMixin.BytesToImage)
.LoggedCatch(this, Observable.Return<BitmapImage>(null))
.ToProperty(this, x => x.Image);
this.WhenAny(x => x.Image, x => x.Value != null ? Visibility.Visible : Visibility.Hidden)
.ToProperty(this, x => x.ImageVisibility);
this.WhenAny(x => x.ImageVisibility, x => x.Value == Visibility.Visible ? Visibility.Hidden : Visibility.Visible)
.ToProperty(this, x => x.ErrorVisibility);
}
}
}