Skip to content

Commit

Permalink
[Settings] Added usage of settings variables in application.
Browse files Browse the repository at this point in the history
The method is as follows:
**Add property to SettingsDataContext.
**Create a visual handler for the property in SettingsView.xaml.
**Modify App.config xml.
**Add updating of ViewModel property in ToggleSettings handler.
**Modify your affected ViewModel to handle the property.
**Use the property in its specific view.

Currently updates happen on toggling off the settings window.
  • Loading branch information
Therzok committed May 14, 2013
1 parent ee2596d commit 5694175
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 71 deletions.
68 changes: 15 additions & 53 deletions SeeGitApp/MainWindow.xaml.cs
Expand Up @@ -13,70 +13,22 @@ namespace SeeGit
public partial class MainWindow : Window
{
private readonly MainWindowViewModel _viewModel;
private readonly Settings _configuration;
private Dictionary<Control, Tuple<string, dynamic>> _defaultSettings;
private static readonly Settings _configuration = new Settings();

public MainWindow()
{
InitializeComponent();
DataContext = _viewModel = new MainWindowViewModel(Dispatcher, path => new RepositoryGraphBuilder(path));

_viewModel.MonitorRepository(Directory.GetCurrentDirectory());

_defaultSettings = new Dictionary<Control, Tuple<string, dynamic>>
{
};

try
{
_configuration = new Settings();
}
catch (ConfigurationErrorsException)
{
}

LoadSettings();
}

/// <summary>
/// Updates the UI elements states to stored values.
/// </summary>
private void LoadSettings()
{
foreach (var element in _defaultSettings)
{
try
{
var val = _configuration.GetSetting(element.Value.Item1, element.Value.Item2);

if (element.Key is CheckBox) // special case for checkboxes, changing "Text" is not correct
((CheckBox)element.Key).IsChecked = Convert.ToBoolean(val);
else // if (element.Key is ComboBox)
((ComboBox)element.Key).Text = val.ToString();
}
catch
{
}
}
}

/// <summary>
/// Saves the UI elements states.
/// A reference to the configuration.
/// </summary>
private void SaveSettings()
public static Settings Configuration
{
foreach (var element in _defaultSettings)
{
string val;
if (element.Key is CheckBox)
val = (bool)((CheckBox)element.Key).IsChecked ? "True" : "False";
else // if (element.Key is ComboBox)
val = ((ComboBox)element.Key).Text;

_configuration.SetSetting(element.Value.Item1, val);
}

_configuration.Save();
get { return _configuration; }
}

private void OnChooseRepository(object sender, RoutedEventArgs args)
Expand All @@ -91,7 +43,17 @@ private void OnRefresh(object sender, ExecutedRoutedEventArgs e)

private void OnToggleSettings(object sender, RoutedEventArgs args)
{
_viewModel.ToggleSettings();
if (!_viewModel.ToggleSettings())
{
foreach (CommitVertex vertex in _viewModel.Graph.Vertices)
{
vertex.AdornerMessageVisibilityType = _configuration.GetSetting("AdornerCommitMessageVisibility", "ExpandedHidden");
vertex.DescriptionShown = _configuration.GetSetting<bool>("DescriptionInExpander", false);
vertex.ShaLength = _configuration.GetSetting<int>("SHALength", 8);
}
_configuration.Save();
_viewModel.Refresh();
}
}
}
}
67 changes: 58 additions & 9 deletions SeeGitApp/Models/CommitVertex.cs
Expand Up @@ -13,9 +13,61 @@ public CommitVertex(string sha, string message)
Message = message;
Branches = new BranchCollection();
Branches.CollectionChanged += (o, e) => RaisePropertyChanged(() => HasBranches);
ShaLength = MainWindow.Configuration.GetSetting<int>("SHALength", 8);
DescriptionShown = MainWindow.Configuration.GetSetting<bool>("DescriptionInExpander", false);
AdornerMessageVisibilityType = MainWindow.Configuration.GetSetting<string>("AdornerCommitMessageVisibility", "ExpandedHidden");
Expanded = false;
}

// Settings
int _shaLength;
public int ShaLength
{
get
{
return _shaLength;
}
set
{
_shaLength = value;
RaisePropertyChanged(() => ShortSha);
}
}

