From 33de634ade16a68c2af22237c725fb3dbf598f76 Mon Sep 17 00:00:00 2001 From: Drewster727 Date: Thu, 7 Apr 2016 14:29:23 -0500 Subject: [PATCH] #125 refactor async task logic to work with mono --- PlexRequests.UI/Models/MovieSearchType.cs | 35 +++++++++ PlexRequests.UI/Modules/SearchModule.cs | 93 +++++++++-------------- PlexRequests.UI/PlexRequests.UI.csproj | 1 + 3 files changed, 74 insertions(+), 55 deletions(-) create mode 100644 PlexRequests.UI/Models/MovieSearchType.cs diff --git a/PlexRequests.UI/Models/MovieSearchType.cs b/PlexRequests.UI/Models/MovieSearchType.cs new file mode 100644 index 000000000..9441ee15f --- /dev/null +++ b/PlexRequests.UI/Models/MovieSearchType.cs @@ -0,0 +1,35 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: PlexType.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +namespace PlexRequests.UI.Models +{ + public enum MovieSearchType + { + Upcoming, + CurrentlyPlaying, + Search + } +} \ No newline at end of file diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 7e001c489..a35a05055 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -51,6 +51,7 @@ using PlexRequests.UI.Models; using System.Threading.Tasks; using TMDbLib.Objects.Search; using PlexRequests.Api.Models.Tv; +using TMDbLib.Objects.General; namespace PlexRequests.UI.Modules { @@ -131,77 +132,59 @@ namespace PlexRequests.UI.Modules private Response UpcomingMovies() { Log.Trace("Loading upcoming movies"); - - return ProcessMovies(new Task>(() => - { - return MovieApi.GetUpcomingMovies().Result.Select(x => new SearchMovie() - { - Adult = x.Adult, - BackdropPath = x.BackdropPath, - GenreIds = x.GenreIds, - Id = x.Id, - OriginalLanguage = x.OriginalLanguage, - OriginalTitle = x.OriginalTitle, - Overview = x.Overview, - Popularity = x.Popularity, - PosterPath = x.PosterPath, - ReleaseDate = x.ReleaseDate, - Title = x.Title, - Video = x.Video, - VoteAverage = x.VoteAverage, - VoteCount = x.VoteCount - }).ToList(); - })); + return ProcessMovies(MovieSearchType.Upcoming, string.Empty); } private Response CurrentlyPlayingMovies() { Log.Trace("Loading currently playing movies"); - - return ProcessMovies(new Task>(() => - { - return MovieApi.GetCurrentPlayingMovies().Result.Select(x => new SearchMovie() - { - Adult = x.Adult, - BackdropPath = x.BackdropPath, - GenreIds = x.GenreIds, - Id = x.Id, - OriginalLanguage = x.OriginalLanguage, - OriginalTitle = x.OriginalTitle, - Overview = x.Overview, - Popularity = x.Popularity, - PosterPath = x.PosterPath, - ReleaseDate = x.ReleaseDate, - Title = x.Title, - Video = x.Video, - VoteAverage = x.VoteAverage, - VoteCount = x.VoteCount - }).ToList(); - })); + return ProcessMovies(MovieSearchType.CurrentlyPlaying, string.Empty); } private Response SearchMovie(string searchTerm) { Log.Trace("Searching for Movie {0}", searchTerm); - - return ProcessMovies(new Task>(() => - { - return MovieApi.SearchMovie(searchTerm).Result; - })); + return ProcessMovies(MovieSearchType.Search, searchTerm); } - private Response ProcessMovies(Task> apiTask) + private Response ProcessMovies(MovieSearchType searchType, string searchTerm) { List taskList = new List(); - List apiMovies = new List(); - - apiTask.ContinueWith((t) => + List apiMovies = new List(); + taskList.Add(Task.Factory.StartNew>(() => + { + switch(searchType) + { + case MovieSearchType.Search: + return MovieApi.SearchMovie(searchTerm).Result.Select(x => new MovieResult() + { + Adult = x.Adult, + BackdropPath = x.BackdropPath, + GenreIds = x.GenreIds, + Id = x.Id, + OriginalLanguage = x.OriginalLanguage, + OriginalTitle = x.OriginalTitle, + Overview = x.Overview, + Popularity = x.Popularity, + PosterPath = x.PosterPath, + ReleaseDate = x.ReleaseDate, + Title = x.Title, + Video = x.Video, + VoteAverage = x.VoteAverage, + VoteCount = x.VoteCount + }).ToList(); + case MovieSearchType.CurrentlyPlaying: + return MovieApi.GetCurrentPlayingMovies().Result.ToList(); + case MovieSearchType.Upcoming: + return MovieApi.GetUpcomingMovies().Result.ToList(); + default: + return new List(); + } + }).ContinueWith((t) => { apiMovies = t.Result; - }); - taskList.Add(apiTask); - apiTask.Start(); + })); Dictionary dbMovies = new Dictionary(); taskList.Add(Task.Factory.StartNew(() => @@ -216,7 +199,7 @@ namespace PlexRequests.UI.Modules Task.WaitAll(taskList.ToArray()); List viewMovies = new List(); - foreach (SearchMovie movie in apiMovies) + foreach (MovieResult movie in apiMovies) { var viewMovie = new SearchMovieViewModel() { diff --git a/PlexRequests.UI/PlexRequests.UI.csproj b/PlexRequests.UI/PlexRequests.UI.csproj index e7076ef55..7d4cbfb9a 100644 --- a/PlexRequests.UI/PlexRequests.UI.csproj +++ b/PlexRequests.UI/PlexRequests.UI.csproj @@ -169,6 +169,7 @@ +