2024-09-17 15:56:28 -07:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2024-09-18 16:46:02 -07:00
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
|
|
|
|
using Microsoft.CmdPal.UI.Pages;
|
|
|
|
|
|
using Microsoft.CmdPal.UI.ViewModels.Messages;
|
2024-11-22 04:47:31 -08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-09-17 15:56:28 -07:00
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.CmdPal.UI.ViewModels;
|
|
|
|
|
|
|
2024-12-06 18:11:55 -08:00
|
|
|
|
public partial class ShellViewModel(IServiceProvider _serviceProvider) : ObservableObject,
|
|
|
|
|
|
IRecipient<NavigateToPageMessage>
|
2024-09-17 15:56:28 -07:00
|
|
|
|
{
|
2024-09-18 16:46:02 -07:00
|
|
|
|
[ObservableProperty]
|
2024-11-22 04:47:31 -08:00
|
|
|
|
public partial bool IsLoaded { get; set; } = false;
|
2024-09-18 16:46:02 -07:00
|
|
|
|
|
2024-12-06 18:11:55 -08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
public partial PageViewModel? CurrentPage { get; set; }
|
|
|
|
|
|
|
2024-09-17 15:56:28 -07:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async Task<bool> LoadAsync()
|
|
|
|
|
|
{
|
2024-12-06 18:11:55 -08:00
|
|
|
|
WeakReferenceMessenger.Default.Register<NavigateToPageMessage>(this);
|
|
|
|
|
|
|
2024-12-02 16:35:56 -08:00
|
|
|
|
var tlcManager = _serviceProvider.GetService<TopLevelCommandManager>();
|
|
|
|
|
|
await tlcManager!.LoadBuiltinsAsync();
|
2024-09-18 16:46:02 -07:00
|
|
|
|
IsLoaded = true;
|
2024-09-17 15:56:28 -07:00
|
|
|
|
|
2024-12-02 16:35:56 -08:00
|
|
|
|
// Built-ins have loaded. We can display our page at this point.
|
|
|
|
|
|
var page = new MainListPage(_serviceProvider);
|
Bind to Page properties, and add support for item propchange's (#186)
A lot of this is just samples work, so skip on down to `src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ActionBarViewModel.cs`.
* Updates the Action Bar with the title of the currently active page when we navigate to it
* listens for PropChanged events from `ICommandItem`s in lists and on `IPage`'s (to display a loading bar)
* Also didn't put this on the ShellPage quite yet, because the diff was already massive, and there's complicated "are _we_ loading the page? did the `IPage` say it was loading?" plumbing that needs to be done.
* adds rudimentary support for showing an exception, if there's an error we catch
* I didn't add log this everywhere yet though, since we're not 100% confident on the messaging infrastructure here
* I decided against putting this on the shell page, because I want users to be able to _go back_ from a page with an exception, and that didn't seem trivial if it was on the shellpage itself
* Then updates a bunch of extensions to better utilize the loading state.
* Then discovers a really weird bug with event callbacks in WinRT, so I decided to wrap those in try/catches to have extensions explode less often. (#181)
* Then also adds a bunch of "evil" samples, to make a unified place of "things to try and break us"
ref #73
---------
Co-authored-by: Mike Griese <zadjii@gmail.com>
2024-12-05 09:43:27 -08:00
|
|
|
|
WeakReferenceMessenger.Default.Send<PerformCommandMessage>(new(new(page!)));
|
2024-09-18 16:46:02 -07:00
|
|
|
|
|
2024-12-02 16:35:56 -08:00
|
|
|
|
// After loading built-ins, and starting navigation, kick off a thread to load extensions.
|
|
|
|
|
|
tlcManager.LoadExtensionsCommand.Execute(null);
|
|
|
|
|
|
_ = Task.Run(async () =>
|
2024-09-18 16:46:02 -07:00
|
|
|
|
{
|
2024-12-02 16:35:56 -08:00
|
|
|
|
await tlcManager.LoadExtensionsCommand.ExecutionTask!;
|
|
|
|
|
|
if (tlcManager.LoadExtensionsCommand.ExecutionTask.Status != TaskStatus.RanToCompletion)
|
|
|
|
|
|
{
|
|
|
|
|
|
// TODO: Handle failure case
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
2024-09-18 16:46:02 -07:00
|
|
|
|
}
|
2024-12-06 18:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
public void Receive(NavigateToPageMessage message) => CurrentPage = message.Page;
|
2024-09-17 15:56:28 -07:00
|
|
|
|
}
|