Skip to content

Commit

Permalink
Artwork picking feature added for Games.
Browse files Browse the repository at this point in the history
  • Loading branch information
DineshSolanki committed Aug 6, 2021
1 parent a2b3c19 commit 9669250
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 45 deletions.
4 changes: 2 additions & 2 deletions FoliCon/Modules/DialogServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public static void ShowAboutBox(this IDialogService dialogService, Action<IDialo
{
dialogService.ShowDialog("AboutBox", new DialogParameters(), callBack);
}
public static void ShowPosterPicker(this IDialogService dialogService, Tmdb tmdbObject, ResultResponse result,int pickedIndex, System.Collections.ObjectModel.ObservableCollection<ListItem> resultData, bool isPickedById, Action<IDialogResult> callBack)
public static void ShowPosterPicker(this IDialogService dialogService, Tmdb tmdbObject, IgdbClass igdbObject, ResultResponse result,int pickedIndex, System.Collections.ObjectModel.ObservableCollection<ListItem> resultData, bool isPickedById, Action<IDialogResult> callBack)
{
var p = new DialogParameters
{
{"pickedIndex", pickedIndex}, {"result", result}, {"tmdbObject",tmdbObject} , {"resultList", resultData},
{"pickedIndex", pickedIndex}, {"result", result}, {"tmdbObject",tmdbObject}, {"igdbObject", igdbObject} , {"resultList", resultData},
{"isPickedById" , isPickedById}
};
dialogService.ShowDialog("PosterPicker", p, callBack);
Expand Down
10 changes: 8 additions & 2 deletions FoliCon/Modules/IGDBClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task<ResultResponse> SearchGameAsync(string query)
{
Contract.Assert(_serviceClient != null);
var r = await _serviceClient.QueryAsync<Game>(IGDBClient.Endpoints.Games,
$"search \"{query}\"; fields name,first_release_date,total_rating,summary,cover.*;");
$"search \"{query}\"; fields artworks.image_id, name,first_release_date,total_rating,summary,cover.*;");
var response = new ResultResponse
{
MediaType = MediaTypes.Game,
Expand All @@ -54,7 +54,7 @@ public async Task<ResultResponse> SearchGameByIdAsync(string id)
{
Contract.Assert(_serviceClient != null);
var r = await _serviceClient.QueryAsync<Game>(IGDBClient.Endpoints.Games,
$"fields name,first_release_date,total_rating,summary,cover.*; where id = {id};");
$"fields artworks.image_id, name,first_release_date,total_rating,summary,cover.*; where id = {id};");
var response = new ResultResponse
{
MediaType = MediaTypes.Game,
Expand All @@ -63,6 +63,12 @@ public async Task<ResultResponse> SearchGameByIdAsync(string id)
return response;
}

public async Task<Artwork[]> GetArtworksByGameIdAsync(string id)
{
var r = await _serviceClient.QueryAsync<Game>(IGDBClient.Endpoints.Games,
$"fields id, artworks.image_id; where id = {id};");
return r.First().Artworks.Values;
}
public static ObservableCollection<ListItem> ExtractGameDetailsIntoListItem(Game[] result)
{
var items = new ObservableCollection<ListItem>();
Expand Down
128 changes: 95 additions & 33 deletions FoliCon/ViewModels/PosterPickerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using FoliCon.Properties.Langs;
using TMDbLib.Objects.Collections;
using IGDB;
using IGDB.Models;
using TMDbLib.Objects.General;
using TMDbLib.Objects.Movies;
using TMDbLib.Objects.Search;
using TMDbLib.Objects.TvShows;
using Collection = TMDbLib.Objects.Collections.Collection;

namespace FoliCon.ViewModels
{
Expand All @@ -40,6 +43,7 @@ public class PosterPickerViewModel : BindableBase, IDialogAware
public ResultResponse Result { get => _result; set => SetProperty(ref _result, value); }
public int PickedIndex { get; private set; }
public Tmdb TmdbObject { get; private set; }
public IgdbClass IgdbObject { get; private set; }
public int TotalPosters { get => _totalPosters; set => SetProperty(ref _totalPosters, value); }
private ObservableCollection<ListItem> resultList;

Expand Down Expand Up @@ -85,53 +89,68 @@ public void OnDialogOpened(IDialogParameters parameters)
Result = parameters.GetValue<ResultResponse>("result");
PickedIndex = parameters.GetValue<int>("pickedIndex");
TmdbObject = parameters.GetValue<Tmdb>("tmdbObject");
IgdbObject = parameters.GetValue<IgdbClass>("igdbObject");
resultList = parameters.GetValue<ObservableCollection<ListItem>>("resultList");
_isPickedById = parameters.GetValue<bool>("isPickedById");
LoadData(_isPickedById ? Result.Result : Result.Result.Results[PickedIndex], Result.MediaType);
LoadData();

}
public void LoadData(dynamic result, string resultType)
public async void LoadData()
{
ImagesWithId images = new();
if (resultType == MediaTypes.Tv)
{
dynamic pickedResult = _isPickedById ? (TvShow)result : (SearchTv)result;
Title = pickedResult.Name;
images = TmdbObject.SearchTvImages(pickedResult.Id);
}
else if (resultType == MediaTypes.Movie)
{
dynamic pickedResult = _isPickedById ? (Movie)result : (SearchMovie)result;
Title = pickedResult.Title;
images = TmdbObject.SearchMovieImages(pickedResult.Id);
}
else if (resultType == MediaTypes.Collection)
{
dynamic pickedResult = _isPickedById ? (Collection)result : (SearchCollection)result;
Title = pickedResult.Name;
images = TmdbObject.SearchCollectionImages(pickedResult.Id);
}
else if (resultType == MediaTypes.Mtv)
var resultType = Result.MediaType;
var response = _isPickedById
? resultType == MediaTypes.Game ? Result.Result[0] : Result.Result
: resultType == MediaTypes.Game ? Result.Result : Result.Result.Results[PickedIndex];

if (resultType != MediaTypes.Game)
{
MediaType mediaType = result.MediaType;
switch (mediaType)
ImagesWithId images = new();
if (resultType == MediaTypes.Tv)
{
dynamic pickedResult = _isPickedById ? (TvShow)response : (SearchTv)response;
Title = pickedResult.Name;
images = TmdbObject.SearchTvImages(pickedResult.Id);
}
else if (resultType == MediaTypes.Movie)
{
dynamic pickedResult = _isPickedById ? (Movie)response : (SearchMovie)response;
Title = pickedResult.Title;
images = TmdbObject.SearchMovieImages(pickedResult.Id);
}
else if (resultType == MediaTypes.Collection)
{
dynamic pickedResult = _isPickedById ? (Collection)response : (SearchCollection)response;
Title = pickedResult.Name;
images = TmdbObject.SearchCollectionImages(pickedResult.Id);
}
else if (resultType == MediaTypes.Mtv)
{
case MediaType.Tv:
MediaType mediaType = response.MediaType;
switch (mediaType)
{
case MediaType.Tv:
{
SearchTv pickedResult = result;
SearchTv pickedResult = response;
Title = pickedResult.Name;
images = TmdbObject.SearchTvImages(pickedResult.Id);
break;
}
case MediaType.Movie:
case MediaType.Movie:
{
SearchMovie pickedResult = result;
SearchMovie pickedResult = response;
Title = pickedResult.Title;
images = TmdbObject.SearchMovieImages(pickedResult.Id);
break;
}
}
}
LoadImages(images);
}
else
{
Artwork[] images =response.Artworks.Values;
LoadImages(images);
}
LoadImages(images);
}

private async void LoadImages(ImagesWithId images)
Expand Down Expand Up @@ -168,12 +187,55 @@ private async void LoadImages(ImagesWithId images)
}
IsBusy = false;
}
private async void LoadImages(Artwork[] images)
{
StopSearch = false;
ImageUrl.Clear();
IsBusy = true;
if (images is not null && images.Length > 0)
{
TotalPosters = images.Length;

foreach (var item in images.GetEnumeratorWithIndex())
{
var image = item.Value;
Index = item.Index + 1;
if (image is not null)
{
var posterPath = image.ImageId != null ? "https://" + ImageHelper.GetImageUrl(item.Value.ImageId, ImageSize.ScreenshotMed)[2..] : null;
var bm = await Util.GetBitmapFromUrlAsync(posterPath);
ImageUrl.Add(new DArtImageList(image.ImageId, Util.LoadBitmap(bm)));
bm.Dispose();
}
if (_stopSearch)
{
break;
}
}
}
else
{
IsBusy = false;
MessageBox.Show(CustomMessageBox.Warning(LangProvider.GetLang("NoPosterFound"), Title));
}
IsBusy = false;
}
private void PickMethod(object parameter)
{
var link = (string)parameter;
var result = _isPickedById ? Result.Result : Result.Result.Results[PickedIndex];
result.PosterPath = link;
resultList[PickedIndex].Poster = link;
var result = _isPickedById
? Result.MediaType == MediaTypes.Game ? Result.Result[0] : Result.Result
: Result.MediaType == MediaTypes.Game ? Result.Result : Result.Result.Results[PickedIndex];
if (Result.MediaType == MediaTypes.Game)
{
result.Cover.Value.ImageId = link;
resultList[PickedIndex].Poster = "https://" + ImageHelper.GetImageUrl(link, ImageSize.HD720)[2..];
}
else
{
result.PosterPath = link;
resultList[PickedIndex].Poster = link;
}
CloseDialog("true");
}

Expand Down
15 changes: 7 additions & 8 deletions FoliCon/ViewModels/SearchResultViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,16 @@ private void MouseDoubleClick()
var pickedIndex = ResultListViewData.Data.IndexOf(ResultListViewData.SelectedItem);
try
{
if (_isPickedById && SearchResult.MediaType != MediaTypes.Game)
if (SearchResult.MediaType == MediaTypes.Game)
{

_dialogService.ShowPosterPicker(_tmdbObject, SearchResult, pickedIndex, ResultListViewData.Data,
_isPickedById, r => { });
if (SearchResult.Result[pickedIndex].Artworks is null)
{
MessageBox.Show(CustomMessageBox.Warning(LangProvider.GetLang("NoPosterFound"), SearchTitle));
return;
}
}
else if (SearchResult.MediaType != MediaTypes.Game)
{
_dialogService.ShowPosterPicker(_tmdbObject, SearchResult, pickedIndex, ResultListViewData.Data,
_dialogService.ShowPosterPicker(_tmdbObject,_igdbObject, SearchResult, pickedIndex, ResultListViewData.Data,
_isPickedById, r => { });
}
}
catch (Exception ex)
{
Expand Down

0 comments on commit 9669250

Please sign in to comment.