Essentials icon indicating copy to clipboard operation
Essentials copied to clipboard

[Enhancement][HapticFeedback] Prepare generator

Open dimonovdd opened this issue 4 years ago • 2 comments

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 main at time of PR
  • [x] Changes adhere to coding standard
  • [x] Updated documentation

dimonovdd avatar Jan 03 '21 10:01 dimonovdd

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 avatar Jan 18 '21 03:01 mattleibow

@mattleibow I have a case in which I very often perform Haptic Feedback. and each time there are 4 actions with UIImpactFeedbackGenerator:

  1. initialize
  2. Prepare
  3. ImpactOccurred
  4. Dispose

probably the new method is more correct, and we should have made it right away and abandoned static HapticFeedback.Perform() method

dimonovdd avatar Jan 18 '21 06:01 dimonovdd

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.

jfversluis avatar Dec 07 '23 14:12 jfversluis