-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from pietrocaselani/episode_image_fix
Split ImageRepository into Movie, Show and Episode Repositories
- Loading branch information
Showing
31 changed files
with
1,003 additions
and
374 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import RxSwift | ||
|
||
public final class DefaultImageRepository: ImageRepository { | ||
private let movieImageRepository: MovieImageRepository | ||
private let showImageRepository: ShowImageRepository | ||
private let episodeImageRepository: EpisodeImageRepository | ||
|
||
public init(movieImageRepository: MovieImageRepository, | ||
showImageRepository: ShowImageRepository, | ||
episodeImageRepository: EpisodeImageRepository) { | ||
self.movieImageRepository = movieImageRepository | ||
self.showImageRepository = showImageRepository | ||
self.episodeImageRepository = episodeImageRepository | ||
} | ||
|
||
public func fetchMovieImages(for movieId: Int, | ||
posterSize: PosterImageSize?, | ||
backdropSize: BackdropImageSize?) -> Maybe<ImagesEntity> { | ||
return movieImageRepository.fetchMovieImages(for: movieId, posterSize: posterSize, backdropSize: backdropSize) | ||
} | ||
|
||
public func fetchShowImages(for showId: Int, | ||
posterSize: PosterImageSize?, | ||
backdropSize: BackdropImageSize?) -> Maybe<ImagesEntity> { | ||
return showImageRepository.fetchShowImages(for: showId, posterSize: posterSize, backdropSize: backdropSize) | ||
} | ||
|
||
public func fetchEpisodeImages(for episode: EpisodeImageInput, size: EpisodeImageSizes?) -> Maybe<URL> { | ||
return episodeImageRepository.fetchEpisodeImages(for: episode, size: size) | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
CouchTrackerCore/Images/EpisodeImageCachedRepository.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import Moya | ||
import RxSwift | ||
import TMDBSwift | ||
import TVDBSwift | ||
|
||
public final class EpisodeImageCachedRepository: EpisodeImageRepository { | ||
typealias TMDBEpisodes = TMDBSwift.Episodes | ||
typealias TVDBEpisodes = TVDBSwift.Episodes | ||
|
||
private let configurationRepository: ConfigurationRepository | ||
private let tmdb: TMDBProvider | ||
private let tvdb: TVDBProvider | ||
private var cache: [Int: URL] | ||
|
||
public init(tmdb: TMDBProvider, | ||
tvdb: TVDBProvider, | ||
configurationRepository: ConfigurationRepository, | ||
cache: [Int: URL] = [Int: URL]()) { | ||
self.configurationRepository = configurationRepository | ||
self.tmdb = tmdb | ||
self.tvdb = tvdb | ||
self.cache = cache | ||
} | ||
|
||
public func fetchEpisodeImages(for episode: EpisodeImageInput, size: EpisodeImageSizes? = nil) -> Maybe<URL> { | ||
let cacheKey = EpisodeImageUtils.cacheKey(episode: episode, size: size) | ||
|
||
guard let imageURL = cache[cacheKey] else { | ||
return fetchImageFromAPIs(episode: episode, size: size) | ||
.do(onNext: { [weak self] url in | ||
self?.cache[cacheKey] = url | ||
}) | ||
} | ||
|
||
return Maybe.just(imageURL) | ||
} | ||
|
||
private func fetchImageFromAPIs(episode: EpisodeImageInput, size: EpisodeImageSizes?) -> Maybe<URL> { | ||
guard let tvdbId = episode.tvdb else { | ||
guard let tmdbId = episode.tmdb else { | ||
return Maybe.empty() | ||
} | ||
|
||
return tmdbObservable(tmdbId, episode.season, episode.number, size?.tmdb ?? .w300) | ||
} | ||
|
||
let tvdbObservable = fetchEpisodeImageFromTVDB(tvdbId, size?.tvdb ?? .normal) | ||
|
||
guard let tmdbId = episode.tmdb else { | ||
return tvdbObservable | ||
} | ||
|
||
return tmdbObservable(tmdbId, episode.season, episode.number, size?.tmdb ?? .w300) | ||
.catchError { _ in tvdbObservable } | ||
.ifEmpty(switchTo: tvdbObservable) | ||
} | ||
|
||
private func tmdbObservable(_ showId: Int, _ season: Int, | ||
_ number: Int, _ size: StillImageSize) -> Maybe<URL> { | ||
return fetchEpisodeImageFromTMDB(showId, season, number, size) | ||
} | ||
|
||
private func fetchEpisodeImageFromTMDB(_ showId: Int, _ season: Int, | ||
_ number: Int, _ size: StillImageSize) -> Maybe<URL> { | ||
let target = TMDBEpisodes.images(showId: showId, season: season, episode: number) | ||
|
||
let apiObservable = TMDBImageUtils.imagesFromAPI(using: tmdb.episodes, with: target) | ||
let maybe = TMDBImageUtils.createImagesEntities(configurationRepository, apiObservable, stillSize: size).asMaybe() | ||
|
||
return maybe.flatMap { entity -> Maybe<URL> in | ||
guard let link = entity.stillImage()?.link, let url = URL(string: link) else { | ||
return Maybe.empty() | ||
} | ||
return Maybe.just(url) | ||
} | ||
} | ||
|
||
private func fetchEpisodeImageFromTVDB(_ tvdbId: Int, _ size: TVDBEpisodeImageSize) -> Maybe<URL> { | ||
let target = TVDBEpisodes.episode(id: tvdbId) | ||
|
||
let api = tvdb.episodes.rx.request(target).filterSuccessfulStatusAndRedirectCodes().map(EpisodeResponse.self) | ||
|
||
return api.flatMapMaybe { episodeResponse -> Maybe<URL> in | ||
guard let filename = episodeResponse.episode.filename else { return Maybe.empty() } | ||
let url = EpisodeImageUtils.tvdbBaseURLFor(size: size).appendingPathComponent(filename) | ||
return Maybe.just(url) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import TVDBSwift | ||
|
||
enum EpisodeImageUtils { | ||
static func tvdbBaseURLFor(size: TVDBEpisodeImageSize?) -> URL { | ||
return (size ?? .normal) == .normal ? TVDB.bannersImageURL : TVDB.smallBannersImageURL | ||
} | ||
|
||
static func cacheKey(episode: EpisodeImageInput, size: EpisodeImageSizes?) -> Int { | ||
var hasher = Hasher() | ||
hasher.combine(HashableEpisodeImageInput(episode)) | ||
size.run { hasher.combine($0) } | ||
return hasher.finalize() | ||
} | ||
} |
Oops, something went wrong.