📄 contactgroupcollection.cs
字号:
namespace Imps.Client.Core
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
public class ContactGroupCollection : ICollection<ContactGroupBase>, IEnumerable<ContactGroupBase>, IEnumerable
{
private List<ContactGroupBase> _col;
private IComparer<ContactGroupBase> _comparer;
private ContactList _contactList;
private bool _sorted;
private object _syncObj;
internal ContactGroupCollection() : this(null)
{
}
internal ContactGroupCollection(ContactList contactList)
{
this._syncObj = new object();
this._col = new List<ContactGroupBase>();
this._contactList = contactList;
}
public void Add(ContactGroupBase item)
{
lock (this.SyncRoot)
{
this._col.Add(item);
this.InnerDoSort();
if (this._contactList != null)
{
this._contactList.InnerOnContactGroupChanged(new ContactGroupChangedEventArgs(item, ContactGroupEventType.Added));
}
}
}
public void Clear()
{
lock (this.SyncRoot)
{
if (this._col.get_Count() != 0)
{
if (this._contactList != null)
{
List<ContactGroupBase>.Enumerator enumerator = this._col.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
ContactGroupBase group = enumerator.get_Current();
this._contactList.InnerOnContactGroupChanged(new ContactGroupChangedEventArgs(group, ContactGroupEventType.Deleted));
}
}
finally
{
enumerator.Dispose();
}
}
this._col.Clear();
}
}
}
public bool Contains(ContactGroupBase item)
{
return this._col.Contains(item);
}
public ContactGroupBase FindContactGroup(int? groupId)
{
if (groupId.get_HasValue())
{
List<ContactGroupBase>.Enumerator enumerator = this._col.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
ContactGroupBase base2 = enumerator.get_Current();
int? nullable = groupId;
if ((base2.Id == nullable.GetValueOrDefault()) && nullable.get_HasValue())
{
return base2;
}
}
}
finally
{
enumerator.Dispose();
}
}
return null;
}
internal void ForceSort()
{
this.InnerDoSort();
}
public ContactGroupBase GetByIndex(int index)
{
return this._col.get_Item(index);
}
public IEnumerator<ContactGroupBase> GetEnumerator()
{
return this._col.GetEnumerator();
}
private void InnerDoSort()
{
if (this._sorted && (this._comparer != null))
{
this._col.Sort(this._comparer);
}
}
public bool Remove(ContactGroupBase item)
{
lock (this.SyncRoot)
{
if (this.Contains(item))
{
if (this._contactList != null)
{
if (this._col.Remove(item))
{
ContactGroupDeletedEventArgs args = new ContactGroupDeletedEventArgs();
args.ContactGroups.Add(item);
this._contactList.InnerOnContactGroupChanged(new ContactGroupChangedEventArgs(item, ContactGroupEventType.Deleted));
return true;
}
}
else
{
return this._col.Remove(item);
}
}
return false;
}
}
public bool Remove(int groupId)
{
lock (this.SyncRoot)
{
ContactGroupBase item = this[new int?(groupId)];
if (item != null)
{
return this.Remove(item);
}
return false;
}
}
void ICollection<ContactGroupBase>.CopyTo(ContactGroupBase[] array, int arrayIndex)
{
throw new NotSupportedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public int Count
{
get
{
return this._col.get_Count();
}
}
public ContactGroupBase this[int? groupId]
{
get
{
if (!groupId.get_HasValue())
{
return null;
}
return this.FindContactGroup(groupId);
}
}
public IComparer<ContactGroupBase> SortComparer
{
get
{
return this._comparer;
}
set
{
this._comparer = value;
this.InnerDoSort();
}
}
public bool Sorted
{
get
{
return this._sorted;
}
set
{
if (this._sorted != value)
{
this._sorted = value;
if (this._sorted && (this._comparer == null))
{
this._comparer = new ContactGroupComparer();
}
this.InnerDoSort();
}
}
}
public object SyncRoot
{
get
{
return this._syncObj;
}
}
bool ICollection<ContactGroupBase>.IsReadOnly
{
get
{
return this._col.get_IsReadOnly();
}
}
private class ContactGroupComparer : IComparer<ContactGroupBase>
{
public int Compare(ContactGroupBase x, ContactGroupBase y)
{
if (x == null)
{
if (y != null)
{
return -1;
}
return 0;
}
if (y == null)
{
return 1;
}
return string.Compare(x.Name, y.Name);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -