RadekVyM / SimpleToolkit

SimpleToolkit is a .NET MAUI library of helpers and simple, fully customizable controls, such as SimpleShell - custom Shell implementation that allows you to create unique navigation experiences.
MIT License
434 stars 44 forks source link

The app crashes with SimpleShell #35

Open ziomek64 opened 11 months ago

ziomek64 commented 11 months ago

Hello, I've just stumbled upon your SimpleToolkit and I wanted to implement it. It looks awesome, has some stuff that I need and also Material3 TabBar in Controls.

Right now I'm getting unhandled exception: Java.Lang.IllegalArgumentException: 'No view found for id 0x5 (unknown) for fragment SimpleFragment{acb1dc6} (0b7617ee-9aaa-4da3-88ab-d0e65a35f48c id=0x5)'

I'm trying to follow the guide to have basic implementation for starters and I swapped everything for SimpleShell. So I'm gonna give you a tour over it. MauiProgram.cs

using MauiApp2.Views;
using MauiApp2.ViewModels;
using Microsoft.Extensions.Logging;
using CommunityToolkit.Maui;
using Material.Components.Maui.Extensions;
using Material.Components.Maui;
using SimpleToolkit.Core;
using SimpleToolkit.SimpleShell;

namespace MauiApp2;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit()
            //TODO .UseMaterialComponents()
            .UseSimpleShell()

            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

#if DEBUG
        builder.Logging.AddDebug();
#endif
        return builder.Build();
    }
}

App.xaml.cs

namespace MauiApp2;

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        MainPage = new AppShell();
    }
}

AppShell.xaml. I have login page route and the main one with tab bar.

<?xml version="1.0" encoding="UTF-8" ?>
<simpleShell:SimpleShell
    x:Class="MauiApp2.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:DetailsViews="clr-namespace:MauiApp2.Views.DetailsViews"
    xmlns:Views="clr-namespace:MauiApp2.Views"
    xmlns:local="clr-namespace:MauiApp2"
    xmlns:simpleShell="clr-namespace:SimpleToolkit.SimpleShell;assembly=SimpleToolkit.SimpleShell"
    Shell.FlyoutBehavior="Disabled">

    <ShellItem FlyoutItemIsVisible="False" Route="LandingPage">
        <ShellContent ContentTemplate="{DataTemplate Views:LandingPage}" />
    </ShellItem>

    <TabBar Route="main">
        <Tab Title="Home" Icon="home.png">
            <ShellContent ContentTemplate="{DataTemplate Views:HomePage}" />
        </Tab>
        <Tab Title="Profile" Icon="user.png">
            <ShellContent ContentTemplate="{DataTemplate Views:ProfilePage}" />
        </Tab>
        <Tab Title="Anime" Icon="play_anime.png">
            <ShellContent ContentTemplate="{DataTemplate Views:AnimeListPage}" />
        </Tab>
        <Tab Title="Manga" Icon="read_manga.png">
            <ShellContent ContentTemplate="{DataTemplate Views:MangaListPage}" />
        </Tab>
        <Tab Title="Settings" Icon="settings.png">
            <ShellContent ContentTemplate="{DataTemplate Views:SettingsPage}" />
        </Tab>
    </TabBar>

</simpleShell:SimpleShell>
using MauiApp2.Views;
using MauiApp2.Views.DetailsViews;
using SimpleToolkit.SimpleShell;

namespace MauiApp2;

public partial class AppShell : SimpleToolkit.SimpleShell.SimpleShell
{
    public AppShell()
    {
        Routing.RegisterRoute("MediaDetailsPage", typeof(MediaDetailsPage));
        Routing.RegisterRoute("MediaCharacterListPage", typeof(MediaCharacterListPage));
        Routing.RegisterRoute("MediaStaffListPage", typeof(MediaStaffListPage));
        InitializeComponent();
    }
}

When I'm loading the app on that login page, it checks for token on apperance etc and goes to main route. I'm pointing this out now that I've also tried with just Shell

            if (validation)
            {
                await SimpleToolkit.SimpleShell.SimpleShell.Current.GoToAsync($"//main");
            }

This is the only Shell and navigation stuff that happens and I'm crashing. When I'm swapping everything back to Shell it will load my app. So I've need some help, because maybe I'm missing something. Are all toolkit nugets supposed to be installed on platform specific projects too? Because I didn't get an option to do so. Just the main one. Which might be the problem..

From my main project file:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFrameworks>net8.0-android</TargetFrameworks>

I don't have 34 after android like in nuget requirements. But yeah it's targetting 34. I don't have 34 because the app just wouldn't start with numbera after android, after .net 8 upgrade.

RadekVyM commented 11 months ago

Hi @ziomek64,

thanks for informing me about the issue. Where exactly do you call SimpleToolkit.SimpleShell.SimpleShell.Current.GoToAsync($"//main");? Does the app crash even if this call is commented out?

ziomek64 commented 11 months ago

It happens in OnAppearing for my login page. The app doesn't crash when its commented or with normal shell

protected override async void OnAppearing()
{
    var token = Preferences.Get("token", "default_value");
    bool validation = true;

    try
    {
        validation = false;
        if (token != "default_value")
        {
            IJsonSerializer serializer = new JsonNetSerializer();
            var provider = new UtcDateTimeProvider();
            IJwtValidator validator = new JwtValidator(serializer, provider);
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); // symmetric
            IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm);

            //TODO decoding z login taska i sprawdzanie expiry w ten sposob
            //var json = decoder.Decode(token);

            validation = true;
        }
    }
    catch (TokenExpiredException)
    {
        Preferences.Set("token", "default_value");
    }
    catch (SignatureVerificationException)
    {
        Preferences.Set("token", "default_value");
    }
    catch (InvalidTokenPartsException)
    {
        Preferences.Set("token", "default_value");
    }
    catch (HttpRequestException httpRequestException)
    {
        Debug.WriteLine(httpRequestException.Message);
        MainThread.BeginInvokeOnMainThread(() =>
        {
            var toast = Toast.Make("No internet.", duration, fontSize);

            toast.Show(cancellationTokenSource.Token);
        });
    }
    catch (Exception e)
    {
        Debug.WriteLine(e);
        if (e.Message.Contains("Unable to resolve host"))
        {
            MainThread.BeginInvokeOnMainThread(() =>
            {
                //DependencyService.Get<Toast>().Show("Unexpected error happened");

                var toast = Toast.Make("No internet.", duration, fontSize);

                toast.Show(cancellationTokenSource.Token);
            });
        }
        else
        {
            MainThread.BeginInvokeOnMainThread(() =>
            {
                //DependencyService.Get<Toast>().Show("Unexpected error happened");

                var toast = Toast.Make("Unexpected error happened.", duration, fontSize);

                toast.Show(cancellationTokenSource.Token);
            });
        }
    }
    finally
    {
        if (validation)
        {
            await SimpleToolkit.SimpleShell.SimpleShell.Current.GoToAsync($"//main");
        }
    }
}

Should simple shell be installed in platform specific projects too or not?

RadekVyM commented 11 months ago

Hi @ziomek64,

I have finally tried to take a look at this issue. However, I probably will not be able to fix it because of the way how creating a handler for a ShellItem works.

A workaround to this issue is to delay the navigation using Task.Delay() a bit:

await Task.Delay(100);
await Shell.Current.GoToAsync($"//main");

The delay is needed only on the first navigation to the initial page. On subsequent navigations to that page and on other pages it should not be needed.

And by the way, I currently do not plan to continue developing the SimpleToolkit.SimpleShell.Controls package, so I do not recommend using that.