Skip to content

Commit

Permalink
More work on 10.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrynixon committed Nov 2, 2016
1 parent 7a1f7ef commit 145ae0b
Showing 92 changed files with 2,534 additions and 711 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -195,3 +195,4 @@ Templates (Project)/Blank/null
/packages/Microsoft.VSSDK.BuildTools.14.1.24720

*.zip
/packages/Newtonsoft.Json.9.0.1
34 changes: 34 additions & 0 deletions Template10.2.BCL/IInfrastructure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Template10.BCL;

namespace Template10.BCL
{
public interface IService
{
}

public interface IInfrastructure<T> where T: IService
{
T Instance { get; set; }
}
}

namespace Template10
{
public static class Extensions
{
public static T GetInfrastructure<T>(this IInfrastructure<T> accessor) where T : IService
{
return accessor.Instance;
}

public static void SetInfrastructure<T>(this IInfrastructure<T> accessor, T value) where T : IService
{
accessor.Instance = value;
}
}
}
159 changes: 159 additions & 0 deletions Template10.2.BCL/ObservableCollectionEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace Template10.BCL
{
public partial class ObservableItemCollectionEx<T>
: ObservableCollection<T>, IDisposable where T : INotifyPropertyChanged
{
private bool _allowRaiseCollectionChanged = true;

public override event NotifyCollectionChangedEventHandler CollectionChanged;

public event EventHandler<ItemPropertyChangedEventArgs> ItemPropertyChanged;

public ObservableItemCollectionEx()
{
base.CollectionChanged += (s, e) =>
{
if (_allowRaiseCollectionChanged)
CollectionChanged?.Invoke(this, e);
};
}

public ObservableItemCollectionEx(IEnumerable<T> collection) : base(collection)
{
base.CollectionChanged += (s, e) =>
{
if (_allowRaiseCollectionChanged)
{
CollectionChanged?.Invoke(this, e);
}
};
}

protected sealed override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
CheckDisposed();
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
RegisterPropertyChanged(e.NewItems);
break;
case NotifyCollectionChangedAction.Move:
break;
case NotifyCollectionChangedAction.Remove:
case NotifyCollectionChangedAction.Replace:
UnRegisterPropertyChanged(e.OldItems);
if (e.NewItems != null)
{
RegisterPropertyChanged(e.NewItems);
}

break;
case NotifyCollectionChangedAction.Reset:
break;
}
base.OnCollectionChanged(e);
}

protected override void ClearItems()
{
UnRegisterPropertyChanged(this);
base.ClearItems();
}

private void RegisterPropertyChanged(IList items)
{
CheckDisposed();
foreach (INotifyPropertyChanged item in items)
{
if (item != null)
{
item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}

private void UnRegisterPropertyChanged(IList items)
{
CheckDisposed();
foreach (INotifyPropertyChanged item in items)
{
if (item != null)
{
item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}

private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
CheckDisposed();
ItemPropertyChanged?.Invoke(this, new ItemPropertyChangedEventArgs(sender, IndexOf((T)sender), e));
}


public void AddRange(IEnumerable<T> items)
{
CheckDisposed();
_allowRaiseCollectionChanged = false;
foreach (var item in items)
{
Add(item);
}
_allowRaiseCollectionChanged = true;
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items));
}

public void RemoveRange(IEnumerable<T> items)
{
CheckDisposed();
_allowRaiseCollectionChanged = false;
foreach (var item in items)
{
Remove(item);
}
_allowRaiseCollectionChanged = true;
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items));
}

bool disposed = false;
public void Dispose()
{
if (!disposed)
{
ClearItems();
disposed = true;
}
}

