<commit_before>﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Net.Http;
using System.Runtime.CompilerServices;
using Windows.ApplicationModel.Core;
using Windows.ApplicationModel.DataTransfer.DragDrop.Core;
using Windows.System;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Core.Preview;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Flurl.Http;
using iHentai.Common;
using iHentai.Common.Helpers;
using iHentai.Common.Tab;
using iHentai.Controls;
using iHentai.Services;
using iHentai.Services.Core;
using Microsoft.Toolkit.Helpers;
using Microsoft.Toolkit.Uwp.UI;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Microsoft.UI.Xaml.Controls;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace iHentai
{
    public sealed partial class RootView : INotifyPropertyChanged
    {
        private bool _isUpdatingTab;
        public int SelectedTabIndex
        {
            get => _selectedTabIndex;
            set
            {
                if (!_isUpdatingTab)
                {
                    _selectedTabIndex = value;
                    SetTitlebarVisibility(Visibility.Visible);
                }
            }
        }

        private Grid _titleBarGrid;
        private int _selectedTabIndex;
        TabManager TabManager { get; } = new TabManager();
        ThemeManager ThemeManager { get; }
        public RootView()
        {
            this.InitializeComponent();
            ThemeManager = new ThemeManager(this);
            CoreApplication.GetCurrentView().TitleBar.Also(it =>
            {
                it.ExtendViewIntoTitleBar = true;
                it.LayoutMetricsChanged += OnCoreTitleBarOnLayoutMetricsChanged;
            });
            ApplicationView.GetForCurrentView().TitleBar.Also(it =>
            {
                it.ButtonBackgroundColor = Colors.Transparent;
                it.ButtonInactiveBackgroundColor = Colors.Transparent;
            });
            SystemNavigationManager.GetForCurrentView().Also(it =>
            {
                it.AppViewBackButtonVisibility =
                    AppViewBackButtonVisibility.Collapsed;
                it.BackRequested += OnSystemBackRequested;
            });
            SystemNavigationManagerPreview.GetForCurrentView().Also(it =>
            {
                it.CloseRequested += WindowOnCloseRequested;
            });
            ImageCache.Instance.InitializeAsync(httpMessageHandler: HentaiHttpMessageHandler.Instance).FireAndForget();
            ImageEx2.WriteableImageCache.Instance.InitializeAsync(httpMessageHandler: HentaiHttpMessageHandler.Instance).FireAndForget();
            FlurlHttp.Configure(it => { it.HttpClientFactory = new HentaiHttpClientFactory(); });
            Singleton<ProgressImageCache>.Instance.InitializeAsync(httpMessageHandler: HentaiHttpMessageHandler.Instance).FireAndForget();
            iHentaiService.Register<IPreferences>(Singleton<Settings>.Instance);
            Singleton<BroadcastCenter>.Instance.Subscribe("set_title_bar_visibility", ((sender, args) =>
            {
                if (args is Visibility visibility)
                {
                    SetTitlebarVisibility(visibility);
                }
            }));
            Singleton<BroadcastCenter>.Instance.Subscribe("tab_toggle_visible", (o, o1) => { ToggleTabBar(); });
            Singleton<BroadcastCenter>.Instance.Subscribe("open_new_tab", (sender, args) =>
            {
                if (args is NewTabArgs item)
                {
                    _isUpdatingTab = true;
                    TabManager.Add(item);
                    _isUpdatingTab = false;
                    SelectedTabIndex = TabManager.Count - 1;
                }
            });
            TabManager.TabItems.CollectionChanged += TabItemsOnCollectionChanged;
            for (var i = 0; i < Singleton<ServiceManager>.Instance.Services.Count; i++)
            {
                var service = Singleton<ServiceManager>.Instance.Services[i];
                RootMenuFlyout.Items.Insert(i, new MenuFlyoutItem().Also(it =>
                {
                    it.Text = service.Name;
                    it.Click += (sender, args) =>
                    {
                        AddTabActivity(new ActivityTabItem(service.StartActivity, intent: service.Intent));
                    };
                }));
            }
        }

        private void SetTitlebarVisibility(Visibility visibility)
        {
            if (visibility == Visibility.Collapsed)
            {
                TitleBarContainer.Visibility = Visibility.Collapsed;
                TransparentTitleBar.Visibility = Visibility.Visible;
                Window.Current.SetTitleBar(TransparentTitleBar);
            }
            else
            {
                TitleBarContainer.Visibility = Visibility.Visible;
                TransparentTitleBar.Visibility = Visibility.Collapsed;
                Window.Current.SetTitleBar((TitleBarContainer.Tag as Grid) ?? SecondaryTitleBarDrag);
            }
        }

        private async void WindowOnCloseRequested(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
        {
            if (Singleton<DownloadManager>.Instance.IsBusy)
            {
                e.Handled = true;
                var dialog = new MessageDialog("Exit will stop the download", "Downloading books now, exit anyway?");
                dialog.Commands.Add(new UICommand("Yes", null));
                dialog.Commands.Add(new UICommand("No", null));
                var result = await dialog.ShowAsync();
                if (result.Label == "Yes")<commit_msg>Fix build<commit_after>﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Net.Http;
using System.Runtime.CompilerServices;
using Windows.ApplicationModel.Core;
using Windows.ApplicationModel.DataTransfer.DragDrop.Core;
using Windows.System;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Core.Preview;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Flurl.Http;
using iHentai.Common;
using iHentai.Common.Helpers;
using iHentai.Common.Tab;
using iHentai.Controls;
using iHentai.Downloader;
using iHentai.Services;
using iHentai.Services.Core;
using Microsoft.Toolkit.Helpers;
using Microsoft.Toolkit.Uwp.UI;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Microsoft.UI.Xaml.Controls;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace iHentai
{
    public sealed partial class RootView : INotifyPropertyChanged
    {
        private bool _isUpdatingTab;
        public int SelectedTabIndex
        {
            get => _selectedTabIndex;
            set
            {
                if (!_isUpdatingTab)
                {
                    _selectedTabIndex = value;
                    SetTitlebarVisibility(Visibility.Visible);
                }
            }
        }

        private Grid _titleBarGrid;
        private int _selectedTabIndex;
        TabManager TabManager { get; } = new TabManager();
        ThemeManager ThemeManager { get; }
        public RootView()
        {
            this.InitializeComponent();
            ThemeManager = new ThemeManager(this);
            CoreApplication.GetCurrentView().TitleBar.Also(it =>
            {
                it.ExtendViewIntoTitleBar = true;
                it.LayoutMetricsChanged += OnCoreTitleBarOnLayoutMetricsChanged;
            });
            ApplicationView.GetForCurrentView().TitleBar.Also(it =>
            {
                it.ButtonBackgroundColor = Colors.Transparent;
                it.ButtonInactiveBackgroundColor = Colors.Transparent;
            });
            SystemNavigationManager.GetForCurrentView().Also(it =>
            {
                it.AppViewBackButtonVisibility =
                    AppViewBackButtonVisibility.Collapsed;
                it.BackRequested += OnSystemBackRequested;
            });
            SystemNavigationManagerPreview.GetForCurrentView().Also(it =>
            {
                it.CloseRequested += WindowOnCloseRequested;
            });
            ImageCache.Instance.InitializeAsync(httpMessageHandler: HentaiHttpMessageHandler.Instance).FireAndForget();
            ImageEx2.WriteableImageCache.Instance.InitializeAsync(httpMessageHandler: HentaiHttpMessageHandler.Instance).FireAndForget();
            FlurlHttp.Configure(it => { it.HttpClientFactory = new HentaiHttpClientFactory(); });
            Singleton<ProgressImageCache>.Instance.InitializeAsync(httpMessageHandler: HentaiHttpMessageHandler.Instance).FireAndForget();
            iHentaiService.Register<IPreferences>(Singleton<Settings>.Instance);
            Singleton<BroadcastCenter>.Instance.Subscribe("set_title_bar_visibility", ((sender, args) =>
            {
                if (args is Visibility visibility)
                {
                    SetTitlebarVisibility(visibility);
                }
            }));
            Singleton<BroadcastCenter>.Instance.Subscribe("tab_toggle_visible", (o, o1) => { ToggleTabBar(); });
            Singleton<BroadcastCenter>.Instance.Subscribe("open_new_tab", (sender, args) =>
            {
                if (args is NewTabArgs item)
                {
                    _isUpdatingTab = true;
                    TabManager.Add(item);
                    _isUpdatingTab = false;
                    SelectedTabIndex = TabManager.Count - 1;
                }
            });
            TabManager.TabItems.CollectionChanged += TabItemsOnCollectionChanged;
            for (var i = 0; i < Singleton<ServiceManager>.Instance.Services.Count; i++)
            {
                var service = Singleton<ServiceManager>.Instance.Services[i];
                RootMenuFlyout.Items.Insert(i, new MenuFlyoutItem().Also(it =>
                {
                    it.Text = service.Name;
                    it.Click += (sender, args) =>
                    {
                        AddTabActivity(new ActivityTabItem(service.StartActivity, intent: service.Intent));
                    };
                }));
            }
        }

        private void SetTitlebarVisibility(Visibility visibility)
        {
            if (visibility == Visibility.Collapsed)
            {
                TitleBarContainer.Visibility = Visibility.Collapsed;
                TransparentTitleBar.Visibility = Visibility.Visible;
                Window.Current.SetTitleBar(TransparentTitleBar);
            }
            else
            {
                TitleBarContainer.Visibility = Visibility.Visible;
                TransparentTitleBar.Visibility = Visibility.Collapsed;
                Window.Current.SetTitleBar((TitleBarContainer.Tag as Grid) ?? SecondaryTitleBarDrag);
            }
        }

        private async void WindowOnCloseRequested(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
        {
            if (DownloadManager.Instance.IsBusy)
            {
                e.Handled = true;
                var dialog = new MessageDialog("Exit will stop the download", "Downloading books now, exit anyway?");
                dialog.Commands.Add(new UICommand("Yes", null));
                dialog.Commands.Add(new UICommand("No", null));
                var result = await dialog.ShowAsync();
                if (result.Label == "Yes")
{'fraction_non_alphanumeric': 0.0892352487796523, 'fraction_numerical': 0.001712768690588336, 'mean_word_length': 2.0530718954248366, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 42, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '638899', 'n_tokens_mistral': 2963, 'n_tokens_neox': 2854, 'n_words': 496}