TinyPubSub icon indicating copy to clipboard operation
TinyPubSub copied to clipboard

Pop unsubscribing

Open petarkekez opened this issue 3 years ago • 2 comments

Hi, to start thanks for the framework.

I had an issue with stuff not unsubscribing with Shell.

This happened because the unsubscription automaticly happend only on the navigation page, code:

if (sender is Application && e.PropertyName == "MainPage")
{
    var page = (sender as Application).MainPage;

    if (page is NavigationPage)
    {
        BindEvents(page as NavigationPage);
    }
}

because of this i needed to update this method to this:

static void App_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    if (sender is Application && e.PropertyName == "MainPage")
    {
        var page = (sender as Application).MainPage;

        if (page is NavigationPage)
        {
            BindEvents(page as NavigationPage);
        }
        else if (page is Shell s)
        {
            s.Navigating += Shell_Navigating;
        }
    }
}
private static void Shell_Navigating(object sender, ShellNavigatingEventArgs e)
{
    if (e.Source == ShellNavigationSource.Pop
        || e.Source == ShellNavigationSource.Remove)
    {
        if (sender is Shell s)
        {
            UnsubscribePage(s.CurrentPage);
        }
    }

    if (e.Source == ShellNavigationSource.PopToRoot)
    {
        if (sender is Shell s)
        {

            UnsubscribePage(s.CurrentPage);
        }
    }

    if (e.Source == ShellNavigationSource.Unknown)
    {
        if(sender is Shell s)
        {
            if( !(s.CurrentPage is MainView) ) // On initial shell setup this is triggered..
            {
                UnsubscribePage(s.CurrentPage);
            }
        }
    }
}

I can add this to a PR if you would like to review/merge it?

petarkekez avatar Mar 01 '21 14:03 petarkekez

Hi! Thanks for using TinyPubSub!

Add it to a PR and we'll take a look at it and merge it if there are no issues with it. We do need to do some testing to make sure it doesn't break anything, but we'll try to be as quick as possible.

And reach out if there are any other questions.

johankson avatar Mar 01 '21 15:03 johankson

Is it OK if I add these changes myself?

johankson avatar May 27 '21 12:05 johankson