public void CheckDisposed()
{
if (disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
}

public class ItemPropertyChangedEventArgs
{
public ItemPropertyChangedEventArgs(object item, int changedIndex, PropertyChangedEventArgs e)
{
ChangedItem = item;
ChangedItemIndex = changedIndex;
PropertyChangedArgs = e;
}

public object ChangedItem { get; set; }

public int ChangedItemIndex { get; set; }

public PropertyChangedEventArgs PropertyChangedArgs { get; set; }
}
}
}
143 changes: 143 additions & 0 deletions Template10.2.BCL/ObservableDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System.Collections.Generic;
using System.Linq;
using Windows.Foundation.Collections;

namespace Template10.BCL
{
public class ObservableDictionary<K, V> : IObservableMap<K, V>
{
private class ObservableDictionaryChangedEventArgs : IMapChangedEventArgs<K>
{
public ObservableDictionaryChangedEventArgs(CollectionChange change, K key)
{
CollectionChange = change;
Key = key;
}

public CollectionChange CollectionChange { get; private set; }
public K Key { get; private set; }
}

private Dictionary<K, V> _dictionary = new Dictionary<K, V>();

public event MapChangedEventHandler<K, V> MapChanged;

private void RaiseMapChanged(CollectionChange change, K key)
{
MapChanged?.Invoke(this, new ObservableDictionaryChangedEventArgs(change, key));
}

public void Add(K key, V value)
{
_dictionary.Add(key, value);
RaiseMapChanged(CollectionChange.ItemInserted, key);
}

public void Add(KeyValuePair<K, V> item) => Add(item.Key, item.Value);

public bool Remove(K key)
{
if (_dictionary.Remove(key))
{
RaiseMapChanged(CollectionChange.ItemRemoved, key);
return true;
}
return false;
}

public bool Remove(KeyValuePair<K, V> item)
{
V currentValue;
if (_dictionary.TryGetValue(item.Key, out currentValue) &&
Equals(item.Value, currentValue) && _dictionary.Remove(item.Key))
{
RaiseMapChanged(CollectionChange.ItemRemoved, item.Key);
return true;
}
return false;
}

public V this[K key]
{
get { return _dictionary[key]; }
set
{
_dictionary[key] = value;
RaiseMapChanged(CollectionChange.ItemChanged, key);
}
}

public void Clear()
{
var priorKeys = _dictionary.Keys.ToArray();
_dictionary.Clear();
foreach (var key in priorKeys)
{
RaiseMapChanged(CollectionChange.ItemRemoved, key);
}
}

public ICollection<K> Keys => _dictionary.Keys;

public bool ContainsKey(K key) => _dictionary.ContainsKey(key);

public bool TryGetValue(K key, out V value) => _dictionary.TryGetValue(key, out value);

public ICollection<V> Values => _dictionary.Values;

public bool Contains(KeyValuePair<K, V> item) => _dictionary.Contains(item);

public int Count => _dictionary.Count;

public bool IsReadOnly => false;

public IEnumerator<KeyValuePair<K, V>> GetEnumerator() => _dictionary.GetEnumerator();

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => _dictionary.GetEnumerator();

public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
{
int arraySize = array.Length;
foreach (var pair in _dictionary)
{
if (arrayIndex >= arraySize) break;
array[arrayIndex++] = pair;
}
}

public bool TrySet(K key, V value)
{
try
{
if (ContainsKey(key))
{
Remove(key);
}
Add(key, value);
return true;
}
catch
{
return false;
}
}

public bool TryGet(K key, out V value)
{
value = default(V);
if (!ContainsKey(key))
{
return false;
}
try
{
value = this[key];
return true;
}
catch
{
return false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Template10.Core")]
[assembly: AssemblyTitle("Template10.2.BCL")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Template10.Core")]
[assembly: AssemblyProduct("Template10.2.BCL")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("53856bf9-cbed-4561-9f2c-2f74dab34a1f")]

// Version information for an assembly consists of the following four values:
//
// Major Version
@@ -33,3 +26,4 @@
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ComVisible(false)]
Loading

0 comments on commit 145ae0b

Please sign in to comment.