bool _descriptionShown;
public bool DescriptionShown
{
get
{
return _descriptionShown;
}
set
{
_descriptionShown = value;
RaisePropertyChanged(() => DescriptionShown);
}
}

public bool AdornerMessageVisibility
{
get;
set;
}

private string _adornerMessageVisibilityType;
public string AdornerMessageVisibilityType
{
set
{
if (value.Equals("Visible"))
AdornerMessageVisibility = true;
else if (value.Equals("Hidden"))
AdornerMessageVisibility = false;
_adornerMessageVisibilityType = value;
RaisePropertyChanged(() => AdornerMessageVisibility);
}
}

public string Sha
{
get;
Expand All @@ -26,7 +78,7 @@ public string ShortSha
{
get
{
return Sha.AtMost(8);
return Sha.AtMost(ShaLength);
}
}

Expand Down Expand Up @@ -71,18 +123,15 @@ public bool OnCurrentBranch
}
}

private bool _expanded;

public bool Expanded
{
get
{
return _expanded;
}
set
{
_expanded = value;
RaisePropertyChanged(() => Expanded);
if (_adornerMessageVisibilityType.Equals("ExpandedHidden"))
{
AdornerMessageVisibility = !value;
RaisePropertyChanged(() => AdornerMessageVisibility);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions SeeGitApp/SeeGitApp.csproj
Expand Up @@ -114,6 +114,7 @@
<Compile Include="Models\RepositoryGraphBuilder.cs" />
<Compile Include="Models\RepositoryGraphLayout.cs" />
<Compile Include="Models\Settings.cs" />
<Compile Include="Settings.cs" />
<Compile Include="ViewModels\DesignTimeMainWindowViewModel.cs" />
<Compile Include="ViewModels\MainWindowViewModel.cs" />
<Compile Include="Views\CommitVertexView.xaml.cs">
Expand Down
3 changes: 2 additions & 1 deletion SeeGitApp/ViewModels/MainWindowViewModel.cs
Expand Up @@ -94,9 +94,10 @@ public void Refresh()
Graph = _graphBuilder.Graph();
}

public void ToggleSettings()
public bool ToggleSettings()
{
_settingsVisible = !_settingsVisible;
return _settingsVisible;
}
}
}
4 changes: 2 additions & 2 deletions SeeGitApp/Views/CommitVertexView.xaml
Expand Up @@ -25,7 +25,7 @@
<local:CommitAdornerBehavior.AdornerTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Message}" Foreground="White" TextWrapping="Wrap" MaxWidth="250" Visibility="{Binding Path=Expanded, Converter={StaticResource InverseBooleanToVisibilityConverter}}" />
<TextBlock Text="{Binding Message}" Foreground="White" TextWrapping="Wrap" MaxWidth="250" Visibility="{Binding Path=AdornerMessageVisibility, Converter={StaticResource BooleanToVisibilityConverter}}" />
</StackPanel>
</DataTemplate>
</local:CommitAdornerBehavior.AdornerTemplate>
Expand Down Expand Up @@ -100,7 +100,7 @@
</Expander.Style>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Message}" Foreground="White" TextWrapping="Wrap" MaxWidth="100" />
<!-- <TextBlock Text="{Binding Description}" Foreground="White" TextWrapping="Wrap" /> -->
<TextBlock Text="{Binding Description}" Foreground="White" TextWrapping="Wrap" Visibility="{Binding DescriptionShown, Converter={StaticResource BooleanToVisibilityConverter}}" />
</StackPanel>
</Expander>
</Border>
Expand Down
9 changes: 6 additions & 3 deletions SeeGitApp/Views/SettingsView.xaml
Expand Up @@ -10,18 +10,21 @@
<StackPanel Orientation="Vertical">
<Label VerticalAlignment="Center" VerticalContentAlignment="Center" Foreground="White" FontSize="10"
FontFamily="Verdana" FontWeight="Bold" Margin="0,0,0,0" Content="Show Commit Adorner Message" />
<ComboBox HorizontalAlignment="Center" Width="196" Padding="4" Foreground="Black" Background="White" ></ComboBox>
<ComboBox Name="CommitAdornerMessage" HorizontalAlignment="Center" Width="196" Padding="4" Foreground="Black" Background="White"
ItemsSource="{Binding Path=CommitAdornerComboItems}" SelectedItem="{Binding Path=CommitAdornerSelectedItem}"/>
</StackPanel>

