using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Runtime.Serialization; using System.Windows.Media; using System.Xml.Serialization; using MahTweets.Mobile.Core.Extensions; using MahTweets.Mobile.Core.Filters; using MahTweets.Mobile.Core.Interfaces; namespace MahTweets.Mobile.Core { [KnownType(typeof(UpdateTypeFilter))] [XmlInclude(typeof(UpdateTypeFilter))] public class StreamModel : MahViewModelBase { private ObservableCollection _filters; private string _groupName; private int _position; private bool _selectAll; public StreamModel() { SelectAll = true; Filters = new ObservableCollection(); } public ObservableCollection Filters { get { return _filters; } set { _filters = value; RaisePropertyChanged(() => Filters); } } public bool SelectAll { get { return _selectAll; } set { _selectAll = value; RaisePropertyChanged(() => SelectAll); } } public string GroupName { get { return _groupName; } set { _groupName = value; RaisePropertyChanged(() => GroupName); } } public int Position { get { return _position; } set { _position = value; RaisePropertyChanged(() => Position); } } public static StreamModel CreateDefault() { return CreateDefault("all"); } public static StreamModel CreateDefault(string name) { return new StreamModel { GroupName = name, Position = 0 }; } public FilterBehaviour IsIgnored(IStatusUpdate update) { try { var applicableFilters = new List(); if (update.Parents != null) { foreach (IMicroblog microblog in update.Parents) { foreach (UpdateType updateType in update.Types) { IEnumerable matchingUpdates = GetFiltersFor(microblog, updateType).Where(f => f.IsIncluded != FilterBehaviour.NoBehaviour); applicableFilters.AddRange(matchingUpdates); } } } // do we have any excludes for these UpdateTypes if (applicableFilters.HasItems(f => f.IsIncluded == FilterBehaviour.Exclude)) return FilterBehaviour.Exclude; if (applicableFilters.HasItems(f => f.IsIncluded == FilterBehaviour.Include)) return FilterBehaviour.Include; } catch (Exception ex) { Debug.WriteLine(ex); } return FilterBehaviour.Exclude; } public void ClearAllContactFilters() { Filters = new ObservableCollection(); RaisePropertyChanged(() => Filters); } public IEnumerable GetFiltersFor(IMicroblog microblog, UpdateType updateType) { try { IEnumerable filters = Filters.OfType(); IEnumerable foundFiltersForMicroblog = filters.Where( filter => filter.Microblog != null && filter.Microblog.Protocol.Matches(microblog.Protocol, true) && filter.Microblog.Credentials.AccountName.Matches(microblog.Credentials.AccountName, true)); if (!foundFiltersForMicroblog.Any()) return new List(); var foundFiltersForUpdate = foundFiltersForMicroblog.Where( filter => (filter.UpdateType != null) ? filter.UpdateType.Type.Matches(updateType.Type, true) : false); return !foundFiltersForUpdate.Any() ? new List() : foundFiltersForUpdate.Cast().ToList(); } catch (Exception ex) { Debug.WriteLine(ex); return null; } } public void Include(IMicroblog blog, UpdateType updateType, Color color) { try { var filters = GetFiltersFor(blog, updateType); if (filters.Any()) { foreach (Filter ff in filters) { ff.IsIncluded = FilterBehaviour.Include; ff.Color = color; } return; } var fs = new UpdateTypeFilter(FilterBehaviour.Include, blog, updateType) { Color = color }; Filters.Add(fs); } catch (Exception ex) { Debug.WriteLine(ex); } } public void Include(IMicroblog blog, UpdateType updateType) { try { IEnumerable filters = GetFiltersFor(blog, updateType); if (filters.Any()) { foreach (Filter ff in filters) { ff.IsIncluded = FilterBehaviour.Include; } return; } var fs = new UpdateTypeFilter(FilterBehaviour.Include, blog, updateType); Filters.Add(fs); } catch (Exception ex) { Debug.WriteLine(ex); } } } }