Permalink
Browse files

adding tags

  • Loading branch information...
1 parent 2f7ef6a commit 997e831bff896aa350b3663011fa6e0a45940e2e Christian Lang committed Feb 27, 2012
View
24 src/Todo.Data/DatabaseInitializer.cs
@@ -16,14 +16,18 @@ protected override void Seed(TodoContext context)
{
base.Seed(context);
+ var cv = context.Tags.Add(new Tag { Name = "CV" });
+ var job = context.Tags.Add(new Tag { Name = "job application" });
+ var shopping = context.Tags.Add(new Tag { Name = "shopping list" });
+
int id = 0;
context.TodoItems.Add(
new TodoItem
{
Id = ++id,
Text = "write CV",
DueDate = new DateTime(2012, 2, 24),
- Tags = new[] { new Tag { Text = "CV" }, new Tag { Text = "job application" } },
+ Tags = new[] { cv, job },
Done = true
});
context.TodoItems.Add(
@@ -32,7 +36,7 @@ protected override void Seed(TodoContext context)
Id = ++id,
Text = "take picture (photographer)",
DueDate = new DateTime(2012, 2, 23),
- Tags = new[] { new Tag { Text = "CV" }, new Tag { Text = "job application" } },
+ Tags = new[] { cv, job },
Done = true
});
context.TodoItems.Add(
@@ -41,7 +45,7 @@ protected override void Seed(TodoContext context)
Id = ++id,
Text = "edit picture",
DueDate = new DateTime(2012, 2, 23),
- Tags = new[] { new Tag { Text = "CV" }, new Tag { Text = "job application" } },
+ Tags = new[] { cv, job },
Done = true
});
context.TodoItems.Add(
@@ -50,7 +54,7 @@ protected override void Seed(TodoContext context)
Id = ++id,
Text = "scan diploma",
DueDate = new DateTime(2012, 2, 25),
- Tags = new[] { new Tag { Text = "job application" } },
+ Tags = new[] { job },
Done = true
});
context.TodoItems.Add(
@@ -59,7 +63,7 @@ protected override void Seed(TodoContext context)
Id = ++id,
Text = "download MCPD transcript",
DueDate = new DateTime(2012, 2, 25),
- Tags = new[] { new Tag { Text = "job application" } },
+ Tags = new[] { job },
Done = true
});
context.TodoItems.Add(
@@ -68,31 +72,31 @@ protected override void Seed(TodoContext context)
Id = ++id,
Text = "send application",
DueDate = new DateTime(2012, 2, 26),
- Tags = new[] { new Tag { Text = "job application" } },
+ Tags = new[] { job },
Done = true
});
context.TodoItems.Add(
new TodoItem
{
Id = ++id,
Text = "wait to be interviewed",
- Tags = new[] { new Tag { Text = "job application" } }
+ Tags = new[] { job }
});
context.TodoItems.Add(
new TodoItem
{
Id = ++id,
Text = "quit my current job",
- DueDate = new DateTime(2012, 3, 31),
- Tags = new[] { new Tag { Text = "job application" } }
+ DueDate = new DateTime(2012, 3, 31),
+ Tags = new[] { job }
});
context.TodoItems.Add(
new TodoItem
{
Id = ++id,
Text = "milk",
DueDate = DateTime.Today.AddDays(1),
- Tags = new[] { new Tag { Text = "shopping list" } }
+ Tags = new[] { shopping }
});
context.TodoItems.Add(
new TodoItem
View
2 src/Todo.Entities/Tag.cs
@@ -8,7 +8,7 @@ public class Tag
public int Id { get; set; }
[Required]
- public string Text { get; set; }
+ public string Name { get; set; }
public virtual ICollection<TodoItem> TodoItems { get; set; }
}
View
34 src/Todo/AppBootstrapper.cs
@@ -18,6 +18,10 @@ protected override void Configure()
{
this.container = new UnityContainer()
.LoadConfiguration();
+
+ ILog logger = new ConsoleLogger();
+ ILog errorLogger = new ErrorLogger();
+ LogManager.GetLog = type => type == typeof(ActionMessage) ? logger : errorLogger;
}
protected override object GetInstance(Type serviceType, string key)
@@ -34,5 +38,35 @@ protected override void BuildUp(object instance)
{
this.container.BuildUp(instance);
}
+
+ private class ConsoleLogger : ILog
+ {
+ public virtual void Info(string format, params object[] args)
+ {
+ Console.WriteLine("Info: " + format, args);
+ }
+
+ public virtual void Warn(string format, params object[] args)
+ {
+ Console.WriteLine("Warning: " + format, args);
+ }
+
+ public virtual void Error(Exception exception)
+ {
+ Console.WriteLine(string.Format("Error: {0}", exception.ToString()));
+ }
+ }
+
+ private class ErrorLogger : ConsoleLogger
+ {
+ public override void Info(string format, params object[] args) { }
+ }
+
+ private class NullLogger : ILog
+ {
+ public void Info(string format, params object[] args) { }
+ public void Warn(string format, params object[] args) { }
+ public void Error(Exception exception) { }
+ }
}
}
View
10 src/Todo/Todo.csproj
@@ -75,10 +75,12 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="ViewModels\FilterViewModel.cs" />
+ <Compile Include="ViewModels\NewTagSearchResultViewModel.cs" />
<Compile Include="ViewModels\NewTodoItemViewModel.cs" />
<Compile Include="ViewModels\ShellViewModel.cs" />
<Compile Include="AppBootstrapper.cs" />
<Compile Include="IShell.cs" />
+ <Compile Include="ViewModels\TagSearchResultViewModel.cs" />
<Compile Include="ViewModels\TagSearchViewModel.cs" />
<Compile Include="ViewModels\TagViewModel.cs" />
<Compile Include="ViewModels\TodoItemViewModel.cs" />
@@ -87,6 +89,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
+ <Page Include="Views\NewTagSearchResultView.xaml">
+ <SubType>Designer</SubType>
+ <Generator>MSBuild:Compile</Generator>
+ </Page>
<Page Include="Views\NewTodoItemView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -95,6 +101,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
+ <Page Include="Views\TagSearchResultView.xaml">
+ <SubType>Designer</SubType>
+ <Generator>MSBuild:Compile</Generator>
+ </Page>
<Page Include="Views\TagSearchView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
View
18 src/Todo/ViewModels/NewTagSearchResultViewModel.cs
@@ -0,0 +1,18 @@
+namespace Todo.ViewModels
+{
+ public class NewTagSearchResultViewModel : TagSearchResultViewModel
+ {
+ public NewTagSearchResultViewModel(string tagName)
+ : base(null)
+ {
+ this.TagName = tagName;
+ }
+
+ public string TagName { get; private set; }
+
+ public override string Text
+ {
+ get { return string.Format("Create \"{0}\"", this.TagName); }
+ }
+ }
+}
View
19 src/Todo/ViewModels/TagSearchResultViewModel.cs
@@ -0,0 +1,19 @@
+namespace Todo.ViewModels
+{
+ using Todo.Entities;
+
+ public class TagSearchResultViewModel
+ {
+ public TagSearchResultViewModel(Tag tag)
+ {
+ this.Tag = tag;
+ }
+
+ public Tag Tag { get; private set; }
+
+ public virtual string Text
+ {
+ get { return this.Tag.Name; }
+ }
+ }
+}
View
47 src/Todo/ViewModels/TagSearchViewModel.cs
@@ -1,14 +1,14 @@
namespace Todo.ViewModels
{
+ using System;
using System.Linq;
- using System.Windows.Controls.Primitives;
+ using System.Windows;
using Caliburn.Micro;
using Todo.Business.Contract;
- using Todo.Views;
-
- public class TagSearchViewModel
+
+ public class TagSearchViewModel : ViewAware
{
private readonly ITagsRepository tagsRepository;
@@ -17,9 +17,11 @@ public class TagSearchViewModel
public TagSearchViewModel()
{
this.tagsRepository = IoC.Get<ITagsRepository>();
- this.TagSearchResult = new BindableCollection<string>();
+ this.TagSearchResults = new BindableCollection<object>();
}
+ public event EventHandler<AddingTagEventArgs> OnAddingTag;
+
public string SearchText
{
get
@@ -30,15 +32,36 @@ public string SearchText
set
{
this.searchText = value;
- this.TagSearchResult.Clear();
- this.TagSearchResult.AddRange(
- from t in this.tagsRepository.FindAll()
- where t.Text.StartsWith(searchText)
- orderby t.Text
- select t.Text);
+ this.TagSearchResults.Clear();
+
+ var tags = from t in this.tagsRepository.FindAll()
+ where t.Name.StartsWith(searchText)
+ orderby t.Name
+ select t;
+
+ if (!tags.Any(t => t.Name == searchText))
+ {
+ this.TagSearchResults.Add(new NewTagSearchResultViewModel(searchText));
+ }
+
+ this.TagSearchResults.AddRange(tags.ToList().Select(t => new TagSearchResultViewModel(t)));
}
}
- public IObservableCollection<string> TagSearchResult { get; set; }
+ public IObservableCollection<object> TagSearchResults { get; set; }
+
+ public void AddTag(TagSearchResultViewModel model)
+ {
+ var view = this.GetView() as Window;
+ if (view != null) view.Close();
+
+ if (this.OnAddingTag != null)
+ this.OnAddingTag(model, new AddingTagEventArgs { SearchResult = model });
+ }
+
+ public class AddingTagEventArgs : EventArgs
+ {
+ public TagSearchResultViewModel SearchResult { get; set; }
+ }
}
}
View
4 src/Todo/ViewModels/TagViewModel.cs
@@ -21,12 +21,12 @@ public string Text
{
get
{
- return this.Tag.Text;
+ return this.Tag.Name;
}
set
{
- this.Tag.Text = value;
+ this.Tag.Name = value;
NotifyOfPropertyChange(() => Text);
}
}
View
25 src/Todo/ViewModels/TodoItemViewModel.cs
@@ -6,7 +6,6 @@
using Caliburn.Micro;
- using Todo.Business.Contract;
using Todo.Entities;
public class TodoItemViewModel : PropertyChangedBase
@@ -57,13 +56,29 @@ public IObservableCollection<TagViewModel> Tags
get { return this.tags; }
}
- public void AddTag()
+ public void ShowTagSearch()
{
- dynamic settings = new ExpandoObject();
- settings.StaysOpen = false;
+ var tagSearch = new TagSearchViewModel();
+ tagSearch.OnAddingTag += this.OnOnAddingTag;
var wm = new WindowManager();
- wm.ShowWindow(new TagSearchViewModel(), null, settings);
+ wm.ShowWindow(tagSearch);
+ }
+
+ private void OnOnAddingTag(object sender, TagSearchViewModel.AddingTagEventArgs e)
+ {
+ Tag tag;
+ if (e.SearchResult is NewTagSearchResultViewModel)
+ {
+ tag = new Tag { Name = ((NewTagSearchResultViewModel)e.SearchResult).TagName };
+ }
+ else
+ {
+ tag = e.SearchResult.Tag;
+ }
+
+ this.item.Tags.Add(tag);
+ this.tags.Add(new TagViewModel(tag));
}
private void OnRemoveTag(object sender, EventArgs eventArgs)
View
23 src/Todo/Views/NewTagSearchResultView.xaml
@@ -0,0 +1,23 @@
+<UserControl x:Class="Todo.Views.NewTagSearchResultView"
+ 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"
+ xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
+ xmlns:cal="http://www.caliburnproject.org" mc:Ignorable="d"
+ d:DesignHeight="20" d:DesignWidth="100">
+ <Button>
+ <Button.Template>
+ <ControlTemplate>
+ <TextBlock Text="{Binding Path=Text}" />
+ </ControlTemplate>
+ </Button.Template>
+ <i:Interaction.Triggers>
+ <i:EventTrigger EventName="Click">
+ <cal:ActionMessage MethodName="AddTag">
+ <cal:Parameter Value="$dataContext" />
+ </cal:ActionMessage>
+ </i:EventTrigger>
+ </i:Interaction.Triggers>
+ </Button>
+</UserControl>
View
23 src/Todo/Views/TagSearchResultView.xaml
@@ -0,0 +1,23 @@
+<UserControl x:Class="Todo.Views.TagSearchResultView"
+ 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"
+ xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
+ xmlns:cal="http://www.caliburnproject.org" mc:Ignorable="d"
+ d:DesignHeight="20" d:DesignWidth="100">
+ <Button>
+ <Button.Template>
+ <ControlTemplate>
+ <TextBlock Text="{Binding Path=Text}" />
+ </ControlTemplate>
+ </Button.Template>
+ <i:Interaction.Triggers>
+ <i:EventTrigger EventName="Click">
+ <cal:ActionMessage MethodName="AddTag">
+ <cal:Parameter Value="$dataContext" />
+ </cal:ActionMessage>
+ </i:EventTrigger>
+ </i:Interaction.Triggers>
+ </Button>
+</UserControl>
View
14 src/Todo/Views/TagSearchView.xaml
@@ -3,17 +3,9 @@
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"
- xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
- xmlns:cal="http://www.caliburnproject.org" mc:Ignorable="d" d:DesignHeight="80" d:DesignWidth="150">
- <!--<i:Interaction.Triggers>
- <i:EventTrigger EventName="LostFocus">
- <cal:ActionMessage MethodName="Close">
- <cal:Parameter Value="$view" />
- </cal:ActionMessage>
- </i:EventTrigger>
- </i:Interaction.Triggers>-->
+ mc:Ignorable="d" d:DesignHeight="80" d:DesignWidth="150">
<StackPanel MinWidth="150">
- <TextBox Name="SearchText" TabIndex="0" />
- <ListBox Name="TagSearchResult" />
+ <TextBox Name="SearchText" />
+ <ItemsControl Name="TagSearchResults" />
</StackPanel>
</UserControl>
View
2 src/Todo/Views/TodoItemView.xaml
@@ -35,6 +35,7 @@
</i:Interaction.Triggers>
</Button>
<DatePicker Name="DueDate" FontSize="12" VerticalAlignment="Center" DockPanel.Dock="Right" Width="130" Margin="5,0,0,0" />
+ <Button Name="ShowTagSearch" DockPanel.Dock="Right">+</Button>
<ItemsControl Name="Tags" VerticalAlignment="Center" DockPanel.Dock="Right">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
@@ -48,7 +49,6 @@
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
- <Button Name="AddTag" DockPanel.Dock="Right">+</Button>
<TextBox Name="Text" FontSize="12" VerticalAlignment="Center" />
</DockPanel>
</Border>

0 comments on commit 997e831

Please sign in to comment.