Power of FreshMVVM: FreshAwaitCommand vs Command
Ever thought of having Xamarin Forms buttons automatically takes care of async calls by disabling them self and reenable once async/await operations are finished, without having us to write any extra logic to control it behaviour . Well, today is your lucky day, FreshMVVM handles such requirements out of box with the help of FreshAwaitCommand.
Let's understand what is FreshMVVM (version 3.5.0) first, FreshMvvm is a super light Mvvm Framework designed specifically for Xamarin.Forms. It's designed to be Easy, Simple and Flexible.
In this sample I am taking example of a ICommand based button and FreshAwaitCommand, in the sample code you will find two buttons vertically, top button is bind to the ICommand and buttom button is bind to FreshAwaitCommand.
Both commands reuses same asynchronous method that uses Task.Delay and suppose to wait for 3 seconds, which resembles the API call and trying to fetch some data that takes 3 seconds to comeback.
When user taps on the first button and continuous to tap on button, you can notice multiple calls are made to method without waiting to complete 3 seconds, which I believe is not good user experience or the app performance.
But, when user taps on the second button, that uses FreshAwaitCommad from FreshMVVM, it actually waits for the delay for 3 seconds and at the same time button is disable for you. Behind the scenes of FreshAwaitCommand it is actually making use of TaskCompletionSource which handles the completion process and returns only after the call is successfully completed. We have to set SetResult to true for this to work.
Here is another example when you should use FreshMVVM, it save you time by doing more with less code and obviously time == money
Here is the link to github for sample code, happy coding.