diff --git a/sample/MauiShakeDetectorSample/MainPage.xaml b/sample/MauiShakeDetectorSample/MainPage.xaml
index 07afb4c..c9f6ef4 100644
--- a/sample/MauiShakeDetectorSample/MainPage.xaml
+++ b/sample/MauiShakeDetectorSample/MainPage.xaml
@@ -36,6 +36,14 @@
Clicked="BtnStartListening_Clicked"
HorizontalOptions="Center" />
+
+
diff --git a/sample/MauiShakeDetectorSample/MainPage.xaml.cs b/sample/MauiShakeDetectorSample/MainPage.xaml.cs
index 152d97c..0a43b84 100644
--- a/sample/MauiShakeDetectorSample/MainPage.xaml.cs
+++ b/sample/MauiShakeDetectorSample/MainPage.xaml.cs
@@ -1,4 +1,5 @@
using MauiShakeDetector;
+using System.Diagnostics;
namespace MauiShakeDetectorSample;
@@ -9,8 +10,6 @@ public MainPage()
{
InitializeComponent();
}
-
- bool isListening = false;
private void BtnStartListening_Clicked(object sender, EventArgs e)
{
if (!ShakeDetector.Default.IsSupported)
@@ -23,27 +22,27 @@ private void BtnStartListening_Clicked(object sender, EventArgs e)
TxtShakeStatus.Text = "Shake Detector is ALready Running";
return;
}
- if (!isListening)
- {
- BtnStartListening.Text = "Stop Listening";
- isListening = true;
-
- ShakeDetector.Default.StartListening();
- ShakeDetector.Default.ShakeDetected += Detector_ShakeDetected;
- return;
- }
- BtnStartListening.Text = "Start Listening";
- isListening = false;
- ShakeDetector.Default.StopListening();
- ShakeDetector.Default.ShakeDetected -= Detector_ShakeDetected;
- TxtShakeStatus.Text = "Shake Detector is Stop Listening";
+ TxtShakeStatus.Text = "Started Listening";
+ //ShakeDetector.Default.AutoStopAfterNoShakes = 5;
+ ShakeDetector.Default.StartListening();
+ ShakeDetector.Default.ShakeDetected += Detector_ShakeDetected;
}
private void Detector_ShakeDetected(object sender, ShakeDetectedEventArgs e)
{
- TxtShakeStatus.Text += $" and No of Shakes {e.NoOfShakes}";
+ TxtShakeStatus.Text = $"No of Shakes {e.NoOfShakes}";
}
+ private void BtnStopListening_Clicked(object sender, EventArgs e)
+ {
+
+ if(ShakeDetector.Default.IsMonitoring)
+ {
+ ShakeDetector.Default.StopListening();
+ ShakeDetector.Default.ShakeDetected -= Detector_ShakeDetected;
+ TxtShakeStatus.Text = "Shake Detector is Stop Listening";
+ }
+ }
}
diff --git a/src/MauiShakeDetector.UnitTest/MauiShakeDetector.UnitTest.csproj b/src/MauiShakeDetector.UnitTest/MauiShakeDetector.UnitTest.csproj
new file mode 100644
index 0000000..ee17e1b
--- /dev/null
+++ b/src/MauiShakeDetector.UnitTest/MauiShakeDetector.UnitTest.csproj
@@ -0,0 +1,30 @@
+
+
+
+ net7.0
+ enable
+ enable
+
+ false
+ true
+
+
+
+
+
+
+
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+ all
+
+
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+ all
+
+
+
+
+
+
+
+
diff --git a/src/MauiShakeDetector.UnitTest/ShakeDetectorDefaultTest.cs b/src/MauiShakeDetector.UnitTest/ShakeDetectorDefaultTest.cs
new file mode 100644
index 0000000..57089c4
--- /dev/null
+++ b/src/MauiShakeDetector.UnitTest/ShakeDetectorDefaultTest.cs
@@ -0,0 +1,5 @@
+namespace MauiShakeDetector.UnitTest;
+public class ShakeDetectorDefaultTest
+{
+
+}
diff --git a/src/MauiShakeDetector.UnitTest/ShakeEventArgsTest.cs b/src/MauiShakeDetector.UnitTest/ShakeEventArgsTest.cs
new file mode 100644
index 0000000..a78cd12
--- /dev/null
+++ b/src/MauiShakeDetector.UnitTest/ShakeEventArgsTest.cs
@@ -0,0 +1,14 @@
+using FluentAssertions;
+
+namespace MauiShakeDetector.UnitTest;
+public class ShakeEventArgsTest
+{
+ [Theory]
+ [InlineData(1)]
+ [InlineData(2)]
+ public void NoOfShakesShouldBeEqualInShakeDetectedEventArgs(int noOfShakes)
+ {
+ var eventArgs = new ShakeDetectedEventArgs(noOfShakes);
+ eventArgs.NoOfShakes.Should().Be(noOfShakes);
+ }
+}
diff --git a/src/MauiShakeDetector.UnitTest/Usings.cs b/src/MauiShakeDetector.UnitTest/Usings.cs
new file mode 100644
index 0000000..8c927eb
--- /dev/null
+++ b/src/MauiShakeDetector.UnitTest/Usings.cs
@@ -0,0 +1 @@
+global using Xunit;
\ No newline at end of file
diff --git a/src/MauiShakeDetector.sln b/src/MauiShakeDetector.sln
index 02a97bc..aa8fca7 100644
--- a/src/MauiShakeDetector.sln
+++ b/src/MauiShakeDetector.sln
@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MauiShakeDetector", "MauiSh
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MauiShakeDetectorSample", "..\sample\MauiShakeDetectorSample\MauiShakeDetectorSample.csproj", "{A7020180-920D-42F8-93FB-BA36927F8B37}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MauiShakeDetector.UnitTest", "MauiShakeDetector.UnitTest\MauiShakeDetector.UnitTest.csproj", "{9BB800E4-ACE7-49DB-BDA9-6626AA36F0DF}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -23,6 +25,10 @@ Global
{A7020180-920D-42F8-93FB-BA36927F8B37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7020180-920D-42F8-93FB-BA36927F8B37}.Release|Any CPU.Build.0 = Release|Any CPU
{A7020180-920D-42F8-93FB-BA36927F8B37}.Release|Any CPU.Deploy.0 = Release|Any CPU
+ {9BB800E4-ACE7-49DB-BDA9-6626AA36F0DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {9BB800E4-ACE7-49DB-BDA9-6626AA36F0DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {9BB800E4-ACE7-49DB-BDA9-6626AA36F0DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {9BB800E4-ACE7-49DB-BDA9-6626AA36F0DF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/src/MauiShakeDetector/IShakeDetector.cs b/src/MauiShakeDetector/IShakeDetector.cs
index 332cb5c..e6e09b7 100644
--- a/src/MauiShakeDetector/IShakeDetector.cs
+++ b/src/MauiShakeDetector/IShakeDetector.cs
@@ -1,69 +1,85 @@
namespace MauiShakeDetector;
public interface IShakeDetector
{
-
+
///
- /// Gets a Value Indicating Whether ShakeDetector is Supported on this Device
+ /// Gets a value indicating whether ShakeDetector is supported on this device
///
bool IsSupported { get; }
///
- /// Gets a Value Indicating Whether ShakeDetector is Already Monitoring
+ /// Gets a value indicating whether ShakeDetector is already monitoring
///
bool IsMonitoring { get; }
///
- /// Get or Set the Value of Shake Detection Threshold
- /// In Summary The Gforce Value for Shake To Be Detected
+ /// Gets or sets the value of shake detection threshold
+ /// In summary, the Gforce value for shake to be detected
///
- double ShakeThresholdGravity { get; set; }
+ double ShakeThresholdGravity { get; set; }
///
- /// Get or Set the value of Minimum Delay betweem Shakes
+ /// Gets or sets the value of minimum delay between shakes
///
- TimeSpan ShakeIntervalInMilliseconds { get; set; }
+ TimeSpan ShakeIntervalInMilliseconds { get; set; }
///
- /// Get or Set the Value of Shake Reset Interval in Milliseconds
+ /// Gets or sets the value of shake reset interval in milliseconds
///
- TimeSpan ShakeResetIntervalInMilliseconds { get; set; }
+ TimeSpan ShakeResetIntervalInMilliseconds { get; set; }
///
- /// Get or Set the Value for Number of Shakes Required Before Shake is Triggered
+ /// Gets or sets the value for number of shakes required before shake is triggered
///
- int MinimumShakeCount { get; set; }
+ int MinimumShakeCount { get; set; }
///
- /// Gets a Value Indicating Whether Haptics is Enabled or not
+ /// Gets a value indicating whether haptics is enabled or not
///
+ ///
+ /// Default is true, you can disable it if you want by setting it to false
+ ///
+ ///
+ /// Will throw Microsoft.Maui.ApplicationModel.FeatureNotSupportedException If MauiShakeDetector.IShakeDetector.IsHapticsSupported is false,
+ /// Therefore Please Check Whether MauiShakeDetector.IShakeDetector.IsHapticsSupported is true before enabling haptics on the device
+ ///
bool IsHapticsEnabled { get; set; }
///
- /// Gets a Value Indicating Whether Haptics Supported in this Device
+ /// Gets a value indicating whether haptics is supported on this device
///
- bool IsHapticsSupported { get;}
+ ///
+ /// Will throw Microsoft.Maui.ApplicationModel.FeatureNotSupportedException If MauiShakeDetector.IShakeDetector.IsHapticsSupported is false
+ ///
+ bool IsHapticsSupported { get; }
+ ///
+ /// Gets or sets the value of haptics duration
+ ///
+ TimeSpan HapticsDurationInMilliseconds { get; set; }
///
- /// Get or Set the Value Of Haptics Duration
+ /// Gets or sets the value of auto stop listening to shake event after number of shakes triggered
///
- TimeSpan HapticsDurationInMilliseconds { get; set; }
+ ///
+ /// Default is 0 and it means feature is disabled by default. You can set the count to some other value to enable this feature
+ ///
+ int AutoStopAfterNoShakes { get; set; }
///
- /// Shake Detected Event for Detecting Whether User Shaked the Device
+ /// Shake detected event for detecting whether user shook the device
///
#nullable enable
event EventHandler? ShakeDetected;
#nullable disable
///
- /// Start listening for Shake Event
+ /// Start listening for shake event
///
///
- /// Speed to monitor the Shake Events.
+ /// Speed to monitor the shake events.
///
///
- /// Will throw Microsoft.Maui.ApplicationModel.FeatureNotSupportedException if MauiShakeDetector.IShakeDetector.IsSupported
- /// is false. Will throw System.InvalidOperationException if MauiShakeDetector.IShakeDetector.IsMonitoring
- /// is true.
+ /// Will throw Microsoft.Maui.ApplicationModel.FeatureNotSupportedException if MauiShakeDetector.IShakeDetector.IsSupported is false.
+ /// Will throw System.InvalidOperationException if MauiShakeDetector.IShakeDetector.IsMonitoring is true.
///
void StartListening(SensorSpeed sensorSpeed = SensorSpeed.Default);
///
- /// Stop Already Monitoring Shake Event
+ /// Stop already monitoring shake event
///
void StopListening();
}
diff --git a/src/MauiShakeDetector/MauiShakeDetector.csproj b/src/MauiShakeDetector/MauiShakeDetector.csproj
index c52e785..7eca600 100644
--- a/src/MauiShakeDetector/MauiShakeDetector.csproj
+++ b/src/MauiShakeDetector/MauiShakeDetector.csproj
@@ -35,9 +35,9 @@
Maui Shake Detector Shake Event Detector Library with Lots of Customization like GForce Tuning, Shake Intervals and Haptics for Shake Events
icon.png
$(AssemblyName) ($(TargetFramework))
- 0.1.0.0
- 0.1.0.0
- 0.1.0
+ 0.2.0.0
+ 0.2.0.0
+ 0.2.0
$(Version)$(VersionSuffix)
true
Maui,Shake,ShakeDetector,MauiShake,
diff --git a/src/MauiShakeDetector/ReleaseNotes.txt b/src/MauiShakeDetector/ReleaseNotes.txt
index 8209632..7eb9a3f 100644
--- a/src/MauiShakeDetector/ReleaseNotes.txt
+++ b/src/MauiShakeDetector/ReleaseNotes.txt
@@ -1,4 +1,8 @@
-v0.1.0
+v0.2.0
+• Added New Property to Auto Stop Listening to Shake Event "AutoStopAfterNoShakes"
+• Enhancements for Comments
+
+v0.1.0
• First Stable Build
• Enhancements for Comments
diff --git a/src/MauiShakeDetector/ShakeDetector.cs b/src/MauiShakeDetector/ShakeDetector.cs
index 3685970..af5e560 100644
--- a/src/MauiShakeDetector/ShakeDetector.cs
+++ b/src/MauiShakeDetector/ShakeDetector.cs
@@ -10,6 +10,7 @@ public static class ShakeDetector
public static bool IsHapticsSupported => Default.IsHapticsSupported;
public static bool IsHapticsEnabled => Default.IsHapticsEnabled;
public static TimeSpan HapticsDurationInMilliseconds => Default.HapticsDurationInMilliseconds;
+ public static int AutoStopAfterNoShakes => Default.AutoStopAfterNoShakes;
public static event EventHandler ShakeDetected
{
diff --git a/src/MauiShakeDetector/ShakeDetectorDefault.cs b/src/MauiShakeDetector/ShakeDetectorDefault.cs
index 4340964..2278911 100644
--- a/src/MauiShakeDetector/ShakeDetectorDefault.cs
+++ b/src/MauiShakeDetector/ShakeDetectorDefault.cs
@@ -2,22 +2,23 @@
internal sealed class ShakeDetectorDefault : IShakeDetector
{
// Properties
- public bool IsSupported { get; set; } = Accelerometer.Default.IsSupported;
- public bool IsMonitoring { get; set; } = Accelerometer.Default.IsMonitoring;
+ public bool IsSupported => Accelerometer.Default.IsSupported;
+ public bool IsMonitoring => Accelerometer.Default.IsMonitoring;
public double ShakeThresholdGravity { get; set; } = 1.9;
public TimeSpan ShakeIntervalInMilliseconds { get; set; } = TimeSpan.FromMilliseconds(500);
public TimeSpan ShakeResetIntervalInMilliseconds { get; set; } = TimeSpan.FromMilliseconds(3000);
public int MinimumShakeCount { get; set; } = 1;
public bool IsHapticsEnabled { get; set; } = true;
- public bool IsHapticsSupported { get; set; } = Vibration.Default.IsSupported;
+ public bool IsHapticsSupported => Vibration.Default.IsSupported;
public TimeSpan HapticsDurationInMilliseconds { get; set; } = TimeSpan.FromMilliseconds(1700);
-
+ public int AutoStopAfterNoShakes { get; set; } = 0;
// Private Fields
TimeSpan currentShakeTimeInMilliseconds = new(DateTime.Now.Ticks);
int currentShakeCount = 0;
+ int currentTriggeredShakesCount = 0;
static bool useSyncContext;
@@ -44,31 +45,44 @@ private void Accelerometer_ReadingChanged(object sender, AccelerometerChangedEve
double gForce = Math.Sqrt(x * x + y * y + z * z);
- if (gForce > ShakeThresholdGravity)
- {
- TimeSpan now = new(DateTime.Now.Ticks);
+ if (gForce < ShakeThresholdGravity) return;
+
+ TimeSpan now = new(DateTime.Now.Ticks);
- if (currentShakeTimeInMilliseconds + ShakeIntervalInMilliseconds > now) return;
+ if (currentShakeTimeInMilliseconds + ShakeIntervalInMilliseconds > now) return;
- if (currentShakeTimeInMilliseconds + ShakeResetIntervalInMilliseconds < now) currentShakeCount = 0;
+ if (currentShakeTimeInMilliseconds + ShakeResetIntervalInMilliseconds < now) currentShakeCount = 0;
- currentShakeTimeInMilliseconds = now;
- currentShakeCount++;
+ currentShakeTimeInMilliseconds = now;
+ currentShakeCount++;
+
+ if (currentShakeCount < MinimumShakeCount) return;
+
+ if (IsHapticsEnabled)
+ {
+ if (!IsHapticsSupported) throw new FeatureNotSupportedException("Haptics is Not Supported in Your Device");
+ Vibration.Default.Vibrate(HapticsDurationInMilliseconds);
+ }
- if (currentShakeCount < MinimumShakeCount) return;
+ if (useSyncContext)
+ {
+ currentTriggeredShakesCount++;
+ MainThread.BeginInvokeOnMainThread(() => ShakeDetected?.Invoke(this, new ShakeDetectedEventArgs(currentShakeCount)));
+ AutoStopAfterNoShakeEvents();
+ return;
+ }
- if (IsHapticsEnabled)
- {
- if (!IsHapticsSupported) throw new FeatureNotSupportedException("Haptics is Not Supported in Your Device");
- Vibration.Default.Vibrate(HapticsDurationInMilliseconds);
- }
+ currentTriggeredShakesCount++;
+ ShakeDetected?.Invoke(this, new ShakeDetectedEventArgs(currentShakeCount));
+ AutoStopAfterNoShakeEvents();
+ }
- if (useSyncContext)
- {
- MainThread.BeginInvokeOnMainThread(() => ShakeDetected?.Invoke(this, new ShakeDetectedEventArgs(currentShakeCount)));
- return;
- }
- ShakeDetected?.Invoke(this, new ShakeDetectedEventArgs(currentShakeCount));
+ private void AutoStopAfterNoShakeEvents()
+ {
+ if(AutoStopAfterNoShakes != 0 && currentTriggeredShakesCount >= AutoStopAfterNoShakes)
+ {
+ currentTriggeredShakesCount = 0;
+ StopListening();
}
}
@@ -77,6 +91,7 @@ public void StopListening()
if (Accelerometer.Default.IsMonitoring)
{
Accelerometer.Default.ReadingChanged -= Accelerometer_ReadingChanged;
+ currentTriggeredShakesCount = 0;
Accelerometer.Default.Stop();
}
}
diff --git a/src/MauiShakeDetector/readme.md b/src/MauiShakeDetector/readme.md
index 8dc827b..76486fc 100644
--- a/src/MauiShakeDetector/readme.md
+++ b/src/MauiShakeDetector/readme.md
@@ -76,6 +76,7 @@ using MauiShakeDetector;
| **ShakeIntervalInMilliseconds** | `TimeSpan` | Get or Set the value of Minimum Delay betweem Shakes |
| **ShakeResetIntervalInMilliseconds** | `TimeSpan` | Get or Set the Value of Shake Reset Interval in Milliseconds |
| **MinimumShakeCount** | `int` | Get or Set the Value for Number of Shakes Required Before Shake is Triggered |
+| **AutoStopAfterNoShakes** | `int` | Gets or sets the value of Auto Stop listening to shake event after number of shakes triggered |
| **HapticsDurationInMilliseconds** | `TimeSpan` | Get or Set the Value Of Haptics Duration |
| **ShakeDetected** | `event` | Shake Detected Event for Detecting Whether User Shaked the Device |
| **StartListening()** | `method` | Start listening for Shake Event |