Skip to content

Commit

Permalink
Sheet fling fixes (#298)
Browse files Browse the repository at this point in the history
* Seems to work

* Android, top and bottom alignment

* Adjustments

* Deleted cheat sheet

* Removed position changed command and event

* Added MoveTo()

* Commands and Events are invoked.

* Removed Alignment

* Refactor

* iOS fixes

* Refactor

* Added overlay

* Added comments

* Update SheetView.xaml.cs

* Sheet can be pinned if snap points are configured correctly

* IgnoreLocalTime in tests

* Overlay attempt

* Open sheet on scrollable content iOS

* Updated/deleted issue repro

* Added bindable property for intercepting drag gestures on content

* Updated doc

* Better algorithm for finding index of closest snap point

* Update DIPS.Xamarin.UI.Tests.csproj

* Delete PositionEventArgs.cs

* Changed default colors

* Changed sheet title default color

* Added PanAwareContentView to iOS

* Add IsBusy to sheet

* Fixed busy view not appearing

* Removed busy view frame shadow

* Intercept scroll draft iOS

* Toggle scrollable content

* File renamings, langauge version bumps

* Removed OverlayBoxView

* Update SheetContentView.cs

* Android scroll interception

* Made Handle change opacity instead of visibility

* Add slight overshoot to animation and adjusted fling

* Disable drag when translating

* MoveTo() animates change

* Don‘t move sheet if it’s in the correct position already

* Fixed fling bugs

* Removed console.writelines

Co-authored-by: Erling Moxnes Kristiansen <emk@dips.no>
  • Loading branch information
ErlingMK and Erling Moxnes Kristiansen authored Jan 5, 2022
1 parent 8e98c59 commit ed0ef2d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 58 deletions.
3 changes: 0 additions & 3 deletions src/DIPS.Xamarin.UI.Android/SheetContentView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@ public override void RequestDisallowInterceptTouchEvent(bool disallowIntercept)
{
base.RequestDisallowInterceptTouchEvent(disallowIntercept);
m_interceptDisallowed = disallowIntercept;
Console.WriteLine($"DISALLOW INTERCEPT: {disallowIntercept}");
}

public override bool OnInterceptTouchEvent(MotionEvent ev)
{
Console.WriteLine($"INTERCEPTING TOUCH EVENT {ev.ActionMasked}");
switch (ev.ActionMasked)
{
case MotionEventActions.Cancel:
Expand Down Expand Up @@ -130,7 +128,6 @@ public override bool OnInterceptTouchEvent(MotionEvent ev)

public override bool OnTouchEvent(MotionEvent e)
{
Console.WriteLine($"TOUCH EVENT {e.ActionMasked}");
var (x, y) = ToDip(e.RawX, e.RawY);

if (e.ActionMasked is MotionEventActions.Up or MotionEventActions.Cancel) // ended gesture
Expand Down
13 changes: 10 additions & 3 deletions src/DIPS.Xamarin.UI/Controls/Sheet/FlingSensitivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
public enum FlingSensitivity
{
/// <summary>
/// 2500 pixels per second
/// 3500 pixels per second
/// </summary>
Low,

/// <summary>
/// 1250 pixels per second
/// 2000 pixels per second
/// </summary>
Medium,

/// <summary>
/// 500 pixels per second
/// 1000 pixels per second
/// </summary>
High
}

internal static class FlingConstants
{
internal static int s_low = 3500;
internal static int s_medium = 2000;
internal static int s_high = 1000;
}
}
11 changes: 5 additions & 6 deletions src/DIPS.Xamarin.UI/Controls/Sheet/FlingSensitivityConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Globalization;
using DIPS.Xamarin.UI.Internal.Xaml.Sheet;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

Expand Down Expand Up @@ -37,17 +36,17 @@ public override object ConvertFromInvariantString(string value)
switch (result)
{
case FlingSensitivity.Low:
return 2500;
return FlingConstants.s_low;
case FlingSensitivity.Medium:
return 1250;
return FlingConstants.s_medium;
case FlingSensitivity.High:
return 500;
return FlingConstants.s_high;
default:
throw new ArgumentOutOfRangeException();
}
}

return 1250;
return FlingConstants.s_medium;
}
}
}
4 changes: 2 additions & 2 deletions src/DIPS.Xamarin.UI/Controls/Sheet/SheetBehaviorProps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public partial class SheetBehavior
/// <see cref="FlingSpeedThreshold" />
/// </summary>
public static readonly BindableProperty FlingSpeedThresholdProperty =
BindableProperty.Create(nameof(FlingSpeedThreshold), typeof(int), typeof(SheetBehavior), 1250);
BindableProperty.Create(nameof(FlingSpeedThreshold), typeof(int), typeof(SheetBehavior), FlingConstants.s_medium );

