PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/JMMClient/JMMClient/Forms/RandomEpisodeForm.xaml.cs

https://bitbucket.org/gibwar/jmm-test
C# | 495 lines | 387 code | 96 blank | 12 comment | 75 complexity | f3c8e4ad7a11137ea44a1a8ec27af86e MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using System.ComponentModel;
  14. using System.Collections.ObjectModel;
  15. namespace JMMClient.Forms
  16. {
  17. /// <summary>
  18. /// Interaction logic for RandomEpisodeForm.xaml
  19. /// </summary>
  20. public partial class RandomEpisodeForm : Window
  21. {
  22. private RandomSeriesEpisodeLevel LevelType = RandomSeriesEpisodeLevel.All;
  23. private object LevelObject = null;
  24. private static Random rndm = new Random();
  25. private static Random eprndm = new Random();
  26. public ICollectionView ViewCategoryNames { get; set; }
  27. public ObservableCollection<string> AllCategoryNames { get; set; }
  28. public static readonly DependencyProperty SeriesProperty = DependencyProperty.Register("Series",
  29. typeof(AnimeSeriesVM), typeof(RandomEpisodeForm), new UIPropertyMetadata(null, null));
  30. public AnimeSeriesVM Series
  31. {
  32. get { return (AnimeSeriesVM)GetValue(SeriesProperty); }
  33. set { SetValue(SeriesProperty, value); }
  34. }
  35. public static readonly DependencyProperty EpisodeProperty = DependencyProperty.Register("Episode",
  36. typeof(AnimeEpisodeVM), typeof(RandomEpisodeForm), new UIPropertyMetadata(null, null));
  37. public AnimeEpisodeVM Episode
  38. {
  39. get { return (AnimeEpisodeVM)GetValue(EpisodeProperty); }
  40. set { SetValue(EpisodeProperty, value); }
  41. }
  42. public static readonly DependencyProperty SeriesExistsProperty = DependencyProperty.Register("SeriesExists",
  43. typeof(bool), typeof(RandomEpisodeForm), new UIPropertyMetadata(false, null));
  44. public bool SeriesExists
  45. {
  46. get { return (bool)GetValue(SeriesExistsProperty); }
  47. set { SetValue(SeriesExistsProperty, value); }
  48. }
  49. public static readonly DependencyProperty EpisodeExistsProperty = DependencyProperty.Register("EpisodeExists",
  50. typeof(bool), typeof(RandomEpisodeForm), new UIPropertyMetadata(false, null));
  51. public bool EpisodeExists
  52. {
  53. get { return (bool)GetValue(EpisodeExistsProperty); }
  54. set { SetValue(EpisodeExistsProperty, value); }
  55. }
  56. public static readonly DependencyProperty SeriesMissingProperty = DependencyProperty.Register("SeriesMissing",
  57. typeof(bool), typeof(RandomEpisodeForm), new UIPropertyMetadata(true, null));
  58. public bool SeriesMissing
  59. {
  60. get { return (bool)GetValue(SeriesMissingProperty); }
  61. set { SetValue(SeriesMissingProperty, value); }
  62. }
  63. public static readonly DependencyProperty EpisodeMissingProperty = DependencyProperty.Register("EpisodeMissing",
  64. typeof(bool), typeof(RandomEpisodeForm), new UIPropertyMetadata(true, null));
  65. public bool EpisodeMissing
  66. {
  67. get { return (bool)GetValue(EpisodeMissingProperty); }
  68. set { SetValue(EpisodeMissingProperty, value); }
  69. }
  70. public static readonly DependencyProperty CategoriesExpandedProperty = DependencyProperty.Register("CategoriesExpanded",
  71. typeof(bool), typeof(RandomEpisodeForm), new UIPropertyMetadata(false, null));
  72. public bool CategoriesExpanded
  73. {
  74. get { return (bool)GetValue(CategoriesExpandedProperty); }
  75. set { SetValue(CategoriesExpandedProperty, value); }
  76. }
  77. public static readonly DependencyProperty CategoriesCollapsedProperty = DependencyProperty.Register("CategoriesCollapsed",
  78. typeof(bool), typeof(RandomEpisodeForm), new UIPropertyMetadata(true, null));
  79. public bool CategoriesCollapsed
  80. {
  81. get { return (bool)GetValue(CategoriesCollapsedProperty); }
  82. set { SetValue(CategoriesCollapsedProperty, value); }
  83. }
  84. public static readonly DependencyProperty SelectedCategoriesProperty = DependencyProperty.Register("SelectedCategories",
  85. typeof(string), typeof(RandomEpisodeForm), new UIPropertyMetadata("", null));
  86. public string SelectedCategories
  87. {
  88. get { return (string)GetValue(SelectedCategoriesProperty); }
  89. set { SetValue(SelectedCategoriesProperty, value); }
  90. }
  91. public static readonly DependencyProperty MatchesFoundProperty = DependencyProperty.Register("MatchesFound",
  92. typeof(int), typeof(RandomEpisodeForm), new UIPropertyMetadata(0, null));
  93. public int MatchesFound
  94. {
  95. get { return (int)GetValue(MatchesFoundProperty); }
  96. set { SetValue(MatchesFoundProperty, value); }
  97. }
  98. public static readonly DependencyProperty SelectedCategoryFilterProperty = DependencyProperty.Register("SelectedCategoryFilter",
  99. typeof(string), typeof(RandomEpisodeForm), new UIPropertyMetadata("Any", null));
  100. public string SelectedCategoryFilter
  101. {
  102. get { return (string)GetValue(SelectedCategoryFilterProperty); }
  103. set { SetValue(SelectedCategoryFilterProperty, value); }
  104. }
  105. public RandomEpisodeForm()
  106. {
  107. InitializeComponent();
  108. btnRandomEpisode.Click += new RoutedEventHandler(btnRandomSeries_Click);
  109. this.Loaded += new RoutedEventHandler(RandomEpisodeForm_Loaded);
  110. btnEditCategories.Click += new RoutedEventHandler(btnEditCategories_Click);
  111. btnEditCategoriesFinish.Click += new RoutedEventHandler(btnEditCategoriesFinish_Click);
  112. lbCategories.MouseDoubleClick += new MouseButtonEventHandler(lbCategories_MouseDoubleClick);
  113. cboCatFilter.Items.Clear();
  114. cboCatFilter.Items.Add("Any");
  115. cboCatFilter.Items.Add("All");
  116. cboCatFilter.SelectedIndex = 0;
  117. }
  118. private void CommandBinding_ToggleWatchedStatus(object sender, ExecutedRoutedEventArgs e)
  119. {
  120. object obj = e.Parameter;
  121. if (obj == null) return;
  122. this.Cursor = Cursors.Wait;
  123. this.IsEnabled = false;
  124. bool newStatus = false;
  125. try
  126. {
  127. if (obj.GetType() == typeof(VideoDetailedVM))
  128. {
  129. VideoDetailedVM vid = obj as VideoDetailedVM;
  130. newStatus = !vid.Watched;
  131. JMMServerVM.Instance.clientBinaryHTTP.ToggleWatchedStatusOnVideo(vid.VideoLocalID, newStatus, JMMServerVM.Instance.CurrentUser.JMMUserID.Value);
  132. MainListHelperVM.Instance.UpdateHeirarchy(vid);
  133. }
  134. if (obj.GetType() == typeof(AnimeEpisodeVM))
  135. {
  136. AnimeEpisodeVM ep = obj as AnimeEpisodeVM;
  137. newStatus = !ep.Watched;
  138. JMMServerBinary.Contract_ToggleWatchedStatusOnEpisode_Response response = JMMServerVM.Instance.clientBinaryHTTP.ToggleWatchedStatusOnEpisode(ep.AnimeEpisodeID,
  139. newStatus, JMMServerVM.Instance.CurrentUser.JMMUserID.Value);
  140. if (!string.IsNullOrEmpty(response.ErrorMessage))
  141. {
  142. MessageBox.Show(response.ErrorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  143. return;
  144. }
  145. MainListHelperVM.Instance.UpdateHeirarchy(response.AnimeEpisode);
  146. }
  147. }
  148. catch (Exception ex)
  149. {
  150. Utils.ShowErrorMessage(ex);
  151. }
  152. finally
  153. {
  154. this.Cursor = Cursors.Arrow;
  155. this.IsEnabled = true;
  156. }
  157. }
  158. void lbCategories_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  159. {
  160. object obj = lbCategories.SelectedItem;
  161. if (obj == null) return;
  162. string catName = obj.ToString();
  163. string currentList = txtSelectedCategories.Text.Trim();
  164. // add to the selected list
  165. int index = currentList.IndexOf(catName, 0, StringComparison.InvariantCultureIgnoreCase);
  166. if (index > -1) return;
  167. if (currentList.Length > 0) currentList += ",";
  168. currentList += catName;
  169. txtSelectedCategories.Text = currentList;
  170. }
  171. void btnEditCategoriesFinish_Click(object sender, RoutedEventArgs e)
  172. {
  173. CategoriesCollapsed = true;
  174. CategoriesExpanded = false;
  175. SelectedCategories = txtSelectedCategories.Text.Trim();
  176. SelectedCategoryFilter = cboCatFilter.SelectedItem.ToString();
  177. }
  178. void btnEditCategories_Click(object sender, RoutedEventArgs e)
  179. {
  180. CategoriesCollapsed = false;
  181. CategoriesExpanded = true;
  182. txtSelectedCategories.Text = SelectedCategories;
  183. if (SelectedCategoryFilter.Equals("Any"))
  184. cboCatFilter.SelectedIndex = 0;
  185. else
  186. cboCatFilter.SelectedIndex = 1;
  187. }
  188. void RandomEpisodeForm_Loaded(object sender, RoutedEventArgs e)
  189. {
  190. SetRandomEpisode();
  191. }
  192. void btnRandomSeries_Click(object sender, RoutedEventArgs e)
  193. {
  194. SetRandomEpisode();
  195. }
  196. private void SetRandomEpisode()
  197. {
  198. try
  199. {
  200. SeriesMissing = true;
  201. SeriesExists = false;
  202. EpisodeMissing = true;
  203. EpisodeExists = false;
  204. ucPlayEpisode.EpisodeExists = false;
  205. ucPlayEpisode.EpisodeMissing = true;
  206. ucPlayEpisode.DataContext = null;
  207. // first let's get a random series
  208. // once we have that we'll get a random episode from that series
  209. // we also have to make sure we ignore series that don't have episodes
  210. // which match the criteria
  211. List<int> ignoredSeries = new List<int>();
  212. List<AnimeSeriesVM> serList = null;
  213. if (LevelType == RandomSeriesEpisodeLevel.GroupFilter)
  214. serList = GetSeriesForGroupFilter();
  215. if (LevelType == RandomSeriesEpisodeLevel.Group)
  216. serList = GetSeriesForGroup();
  217. if (LevelType == RandomSeriesEpisodeLevel.Series)
  218. {
  219. serList = new List<AnimeSeriesVM>();
  220. AnimeSeriesVM ser = LevelObject as AnimeSeriesVM;
  221. if (ser != null) serList.Add(ser);
  222. }
  223. if (serList != null && serList.Count > 0)
  224. {
  225. Dictionary<int, AnimeSeriesVM> dictSeries = new Dictionary<int, AnimeSeriesVM>();
  226. foreach (AnimeSeriesVM ser in serList)
  227. dictSeries[ser.AnimeSeriesID.Value] = ser;
  228. bool needEpisode = true;
  229. while (needEpisode)
  230. {
  231. if (dictSeries.Values.Count == 0)
  232. break;
  233. List<AnimeSeriesVM> tempSerList = new List<AnimeSeriesVM>(dictSeries.Values);
  234. Series = tempSerList[rndm.Next(0, tempSerList.Count)];
  235. // get all the episodes
  236. List<AnimeEpisodeVM> epList = new List<AnimeEpisodeVM>();
  237. foreach (AnimeEpisodeVM ep in Series.AllEpisodes)
  238. {
  239. bool useEp = false;
  240. if (chkWatched.IsChecked.Value && ep.Watched)
  241. useEp = true;
  242. if (chkUnwatched.IsChecked.Value && !ep.Watched)
  243. useEp = true;
  244. if (ep.LocalFileCount == 0)
  245. useEp = false;
  246. if (ep.EpisodeTypeEnum != EpisodeType.Episode && ep.EpisodeTypeEnum != EpisodeType.Special)
  247. useEp = false;
  248. if (useEp) epList.Add(ep);
  249. }
  250. if (epList.Count > 0)
  251. {
  252. Episode = epList[eprndm.Next(0, epList.Count)];
  253. needEpisode = false;
  254. SeriesMissing = false;
  255. SeriesExists = true;
  256. EpisodeMissing = false;
  257. EpisodeExists = true;
  258. ucPlayEpisode.DataContext = Episode;
  259. }
  260. else
  261. dictSeries.Remove(Series.AnimeSeriesID.Value);
  262. }
  263. }
  264. }
  265. catch (Exception ex)
  266. {
  267. Utils.ShowErrorMessage(ex);
  268. }
  269. }
  270. private List<AnimeSeriesVM> GetSeriesForGroupFilter()
  271. {
  272. List<AnimeSeriesVM> serList = new List<AnimeSeriesVM>();
  273. try
  274. {
  275. if (LevelType != RandomSeriesEpisodeLevel.GroupFilter) return serList;
  276. GroupFilterVM gf = LevelObject as GroupFilterVM;
  277. if (gf == null) return serList;
  278. List<AnimeGroupVM> grps = new List<AnimeGroupVM>(MainListHelperVM.Instance.AllGroups);
  279. foreach (AnimeGroupVM grp in grps)
  280. {
  281. // ignore sub groups
  282. if (grp.AnimeGroupParentID.HasValue) continue;
  283. if (gf.EvaluateGroupFilter(grp))
  284. {
  285. foreach (AnimeSeriesVM ser in grp.AllAnimeSeries)
  286. {
  287. if (gf.EvaluateGroupFilter(ser))
  288. {
  289. // categories
  290. if (!string.IsNullOrEmpty(SelectedCategories))
  291. {
  292. string filterParm = SelectedCategories.Trim();
  293. string[] cats = filterParm.Split(',');
  294. bool foundCat = false;
  295. if (cboCatFilter.SelectedIndex == 1) foundCat = true; // all
  296. int index = 0;
  297. foreach (string cat in cats)
  298. {
  299. if (cat.Trim().Length == 0) continue;
  300. if (cat.Trim() == ",") continue;
  301. index = ser.CategoriesString.IndexOf(cat, 0, StringComparison.InvariantCultureIgnoreCase);
  302. if (cboCatFilter.SelectedIndex == 0) // any
  303. {
  304. if (index > -1)
  305. {
  306. foundCat = true;
  307. break;
  308. }
  309. }
  310. else //all
  311. {
  312. if (index < 0)
  313. {
  314. foundCat = false;
  315. break;
  316. }
  317. }
  318. }
  319. if (!foundCat) continue;
  320. }
  321. serList.Add(ser);
  322. }
  323. }
  324. }
  325. }
  326. }
  327. catch (Exception ex)
  328. {
  329. Utils.ShowErrorMessage(ex);
  330. }
  331. return serList;
  332. }
  333. private List<AnimeSeriesVM> GetSeriesForGroup()
  334. {
  335. List<AnimeSeriesVM> serList = new List<AnimeSeriesVM>();
  336. try
  337. {
  338. if (LevelType != RandomSeriesEpisodeLevel.Group) return serList;
  339. AnimeGroupVM grp = LevelObject as AnimeGroupVM;
  340. if (grp == null) return serList;
  341. foreach (AnimeSeriesVM ser in grp.AllAnimeSeries)
  342. {
  343. // categories
  344. if (!string.IsNullOrEmpty(SelectedCategories))
  345. {
  346. string filterParm = SelectedCategories.Trim();
  347. string[] cats = filterParm.Split(',');
  348. bool foundCat = false;
  349. if (cboCatFilter.SelectedIndex == 1) foundCat = true; // all
  350. int index = 0;
  351. foreach (string cat in cats)
  352. {
  353. if (cat.Trim().Length == 0) continue;
  354. if (cat.Trim() == ",") continue;
  355. index = ser.CategoriesString.IndexOf(cat, 0, StringComparison.InvariantCultureIgnoreCase);
  356. if (cboCatFilter.SelectedIndex == 0) // any
  357. {
  358. if (index > -1)
  359. {
  360. foundCat = true;
  361. break;
  362. }
  363. }
  364. else //all
  365. {
  366. if (index < 0)
  367. {
  368. foundCat = false;
  369. break;
  370. }
  371. }
  372. }
  373. if (!foundCat) continue;
  374. }
  375. serList.Add(ser);
  376. }
  377. }
  378. catch (Exception ex)
  379. {
  380. Utils.ShowErrorMessage(ex);
  381. }
  382. return serList;
  383. }
  384. public void Init(RandomSeriesEpisodeLevel levelType, object levelObject)
  385. {
  386. LevelType = levelType;
  387. LevelObject = levelObject;
  388. PopulateCategories();
  389. }
  390. private void PopulateCategories()
  391. {
  392. AllCategoryNames = new ObservableCollection<string>();
  393. ViewCategoryNames = CollectionViewSource.GetDefaultView(AllCategoryNames);
  394. List<string> catsRaw = JMMServerVM.Instance.clientBinaryHTTP.GetAllCategoryNames();
  395. foreach (string cat in catsRaw)
  396. AllCategoryNames.Add(cat);
  397. }
  398. }
  399. }