Guest User

Untitled

a guest
May 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. //ViewModelBase implements INaviagtionAware and INotifyPropertyChanged
  2. public class MainPageViewModel : ViewModelBase
  3. {
  4. private string _object1Id;
  5. private string _object2Id;
  6. private Task _getObjectsFromIdsTask;
  7. private Object1 _object1;
  8. private Object2 _object2;
  9. private DelegateCommand _refreshListViewSourceCommand;
  10. private bool _isIsRefreshing;
  11. public Object1 Object1
  12. {
  13. get
  14. {
  15. return _object1;
  16. }
  17. set
  18. {
  19. SetProperty ( ref _object1 , value );
  20. }
  21. }
  22. public Object2 Object2
  23. {
  24. get
  25. {
  26. return _object2;
  27. }
  28. set
  29. {
  30. SetProperty ( ref _object2 , value );
  31. }
  32. }
  33. public DelegateCommand RefreshListViewSourceCommand
  34. {
  35. get
  36. {
  37. return _refreshListViewSourceCommand ?? ( _refreshListViewSourceCommand = new DelegateCommand ( RefreshListViewSource , CanExecuteRefreshListViewSource ).ObservesProperty ( () => IsRefreshing ) );
  38. }
  39. }
  40. public bool IsRefreshing
  41. {
  42. get
  43. {
  44. return _isIsRefreshing;
  45. }
  46. set
  47. {
  48. SetProperty ( ref _isIsRefreshing , value );
  49. }
  50. }
  51. public MainPageViewModel ( INavigationService navigationService )
  52. : base ( navigationService )
  53. {
  54. Title = "Main Page";
  55. }
  56. //Called when the page is first loadded and on each pull-to-refresh.
  57. private async void RefreshListViewSource ()
  58. {
  59. IsRefreshing = true;
  60. try
  61. {
  62. //If this is truen then objects where set from either another viewmodel or the task has completed succesfully before this method was called from the view.
  63. if ( Object1 != null &&
  64. Object2 != null )
  65. {
  66. //Populate the listview.
  67. }
  68. else if ( _getObjectsFromIdsTask != null )
  69. {
  70. switch ( _getObjectsFromIdsTask.Status )
  71. {
  72. case TaskStatus.Created :
  73. case TaskStatus.WaitingForActivation :
  74. case TaskStatus.WaitingToRun :
  75. case TaskStatus.Running :
  76. case TaskStatus.WaitingForChildrenToComplete :
  77. //If any of the above then we wait for its completion.
  78. await _getObjectsFromIdsTask;
  79. break;
  80. case TaskStatus.Faulted :
  81. case TaskStatus.Canceled :
  82. //If an error occurs then we restart the task.
  83. _getObjectsFromIdsTask = GetObjectsFromIds ();
  84. await _getObjectsFromIdsTask;
  85. break;
  86. case TaskStatus.RanToCompletion :
  87. //If completed then do nothing.
  88. break;
  89. default :
  90. throw new ArgumentOutOfRangeException ();
  91. }
  92. }
  93. else
  94. {
  95. //If it is null then this method was called from the view before the task is set, should I delete it.
  96. //This branch should never be reached
  97. _getObjectsFromIdsTask = GetObjectsFromIds ();
  98. await _getObjectsFromIdsTask;
  99. }
  100.  
  101. //If we get to here then the objects must be set
  102. //Populate the listview.
  103. }
  104. catch ( Exception e )
  105. {
  106. Debug.WriteLine ( e );
  107. }
  108.  
  109. IsRefreshing = false;
  110. }
  111. private bool CanExecuteRefreshListViewSource ()
  112. {
  113. return ! IsRefreshing;
  114. }
  115. //Gets the objects from a web service by thier ids.
  116. private async Task GetObjectsFromIds ()
  117. {
  118. //Uses _objectId1 and _objectId2 to get the objects.
  119. Task < Object1 > object1 = Task.Delay ( 100 ).ContinueWith ( task => new Object1 () );
  120. Task < Object2 > object2 = Task.Delay ( 100 ).ContinueWith ( task => new Object2 () );
  121. Object1 = await object1;
  122. Object2 = await object2;
  123. }
  124. //called when navigation to the page. I used OnNavigatingTo and not OnNavigatedTo to reduce the chance that the refresh method would be called befor the objects are set.
  125. public override void OnNavigatingTo ( NavigationParameters parameters )
  126. {
  127. base.OnNavigatingTo ( parameters );
  128. //If Ids are sent from another viewmodel then I have to get their objects from a web service. This would happen in case of deep linking from notification or a similar scenario.
  129. if ( parameters.ContainsKey ( "Object1Id" ) &&
  130. parameters.ContainsKey ( "Object2Id" ) )
  131. {
  132. _object1Id = parameters.GetValue < string > ( "Object1" );
  133. _object2Id = parameters.GetValue < string > ( "Object2" );
  134. _getObjectsFromIdsTask = GetObjectsFromIds ();
  135. }
  136. //If the full objects are sent, then I am setting the objects right here.
  137. else if ( parameters.ContainsKey ( "Object1" ) &&
  138. parameters.ContainsKey ( "Object2" ) )
  139. {
  140. Object1 = parameters.GetValue < Object1 > ( "Object1" );
  141. Object2 = parameters.GetValue < Object2 > ( "Object2" );
  142. }
  143. }
  144. }
Add Comment
Please, Sign In to add comment