/// <summary>
/// <see cref="SheetOpeningStrategy" />
Expand Down Expand Up @@ -390,7 +390,7 @@ public double TitleSize
/// This is a bindable property.
/// </summary>
/// <remarks>
/// Default value is 1250. Unit is pixels per second. Pre-defined values are <see cref="FlingSensitivity.Low" />,
/// Default value is 2000 . Unit is pixels per second. Pre-defined values are <see cref="FlingSensitivity.Low" />,
/// <see cref="FlingSensitivity.Medium" /> and <see cref="FlingSensitivity.High" />
/// </remarks>
[TypeConverter(typeof(FlingSensitivityConverter))]
Expand Down
40 changes: 22 additions & 18 deletions src/DIPS.Xamarin.UI/Internal/Xaml/Sheet/SheetView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,15 @@ public void SendPan(float totalX, float totalY, float distanceX, float distanceY
OnDragStarted();
break;
case GestureStatus.Running:
if (m_isTranslating) return;
if (m_isTranslating)
{
this.CancelAnimations();
}

MoveSheet(distanceY);
RecordDelta(distanceY);
break;
case GestureStatus.Completed:
MoveSheet(distanceY);
OnDragEnded();
break;
case GestureStatus.Canceled:
Expand Down Expand Up @@ -178,7 +181,7 @@ private async Task Minimize(float velocity = 1500)

internal Task Close()
{
return TranslateSheetTo(m_sheetBehaviour.Alignment == AlignmentOptions.Bottom ? Height : -Height, 1750);
return TranslateSheetTo(m_sheetBehaviour.Alignment == AlignmentOptions.Bottom ? Height : -Height);
}

private void SetState(SheetState state)
Expand Down Expand Up @@ -231,20 +234,24 @@ internal async Task Open()

private void OnDragStarted()
{
m_prevDuration = 0;
m_watch = Stopwatch.StartNew();
}

private void OnDragEnded()
private async Task OnDragEnded()
{
m_watch.Stop();

var direction = SheetViewUtility.FindLatestDragDirection(ref m_latestDeltas);

if (TryFling(direction, out var velocity)) { }
else

var (didFling, velocity) = await TryFling(direction);

if (didFling)
{
SnapTo(direction, velocity);
return;
}

await SnapTo(direction, velocity);
}

private async Task SnapTo(DragDirection dragDirection, float velocity)
Expand Down Expand Up @@ -282,6 +289,7 @@ private async Task TranslateSheetTo(double y, float velocity = 1500)

duration = duration > 250 ? 250 : duration < 200 ? 200 : duration;

this.CancelAnimations();
await this.TranslateTo(0, y + (dir ? 5 : -5), (uint)duration, Easing.CubicOut);
await this.TranslateTo(0, y, 225, Easing.CubicOut);

Expand All @@ -299,32 +307,28 @@ private void NotifyPositionChange()
m_sheetBehaviour.Position = 1 - this.YValueToRatio(TranslationY);
}

private bool TryFling(DragDirection dragDirection, out float velocity)
private async Task<(bool, float)> TryFling(DragDirection dragDirection)
{
var (thresholdReached, v) = SheetViewUtility.IsThresholdReached(ref m_latestDeltas, PixelsPerSecond);

velocity = v;

if (thresholdReached)
{
switch (dragDirection)
{
case DragDirection.Up when m_sheetBehaviour.Alignment == AlignmentOptions.Bottom:
case DragDirection.Down when m_sheetBehaviour.Alignment == AlignmentOptions.Top:
Maximize(v);
break;
await Maximize(v);
return (true, v);
case DragDirection.Down when m_sheetBehaviour.Alignment == AlignmentOptions.Bottom:
case DragDirection.Up when m_sheetBehaviour.Alignment == AlignmentOptions.Top:
Minimize(v);
break;
await Minimize(v);
return (true, v);
default:
throw new ArgumentOutOfRangeException(nameof(dragDirection), dragDirection, null);
}

return true;
}

return false;
return (false, v);
}

internal void Initialize()
Expand Down
4 changes: 2 additions & 2 deletions src/DIPS.Xamarin.UI/Internal/Xaml/Sheet/SheetViewUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ internal static (bool, float) IsThresholdReached(ref (float, long)[] latestDelta
latestDeltas = new (float, long)[latestDeltas.Length];

var velocity = Math.Abs(length) / duration * 1000;
if (length < 25) return (false, velocity); // must always travel more than this

if (duration < 60) return (false, velocity); // must always travel more than this

return (velocity >= threshold, velocity);
}
Expand Down
Loading

0 comments on commit ed0ef2d

Please sign in to comment.