From 9669250c0fe4de4dff0cc3180bbfded5229d5772 Mon Sep 17 00:00:00 2001 From: Dinesh Solanki <15937452+DineshSolanki@users.noreply.github.com> Date: Sat, 7 Aug 2021 00:23:39 +0530 Subject: [PATCH] Artwork picking feature added for Games. --- FoliCon/Modules/DialogServiceExtensions.cs | 4 +- FoliCon/Modules/IGDBClass.cs | 10 +- FoliCon/ViewModels/PosterPickerViewModel.cs | 128 +++++++++++++++----- FoliCon/ViewModels/SearchResultViewModel.cs | 15 ++- 4 files changed, 112 insertions(+), 45 deletions(-) diff --git a/FoliCon/Modules/DialogServiceExtensions.cs b/FoliCon/Modules/DialogServiceExtensions.cs index a022815f..153878c1 100644 --- a/FoliCon/Modules/DialogServiceExtensions.cs +++ b/FoliCon/Modules/DialogServiceExtensions.cs @@ -58,11 +58,11 @@ public static void ShowAboutBox(this IDialogService dialogService, Action resultData, bool isPickedById, Action callBack) + public static void ShowPosterPicker(this IDialogService dialogService, Tmdb tmdbObject, IgdbClass igdbObject, ResultResponse result,int pickedIndex, System.Collections.ObjectModel.ObservableCollection resultData, bool isPickedById, Action 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); diff --git a/FoliCon/Modules/IGDBClass.cs b/FoliCon/Modules/IGDBClass.cs index 0bf4443c..e4239351 100644 --- a/FoliCon/Modules/IGDBClass.cs +++ b/FoliCon/Modules/IGDBClass.cs @@ -42,7 +42,7 @@ public async Task SearchGameAsync(string query) { Contract.Assert(_serviceClient != null); var r = await _serviceClient.QueryAsync(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, @@ -54,7 +54,7 @@ public async Task SearchGameByIdAsync(string id) { Contract.Assert(_serviceClient != null); var r = await _serviceClient.QueryAsync(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, @@ -63,6 +63,12 @@ public async Task SearchGameByIdAsync(string id) return response; } + public async Task GetArtworksByGameIdAsync(string id) + { + var r = await _serviceClient.QueryAsync(IGDBClient.Endpoints.Games, + $"fields id, artworks.image_id; where id = {id};"); + return r.First().Artworks.Values; + } public static ObservableCollection ExtractGameDetailsIntoListItem(Game[] result) { var items = new ObservableCollection(); diff --git a/FoliCon/ViewModels/PosterPickerViewModel.cs b/FoliCon/ViewModels/PosterPickerViewModel.cs index 596137da..6a806ce3 100644 --- a/FoliCon/ViewModels/PosterPickerViewModel.cs +++ b/FoliCon/ViewModels/PosterPickerViewModel.cs @@ -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 { @@ -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 resultList; @@ -85,53 +89,68 @@ public void OnDialogOpened(IDialogParameters parameters) Result = parameters.GetValue("result"); PickedIndex = parameters.GetValue("pickedIndex"); TmdbObject = parameters.GetValue("tmdbObject"); + IgdbObject = parameters.GetValue("igdbObject"); resultList = parameters.GetValue>("resultList"); _isPickedById = parameters.GetValue("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) @@ -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"); } diff --git a/FoliCon/ViewModels/SearchResultViewModel.cs b/FoliCon/ViewModels/SearchResultViewModel.cs index 52469b3a..6bc5eb8b 100644 --- a/FoliCon/ViewModels/SearchResultViewModel.cs +++ b/FoliCon/ViewModels/SearchResultViewModel.cs @@ -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) {