Permalink
Browse files

Add a JSON view, as well as the ability to switch views

  • Loading branch information...
1 parent 4887a3d commit 590865ef9d2b6ef7314c04756fa5bdb1ddd5fbf0 @paulcbetts committed May 8, 2012
@@ -106,6 +106,9 @@
<Compile Include="Views\CacheView.xaml.cs">
<DependentUpon>CacheView.xaml</DependentUpon>
</Compile>
+ <Compile Include="Views\JsonValueView.xaml.cs">
+ <DependentUpon>JsonValueView.xaml</DependentUpon>
+ </Compile>
<Compile Include="Views\OpenCacheView.xaml.cs">
<DependentUpon>OpenCacheView.xaml</DependentUpon>
</Compile>
@@ -128,6 +131,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
+ <Page Include="Views\JsonValueView.xaml">
+ <Generator>MSBuild:Compile</Generator>
+ <SubType>Designer</SubType>
+ </Page>
<Page Include="Views\OpenCacheView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -52,7 +52,9 @@ IKernel createStandardKernel()
ret.Bind<IViewForViewModel<CacheViewModel>>().To<CacheView>();
ret.Bind<ICacheValueViewModel>().To<TextValueViewModel>().Named("Text");
+ ret.Bind<ICacheValueViewModel>().To<JsonValueViewModel>().Named("Json");
ret.Bind<IViewForViewModel<TextValueViewModel>>().To<TextValueView>();
+ ret.Bind<IViewForViewModel<JsonValueViewModel>>().To<JsonValueView>();
return ret;
}
@@ -12,6 +12,7 @@ public interface ICacheViewModel : IRoutableViewModel
ReactiveCollection<string> Keys { get; }
string SelectedKey { get; set; }
ICacheValueViewModel SelectedValue { get; }
+ string SelectedViewer { get; set; }
}
public class CacheViewModel : ReactiveObject, ICacheViewModel
@@ -29,6 +30,12 @@ public class CacheViewModel : ReactiveObject, ICacheViewModel
get { return _SelectedValue.Value; }
}
+ string _SelectedViewer;
+ public string SelectedViewer {
+ get { return _SelectedViewer; }
+ set { this.RaiseAndSetIfChanged(x => x.SelectedViewer, value); }
+ }
+
ObservableAsPropertyHelper<string> _UrlPathSegment;
public string UrlPathSegment {
get { return _UrlPathSegment.Value; }
@@ -51,17 +58,19 @@ public CacheViewModel(IScreen hostScreen, IAppState appState)
cache.GetAllKeys().ForEach(x => Keys.Add(x));
});
- this.WhenAny(x => x.SelectedKey, x => x.Value)
- .Where(x => x != null)
+ SelectedViewer = "Text";
+
+ this.WhenAny(x => x.SelectedKey, x => x.SelectedViewer, (k,v) => k.Value)
+ .Where(x => x != null && SelectedViewer != null)
.SelectMany(x => appState.CurrentCache.GetAsync(x))
- .Select(createValueViewModel)
+ .Select(x => createValueViewModel(x, SelectedViewer))
.LoggedCatch(this, Observable.Return<ICacheValueViewModel>(null))
.ToProperty(this, x => x.SelectedValue);
}
- static ICacheValueViewModel createValueViewModel(byte[] x)
+ static ICacheValueViewModel createValueViewModel(byte[] x, string viewerType)
{
- var ret = RxApp.GetService<ICacheValueViewModel>("Text");
+ var ret = RxApp.GetService<ICacheValueViewModel>(viewerType);
ret.Model = x;
return ret;
}
@@ -3,6 +3,7 @@
using System.Linq;
using System.Reactive.Linq;
using System.Text;
+using Newtonsoft.Json;
using ReactiveUI;
namespace AkavacheExplorer.ViewModels
@@ -36,4 +37,37 @@ public TextValueViewModel()
.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);
+ }
+ }
}
@@ -7,14 +7,24 @@
d:DesignHeight="300" d:DesignWidth="300">
<Border Padding="24">
<Grid DataContext="{Binding ViewModel, ElementName=userControl}">
- <Grid.ColumnDefinitions>
+ <Grid.RowDefinitions>
+ <RowDefinition Height="Auto"/>
+ <RowDefinition/>
+ </Grid.RowDefinitions>
+ <Grid.ColumnDefinitions>
<ColumnDefinition Width="0.421*"/>
<ColumnDefinition Width="0.579*"/>
</Grid.ColumnDefinitions>
- <ListBox ItemsSource="{Binding Keys}" SelectedItem="{Binding SelectedKey, Mode=TwoWay}" Margin="0,0,6,0" />
- <GridSplitter Width="5" HorizontalAlignment="Right" Background="Gray" />
- <local:ViewModelViewHost ViewModel="{Binding SelectedValue}" Grid.Column="1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />
+ <ListBox ItemsSource="{Binding Keys}" SelectedItem="{Binding SelectedKey, Mode=TwoWay}" Margin="0,0,6,0" Grid.RowSpan="2" />
+ <GridSplitter Width="5" HorizontalAlignment="Right" Background="Gray" Grid.RowSpan="2" />
+
+ <StackPanel Orientation="Horizontal" Grid.Column="1">
+ <RadioButton x:Name="textRadio" Content="Text" Margin="16,0,0,0" IsChecked="True" Tag="Text"/>
+ <RadioButton x:Name="jsonRadio" Content="JSON" Margin="16,0,0,0" Tag="Json" />
+ </StackPanel>
+
+ <local:ViewModelViewHost ViewModel="{Binding SelectedValue}" Grid.Column="1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="1" />
</Grid>
</Border>
</UserControl>
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Reactive.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
@@ -13,6 +14,7 @@
using System.Windows.Shapes;
using AkavacheExplorer.ViewModels;
using ReactiveUI.Routing;
+using ReactiveUI.Xaml;
namespace AkavacheExplorer.Views
{
@@ -24,6 +26,11 @@ public partial class CacheView : UserControl, IViewForViewModel<CacheViewModel>
public CacheView()
{
InitializeComponent();
+
+ Observable.Merge(
+ textRadio.ObservableFromDP(x => x.IsChecked).Where(x => x.Value == true).Select(x => x.Sender.Tag),
+ jsonRadio.ObservableFromDP(x => x.IsChecked).Where(x => x.Value == true).Select(x => x.Sender.Tag)
+ ).Subscribe(x => ViewModel.SelectedViewer = (string)x);
}
public CacheViewModel ViewModel {
@@ -0,0 +1,13 @@
+<UserControl x:Class="AkavacheExplorer.Views.JsonValueView"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+ mc:Ignorable="d" x:Name="userControl"
+ d:DesignHeight="300" d:DesignWidth="300">
+ <Grid DataContext="{Binding ViewModel, ElementName=userControl}">
+ <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden">
+ <TextBox IsReadOnly="True" Text="{Binding TextToDisplay, Mode=OneWay}" FontFamily="Consolas" TextWrapping="WrapWithOverflow" />
+ </ScrollViewer>
+ </Grid>
+</UserControl>
@@ -0,0 +1,41 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+using AkavacheExplorer.ViewModels;
+using ReactiveUI.Routing;
+
+namespace AkavacheExplorer.Views
+{
+ /// <summary>
+ /// Interaction logic for TextValueView.xaml
+ /// </summary>
+ public partial class TextValueView : UserControl, IViewForViewModel<TextValueViewModel>
+ {
+ public TextValueView()
+ {
+ InitializeComponent();
+ }
+
+ public TextValueViewModel ViewModel {
+ get { return (TextValueViewModel)GetValue(ViewModelProperty); }
+ set { SetValue(ViewModelProperty, value); }
+ }
+ public static readonly DependencyProperty ViewModelProperty =
+ DependencyProperty.Register("ViewModel", typeof(TextValueViewModel), typeof(TextValueView), new UIPropertyMetadata(null));
+
+ object IViewForViewModel.ViewModel {
+ get { return ViewModel; }
+ set { ViewModel = (TextValueViewModel) value; }
+ }
+ }
+}
@@ -6,6 +6,8 @@
mc:Ignorable="d" x:Name="userControl"
d:DesignHeight="300" d:DesignWidth="300">
<Grid DataContext="{Binding ViewModel, ElementName=userControl}">
- <TextBox IsReadOnly="True" Text="{Binding TextToDisplay, Mode=OneWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextWrapping="WrapWithOverflow" />
+ <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden">
+ <TextBox IsReadOnly="True" Text="{Binding TextToDisplay, Mode=OneWay}" TextWrapping="WrapWithOverflow" FontFamily="Consolas" />
+ </ScrollViewer>
</Grid>
</UserControl>
@@ -17,25 +17,25 @@
namespace AkavacheExplorer.Views
{
/// <summary>
- /// Interaction logic for TextValueView.xaml
+ /// Interaction logic for JsonValueView.xaml
/// </summary>
- public partial class TextValueView : UserControl, IViewForViewModel<TextValueViewModel>
+ public partial class JsonValueView : UserControl, IViewForViewModel<JsonValueViewModel>
{
- public TextValueView()
+ public JsonValueView()
{
InitializeComponent();
}
- public TextValueViewModel ViewModel {
- get { return (TextValueViewModel)GetValue(ViewModelProperty); }
+ public JsonValueViewModel ViewModel {
+ get { return (JsonValueViewModel)GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
public static readonly DependencyProperty ViewModelProperty =
- DependencyProperty.Register("ViewModel", typeof(TextValueViewModel), typeof(TextValueView), new UIPropertyMetadata(null));
+ DependencyProperty.Register("ViewModel", typeof(JsonValueViewModel), typeof(JsonValueView), new UIPropertyMetadata(null));
object IViewForViewModel.ViewModel {
get { return ViewModel; }
- set { ViewModel = (TextValueViewModel) value; }
+ set { ViewModel = (JsonValueViewModel) value; }
}
}
}

0 comments on commit 590865e

Please sign in to comment.