Essentials
Essentials copied to clipboard
[Enhancement][HapticFeedback] Prepare generator
Description of Change
This revision is necessary for applications that often use HapticFeedback. This changes will improve performance on iOS, UWP and Tizen. Using the new method, we will not create a new object for HapticFeedback every time. It doesn't break the old API.
API Changes
Added:
public static class HapticFeedback
{
public static void Perform(HapticFeedbackType type = HapticFeedbackType.Click)
{
using var generator = PrepareGenerator(type);
generator.Perform();
}
//new method
public static HapticFeedbackGenerator PrepareGenerator(HapticFeedbackType type = HapticFeedbackType.Click);
}
public class HapticFeedbackGenerator : IDisposable
{
public void Perform();
public void Dispose();
}
PR Checklist
- [x] Has tests (if omitted, state reason in description)
- [x] Has samples (if omitted, state reason in description)
- [x] Rebased on top of
mainat time of PR - [x] Changes adhere to coding standard
- [x] Updated documentation
Maybe I am not understanding this, but what is the benefit/purpose of this split logic. I get for iOS that is how it works, but how does this split benefit over calling static haptic methods twice or more?
@mattleibow
I have a case in which I very often perform Haptic Feedback.
and each time there are 4 actions with UIImpactFeedbackGenerator:
- initialize
- Prepare
- ImpactOccurred
- Dispose
probably the new method is more correct, and we should have made it right away and abandoned static HapticFeedback.Perform() method
I've been going back and forth about still adding this to Xamarin.Essentials but in the end decided not to. It introduces a bunch of new APIs which we don't really want. I like the optimization here, which would've been great if we could've added it at the time this was opened.
Sorry this wasn't finalized sooner and thank you so much for all the time and effort here! I'll see to maybe still port this to .NET MAUI so we can still benefit from your work.