/MALClient.XShared/ViewModels/Forums/ForumsMainViewModel.cs

https://github.com/Drutol/MALClient · C# · 244 lines · 206 code · 38 blank · 0 comment · 21 complexity · e9be596a25099e634d664506ed2e1870 MD5 · raw file

  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Input;
  6. using GalaSoft.MvvmLight;
  7. using GalaSoft.MvvmLight.Command;
  8. using MALClient.Models.Enums;
  9. using MALClient.Models.Models.Forums;
  10. using MALClient.XShared.Delegates;
  11. using MALClient.XShared.Interfaces;
  12. using MALClient.XShared.NavArgs;
  13. using MALClient.XShared.Utils;
  14. namespace MALClient.XShared.ViewModels.Forums
  15. {
  16. public class ForumsMainViewModel : ViewModelBase
  17. {
  18. public event AmbiguousNavigationRequest<ForumsPageIndex> NavigationRequested;
  19. public ObservableCollection<ForumBoards> PinnedBoards { get; } = new ObservableCollection<ForumBoards>();
  20. public ObservableCollection<ForumTopicLightEntry> PinnedTopics { get; } = new ObservableCollection<ForumTopicLightEntry>();
  21. public ForumTopicLightEntry SelectedForumTopicLightEntry
  22. {
  23. get { return null; }
  24. set
  25. {
  26. if(value != null)
  27. GotoPinnedTopic(value);
  28. }
  29. }
  30. private ICommand _removePinnedBoardCommand;
  31. public ICommand RemovePinnedBoardCommand
  32. =>
  33. _removePinnedBoardCommand ??
  34. (_removePinnedBoardCommand = new RelayCommand<ForumBoards>(RemoveFavouriteBoard));
  35. private ICommand _gotoPinnedBoardCommand;
  36. public ICommand GotoPinnedBoardCommand
  37. =>
  38. _gotoPinnedBoardCommand ??
  39. (_gotoPinnedBoardCommand = new RelayCommand<ForumBoards>(GotoFavouriteBoard));
  40. private ICommand _gotoPinnedTopicCommand;
  41. public ICommand GotoPinnedTopicCommand
  42. =>
  43. _gotoPinnedTopicCommand ??
  44. (_gotoPinnedTopicCommand = new RelayCommand<ForumTopicLightEntry>(GotoPinnedTopic));
  45. private ICommand _unpinTopicCommand;
  46. public ICommand UnpinTopicCommand
  47. =>
  48. _unpinTopicCommand ??
  49. (_unpinTopicCommand = new RelayCommand<ForumTopicLightEntry>(topic => PinnedTopics.Remove(topic)));
  50. private ICommand _navigateRecentTopicsCommand;
  51. public ICommand NavigateRecentTopicsCommand
  52. =>
  53. _navigateRecentTopicsCommand ??
  54. (_navigateRecentTopicsCommand = new RelayCommand(GotoMyRecentTopics));
  55. private ICommand _navigateMalClientTopicsCommand;
  56. public ICommand NavigateMalClientTopicsCommand
  57. =>
  58. _navigateMalClientTopicsCommand ??
  59. (_navigateMalClientTopicsCommand = new RelayCommand(GotoMalClientTopic));
  60. private ICommand _navigateWatchedTopicsCommand;
  61. public ICommand NavigateWatchedTopicsCommand
  62. =>
  63. _navigateWatchedTopicsCommand ??
  64. (_navigateWatchedTopicsCommand = new RelayCommand(GotoWatchedTopics));
  65. private ICommand _navigateStarredMessages;
  66. public ICommand NavigateStarredMessages
  67. =>
  68. _navigateStarredMessages ??
  69. (_navigateStarredMessages = new RelayCommand(GotoStarredMessages));
  70. public ISelfBackNavAware CurrentBackNavRegistrar { get; set; }
  71. public void Init(ForumsNavigationArgs args)
  72. {
  73. if (args == null)
  74. {
  75. ViewModelLocator.NavMgr.ResetMainBackNav();
  76. ViewModelLocator.NavMgr.RegisterBackNav(PageIndex.PageAnimeList, null);
  77. args = new ForumsNavigationArgs { Page = ForumsPageIndex.PageIndex };
  78. }
  79. CurrentBackNavRegistrar = null;
  80. NavigationRequested?.Invoke(args.Page, args);
  81. }
  82. private bool _pinnedTopicsLoaded;
  83. public async void LoadPinnedTopics()
  84. {
  85. if(_pinnedTopicsLoaded)
  86. return;
  87. _pinnedTopicsLoaded = true;
  88. foreach (var item in (await ResourceLocator.DataCacheService.RetrieveDataRoaming<List<ForumTopicLightEntry>>("pinned_forum_topics.json", -1)) ?? new List<ForumTopicLightEntry>())
  89. {
  90. PinnedTopics.Add(item);
  91. }
  92. }
  93. public async Task SavePinnedTopics()
  94. {
  95. await ResourceLocator.DataCacheService.SaveDataRoaming(PinnedTopics.ToList(), "pinned_forum_topics.json");
  96. }
  97. public ForumsMainViewModel()
  98. {
  99. if (!string.IsNullOrEmpty(Settings.ForumsPinnedBoards))
  100. {
  101. foreach (var item in Settings.ForumsPinnedBoards.Split(',').Select(int.Parse).Cast<ForumBoards>())
  102. {
  103. PinnedBoards.Add(item);
  104. }
  105. }
  106. }
  107. public void AddFavouriteBoard(ForumBoards board)
  108. {
  109. if (!PinnedBoards.Contains(board))
  110. {
  111. PinnedBoards.Add(board);
  112. Settings.ForumsPinnedBoards = string.Join(",", PinnedBoards.Cast<int>());
  113. }
  114. }
  115. private void RemoveFavouriteBoard(ForumBoards board)
  116. {
  117. PinnedBoards.Remove(board);
  118. Settings.ForumsPinnedBoards = string.Join(",", PinnedBoards.Cast<int>());
  119. }
  120. private void GotoFavouriteBoard(ForumBoards board)
  121. {
  122. if (CurrentBackNavRegistrar == null)
  123. {
  124. ViewModelLocator.NavMgr.ResetMainBackNav();
  125. ViewModelLocator.NavMgr.RegisterBackNav(PageIndex.PageForumIndex, new ForumsNavigationArgs());
  126. }
  127. else
  128. {
  129. CurrentBackNavRegistrar.RegisterSelfBackNav();
  130. }
  131. ViewModelLocator.GeneralMain.Navigate(PageIndex.PageForumIndex,new ForumsBoardNavigationArgs(board));
  132. }
  133. private void GotoPinnedTopic(ForumTopicLightEntry topic)
  134. {
  135. if (CurrentBackNavRegistrar == null)
  136. {
  137. ViewModelLocator.NavMgr.ResetMainBackNav();
  138. ViewModelLocator.NavMgr.RegisterBackNav(PageIndex.PageForumIndex, new ForumsNavigationArgs());
  139. if(topic.SourceBoard != null)
  140. ViewModelLocator.NavMgr.RegisterBackNav(PageIndex.PageForumIndex, new ForumsBoardNavigationArgs(topic.SourceBoard.Value));
  141. }
  142. else
  143. {
  144. CurrentBackNavRegistrar.RegisterSelfBackNav();
  145. }
  146. ViewModelLocator.GeneralMain.Navigate(PageIndex.PageForumIndex, new ForumsTopicNavigationArgs(topic.Id,topic.Lastpost ? (int?)-1 : null,1));
  147. }
  148. private void GotoMyRecentTopics()
  149. {
  150. if (CurrentBackNavRegistrar == null)
  151. {
  152. ViewModelLocator.NavMgr.ResetMainBackNav();
  153. ViewModelLocator.NavMgr.RegisterBackNav(PageIndex.PageForumIndex, new ForumsNavigationArgs());
  154. }
  155. else
  156. {
  157. CurrentBackNavRegistrar.RegisterSelfBackNav();
  158. }
  159. ViewModelLocator.GeneralMain.Navigate(PageIndex.PageForumIndex, new ForumsBoardNavigationArgs(Credentials.UserName));
  160. }
  161. private void GotoWatchedTopics()
  162. {
  163. if (CurrentBackNavRegistrar == null)
  164. {
  165. ViewModelLocator.NavMgr.ResetMainBackNav();
  166. ViewModelLocator.NavMgr.RegisterBackNav(PageIndex.PageForumIndex, new ForumsNavigationArgs());
  167. }
  168. else
  169. {
  170. CurrentBackNavRegistrar.RegisterSelfBackNav();
  171. }
  172. ViewModelLocator.GeneralMain.Navigate(PageIndex.PageForumIndex, new ForumsBoardNavigationArgs(ForumBoardPageWorkModes.WatchedTopics));
  173. }
  174. private void GotoMalClientTopic()
  175. {
  176. if (CurrentBackNavRegistrar == null)
  177. {
  178. ViewModelLocator.NavMgr.ResetMainBackNav();
  179. ViewModelLocator.NavMgr.RegisterBackNav(PageIndex.PageForumIndex, new ForumsNavigationArgs());
  180. ViewModelLocator.NavMgr.RegisterBackNav(PageIndex.PageForumIndex, new ForumsBoardNavigationArgs(ForumBoardPageWorkModes.WatchedTopics));
  181. }
  182. else
  183. {
  184. CurrentBackNavRegistrar.RegisterSelfBackNav();
  185. }
  186. #if ANDROID
  187. ViewModelLocator.GeneralMain.Navigate(PageIndex.PageForumIndex, new ForumsTopicNavigationArgs("1626591", null));
  188. #else
  189. ViewModelLocator.GeneralMain.Navigate(PageIndex.PageForumIndex, new ForumsTopicNavigationArgs("1499207", null));
  190. #endif
  191. }
  192. private void GotoStarredMessages()
  193. {
  194. if (CurrentBackNavRegistrar == null)
  195. {
  196. ViewModelLocator.NavMgr.ResetMainBackNav();
  197. ViewModelLocator.NavMgr.RegisterBackNav(PageIndex.PageForumIndex, new ForumsNavigationArgs());
  198. }
  199. else
  200. {
  201. CurrentBackNavRegistrar.RegisterSelfBackNav();
  202. }
  203. ViewModelLocator.GeneralMain.Navigate(PageIndex.PageForumIndex, new ForumStarredMessagesNavigationArgs());
  204. }
  205. }
  206. }