Skip to content

Commit

Permalink
Added OnBeforeOpen/Close events
Browse files Browse the repository at this point in the history
  • Loading branch information
haavamoa committed Jan 23, 2020
1 parent c0f4420 commit a62c68b
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 68 deletions.
153 changes: 97 additions & 56 deletions src/DIPS.Xamarin.UI/Controls/Sheet/SheetBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public class SheetBehavior : Behavior<ModalityLayout>, IModalityHandler

private SheetView? m_sheetView;

/// <summary>
/// <see cref="OnBeforeOpenCommand"/>
/// </summary>
public static readonly BindableProperty OnBeforeOpenCommandProperty = BindableProperty.Create(nameof(OnBeforeOpenCommand), typeof(ICommand), typeof(SheetBehavior));

/// <summary>
/// <see cref="OnBeforeOpenCommandParameter"/>
/// </summary>
public static readonly BindableProperty OnBeforeOpenCommandParameterProperty = BindableProperty.Create(nameof(OnBeforeOpenCommandParameter), typeof(object), typeof(SheetBehavior));

/// <summary>
/// <see cref="OnOpenCommand"/>
/// </summary>
Expand All @@ -37,6 +47,16 @@ public class SheetBehavior : Behavior<ModalityLayout>, IModalityHandler
typeof(object),
typeof(SheetBehavior));

/// <summary>
/// <see cref="OnBeforeCloseCommand"/>
/// </summary>
public static readonly BindableProperty OnBeforeCloseCommandProperty = BindableProperty.Create(nameof(OnBeforeCloseCommand), typeof(ICommand), typeof(SheetBehavior));

/// <summary>
/// <see cref="OnBeforeCloseCommandParameter"/>
/// </summary>
public static readonly BindableProperty OnBeforeCloseCommandParameterProperty = BindableProperty.Create(nameof(OnBeforeCloseCommandParameter), typeof(object), typeof(SheetBehavior));

/// <summary>
/// <see cref="OnCloseCommand"/>
/// </summary>
Expand Down Expand Up @@ -89,7 +109,13 @@ public class SheetBehavior : Behavior<ModalityLayout>, IModalityHandler
nameof(SheetContent),
typeof(View),
typeof(SheetView),
new ContentView() { HeightRequest = 100, VerticalOptions = LayoutOptions.Start });
new ContentView() { HeightRequest = 100, VerticalOptions = LayoutOptions.Start }, propertyChanged:OnSheetContentPropertyChanged);

private static void OnSheetContentPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
{
if (!(bindable is SheetBehavior sheetBehavior)) return;
sheetBehavior.UpdateBindingContextForSheetContent();
}

/// <summary>
/// <see cref="BackgroundColor" />
Expand Down Expand Up @@ -165,22 +191,7 @@ public class SheetBehavior : Behavior<ModalityLayout>, IModalityHandler
typeof(SheetBehavior),
ColorPalette.QuinaryAir);

/// <summary>
/// <see cref="DrawOptions"/>
/// </summary>
public static readonly BindableProperty DrawOptionsProperty = BindableProperty.Create(nameof(DrawOptions), typeof(SheetContentDrawOptions), typeof(SheetBehavior));

/// <summary>
/// Determines when the sheet content should be drawn on the screen.
/// </summary>
public SheetContentDrawOptions DrawOptions
{
get => (SheetContentDrawOptions)GetValue(DrawOptionsProperty);
set => SetValue(DrawOptionsProperty, value);
}

private bool m_fromIsDraggingContext;
private bool m_isLazyLoadingView;

/// <summary>
/// Determines the position of the sheet when it appears.
Expand Down Expand Up @@ -254,12 +265,38 @@ public bool IsOpen
}

/// <summary>
/// Event that gets raised when the sheet has completed it's animation and is open
/// Event that gets raised when the sheet is about to start it's animation to open.
/// </summary>
public event EventHandler? OnBeforeOpen;