<StackPanel Orientation="Vertical">
<Label VerticalAlignment="Center" VerticalContentAlignment="Center" Foreground="White" FontSize="10"
FontFamily="Verdana" FontWeight="Bold" Margin="0,0,0,0" Content="Show Commit Description" />
<CheckBox HorizontalAlignment="Center" Margin="10 2 0 0" Background="White" />
<CheckBox Name="CommitDescription" HorizontalAlignment="Center" Margin="10 2 0 0" Background="White"
IsChecked="{Binding Path=CommitDescriptionShown}"/>
</StackPanel>

<StackPanel Orientation="Vertical">
<Label VerticalAlignment="Center" VerticalContentAlignment="Center" Foreground="White" FontSize="10"
FontFamily="Verdana" FontWeight="Bold" Margin="0,0,0,0" Content="SHA Length" />
<TextBox Width="60" HorizontalAlignment="Center"/>
<TextBox Name="SHALength" Width="60" HorizontalAlignment="Center" Text="{Binding Path=SHALengthInput, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</StackPanel>
</Border>
Expand Down
71 changes: 70 additions & 1 deletion SeeGitApp/Views/SettingsView.xaml.cs
@@ -1,4 +1,5 @@
using System;
using SeeGit.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -19,9 +20,77 @@ namespace SeeGit.Views
/// </summary>
public partial class SettingsView : UserControl
{

public SettingsView()
{
DataContext = new SettingsDataContext();
InitializeComponent();
}

public class SettingsDataContext
{
private static List<string> commitAdornerComboList = new List<string>();
private static Settings _config;

public SettingsDataContext()
{
_config = MainWindow.Configuration;

SetComboItems();

CommitAdornerSelectedItem = _config.GetSetting<string>("AdornerCommitMessageVisibility", "ExpandedHidden");
SHALengthInput = _config.GetSetting<string>("SHALength", "8");
CommitDescriptionShown = _config.GetSetting<bool>("DescriptionInExpander", false);
}

private void SetComboItems()
{
commitAdornerComboList.Add("ExpandedHidden");
commitAdornerComboList.Add("Visible");
commitAdornerComboList.Add("Hidden");
}

// Data bindings
public List<string> CommitAdornerComboItems
{
get { return commitAdornerComboList; }
}

public string CommitAdornerSelectedItem
{
get
{
return _config.GetSetting<string>("AdornerCommitMessageVisibility", commitAdornerComboList[0]);
}
set
{
_config.SetSetting("AdornerCommitMessageVisibility", value);
}
}

public string SHALengthInput
{
get
{
return _config.GetSetting<string>("SHALength", "8");
}
set
{
_config.SetSetting("SHALength", value);
}
}

public bool CommitDescriptionShown
{
get
{
return _config.GetSetting<bool>("DescriptionInExpander", false);
}
set
{
_config.SetSetting("DescriptionInExpander", value ? "True" : "False");
}
}
}
}
}
2 changes: 1 addition & 1 deletion UnitTests/Models/CommitVertexTests.cs
Expand Up @@ -15,7 +15,7 @@ public void SetsProperties()
Assert.Equal("shasha", commit.Sha);
Assert.Equal("commit message", commit.Message);
Assert.NotNull(commit.Branches);
Assert.Equal(false, commit.Expanded);

}
}

Expand Down
2 changes: 1 addition & 1 deletion UnitTests/Models/SettingsTests.cs
Expand Up @@ -15,7 +15,7 @@ public void SetsProperties()
var settings = new Settings();

// Get From File Test
Assert.Equal(true, Convert.ToBoolean(settings.GetSetting("TrueSetting", String.Empty)));
Assert.Equal(true, settings.GetSetting<bool>("TrueSetting", false));

// Default Setting Test
Assert.Equal("default", settings.GetSetting("asdf", "default", true));
Expand Down

0 comments on commit 5694175

Please sign in to comment.