Skip to content

Commit

Permalink
#125 refactor async task logic to work with mono
Browse files Browse the repository at this point in the history
  • Loading branch information
Drewster727 committed Apr 7, 2016
1 parent a6695db commit 33de634
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 55 deletions.
35 changes: 35 additions & 0 deletions PlexRequests.UI/Models/MovieSearchType.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
93 changes: 38 additions & 55 deletions PlexRequests.UI/Modules/SearchModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
using System.Threading.Tasks;
using TMDbLib.Objects.Search;
using PlexRequests.Api.Models.Tv;
using TMDbLib.Objects.General;

namespace PlexRequests.UI.Modules
{
Expand Down Expand Up @@ -131,77 +132,59 @@ private Negotiator RequestLoad()
private Response UpcomingMovies()
{
Log.Trace("Loading upcoming movies");

return ProcessMovies(new Task<List<SearchMovie>>(() =>
{
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<List<SearchMovie>>(() =>
{
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<List<SearchMovie>>(() =>
{
return MovieApi.SearchMovie(searchTerm).Result;
}));
return ProcessMovies(MovieSearchType.Search, searchTerm);
}

private Response ProcessMovies(Task<List<SearchMovie>> apiTask)
private Response ProcessMovies(MovieSearchType searchType, string searchTerm)
{
List<Task> taskList = new List<Task>();

List<SearchMovie> apiMovies = new List<SearchMovie>();

apiTask.ContinueWith((t) =>
List<MovieResult> apiMovies = new List<MovieResult>();
taskList.Add(Task.Factory.StartNew<List<MovieResult>>(() =>
{
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<MovieResult>();
}
}).ContinueWith((t) =>
{
apiMovies = t.Result;
});
taskList.Add(apiTask);
apiTask.Start();
}));

Dictionary<int, RequestedModel> dbMovies = new Dictionary<int, RequestedModel>();
taskList.Add(Task.Factory.StartNew(() =>
Expand All @@ -216,7 +199,7 @@ private Response ProcessMovies(Task<List<SearchMovie>> apiTask)
Task.WaitAll(taskList.ToArray());

List<SearchMovieViewModel> viewMovies = new List<SearchMovieViewModel>();
foreach (SearchMovie movie in apiMovies)
foreach (MovieResult movie in apiMovies)
{
var viewMovie = new SearchMovieViewModel()
{
Expand Down
1 change: 1 addition & 0 deletions PlexRequests.UI/PlexRequests.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
<Compile Include="Helpers\TvSender.cs" />
<Compile Include="Helpers\ValidationHelper.cs" />
<Compile Include="Models\DatatablesModel.cs" />
<Compile Include="Models\MovieSearchType.cs" />
<Compile Include="Models\QualityModel.cs" />
<Compile Include="Models\SearchViewModel.cs" />
<Compile Include="Models\SearchMusicViewModel.cs" />
Expand Down

0 comments on commit 33de634

Please sign in to comment.