Skip to content

Commit

Permalink
Update variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
brminnick committed Jan 22, 2025
1 parent 16919f7 commit a7f0084
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
</pages:BasePage.Resources>

<VerticalStackLayout Spacing="12">
<Button x:Name="DisplayCustomSnackbarButton2"
Text="Display Custom Snackbar"
Clicked="DisplayCustomSnackbarButtonClicked2"
TextColor="{Binding Source={RelativeSource Self}, Path=BackgroundColor, Converter={StaticResource ColorToColorForTextConverter}, x:DataType=Button}"/>


<Label Text="The Snackbar is a timed alert that appears at the bottom of the screen by default. It is dismissed after a configurable duration of time. Snackbar is fully customizable and can be anchored to any IView."
LineBreakMode = "WordWrap" />

Expand All @@ -31,7 +25,12 @@
Text="Display Default Snackbar"/>

<Button x:Name="DisplayCustomSnackbarButton"
Text="Display Custom Snackbar"
Clicked="DisplayCustomSnackbarButtonClicked"
TextColor="{Binding Source={RelativeSource Self}, Path=BackgroundColor, Converter={StaticResource ColorToColorForTextConverter}, x:DataType=Button}"/>

<Button x:Name="DisplayCustomSnackbarButtonAnchoredToButton"
Clicked="DisplayCustomSnackbarAnchoredToButtonClicked"
TextColor="{Binding Source={RelativeSource Self}, Path=BackgroundColor, Converter={StaticResource ColorToColorForTextConverter}, x:DataType=Button}"/>

