Power of FreshMVVM: FreshAwaitCommand vs Command

3 FreshMVVM.png

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.

Screen-Shot-2019-02-11-at-2.43.32-pm-579x1024.png

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.

Screen-Shot-2019-02-11-at-2.44.22-pm.png

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.

Screen-Shot-2019-02-11-at-2.45.22-pm.png

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.

Screen-Shot-2019-02-11-at-2.45.45-pm-1024x197.png

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.