Oh God, SignalR is making my life better!

I discovered a world of possibilities. Asynchronous operations all around, clients for every Windows platform and even for websites. This is going to be really funny Open-mouthed smile

My new personal project is a system to control orders in a restaurant. The waiters make orders than consist of several products.

This is the client for the Kitchen:

  • The left part is where the orders are shown sorted by time elapsed since they were ordered.
  • The right part is lists every product.

image

This is the other client, for waiters. It has a very very basic interface with a huge button that creates a sample order (actually, a blank order).

client

Another component is the maintenance tool, that is used to enter new waiters, products, tables…

image

I’m learning a lot with this and I’m covering 3 different frameworks here!

  • Windows 8 (8.1)
  • Windows Phone 8
  • Windows Desktop (WPF)

I’m excited about sharing what I’m doing, snippets, code and my mistakes (and how you don’t have to make them!)  Winking smile

As I always say, the best way to learn is facing it and take it from the horns.

Herculean programming!!

(Great game this Mutant Fighter, from Data East!)

ASP.NET Adventure. My new project using SignalR.

image

I’m currently working on a set of applications to handle the main tasks of my family’s restaurant. This is a snapshot of the client for the kitchen. It’s on a very early state, but you can mark product orders as “Done”. It uses Entity Framework and some concepts of Domain-Driven Design. It’s also using Windows 8.1 and Windows Phone 8 for the clients (the waiters will have another kind of client to request orders to the kitchen.

SignalR is needed to notify waiters when an order is ready (or even when a product is ready). I wanted to share the progress on this! Smile For the moment, I think Windows 8 is a bit limiting in terms of layout. WPF is far more complete. But it doesn’t feel bad at all. I think the lack of Triggers for Styles will be a big headache, but some people are working on a MVVM-friendly way to face this using Visual States.

I also feel the asynchrony will be a major let down in a later stage of the solution, but with a bit of help, it can be done. I want to start it simple!

I hope you like it. The background is a bit lame, but I like blue hues! haha Open-mouthed smile

Hey, my picture appears in each order, but a picture of each waiter will appear in a few iterations, of course Smile with tongue out

JavaScript is coming

Although JavaScript = I hate, I think it’s the only way to go to get good opportunities. Desktop applications are not having a good time. The platform war is producing a spectacular rising of JavaScript. I will take the suggestion of Glenn Block seriously. I know he is right. And many others are, but I’m sometimes like a mule. I always try invest my time in things that are good, elegant and have a promising future. JavaScript is rather ugly, but it is the last trend… because web is everywhere. Web applications are accessible and (theoretically) multiplatform so,

LET’S GO WITH JAVASCRIPT!

Great for MVVM: Behavior that executes a Command on Double Click!

Did you ever wanted to accept a dialog when the user double clicks an item in a ListBox?

Now you can! This is the Interaction Behavior you have been looking for! apply it to any Selector derived class and it’s done! You only have to set the Command property of the behavior to any Command and it will execute when it user clicks an item twice. Woohoo!

I think it works like a charm.

public class SelectorDoubleClickBehavior : Behavior
    {
        private FrameworkElement element;

        protected override void OnAttached()
        {
            AssociatedObject.SelectionChanged += AssociatedObjectOnSelectionChanged;
            base.OnAttached();
        }

        private void AssociatedObjectOnSelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
        {
            DettachEventIfAny();

            var selectedItem = AssociatedObject.SelectedItem;

            if (AssociatedObject.IsItemItsOwnContainer(selectedItem))
            {
                element = (FrameworkElement) selectedItem;
            }
            else
            {
                AssociatedObject.ItemContainerGenerator.ContainerFromItem(selectedItem);
            }

            element.PreviewMouseDown += ElementOnPreviewMouseDown;
        }

        protected override void OnDetaching()
        {
            DettachEventIfAny();

            base.OnDetaching();
        }

        private void DettachEventIfAny()
        {
            if (element != null)
            {
                element.PreviewMouseDown -= ElementOnPreviewMouseDown;
            }
        }

        private void ElementOnPreviewMouseDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            if (mouseButtonEventArgs.ClickCount == 2)
            {
                if (Command != null)
                {
                    Command.Execute(null);
                }
            }
        }

        #region Command
        public static readonly DependencyProperty CommandProperty =
          DependencyProperty.Register("Command", typeof(ICommand), typeof(SelectorDoubleClickBehavior),
            new FrameworkPropertyMetadata((ICommand)null));

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        #endregion

    }

Example of usage:

 
            Item 1
            Item 2


        

This is GOOD, boy!

EDIT: Do you know the best of this? The Cinch MVVM Library by Sacha Barber already included something very similar! But it behaves a diffently: if the command you associate to the behavior can not be executed, the Selector you applied the behavior to will be disabled!

Good job,  Sacha 🙂

ComboBox to link one entity to another.

I don’t forget WPF! Here is a pretty common situation in which you may need to link (via UI) two entities.

For example, you have a list of people and a list of programming languages.

In a view need to assign to each person their favorite programming language.

  • José Manuel => C#
  • Luis Javier => Java

Something like this:

Linking

This is the code I had to write! 🙂 (The XAML is the important thing here).

<Window x:Class="Lenguage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:lenguage="clr-namespace:Lenguage"
        xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <collections:ArrayList x:Key="People">
            <lenguage:Person Name="José Manuel"></lenguage:Person>
            <lenguage:Person Name="Luis Javier"></lenguage:Person>
        </collections:ArrayList>
        <collections:ArrayList x:Key="ProgrammingLanguages">
            <lenguage:ProgrammingLanguage Name="C#"></lenguage:ProgrammingLanguage>
            <lenguage:ProgrammingLanguage Name="Java"></lenguage:ProgrammingLanguage>
        </collections:ArrayList>
    </Window.Resources>
    <Grid>
        <DataGrid ItemsSource="{StaticResource People}" AutoGenerateColumns="False" CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Person" Width="*" Binding="{Binding Name}" />
                <DataGridTemplateColumn Header="Favorite language" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate DataType="{x:Type lenguage:Person}">
                            <ComboBox DisplayMemberPath="Name"                                
                                      ItemsSource="{StaticResource ProgrammingLanguages}"
                                      SelectedItem="{Binding FavoriteLanguage, UpdateSourceTrigger=PropertyChanged}"
                                      IsSynchronizedWithCurrentItem="False"                                      
                                      />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

SignalR + Reactive Extensions?

Juan María (@gulnor) tipped me this morning that the scenario might be appropriate for Reactive Extensions. Another noticeable lack of knowledge in my experience!

How to handle such a big amount of complex things to learn? I have looked for more info but I found some great breakers out there: samples for V1, samples using Visual Basic, downloads that have a incomplete set of files (therefore, not working solutions)…

Luckily enough, I have found somebody that appears to know a lot on this topic: Jim Wooley (@jimwooley). I hope he is going to give me some help with the intricacies of this strange adventure 🙂

Stick around!