From 704a320d81c9bbdb6238fc56a2a17f4c2a819388 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Thu, 9 Feb 2017 12:29:10 +0100 Subject: [PATCH] Refactor visibility code References #33 --- .../Lawo.EmberPlusSharp.csproj | 1 + Lawo.EmberPlusSharp/Model/CollectionNode`2.cs | 14 +-------- .../Model/DynamicNodeHelper.cs | 12 +------- Lawo.EmberPlusSharp/Model/Node`1.cs | 13 +-------- Lawo.EmberPlusSharp/Model/VisibilityHelper.cs | 29 +++++++++++++++++++ 5 files changed, 33 insertions(+), 36 deletions(-) create mode 100644 Lawo.EmberPlusSharp/Model/VisibilityHelper.cs diff --git a/Lawo.EmberPlusSharp/Lawo.EmberPlusSharp.csproj b/Lawo.EmberPlusSharp/Lawo.EmberPlusSharp.csproj index 8d6fca4a..9221d4eb 100644 --- a/Lawo.EmberPlusSharp/Lawo.EmberPlusSharp.csproj +++ b/Lawo.EmberPlusSharp/Lawo.EmberPlusSharp.csproj @@ -182,6 +182,7 @@ + diff --git a/Lawo.EmberPlusSharp/Model/CollectionNode`2.cs b/Lawo.EmberPlusSharp/Model/CollectionNode`2.cs index 37f8be6b..e8bb17f0 100644 --- a/Lawo.EmberPlusSharp/Model/CollectionNode`2.cs +++ b/Lawo.EmberPlusSharp/Model/CollectionNode`2.cs @@ -60,19 +60,7 @@ internal sealed override bool ChangeVisibility(IElement child) { if (!base.ChangeVisibility(child)) { - if (child.IsOnline) - { - var typedChild = (TElement)child; - - if (!this.children.Contains(typedChild)) - { - this.children.Add(typedChild); - } - } - else - { - this.children.Remove((TElement)child); - } + VisibilityHelper.ChangeVisibility(this.children, (TElement)child); } return true; diff --git a/Lawo.EmberPlusSharp/Model/DynamicNodeHelper.cs b/Lawo.EmberPlusSharp/Model/DynamicNodeHelper.cs index 0c7df615..9426bd39 100644 --- a/Lawo.EmberPlusSharp/Model/DynamicNodeHelper.cs +++ b/Lawo.EmberPlusSharp/Model/DynamicNodeHelper.cs @@ -35,17 +35,7 @@ internal static bool ChangeVisibility( { if (!baseImpl(child)) { - if (child.IsOnline) - { - if (!dynamicChildren.Contains(child)) - { - dynamicChildren.Add(child); - } - } - else - { - dynamicChildren.Remove(child); - } + VisibilityHelper.ChangeVisibility(dynamicChildren, child); } return true; diff --git a/Lawo.EmberPlusSharp/Model/Node`1.cs b/Lawo.EmberPlusSharp/Model/Node`1.cs index a7ad9e48..e4781ee3 100644 --- a/Lawo.EmberPlusSharp/Model/Node`1.cs +++ b/Lawo.EmberPlusSharp/Model/Node`1.cs @@ -60,18 +60,7 @@ internal Node() [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "Method is not public, CA bug?")] internal override bool ChangeVisibility(IElement child) { - if (child.IsOnline) - { - if (!this.observableChildren.Contains(child)) - { - this.observableChildren.Add(child); - } - } - else - { - this.observableChildren.Remove(child); - } - + VisibilityHelper.ChangeVisibility(this.observableChildren, child); return base.ChangeVisibility(child); } diff --git a/Lawo.EmberPlusSharp/Model/VisibilityHelper.cs b/Lawo.EmberPlusSharp/Model/VisibilityHelper.cs new file mode 100644 index 00000000..61fc086c --- /dev/null +++ b/Lawo.EmberPlusSharp/Model/VisibilityHelper.cs @@ -0,0 +1,29 @@ +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Copyright 2012-2017 Lawo AG (http://www.lawo.com). +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace Lawo.EmberPlusSharp.Model +{ + using System.Collections.Generic; + + internal static class VisibilityHelper + { + internal static void ChangeVisibility(ICollection children, T child) + where T : IElement + { + if (child.IsOnline) + { + if (!children.Contains(child)) + { + children.Add(child); + } + } + else + { + children.Remove(child); + } + } + } +}