Skip to content

Commit

Permalink
Updated StatusBarBehavior to use the NavigatedTo event (#1471)
Browse files Browse the repository at this point in the history
* Updated StatusBarBehavior to use the NavigatedTo event for setting the color and style and updated the Sample app.

* Added fact to verify expected exception on NavigatedTo event.

* Update src/CommunityToolkit.Maui.UnitTests/Behaviors/StatusBarBehaviorTests.cs

It wasn't inside the if before so forgot to remove it after wrapping it :)

Co-authored-by: Pedro Jesus <[email protected]>

* Removed unit test I do not know how to trigger OnAttachedTo without using reflection.

* Added enum StatusBarApplyOn to control when the StatusBarBehavior applies its configured color and style.

* Updated sample to use the new enum

---------

Co-authored-by: Mikael Nensén <[email protected]>
Co-authored-by: Pedro Jesus <[email protected]>
Co-authored-by: Shaun Lawrence <[email protected]>
  • Loading branch information
4 people authored Feb 25, 2024
1 parent 7611c28 commit 9d3e2fb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Maui.Sample.ViewModels.Behaviors;
using CommunityToolkit.Maui.Behaviors;
using CommunityToolkit.Maui.Sample.ViewModels.Behaviors;

namespace CommunityToolkit.Maui.Sample.Pages.Behaviors;

Expand All @@ -7,5 +8,18 @@ public class BehaviorsGalleryPage : BaseGalleryPage<BehaviorsGalleryViewModel>
public BehaviorsGalleryPage(IDeviceInfo deviceInfo, BehaviorsGalleryViewModel behaviorsGalleryViewModel)
: base("Behaviors", deviceInfo, behaviorsGalleryViewModel)
{
#if ANDROID || IOS
AddStatusBarBehavior();
#endif
}

void AddStatusBarBehavior()
{
Behaviors.Add(new StatusBarBehavior
{
StatusBarColor = Color.FromRgb(25, 118, 210),
StatusBarStyle = Core.StatusBarStyle.LightContent,
ApplyOn = StatusBarApplyOn.OnPageNavigatedTo,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@

<Label Text="Select StatusBar and NavigationBar style" />

<RadioButton VerticalOptions="Center" IsChecked="{Binding IsDefaultChecked}">
<RadioButton.Content>
<Label Text="Default" Margin="10,0,0,0" VerticalTextAlignment="Center" VerticalOptions="Center" />
</RadioButton.Content>
</RadioButton>
<RadioButton
Content="Default"
IsChecked="{Binding IsDefaultChecked}"
VerticalOptions="Center" />

<RadioButton VerticalOptions="Center" IsChecked="{Binding IsLightContentChecked}">
<RadioButton.Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@

using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Core.Platform;
using CommunityToolkit.Maui.Extensions;

namespace CommunityToolkit.Maui.Behaviors;

/// <summary>
/// When to apply the status bar color and style.
/// </summary>
public enum StatusBarApplyOn
{
/// <summary>
/// Apply color and style when the behavior has been attached to the page.
/// </summary>
OnBehaviorAttachedTo,

/// <summary>
/// Apply color and style when the page has been navigated to.
/// </summary>
OnPageNavigatedTo,
}

/// <summary>
/// <see cref="PlatformBehavior{TView,TPlatformView}"/> that controls the Status bar color
/// </summary>
Expand All @@ -19,13 +34,18 @@ public class StatusBarBehavior : PlatformBehavior<Page>
public static readonly BindableProperty StatusBarColorProperty =
BindableProperty.Create(nameof(StatusBarColor), typeof(Color), typeof(StatusBarBehavior), Colors.Transparent);


/// <summary>
/// <see cref="BindableProperty"/> that manages the StatusBarColor property.
/// </summary>
public static readonly BindableProperty StatusBarStyleProperty =
BindableProperty.Create(nameof(StatusBarStyle), typeof(StatusBarStyle), typeof(StatusBarBehavior), StatusBarStyle.Default);

/// <summary>
/// <see cref="BindableProperty"/> that manages the ApplyOn property.
/// </summary>
public static readonly BindableProperty ApplyOnProperty =
BindableProperty.Create(nameof(ApplyOn), typeof(StatusBarApplyOn), typeof(StatusBarBehavior), StatusBarApplyOn.OnBehaviorAttachedTo);

/// <summary>
/// Property that holds the value of the Status bar color.
/// </summary>
Expand All @@ -44,6 +64,16 @@ public StatusBarStyle StatusBarStyle
set => SetValue(StatusBarStyleProperty, value);
}

/// <summary>
/// When the status bar color and style should be applied.
/// </summary>
public StatusBarApplyOn ApplyOn
{
get => (StatusBarApplyOn)GetValue(ApplyOnProperty);
set => SetValue(ApplyOnProperty, value);
}


#if !(WINDOWS || MACCATALYST || TIZEN)

/// <inheritdoc />
Expand All @@ -55,28 +85,49 @@ protected override void OnAttachedTo(Page bindable, Android.Views.View platformV
protected override void OnAttachedTo(Page bindable, object platformView)
#endif
{
StatusBar.SetColor(StatusBarColor);
StatusBar.SetStyle(StatusBarStyle);
if (ApplyOn == StatusBarApplyOn.OnBehaviorAttachedTo)
{
StatusBar.SetColor(StatusBarColor);
StatusBar.SetStyle(StatusBarStyle);
}

bindable.NavigatedTo += OnPageNavigatedTo;
#if IOS
bindable.SizeChanged += new EventHandler(page_SizeChanged);
bindable.SizeChanged += OnPageSizeChanged;
#endif
}

#if IOS
/// <inheritdoc />
#if IOS
protected override void OnDetachedFrom(Page bindable, UIKit.UIView platformView)
#elif ANDROID
protected override void OnDetachedFrom(Page bindable, Android.Views.View platformView)
#else
protected override void OnDetachedFrom(Page bindable, object platformView)
#endif
{
bindable.SizeChanged -= new EventHandler(page_SizeChanged);
}
#if IOS
bindable.SizeChanged -= OnPageSizeChanged;
#endif
bindable.NavigatedTo -= OnPageNavigatedTo;
}

#if IOS
void page_SizeChanged(object? sender, EventArgs e)
void OnPageSizeChanged(object? sender, EventArgs e)
{
StatusBar.UpdateBarSize();
}
#endif

void OnPageNavigatedTo(object? sender, NavigatedToEventArgs e)
{
if (ApplyOn == StatusBarApplyOn.OnPageNavigatedTo)
{
StatusBar.SetColor(StatusBarColor);
StatusBar.SetStyle(StatusBarStyle);
}
}

/// <inheritdoc />
protected override void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
Expand Down

0 comments on commit 9d3e2fb

Please sign in to comment.