/// <summary>
/// Command that execute when the sheet is about to start it's animation to open.
/// This is a bindable property.
/// </summary>
public ICommand OnBeforeOpenCommand
{
get => (ICommand)GetValue(OnBeforeOpenCommandProperty);
set => SetValue(OnBeforeOpenCommandProperty, value);
}

/// <summary>
/// Parameter to pass to <see cref="OnBeforeOpenCommand"/>.
/// This is a bindable property.
/// </summary>
public object OnBeforeOpenCommandParameter
{
get => (object)GetValue(OnBeforeOpenCommandParameterProperty);
set => SetValue(OnBeforeOpenCommandParameterProperty, value);
}

/// <summary>
/// Event that gets raised when the sheet has completed it's animation and is open.
/// </summary>
public event EventHandler? OnOpen;

/// <summary>
/// Command that executes when the sheet has completed it's animation and is open.
/// This is a bindable property.
/// </summary>
public ICommand OnOpenCommand
{
Expand All @@ -269,6 +306,7 @@ public ICommand OnOpenCommand

/// <summary>
/// The parameter to pass to the <see cref="OnOpenCommand"/>.
/// This is a bindable property.
/// </summary>
public object OnOpenCommandParameter
{
Expand All @@ -277,12 +315,38 @@ public object OnOpenCommandParameter
}

/// <summary>
/// Event that gets raised when the sheet has completed it's animation and is closed
/// Event that gets raised before the sheet start it's animation when closing
/// </summary>
public event EventHandler? OnBeforeClose;

/// <summary>
/// Command that execute before the sheet start it's animation when closing.
/// This is a bindable property.
/// </summary>
public ICommand OnBeforeCloseCommand
{
get => (ICommand)GetValue(OnBeforeCloseCommandProperty);
set => SetValue(OnBeforeCloseCommandProperty, value);
}

/// <summary>
/// The parameter to pass to <see cref="OnBeforeCloseCommand"/>.
/// This is a bindable property.
/// </summary>
public object OnBeforeCloseCommandParameter
{
get => (object)GetValue(OnBeforeCloseCommandParameterProperty);
set => SetValue(OnBeforeCloseCommandParameterProperty, value);
}

/// <summary>
/// Event that gets raised when the sheet has completed it's animation and is closed.
/// </summary>
public event EventHandler? OnClose;

/// <summary>
/// Command that executes when the sheet has completed it's animation and is closed.
/// This is a bindable property.
/// </summary>
public ICommand OnCloseCommand
{
Expand Down Expand Up @@ -342,7 +406,7 @@ public double Position
/// This is a bindable property.
/// <remarks><see cref="BindingContextFactory" /> to set the binding context when the sheet opens</remarks>
/// </summary>
public View SheetContent
public View? SheetContent
{
get => (View)GetValue(SheetContentProperty);
set => SetValue(SheetContentProperty, value);
Expand Down Expand Up @@ -418,19 +482,14 @@ private async void ToggleSheetVisibility()
return;
}

m_isLazyLoadingView = true;

if (IsOpen)
{
m_sheetView = new SheetView(this);

if (DrawOptions == SheetContentDrawOptions.BeforeOpen)
{
m_sheetView.SheetContentView.Content = SheetContent;
}
OnBeforeOpenCommand?.Execute(OnBeforeOpenCommandParameter);
OnBeforeOpen?.Invoke(this, new EventArgs());

m_sheetView = new SheetView(this);
m_sheetView.Initialize();
SheetContent.BindingContext = BindingContextFactory?.Invoke() ?? BindingContext;
UpdateBindingContextForSheetContent();

//Set height / width
var widthConstraint = Constraint.RelativeToParent(r => m_modalityLayout.Width);
Expand All @@ -451,32 +510,23 @@ private async void ToggleSheetVisibility()
//Set position based on size of content
if (Position <= 0)
{
if (DrawOptions == SheetContentDrawOptions.OnOpen)
{
Position = MinPosition;
}
else
{
//Calculate what size the content needs if the position is set to 0
//Calculate what size the content needs if the position is set to 0
Position = m_sheetView.SheetContentHeightRequest / m_modalityLayout.Height;
}
}
else //Set position from input
{
await TranslateBasedOnPosition(false);
OnOpenCommand?.Execute(OnOpenCommandParameter);
OnOpen?.Invoke(this, new EventArgs());
}

if (DrawOptions == SheetContentDrawOptions.OnOpen)
{
m_sheetView.SheetContentView.Content = SheetContent;
}
}
else
{
if (m_sheetView == null) return;

OnBeforeCloseCommand?.Execute(OnBeforeCloseCommandParameter);
OnBeforeClose?.Invoke(this, new EventArgs());

var y = Alignment switch {
AlignmentOptions.Bottom => m_modalityLayout.Height,
AlignmentOptions.Top => -m_modalityLayout.Height,
Expand All @@ -492,6 +542,12 @@ private async void ToggleSheetVisibility()
m_fromIsOpenContext = false;
}

private void UpdateBindingContextForSheetContent()
{
if(SheetContent == null) return;
SheetContent.BindingContext = BindingContextFactory?.Invoke() ?? BindingContext;
}

private async Task TranslateBasedOnPosition(bool shouldExecuteOpenedCommand = true)
{
if (!IsOpen) return;
Expand Down Expand Up @@ -595,19 +651,4 @@ public enum ContentAlignment
/// </summary>
Fill
}

/// <summary>
/// The sheet content draw options
/// </summary>
public enum SheetContentDrawOptions
{
/// <summary>
/// Draw the sheet content before the sheet is open, this happens the moment the sheet starts to open
/// </summary>
BeforeOpen = 0,
/// <summary>
/// Draw the sheet content when the sheet is open, this happens after the sheet has animated to it's position
/// </summary>
OnOpen
}
}
3 changes: 2 additions & 1 deletion src/DIPS.Xamarin.UI/Internal/Xaml/SheetView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<ContentView x:Name="sheetContentView" />
<ContentView x:Name="sheetContentView"
Content="{Binding SheetContent}"/>
</Grid>
</Grid>
</Frame>
Expand Down
14 changes: 3 additions & 11 deletions src/Samples/DIPS.Xamarin.UI.Samples/Controls/Sheet/SheetPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,9 @@
MinPosition="{Binding MinPosition}"
MaxPosition="{Binding MaxPosition}"
BindingContextFactory="{Binding SheetViewModelFactory}"
DrawOptions="OnOpen">
<StackLayout>
<Label Text="{Binding Source={x:Reference entry}, Path=Text}" />
<Label Text="{Binding Source={x:Reference entry}, Path=Text}" />
<Label Text="{Binding Source={x:Reference entry}, Path=Text}" />
<Label Text="{Binding Source={x:Reference entry}, Path=Text}" />
<Label Text="{Binding Source={x:Reference entry}, Path=Text}" />
<Entry x:Name="entry" Placeholder="TextEntry" />
<Button Text="Close sheet"
dxui:Modality.CloseOnClick="True" />
</StackLayout>
OnOpen="SheetBehavior_OnOnOpen"
OnClose="SheetBehavior_OnOnClose"
>
</dxui:SheetBehavior>
</dxui:ModalityLayout.Behaviors>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ public SheetPage()
{
InitializeComponent();
}

private void SheetBehavior_OnOnOpen(object sender, EventArgs e)
{
if (!(sender is SheetBehavior sheetBehavior)) return;
var label = new Label() { };
label.SetBinding(Label.TextProperty, "Title");
sheetBehavior.SheetContent = label;
}

private void SheetBehavior_OnOnClose(object sender, EventArgs e)
{
if (!(sender is SheetBehavior sheetBehavior)) return;
sheetBehavior.SheetContent = null;
}
}

public class SheetPageViewModel : INotifyPropertyChanged
Expand Down

0 comments on commit a62c68b

Please sign in to comment.