// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using CommunityToolkit.Tooling.SampleGen.Metadata;
using CommunityToolkit.Tooling.SampleGen;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation.Collections;
using CommunityToolkit.App.Shared.Pages;
#if !WINAPPSDK
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
#else
using DispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
#endif
namespace CommunityToolkit.App.Shared;
///
/// Kicks off the loading process and determines whether to display a single-sample or multi-sample view.
///
public sealed partial class AppLoadingView : Page
{
///
/// Creates a new instance of .
///
public AppLoadingView()
{
this.InitializeComponent();
}
///
/// Backing dependency property for .
///
public static readonly DependencyProperty IsLoadingProperty =
DependencyProperty.Register(nameof(IsLoading), typeof(bool), typeof(AppLoadingView), new PropertyMetadata(false));
///
/// Backing dependency property for .
///
public static readonly DependencyProperty LoadingMessageProperty =
DependencyProperty.Register(nameof(LoadingMessage), typeof(string), typeof(AppLoadingView), new PropertyMetadata(string.Empty));
///
/// Gets or sets a value indicating whether loading operations are being performed.
///
public bool IsLoading
{
get { return (bool)GetValue(IsLoadingProperty); }
set { SetValue(IsLoadingProperty, value); }
}
///
/// Gets or sets the displayed loading message.
///
public string LoadingMessage
{
get { return (string)GetValue(LoadingMessageProperty); }
set { SetValue(LoadingMessageProperty, value); }
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
IsLoading = true;
LoadingMessage = "Loading...";
var sampleDocs = FindReferencedDocumentPages().ToArray();
if (sampleDocs.Length == 0)
{
IsLoading = false;
LoadingMessage = "No sample pages were found :(";
return;
}
#if ALL_SAMPLES
ScheduleNavigate(typeof(Shell), sampleDocs);
#else
var samples = FindReferencedSamples().ToArray();
(IEnumerable Samples, IEnumerable Docs, bool AreDocsFirst) displayInfo = (samples, sampleDocs, false);
ScheduleNavigate(typeof(TabbedPage), displayInfo);
#endif
}
// Needed because Frame.Navigate doesn't work inside of the OnNavigatedTo override.
private void ScheduleNavigate(Type type, object? param = null)
{
DispatcherQueue.GetForCurrentThread().TryEnqueue(() =>
{
#if !NETFX_CORE
Frame.Navigate(type, param);
#else
Frame.NavigateToType(type, param, new FrameNavigationOptions { IsNavigationStackEnabled = false });
#endif
});
}
private IEnumerable FindReferencedDocumentPages()
{
return ToolkitDocumentRegistry.Execute();
}
private IEnumerable FindReferencedSamples()
{
return ToolkitSampleRegistry.Listing.Values;
}
}