Skip to content

Commit

Permalink
refactor: Simplify media type handling in PosterPickerViewModel
Browse files Browse the repository at this point in the history
Refactored PosterPickerViewModel to streamline media type handling by extracting methods for different media types. Improved readability and maintainability by reducing redundancy.
  • Loading branch information
DineshSolanki committed Jul 26, 2024
1 parent 22177c1 commit b35e522
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 56 deletions.
2 changes: 1 addition & 1 deletion FoliCon/Modules/TMDB/TMDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Tmdb(ref TMDbClient serviceClient, ref List<PickedListItem> listDataTable
{
Logger.Debug("Initializing TMDB Helper Class");
_tmdbService = new TmdbService(serviceClient);
_dataTransformer = new TmdbDataTransformer(ref listDataTable, ref imgDownloadList);
_dataTransformer = new TmdbDataTransformer(ref listDataTable, ref imgDownloadList, serviceClient);
Logger.Debug("Initialized TMDB Helper Class");
}

Expand Down
4 changes: 2 additions & 2 deletions FoliCon/Modules/TMDB/TMDBService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public async Task<ResultResponse> SearchByIdAsync(int id, string mediaType)
{
Logger.Info("Searching for {Id} in {MediaType}", id, mediaType);

if (!_mediaTypeHandlers.ContainsKey(mediaType))
if (!_mediaTypeHandlers.TryGetValue(mediaType, out var handler))
{
throw new ArgumentException($"Invalid media type: {mediaType}");
}

var r = await _mediaTypeHandlers[mediaType](id);
var r = await handler(id);

return new ResultResponse
{
Expand Down
138 changes: 85 additions & 53 deletions FoliCon/ViewModels/PosterPickerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,72 +109,99 @@ public void OnDialogOpened(IDialogParameters parameters)
}
public async Task LoadData()
{
var resultType = Result.MediaType;
dynamic response;
if (_isPickedById)
var resultType = _result.MediaType;
var response = GetResponse(resultType);

if (resultType == MediaTypes.Game)
{
response = resultType == MediaTypes.Game ? Result.Result[0] : Result.Result;
await LoadGameData(response);
}
else
{
response = resultType == MediaTypes.Game ? Result.Result[PickedIndex] : Result.Result.Results[PickedIndex];
await LoadMediaData(resultType, response);
}
}

if (resultType != MediaTypes.Game)
#region load data

private dynamic GetResponse(string resultType)
{
return (_isPickedById, resultType) switch
{
ImagesWithId images = new();
if (resultType == MediaTypes.Tv)
{
dynamic pickedResult = _isPickedById ? (TvShow)response : (SearchTv)response;
Title = pickedResult.Name;
images = await TmdbObject.SearchTvImages(pickedResult.Id);
}
else if (resultType == MediaTypes.Movie)
{
dynamic pickedResult = _isPickedById ? (Movie)response : (SearchMovie)response;
Title = pickedResult.Title;
images = await TmdbObject.SearchMovieImages(pickedResult.Id);
}
else if (resultType == MediaTypes.Collection)
{
dynamic pickedResult = _isPickedById ? (Collection)response : (SearchCollection)response;
Title = pickedResult.Name;
images = await TmdbObject.SearchCollectionImages(pickedResult.Id);
}
else if (resultType == MediaTypes.Mtv)
{
MediaType mediaType = response.MediaType;
switch (mediaType)
{
case MediaType.Tv:
{
SearchTv pickedResult = response;
Title = pickedResult.Name;
images = await TmdbObject.SearchTvImages(pickedResult.Id);
break;
}
case MediaType.Movie:
{
SearchMovie pickedResult = response;
Title = pickedResult.Title;
images = await TmdbObject.SearchMovieImages(pickedResult.Id);
break;
}
}
}
(true, MediaTypes.Game) => Result.Result[0],
(true, _) => Result.Result,
(false, MediaTypes.Game) => Result.Result[PickedIndex],
(false, _) => Result.Result.Results[PickedIndex]
};
}
private async Task LoadMediaData(string resultType, dynamic response)
{
ImagesWithId images = await GetTitleAndImages(resultType, response);

if (images != null)
{
var imageDataWrappers = images.Posters.Select(imageData => new ImageDataWrapper(images.Id, imageData, TmdbObject.GetClient()));
await LoadImages(imageDataWrappers);
}
else
}

private async Task<ImagesWithId> GetTitleAndImages(string resultType, dynamic response)
{
(string title, ImagesWithId images) result = resultType switch
{
Logger.Debug("Media Type is Game, loading images from IGDB");
Artwork[] images = response.Artworks.Values;
var artworkWrappers = images.Select(artwork => new ArtworkWrapper(artwork));
await LoadImages(artworkWrappers);
}
MediaTypes.Tv => await GetTvData(response),
MediaTypes.Movie => await GetMovieData(response),
MediaTypes.Collection => await GetCollectionData(response),
MediaTypes.Mtv => await GetMtvData(response),
_ => throw new ArgumentException($"Unexpected media type: {resultType}")
};

Title = result.title;
return result.images;
}

private async Task<(string title, ImagesWithId images)> GetTvData(dynamic response)
{
dynamic pickedResult = _isPickedById ? (TvShow)response : (SearchTv)response;
var images = await TmdbObject.SearchTvImages(pickedResult.Id);
return (pickedResult.Name, images);
}

private async Task<(string title, ImagesWithId images)> GetMovieData(dynamic response)
{
dynamic pickedResult = _isPickedById ? (Movie)response : (SearchMovie)response;
var images = await TmdbObject.SearchMovieImages(pickedResult.Id);
return (pickedResult.Title, images);
}

private async Task<(string title, ImagesWithId images)> GetCollectionData(dynamic response)
{
dynamic pickedResult = _isPickedById ? (Collection)response : (SearchCollection)response;
var images = await TmdbObject.SearchCollectionImages(pickedResult.Id);
return (pickedResult.Name, images);
}

private async Task<(string title, ImagesWithId images)> GetMtvData(dynamic response)
{
return response.MediaType switch
{
MediaType.Tv => await GetTvData(response),
MediaType.Movie => await GetMovieData(response),
_ => throw new ArgumentException($"Unexpected MTV media type: {response.MediaType}")
};
}

private async Task LoadGameData(dynamic response)
{
Logger.Debug("Media Type is Game, loading images from IGDB");
Artwork[] images = response.Artworks.Values;
var artworkWrappers = images.Select(artwork => new ArtworkWrapper(artwork));
await LoadImages(artworkWrappers);
}

#endregion


private async Task LoadImages(IEnumerable<IImage> images)
{
ResetState();
Expand All @@ -190,7 +217,9 @@ private async Task LoadImages(IEnumerable<IImage> images)
}
IsBusy = false;
}


#region LoadImages

private void ResetState()
{
StopSearch = false;
Expand Down Expand Up @@ -232,6 +261,9 @@ private void HandleNoImagesFound()
Logger.Warn("No posters found for {Title}", Title);
MessageBox.Show(CustomMessageBox.Warning(LangProvider.GetLang("NoPosterFound"), Title));
}

#endregion

private void PickMethod(DArtImageList pickedImage)
{
Logger.Info("Pick Method called with parameter: {Parameter}", pickedImage);
Expand Down

0 comments on commit b35e522

Please sign in to comment.