<commit_before>﻿using System;
using System.ComponentModel.Composition;
using System.Reactive.Linq;
using GitHub.Api;
using GitHub.Services;
using GitHub.VisualStudio.Base;
using GitHub.VisualStudio.Helpers;
using Microsoft.TeamFoundation.Controls;
using GitHub.UI;
using GitHub.VisualStudio.UI;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Extensions;
using GitHub.Logging;
using Serilog;
using System.Collections.Specialized;
using GitHub.Settings;

namespace GitHub.VisualStudio.TeamExplorer.Home
{
    [TeamExplorerNavigationItem(ForkNavigationItemId, NavigationItemPriority.Fork)]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class ForkNavigationItem : TeamExplorerNavigationItemBase
    {
        static readonly ILogger log = LogManager.ForContext<ForkNavigationItem>();

        public const string ForkNavigationItemId = "5245767A-B657-4F8E-BFEE-F04159F1DDA6";

        readonly IDialogService dialogService;
        IConnectionManager connectionManager;

        [ImportingConstructor]
        public ForkNavigationItem(IGitHubServiceProvider serviceProvider,
            ISimpleApiClientFactory apiFactory,
            ITeamExplorerServiceHolder holder,
            IDialogService dialogService,
            IPackageSettings packageSettings)
            : base(serviceProvider, apiFactory, holder, Octicon.repo_forked)
        {
            this.dialogService = dialogService;

            Text = Resources.ForkNavigationItemText;
            ArgbColor = Colors.PurpleNavigationItem.ToInt32();
            ConnectionManager.Connections.CollectionChanged += ConnectionsChanged;

            IsVisible = packageSettings?.ForkButton ?? false;
            packageSettings.PropertyChanged += (sender, args) =>
            {
                if (args.PropertyName == nameof(packageSettings.ForkButton))
                {
                    IsVisible = packageSettings.ForkButton;
                }
            };
        }

        IConnectionManager ConnectionManager
        {
            get
            {
                // We can't receive IConnectionManager in the constructor because Invalidate()
                // is called from the base constructor and so we don't have chance to save it
                // to a field.
                if (connectionManager == null)
                {
                    connectionManager = ServiceProvider.GetService<IConnectionManager>();
                }

                return connectionManager;
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            ConnectionManager.Connections.CollectionChanged -= ConnectionsChanged;
        }

        public override async void Execute()
        {
            var connection = await connectionManager.GetConnection(ActiveRepo);

            if (connection?.IsLoggedIn == true)
            {
                await dialogService.ShowForkDialog(ActiveRepo, connection);
            }
        }

        public override async void Invalidate()
        {
            try
            {
                IsVisible = false;

                if (await IsAGitHubRepo())
                {
                    var connection = await ConnectionManager.GetConnection(ActiveRepo);
                    IsVisible = connection?.IsLoggedIn ?? false;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Error updating ForkNavigationItem visibility");
            }
        }

        private void ConnectionsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            Invalidate();
        }
    }
}<commit_msg>Correctly controlling navigation item visibility<commit_after>﻿using System;
using System.ComponentModel.Composition;
using System.Reactive.Linq;
using GitHub.Api;
using GitHub.Services;
using GitHub.VisualStudio.Base;
using GitHub.VisualStudio.Helpers;
using Microsoft.TeamFoundation.Controls;
using GitHub.UI;
using GitHub.VisualStudio.UI;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Extensions;
using GitHub.Logging;
using Serilog;
using System.Collections.Specialized;
using GitHub.Settings;

namespace GitHub.VisualStudio.TeamExplorer.Home
{
    [TeamExplorerNavigationItem(ForkNavigationItemId, NavigationItemPriority.Fork)]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class ForkNavigationItem : TeamExplorerNavigationItemBase
    {
        static readonly ILogger log = LogManager.ForContext<ForkNavigationItem>();

        public const string ForkNavigationItemId = "5245767A-B657-4F8E-BFEE-F04159F1DDA6";

        readonly IDialogService dialogService;
        readonly IPackageSettings packageSettings;
        IConnectionManager connectionManager;

        [ImportingConstructor]
        public ForkNavigationItem(IGitHubServiceProvider serviceProvider,
            ISimpleApiClientFactory apiFactory,
            ITeamExplorerServiceHolder holder,
            IDialogService dialogService,
            IPackageSettings packageSettings)
            : base(serviceProvider, apiFactory, holder, Octicon.repo_forked)
        {
            this.dialogService = dialogService;
            this.packageSettings = packageSettings;

            Text = Resources.ForkNavigationItemText;
            ArgbColor = Colors.PurpleNavigationItem.ToInt32();
            ConnectionManager.Connections.CollectionChanged += ConnectionsChanged;

            packageSettings.PropertyChanged += (sender, args) =>
            {
                if (args.PropertyName == nameof(packageSettings.ForkButton))
                {
                    IsVisible = packageSettings.ForkButton;
                }
            };
        }

        IConnectionManager ConnectionManager
        {
            get
            {
                // We can't receive IConnectionManager in the constructor because Invalidate()
                // is called from the base constructor and so we don't have chance to save it
                // to a field.
                if (connectionManager == null)
                {
                    connectionManager = ServiceProvider.GetService<IConnectionManager>();
                }

                return connectionManager;
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            ConnectionManager.Connections.CollectionChanged -= ConnectionsChanged;
        }

        public override async void Execute()
        {
            var connection = await connectionManager.GetConnection(ActiveRepo);

            if (connection?.IsLoggedIn == true)
            {
                await dialogService.ShowForkDialog(ActiveRepo, connection);
            }
        }

        public override async void Invalidate()
        {
            try
            {
                IsVisible = false;
                
                if (packageSettings.ForkButton && await IsAGitHubRepo())
                {
                    var connection = await ConnectionManager.GetConnection(ActiveRepo);
                    IsVisible = connection?.IsLoggedIn ?? false;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Error updating ForkNavigationItem visibility");
            }
        }

        private void ConnectionsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            Invalidate();
        }
    }
}
{'fraction_non_alphanumeric': 0.06726397432468574, 'fraction_numerical': 0.00561647499331372, 'mean_word_length': 2.056395586432366, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '2888012', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1707, 'n_words': 379}