Historical Collection

Topics: Framework Services
Dec 28, 2013 at 1:02 AM
Edited Dec 28, 2013 at 3:08 AM
Hello,
I was in need of a Conductor that had a history. More specifically, I wanted to have a navigation system where I could drill down deep and then when I closed the items, it would pop back to previous views.

The current Conductor 'Collections' didn't quite work for my needs as is, so I came up with a HistoricalConductor (source below),

I'm not entirely convinced this is the best way to solve the problem, or if I should be using some sort of NavigationSystem.

Does anyone have any alternative ideas how to implement this, or critique of my code?
    public class HistoricalConductor<T> : Conductor<T> where T:class
    {
        private BindableCollection<T> _ScreenHistory = new BindableCollection<T>();

        /// <summary>
        /// History of all Screens, starting with the first one
        /// </summary>
        public BindableCollection<T> ScreenHistory
        {
            get { return _ScreenHistory; }
            set { _ScreenHistory = value; NotifyOfPropertyChange(() => ScreenHistory); }
        }

        private bool _KeepHistoryNeat = true;
        /// <summary>
        /// If true, when an item is Activated a check of history will be performed for that item.
        /// If that item exists, all history infront of that item will be erased.
        /// </summary>
        public bool KeepHistoryNeat
        {
            get { return _KeepHistoryNeat; }
            set { _KeepHistoryNeat = value; NotifyOfPropertyChange(() => KeepHistoryNeat); }
        }
        

        protected override T EnsureItem(T newItem)
        {
            if (newItem == null)
            {
                if (ScreenHistory.Count == 0)
                    return base.EnsureItem(newItem);
                ScreenHistory.RemoveAt(ScreenHistory.Count - 1);

                if (ScreenHistory.Count == 0)
                    return base.EnsureItem(newItem);
                newItem = ScreenHistory[ScreenHistory.Count - 1];
                return base.EnsureItem(newItem);
            }
            else
            {
                if(KeepHistoryNeat)
                {
                    var pos = ScreenHistory.IndexOf(newItem);
                    if(pos > -1)
                    {
                        while (ScreenHistory.Count > pos + 1)
                            ScreenHistory.RemoveAt(ScreenHistory.Count - 1);
                        newItem = ScreenHistory[ScreenHistory.Count - 1];
                        return base.EnsureItem(newItem);
                    }
                }
                var ret = base.EnsureItem(newItem);
                ScreenHistory.Add(ret);
                return ret;
            }

        }