<commit_before>﻿using System.Collections;
using System.Collections.Specialized;
using Android.Views;
using Android.Widget;
using Storm.Mvvm.Interfaces;

namespace Storm.Mvvm
{
	public class BindableAdapter : BaseAdapter<object>, IMvvmAdapter, ISearchableAdapter
	{
		private ITemplateSelector _templateSelector;
		private IList _collection;

		public object Collection
		{
			get { return _collection; }
			set
			{
				if (!object.Equals(_collection, value))
				{
					Unregister(_collection);
					_collection = value as IList;
					Register(_collection);
					NotifyDataChanged();
				}
			}
		}

		public ITemplateSelector TemplateSelector
		{
			get { return _templateSelector; }
			set
			{
				if (!object.Equals(_templateSelector, value))
				{
					_templateSelector = value;
					NotifyDataChanged();
				}
			}
		}

		public override object this[int position]
		{
			get
			{
				if (_collection == null || position < 0 || position >= _collection.Count)
				{
					return null;
				}
				return _collection[position];
			}
		}

		public override long GetItemId(int position)
		{
			return position;
		}

		public override View GetView(int position, View convertView, ViewGroup parent)
		{
			return TemplateSelector.GetView(this[position], parent, convertView);
		}

		public override int Count
		{
			get
			{
				return _collection == null ? 0 : _collection.Count;
			}
		}

		public int IndexOf(object value)
		{
			return _collection == null ? -1 : _collection.IndexOf(value);
		}

		private void Unregister(object collection)
		{
			INotifyCollectionChanged observable = collection as INotifyCollectionChanged;
			if (observable != null)
			{
				observable.CollectionChanged -= OnCollectionChanged;
			}
		}

		private void Register(object collection)
		{
			INotifyCollectionChanged observable = collection as INotifyCollectionChanged;
			if (observable != null)
			{
				observable.CollectionChanged += OnCollectionChanged;
			}
		}

		private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
		{
			NotifyDataChanged();
		}

		private void NotifyDataChanged()
		{
			if (TemplateSelector != null)
			{
				NotifyDataSetChanged();
			}
		}
	}
}<commit_msg>Add support for IEnumerable in android adapters<commit_after>﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using Android.Views;
using Android.Widget;
using Storm.Mvvm.Interfaces;

namespace Storm.Mvvm
{
	public class BindableAdapter : BaseAdapter<object>, IMvvmAdapter, ISearchableAdapter
	{
		private ITemplateSelector _templateSelector;
		private IList _collection;

		public object Collection
		{
			get { return _collection; }
			set
			{
				if (!object.Equals(_collection, value))
				{
					Unregister(_collection);
					_collection = value as IList;
					if (_collection == null)
					{
						if (value is IEnumerable)
						{
							_collection = ToIList((IEnumerable) value);
						}
						else if (value != null)
						{
							throw new InvalidOperationException("Binding with adapter only support collection binding which implement IEnumerable or IList");
						}
					}
					Register(_collection);
					NotifyDataChanged();
				}
			}
		}
		
		public ITemplateSelector TemplateSelector
		{
			get { return _templateSelector; }
			set
			{
				if (!object.Equals(_templateSelector, value))
				{
					_templateSelector = value;
					NotifyDataChanged();
				}
			}
		}

		public override object this[int position]
		{
			get
			{
				if (_collection == null || position < 0 || position >= _collection.Count)
				{
					return null;
				}
				return _collection[position];
			}
		}

		public override long GetItemId(int position)
		{
			return position;
		}

		public override View GetView(int position, View convertView, ViewGroup parent)
		{
			return TemplateSelector.GetView(this[position], parent, convertView);
		}

		public override int Count
		{
			get
			{
				return _collection == null ? 0 : _collection.Count;
			}
		}

		public int IndexOf(object value)
		{
			return _collection == null ? -1 : _collection.IndexOf(value);
		}

		private void Unregister(object collection)
		{
			INotifyCollectionChanged observable = collection as INotifyCollectionChanged;
			if (observable != null)
			{
				observable.CollectionChanged -= OnCollectionChanged;
			}
		}

		private void Register(object collection)
		{
			INotifyCollectionChanged observable = collection as INotifyCollectionChanged;
			if (observable != null)
			{
				observable.CollectionChanged += OnCollectionChanged;
			}
		}

		private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
		{
			NotifyDataChanged();
		}

		private void NotifyDataChanged()
		{
			if (TemplateSelector != null)
			{
				NotifyDataSetChanged();
			}
		}

		private IList ToIList(IEnumerable source)
		{
			ArrayList result = new ArrayList();
			foreach (object item in source)
			{
				result.Add(item);
			}
			return result;
		}
	}
}
{'fraction_non_alphanumeric': 0.0867063492063492, 'fraction_numerical': 0.0011904761904761906, 'mean_word_length': 3.3085470085470083, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4552164', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1430, 'n_words': 358}