<Button x:Name="DisplaySnackbarInModalButton"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public SnackbarPage(SnackbarViewModel snackbarViewModel) : base(snackbarViewMode
{
InitializeComponent();

DisplayCustomSnackbarButton.Text = displayCustomSnackbarText;
DisplayCustomSnackbarButtonAnchoredToButton.Text = displayCustomSnackbarText;

Snackbar.Shown += Snackbar_Shown;
Snackbar.Dismissed += Snackbar_Dismissed;
Expand All @@ -34,9 +34,9 @@ public SnackbarPage(SnackbarViewModel snackbarViewModel) : base(snackbarViewMode
async void DisplayDefaultSnackbarButtonClicked(object? sender, EventArgs args) =>
await this.DisplaySnackbar("This is a Snackbar.\nIt will disappear in 3 seconds.\nOr click OK to dismiss immediately");

async void DisplayCustomSnackbarButtonClicked(object? sender, EventArgs args)
async void DisplayCustomSnackbarAnchoredToButtonClicked(object? sender, EventArgs args)
{
if (DisplayCustomSnackbarButton.Text is displayCustomSnackbarText)
if (DisplayCustomSnackbarButtonAnchoredToButton.Text is displayCustomSnackbarText)
{
var options = new SnackbarOptions
{
Expand All @@ -52,20 +52,20 @@ async void DisplayCustomSnackbarButtonClicked(object? sender, EventArgs args)
"This is a customized Snackbar",
async () =>
{
await DisplayCustomSnackbarButton.BackgroundColorTo(colors[Random.Shared.Next(colors.Count)], length: 500);
DisplayCustomSnackbarButton.Text = displayCustomSnackbarText;
await DisplayCustomSnackbarButtonAnchoredToButton.BackgroundColorTo(colors[Random.Shared.Next(colors.Count)], length: 500);
DisplayCustomSnackbarButtonAnchoredToButton.Text = displayCustomSnackbarText;
},
FontAwesomeIcons.Microsoft,
TimeSpan.FromSeconds(30),
options,
DisplayCustomSnackbarButton);
DisplayCustomSnackbarButtonAnchoredToButton);

var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
await customSnackbar.Show(cts.Token);

DisplayCustomSnackbarButton.Text = dismissCustomSnackbarText;
DisplayCustomSnackbarButtonAnchoredToButton.Text = dismissCustomSnackbarText;
}
else if (DisplayCustomSnackbarButton.Text is dismissCustomSnackbarText)
else if (DisplayCustomSnackbarButtonAnchoredToButton.Text is dismissCustomSnackbarText)
{
if (customSnackbar is not null)
{
Expand All @@ -75,14 +75,15 @@ async void DisplayCustomSnackbarButtonClicked(object? sender, EventArgs args)
customSnackbar.Dispose();
}

DisplayCustomSnackbarButton.Text = displayCustomSnackbarText;
DisplayCustomSnackbarButtonAnchoredToButton.Text = displayCustomSnackbarText;
}
else
{
throw new NotSupportedException($"{nameof(DisplayCustomSnackbarButton)}.{nameof(ITextButton.Text)} Not Recognized");
throw new NotSupportedException($"{nameof(DisplayCustomSnackbarButtonAnchoredToButton)}.{nameof(ITextButton.Text)} Not Recognized");
}
}
async void DisplayCustomSnackbarButtonClicked2(object sender, EventArgs e)

async void DisplayCustomSnackbarButtonClicked(object sender, EventArgs e)
{
var options = new SnackbarOptions
{
Expand All @@ -94,9 +95,10 @@ async void DisplayCustomSnackbarButtonClicked2(object sender, EventArgs e)
CornerRadius = new CornerRadius(10),
Font = Font.SystemFontOfSize(14),
};
await DisplayCustomSnackbarButton2.DisplaySnackbar(

await DisplayCustomSnackbarButton.DisplaySnackbar(
"This is a customized Snackbar",
() => DisplayCustomSnackbarButton2.BackgroundColor = Colors.Blue,
() => DisplayCustomSnackbarButton.BackgroundColor = Colors.Blue,
"Close",
TimeSpan.FromSeconds(5),
options);
Expand Down
6 changes: 3 additions & 3 deletions src/CommunityToolkit.Maui.Core/Views/Alert/Alert.macios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public void Dismiss()
/// <summary>
/// Show the <see cref="Alert"/> on the screen
/// </summary>
/// <param name="stretch">Should stretch alert container horizontally</param>
public void Show(bool stretch = false)
/// <param name="shouldFillAndExpandHorizontally">Should stretch container horizontally to fit the screen</param>
public void Show(bool shouldFillAndExpandHorizontally = false)
{
AlertView.AnchorView = Anchor;

AlertView.Setup(stretch);
AlertView.Setup(shouldFillAndExpandHorizontally);

timer = NSTimer.CreateScheduledTimer(Duration, t =>
{
Expand Down
35 changes: 21 additions & 14 deletions src/CommunityToolkit.Maui.Core/Views/Alert/AlertView.macios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ public class AlertView : UIView
/// PopupView Children
/// </summary>
public IReadOnlyList<UIView> Children => children;

/// <summary>
/// <see cref="UIView"/> on which Alert will appear. When null, <see cref="AlertView"/> will appear at bottom of screen.
/// <see cref="AlertViewVisualOptions"/>
/// </summary>
public UIView? AnchorView { get; set; }
public AlertViewVisualOptions VisualOptions { get; } = new();

/// <summary>
/// <see cref="AlertViewVisualOptions"/>
/// <see cref="UIView"/> on which Alert will appear. When null, <see cref="AlertView"/> will appear at bottom of screen.
/// </summary>
public AlertViewVisualOptions VisualOptions { get; } = new();
public UIView? AnchorView { get; set; }

/// <summary>
/// Container of <see cref="AlertView"/>
Expand All @@ -50,27 +50,31 @@ public class AlertView : UIView
/// <summary>
/// Initializes <see cref="AlertView"/>
/// </summary>
/// <param name="stretch">Should stretch container horizontally</param>
public void Setup(bool stretch = false)
/// <param name="shouldFillAndExpandHorizontally">Should stretch container horizontally to fit the screen</param>
public void Setup(bool shouldFillAndExpandHorizontally = false)
{
Initialize();
ConstraintInParent(stretch);
ConstraintInParent(shouldFillAndExpandHorizontally);
}

/// <inheritdoc />
public override void LayoutSubviews()
{
base.LayoutSubviews();
_ = Container ?? throw new InvalidOperationException($"{nameof(AlertView)}.{nameof(Initialize)} not called");

if (Container is null)
{
throw new InvalidOperationException($"{nameof(AlertView)}.{nameof(Initialize)} must be called before {nameof(LayoutSubviews)}");
}

if (AnchorView is null)
{
this.SafeBottomAnchor().ConstraintEqualTo(ParentView.SafeBottomAnchor(), -defaultSpacing).Active = true;
this.SafeTopAnchor().ConstraintGreaterThanOrEqualTo(ParentView.SafeTopAnchor(), defaultSpacing).Active = true;
}
else
else if (AnchorView.Superview is not null)
{
var anchorViewPosition = AnchorView.Superview!.ConvertRectToView(AnchorView.Frame, null);
var anchorViewPosition = AnchorView.Superview.ConvertRectToView(AnchorView.Frame, null);
if (anchorViewPosition.Top < Container.Frame.Height + SafeAreaLayoutGuide.LayoutFrame.Bottom)
{
this.SafeTopAnchor().ConstraintEqualTo(AnchorView.SafeBottomAnchor(), defaultSpacing).Active = true;
Expand All @@ -82,11 +86,14 @@ public override void LayoutSubviews()
}
}

void ConstraintInParent(bool stretch)
void ConstraintInParent(bool shouldFillAndExpandHorizontally)
{
_ = Container ?? throw new InvalidOperationException($"{nameof(AlertView)}.{nameof(Initialize)} not called");
if (Container is null)
{
throw new InvalidOperationException($"{nameof(AlertView)}.{nameof(Initialize)} must be called before {nameof(LayoutSubviews)}");
}

if (stretch)
if (shouldFillAndExpandHorizontally)
{
this.SafeLeadingAnchor().ConstraintEqualTo(ParentView.SafeLeadingAnchor(), defaultSpacing).Active = true;
this.SafeTrailingAnchor().ConstraintEqualTo(ParentView.SafeTrailingAnchor(), -defaultSpacing).Active = true;
Expand Down

0 comments on commit a7f0084

Please sign in to comment.