<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using Caliburn.Micro;
using Cobalt.Common.IoC;
using Cobalt.ViewModels.Pages;
using Cobalt.Views.Pages;

namespace Cobalt.ViewModels.Utils
{
    public interface INavigationService : INotifyPropertyChanged, IDisposable
    {
        PageView ActivePage { get; }
        PageViewModel ActiveItem { get; }
        void NavigateTo<T>() where T : PageViewModel;
        void NavigateToType(Type value);
    }

    public class NavigationService : Conductor<PageViewModel>.Collection.OneActive, INavigationService
    {
        private readonly Dictionary<Type, (PageViewModel ViewModel, PageView View)> _existing;
        private readonly IResourceScope _resolver;
        private PageView _activePage;

        public NavigationService(IResourceScope resolver)
        {
            _resolver = resolver;
            _existing = new Dictionary<Type, (PageViewModel ViewModel, PageView View)>();
            //needed for conductors that aren't shown
            ((IActivate) this).Activate();
        }

        public PageView ActivePage
        {
            get => _activePage;
            set
            {
                _activePage = value;
                NotifyOfPropertyChange();
            }
        }

        public void NavigateTo<T>() where T : PageViewModel
        {
            NavigateToType(typeof(T));
        }

        public void NavigateToType(Type value)
        {
            if (value == null || !typeof(PageViewModel).IsAssignableFrom(value)) return;
            if (_existing.ContainsKey(value))
            {
                var (vm, page) = _existing[value];
                ActivePage = page;
                ActivateItem(vm);
            }
            else
            {
                var vm = _resolver.Resolve<PageViewModel>(value);
                ActivePage = (PageView) ViewLocator.LocateForModel(vm, null, null);
                ViewModelBinder.Bind(vm, _activePage, null);
                ActivateItem(vm);
                _existing[value] = (vm, ActivePage);
            }
        }

        public void Dispose()
        {
            _resolver.Dispose();
        }
    }
}<commit_msg>use cache instead of dictionary<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using Caliburn.Micro;
using Cobalt.Common.IoC;
using Cobalt.Common.Util;
using Cobalt.ViewModels.Pages;
using Cobalt.Views.Pages;

namespace Cobalt.ViewModels.Utils
{
    public interface INavigationService : INotifyPropertyChanged, IDisposable
    {
        PageView ActivePage { get; }
        PageViewModel ActiveItem { get; }
        void NavigateTo<T>() where T : PageViewModel;
        void NavigateToType(Type value);
    }

    public class NavigationService : Conductor<PageViewModel>.Collection.OneActive, INavigationService
    {
        private readonly Cache<Type, PageViewModel> _existingViewModels;
        private readonly Cache<Type, PageView> _existingViews;
        private readonly IResourceScope _resolver;
        private PageView _activePage;

        public NavigationService(IResourceScope resolver)
        {
            _resolver = resolver;
            _existingViewModels = new Cache<Type, PageViewModel>();
            _existingViews = new Cache<Type, PageView>();
            //needed for conductors that aren't shown
            ((IActivate) this).Activate();
        }

        public PageView ActivePage
        {
            get => _activePage;
            set
            {
                _activePage = value;
                NotifyOfPropertyChange();
            }
        }

        public void NavigateTo<T>() where T : PageViewModel
        {
            NavigateToType(typeof(T));
        }

        public void NavigateToType(Type value)
        {
            if (value == null || !typeof(PageViewModel).IsAssignableFrom(value)) return;
            if(_existingViewModels.TryGetValue(value, out var viewModel) && _existingViews.TryGetValue(value, out var view))
            {
                ActivePage = view;
                ActivateItem(viewModel);
            }
            else
            {
                var vm = _resolver.Resolve<PageViewModel>(value);
                ActivePage = (PageView) ViewLocator.LocateForModel(vm, null, null);
                ViewModelBinder.Bind(vm, _activePage, null);
                ActivateItem(vm);

                _existingViewModels.Add(value, vm);
                _existingViews.Add(value, ActivePage);
            }
        }

        public void Dispose()
        {
            _resolver.Dispose();
        }
    }
}
{'fraction_non_alphanumeric': 0.08406794237798323, 'fraction_numerical': 0.0, 'mean_word_length': 1.972523961661342, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '4813487', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1176, 'n_words': 270}