Skip to content

Commit

Permalink
Extend logging of page lifecycle events to better analyse behaviour o…
Browse files Browse the repository at this point in the history
…n iOS and Android.
  • Loading branch information
stephanpalmer committed Jun 11, 2021
1 parent 85d8425 commit a91842b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 36 deletions.
6 changes: 3 additions & 3 deletions DemoApp/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public App()
public void ToggleFlyout()
{
if (MainPage is FlyoutPage)
MainPage = new NavigationPage(new NavigationDemoPage());
MainPage = new NavigationPage(new NavigationDemoPage()).AddPageLog();
else
MainPage = new FlyoutPage {
Flyout = new MenuPage(),
Detail = new NavigationPage(new NavigationDemoPage()),
};
Detail = new NavigationPage(new NavigationDemoPage()).AddPageLog(),
}.AddPageLog();
}

public static void ShowMessage(string title, string message)
Expand Down
1 change: 1 addition & 0 deletions DemoApp/DemoApp.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Compile Include="$(MSBuildThisFileDirectory)DemoPages\TitleViewPage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DemoPages\TabbedTitleViewPage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DemoPages\PopupDemoPage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PageExtensions.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)DemoPages\" />
Expand Down
17 changes: 2 additions & 15 deletions DemoApp/DemoPages/NavigationDemoPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class NavigationDemoPage : ContentPage
public NavigationDemoPage(string title = "Navigation")
{
Title = title;
PageExtensions.AddPageLog(this);

Content = new StackLayout {
VerticalOptions = LayoutOptions.CenterAndExpand,
Expand All @@ -30,7 +31,7 @@ public NavigationDemoPage(string title = "Navigation")
Command = new Command(obj => Navigation.PopModalAsync()),
},
new DemoButton("PushModalAsync NavigationPage") {
Command = new Command(o => Navigation.PushModalAsync(new NavigationPage(new NavigationDemoPage(title + " ^")))),
Command = new Command(o => Navigation.PushModalAsync(new NavigationPage(new NavigationDemoPage(title + " ^")).AddPageLog())),
},
new DemoLabel("Flyout:"),
new DemoButton("Toggle Flyout MainPage") {
Expand All @@ -46,20 +47,6 @@ public NavigationDemoPage(string title = "Navigation")
(Content as StackLayout).Children.Insert(0, new DemoLabel("Title: " + title));
}

protected override void OnAppearing()
{
base.OnAppearing();

App.PageLog += $"A({Title}) ";
}

protected override void OnDisappearing()
{
App.PageLog += $"D({Title}) ";

base.OnDisappearing();
}

async void ShowAlert()
{
await DisplayAlert("Alert title", "Alert message", "Ok");
Expand Down
23 changes: 7 additions & 16 deletions DemoApp/DemoPages/TabbedPageDemoPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ public class TabbedPageDemoPage : TabbedPage
public TabbedPageDemoPage()
{
Title = "TabbedPage";

Appearing += (s, e) => App.PageLog += $"A({Title}) ";
Disappearing += (s, e) => App.PageLog += $"D({Title}) ";
PageExtensions.AddPageLog(this);

var contentPageA = new ContentPage {
Title = "Tab A",
Expand All @@ -20,9 +18,7 @@ public TabbedPageDemoPage()
new Button{Text = "Open Subpage", Command = new Command(OpenSubpage)},
}
}
};
contentPageA.Appearing += (s, e) => App.PageLog += $"A(Tab A) ";
contentPageA.Disappearing += (s, e) => App.PageLog += $"D(Tab A) ";
}.AddPageLog();

var contentPageB = new ContentPage {
Title = "Tab B",
Expand All @@ -32,9 +28,7 @@ public TabbedPageDemoPage()
},
},
ToolbarItems = { new DemoToolbarItem() }
};
contentPageB.Appearing += (s, e) => App.PageLog += $"A(Tab B) ";
contentPageB.Disappearing += (s, e) => App.PageLog += $"D(Tab B) ";
}.AddPageLog();

Children.Add(contentPageA);
Children.Add(contentPageB);
Expand All @@ -43,30 +37,27 @@ public TabbedPageDemoPage()
async void OpenModalPage()
{
var page = new ContentPage {
Title = "Modal",
Content = new StackLayout {
Children = {
new Label{Text = "This is a modal page"},
new Button{Text = "Close", Command = new Command(() => Navigation.PopModalAsync())},
}
}
};
page.Appearing += (s, e) => App.PageLog += $"A(Modal) ";
page.Disappearing += (s, e) => App.PageLog += $"D(Modal) ";
}.AddPageLog();
await Navigation.PushModalAsync(page);
}

async void OpenSubpage()
{
var page = new ContentPage {
Title = "Some Subpage",
Title = "Subpage",
Content = new StackLayout {
Children = {
new Label{Text = "This is a sub page"},
}
}
};
page.Appearing += (s, e) => App.PageLog += $"A(Subpage) ";
page.Disappearing += (s, e) => App.PageLog += $"D(Subpage) ";
}.AddPageLog();
await Navigation.PushAsync(page);
}
}
Expand Down
2 changes: 1 addition & 1 deletion DemoApp/MenuPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ DemoButton CreateMenuButton(string title, Func<Page> pageCreator)
return new DemoButton(title) {
Command = new Command(o => {
var mainPage = (Application.Current.MainPage as FlyoutPage);
mainPage.Detail = new NavigationPage(pageCreator.Invoke());
mainPage.Detail = new NavigationPage(pageCreator.Invoke()).AddPageLog();
mainPage.IsPresented = false;
}),
};
Expand Down
33 changes: 33 additions & 0 deletions DemoApp/PageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using Xamarin.Forms;

namespace DemoApp
{
public static class PageExtensions
{
public static T AddPageLog<T>(this T page) where T : Page
{
page.Appearing += OnAppearing;
page.Disappearing += OnDisappearing;
return page;
}

static void OnAppearing(object sender, EventArgs e)
{
var page = sender as Page;
var logMessage = $"A({GetLogName(page)})";
App.PageLog += $"{logMessage} ";
Console.WriteLine(logMessage);
}

static void OnDisappearing(object sender, EventArgs e)
{
var page = sender as Page;
var logMessage = $"D({GetLogName(page)})";
App.PageLog += $"{logMessage} ";
Console.WriteLine(logMessage);
}

static string GetLogName(Page page) => page.Title ?? page.GetType().Name;
}
}
2 changes: 1 addition & 1 deletion Tests/NavigationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void TestModalPopToRootEvent()
var expectedLog = "A(Navigation) ";

Tap("PushModalAsync NavigationPage");
Assert.That(App.PageLog, Is.EqualTo(expectedLog += "D(Navigation) A(Navigation ^) "));
Assert.That(App.PageLog, Is.EqualTo(expectedLog += "D(Navigation) A(NavigationPage) A(Navigation ^) "));

Tap("PushAsync");
Assert.That(App.PageLog, Is.EqualTo(expectedLog += "D(Navigation ^) A(Navigation ^ >) "));
Expand Down

0 comments on commit a91842b

Please sign in to comment.