Skip to content

Commit

Permalink
Updated isToggled sample
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMisiukevich committed May 21, 2019
1 parent 8b6f285 commit b9cd770
Show file tree
Hide file tree
Showing 22 changed files with 677 additions and 291 deletions.
8 changes: 8 additions & 0 deletions TouchEffect/Delegates/AnimationStartedHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using TouchEffect.EventArgs;
using Xamarin.Forms;

namespace TouchEffect.Delegates
{
public delegate void AnimationStartedHandler(VisualElement sender, AnimationStartedEventArgs args);
}

16 changes: 16 additions & 0 deletions TouchEffect/EventArgs/AnimationStartedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using TouchEffect.Enums;

namespace TouchEffect.EventArgs
{
public class AnimationStartedEventArgs : System.EventArgs
{
public AnimationStartedEventArgs(TouchState state, int duration)
{
State = state;
Duration = duration;
}

public TouchState State { get; }
public int Duration { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using TouchEffect.Enums;
using Xamarin.Forms;

namespace TouchEffect
namespace TouchEffect.Interfaces
{
internal interface ITouchEff
public interface ITouchEff
{
ICommand Command { get; }

Expand Down Expand Up @@ -69,5 +69,7 @@ internal interface ITouchEff
void RaiseStatusChanged();

void RaiseCompleted();

void RaiseAnimationStarted(TouchState state, int duration);
}
}
12 changes: 10 additions & 2 deletions TouchEffect/TouchEff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.ComponentModel;
using System;
using System.Threading.Tasks;
using System.Threading;
using TouchEffect.Interfaces;

namespace TouchEffect
{
Expand All @@ -20,7 +22,7 @@ public TouchEff() : base($"{nameof(TouchEffect)}.{nameof(TouchEff)}")
StateChanged += (sender, args) => ForceUpdateState();
}

internal TouchEff(Func<ITouchEff, TouchState, double?, Task> animationTaskGetter) : this()
internal TouchEff(Func<ITouchEff, TouchState, int, CancellationToken, Task> animationTaskGetter) : this()
=> _visualManager.SetCustomAnimationTask(animationTaskGetter);

public event TEffectStatusChangedHandler StatusChanged;
Expand All @@ -29,6 +31,8 @@ internal TouchEff(Func<ITouchEff, TouchState, double?, Task> animationTaskGetter

public event TEffectCompletedHandler Completed;

public event AnimationStartedHandler AnimationStarted;

public static readonly BindableProperty CommandProperty = BindableProperty.CreateAttached(
nameof(Command),
typeof(ICommand),
Expand Down Expand Up @@ -501,7 +505,11 @@ public void RaiseStatusChanged()
[EditorBrowsable(EditorBrowsableState.Never)]
public void RaiseCompleted()
=> Completed?.Invoke(Control, new TouchCompletedEventArgs(CommandParameter));


[EditorBrowsable(EditorBrowsableState.Never)]
public void RaiseAnimationStarted(TouchState state, int duration)
=> AnimationStarted?.Invoke(Control, new AnimationStartedEventArgs(state, duration));

[EditorBrowsable(EditorBrowsableState.Never)]
public void ForceUpdateState(bool animated = true)
{
Expand Down
1 change: 1 addition & 0 deletions TouchEffect/TouchEffect.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Folder Include="EventArgs\" />
<Folder Include="Enums\" />
<Folder Include="Extensions\" />
<Folder Include="Interfaces\" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Controls\TouchColorView.cs" />
Expand Down
30 changes: 26 additions & 4 deletions TouchEffect/TouchImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using TouchEffect.Extensions;
using Xamarin.Forms;
using System.Threading.Tasks;
using System.Threading;
using TouchEffect.Interfaces;

namespace TouchEffect
{
Expand Down Expand Up @@ -54,6 +56,12 @@ public TouchImage()
bindable.GetTouchEff()?.ForceUpdateState();
});

public static readonly BindableProperty ShouldSetImageOnAnimationEndProperty = BindableProperty.Create(
nameof(ShouldSetImageOnAnimationEnd),
typeof(bool),
typeof(TouchImage),
default(bool));

public ImageSource RegularBackgroundImageSource
{
get => GetValue(RegularBackgroundImageSourceProperty) as ImageSource;
Expand All @@ -78,15 +86,21 @@ public Aspect PressedBackgroundImageAspect
set => SetValue(PressedBackgroundImageAspectProperty, value);
}

private Task GetAnimationTask(ITouchEff sender, TouchState state, double? durationMultiplier = null)
public bool ShouldSetImageOnAnimationEnd
{
get => (bool)GetValue(ShouldSetImageOnAnimationEndProperty);
set => SetValue(ShouldSetImageOnAnimationEndProperty, value);
}

private async Task GetAnimationTask(ITouchEff sender, TouchState state, int duration, CancellationToken token)
{
var regularBackgroundImageSource = RegularBackgroundImageSource;
var pressedBackgroundImageSource = PressedBackgroundImageSource;

if (regularBackgroundImageSource == null &&
pressedBackgroundImageSource == null)
{
return Task.FromResult(false);
return;
}

var aspect = RegularBackgroundImageAspect;
Expand All @@ -97,15 +111,23 @@ private Task GetAnimationTask(ITouchEff sender, TouchState state, double? durati
source = pressedBackgroundImageSource;
}

if(ShouldSetImageOnAnimationEnd)
{
await Task.Delay(duration, token);
}

if(token.IsCancellationRequested)
{
return;
}

lock (_setImageLocker)
{
BatchBegin();
Aspect = aspect;
Source = source;
BatchCommit();
}

return Task.FromResult(true);
}
}
}
7 changes: 7 additions & 0 deletions TouchEffect/TouchView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using TouchEffect.Enums;
using TouchEffect.Extensions;
using System;
using TouchEffect.Interfaces;

namespace TouchEffect
{
Expand All @@ -17,6 +18,8 @@ public class TouchView : AbsoluteLayout, ITouchEff

public event TouchViewCompletedHandler Completed;

public event AnimationStartedHandler AnimationStarted;

public static readonly BindableProperty CommandProperty = BindableProperty.Create(
nameof(Command),
typeof(ICommand),
Expand Down Expand Up @@ -554,5 +557,9 @@ private void ForceStateChanged()
SetBackgroundImage(state);
_visualManager.ChangeStateAsync(this, state, true);
}

public void RaiseAnimationStarted(TouchState state, int duration)
{
}
}
}
Loading

0 comments on commit b9cd770

Please sign in to comment.