Skip to content

Commit

Permalink
Fix exception on constraint, fix modal navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk committed Jan 28, 2025
1 parent cd5b5f6 commit e4902ec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</pages:BasePage.Resources>

<Grid RowSpacing="12"
RowDefinitions="70,20,40,40,40,20,*">
RowDefinitions="70,20,40,40,40,20">
<Label Grid.Row="0"
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."
HorizontalTextAlignment="Justify"
Expand All @@ -43,12 +43,6 @@

<Label Grid.Row="5"
x:Name="SnackbarShownStatus" />

<Button Grid.Row="6"
x:Name="DisplaySnackbarButtonAnchoredToButtonOnBottom"
VerticalOptions="End"
Text="Display Snackbar Anchored to this Button"
Clicked="DisplaySnackbarButtonAnchoredToButtonOnBottomClicked"/>
</Grid>

</pages:BasePage>
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ async void DisplayCustomSnackbarAnchoredToButtonClicked(object? sender, EventArg
throw new NotSupportedException($"{nameof(DisplayCustomSnackbarButtonAnchoredToButton)}.{nameof(ITextButton.Text)} Not Recognized");
}
}

async void DisplaySnackbarButtonAnchoredToButtonOnBottomClicked(object sender, EventArgs e)
{
await DisplaySnackbarButtonAnchoredToButtonOnBottom.DisplaySnackbar(
"This Snackbar is anchored to the button on the bottom of a page. This Snackbar appears above the button to avoid clipping the Snackbar on the bottom of the Page.",
() => DisplaySnackbarButtonAnchoredToButtonOnBottom.BackgroundColor = Colors.Blue,
"Close",
TimeSpan.FromSeconds(5));
}

void Snackbar_Dismissed(object? sender, EventArgs e)
{
Expand All @@ -107,6 +98,20 @@ async void DisplaySnackbarInModalButtonClicked(object? sender, EventArgs e)
{
if (Application.Current?.Windows[0].Page is Page mainPage)
{
var button = new Button()
.CenterHorizontal()
.Text("Display Snackbar");
button.Command = new AsyncRelayCommand(token => button.DisplaySnackbar(
"This Snackbar is anchored to the button on the bottom to avoid clipping the Snackbar on the top of the Page.",
() => { },
"Close",
TimeSpan.FromSeconds(5), token: token));

var backButton = new Button()
.CenterHorizontal()
.Text("Back to Snackbar MainPage");
backButton.Command = new AsyncRelayCommand(mainPage.Navigation.PopModalAsync);

await mainPage.Navigation.PushModalAsync(new ContentPage
{
Content = new VerticalStackLayout
Expand All @@ -115,19 +120,11 @@ await mainPage.Navigation.PushModalAsync(new ContentPage

Children =
{
new Button { Command = new AsyncRelayCommand(static token => Snackbar.Make("Snackbar in a Modal MainPage").Show(token)) }
.Top().CenterHorizontal()
.Text("Display Snackbar"),

new Label()
.Center().TextCenter()
.Text("This is a Modal MainPage"),
button,

new Button { Command = new AsyncRelayCommand(mainPage.Navigation.PopModalAsync) }
.Bottom().CenterHorizontal()
.Text("Back to Snackbar MainPage")
backButton
}
}.Center()
}
}.Padding(12));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class AlertView(bool shouldFillAndExpandHorizontally) : UIView
{
const int defaultSpacing = 10;
readonly List<UIView> children = [];
readonly bool shouldFillAndExpandHorizontally = shouldFillAndExpandHorizontally;

/// <summary>
/// Parent UIView
Expand Down Expand Up @@ -71,7 +70,7 @@ public void Setup()
public override void LayoutSubviews()
{
base.LayoutSubviews();

if (AnchorView is null)
{
this.SafeBottomAnchor().ConstraintEqualTo(ParentView.SafeBottomAnchor(), -defaultSpacing).Active = true;
Expand All @@ -80,11 +79,12 @@ public override void LayoutSubviews()
else if (AnchorView.Superview is not null
&& AnchorView.Superview.ConvertRectToView(AnchorView.Frame, null).Top < Container.Frame.Height + SafeAreaLayoutGuide.LayoutFrame.Bottom)
{
this.SafeTopAnchor().ConstraintEqualTo(AnchorView.SafeBottomAnchor(), defaultSpacing).Active = true;
var top = AnchorView.Superview.Frame.Top + AnchorView.Frame.Height + defaultSpacing;
this.SafeTopAnchor().ConstraintEqualTo(ParentView.TopAnchor, top).Active = true;
}
else
{
this.SafeBottomAnchor().ConstraintEqualTo(AnchorView.SafeTopAnchor(), -defaultSpacing).Active = true;
this.SafeBottomAnchor().ConstraintEqualTo(AnchorView.SafeTopAnchor(), 0).Active = true;
}
}

Expand Down

0 comments on commit e4902ec

Please sign in to comment.