From 4dcfb62661454b3068b2285a277fe9ede5147a8f Mon Sep 17 00:00:00 2001 From: Pietro Caselani Date: Sun, 19 May 2019 17:36:12 -0300 Subject: [PATCH] Fix more warning --- .swiftlint.yml | 1 + TVDBSwift/TVDBTokenRequestInterceptor.swift | 4 +- TVDBSwiftTestable/Empty.swift | 0 TVDBSwiftTestable/TVDBTestableBundle.swift | 15 ++++ TraktSwift/Models/Airs.swift | 22 +---- TraktSwift/Models/Base/BaseEpisode.swift | 30 +------ TraktSwift/Models/Base/BaseIds.swift | 20 ++--- TraktSwift/Models/Base/BaseMovie.swift | 34 +------- TraktSwift/Models/Base/BaseSeason.swift | 24 +----- TraktSwift/Models/Base/BaseShow.swift | 50 +---------- .../Models/Base/BaseTrendingEntity.swift | 6 +- .../Models/Base/StandardMediaEntity.swift | 52 +++++------- TraktSwift/Models/Movie/Movie.swift | 65 +++++++-------- TraktSwift/Models/Movie/MovieIds.swift | 12 ++- TraktSwift/Models/SearchResult.swift | 26 +----- TraktSwift/Models/Show/Episode.swift | 29 ++++--- TraktSwift/Models/Show/EpisodeIds.swift | 18 ++-- TraktSwift/Models/Show/Season.swift | 40 +-------- TraktSwift/Models/Show/SeasonIds.swift | 26 +----- TraktSwift/Models/Show/Show.swift | 83 ++++++++----------- TraktSwift/Models/Show/ShowIds.swift | 21 +++-- .../Models/Sync/HistoryParameters.swift | 28 +------ .../Models/Trending/TrendingMovie.swift | 12 ++- TraktSwift/Models/Trending/TrendingShow.swift | 12 ++- TraktSwiftTests/SeasonsTests.swift | 2 +- TraktSwiftTests/ShowsTest.swift | 2 +- TraktSwiftTests/TraktTests.swift | 12 +-- 27 files changed, 207 insertions(+), 439 deletions(-) delete mode 100644 TVDBSwiftTestable/Empty.swift create mode 100644 TVDBSwiftTestable/TVDBTestableBundle.swift diff --git a/.swiftlint.yml b/.swiftlint.yml index fbb0e3c4..304edfcf 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -10,6 +10,7 @@ excluded: - TMDBSwiftTests - TMDBSwiftTestable - TVDBSwiftTests + - TVDBSwiftTestable - CouchTrackerPlayground.playground - Project.swift diff --git a/TVDBSwift/TVDBTokenRequestInterceptor.swift b/TVDBSwift/TVDBTokenRequestInterceptor.swift index ce4d69cc..06c36291 100644 --- a/TVDBSwift/TVDBTokenRequestInterceptor.swift +++ b/TVDBSwift/TVDBTokenRequestInterceptor.swift @@ -8,7 +8,9 @@ final class TVDBTokenRequestInterceptor: RequestInterceptor { self.tvdb = tvdb } - func intercept(target: T.Type, endpoint: Endpoint, done: @escaping MoyaProvider.RequestResultClosure) where T: TVDBType { + func intercept(target: T.Type, + endpoint: Endpoint, + done: @escaping MoyaProvider.RequestResultClosure) where T: TVDBType { guard let tvdb = self.tvdb else { done(.failure(MoyaError.requestMapping(endpoint.url))) return diff --git a/TVDBSwiftTestable/Empty.swift b/TVDBSwiftTestable/Empty.swift deleted file mode 100644 index e69de29b..00000000 diff --git a/TVDBSwiftTestable/TVDBTestableBundle.swift b/TVDBSwiftTestable/TVDBTestableBundle.swift new file mode 100644 index 00000000..099b21ac --- /dev/null +++ b/TVDBSwiftTestable/TVDBTestableBundle.swift @@ -0,0 +1,15 @@ +public final class TVDBTestableBundle { + public static let bundle = Bundle(for: TVDBTestableBundle.self) + + public static func url(forResource name: String) -> URL { + return bundle.url(forResource: name, withExtension: "json")! + } + + public static func data(forResource name: String) throws -> Data { + return try Data(contentsOf: url(forResource: name)) + } + + public static func decode(resource name: String, decoder: JSONDecoder = .init()) throws -> T { + return try decoder.decode(T.self, from: data(forResource: name)) + } +} diff --git a/TraktSwift/Models/Airs.swift b/TraktSwift/Models/Airs.swift index 1fe7d0a4..7b577b4e 100755 --- a/TraktSwift/Models/Airs.swift +++ b/TraktSwift/Models/Airs.swift @@ -1,4 +1,4 @@ -public final class Airs: Codable, Hashable { +public struct Airs: Codable, Hashable { public let day: String? public let time: String? public let timezone: String @@ -7,29 +7,11 @@ public final class Airs: Codable, Hashable { case day, time, timezone } - public required init(from decoder: Decoder) throws { + public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) day = try container.decodeIfPresent(String.self, forKey: .day) time = try container.decodeIfPresent(String.self, forKey: .time) timezone = try container.decode(String.self, forKey: .timezone) } - - public var hashValue: Int { - var hash = timezone.hashValue - - if let dayHash = day?.hashValue { - hash ^= dayHash - } - - if let timeHash = time?.hashValue { - hash ^= timeHash - } - - return hash - } - - public static func == (lhs: Airs, rhs: Airs) -> Bool { - return lhs.hashValue == rhs.hashValue - } } diff --git a/TraktSwift/Models/Base/BaseEpisode.swift b/TraktSwift/Models/Base/BaseEpisode.swift index 317339e6..a5d7e54d 100755 --- a/TraktSwift/Models/Base/BaseEpisode.swift +++ b/TraktSwift/Models/Base/BaseEpisode.swift @@ -1,6 +1,6 @@ import Foundation -public final class BaseEpisode: Codable, Hashable { +public struct BaseEpisode: Codable, Hashable { public let number: Int public let collectedAt: Date? public let plays: Int? @@ -15,7 +15,7 @@ public final class BaseEpisode: Codable, Hashable { case completed } - public required init(from decoder: Decoder) throws { + public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) number = try container.decode(Int.self, forKey: .number) @@ -28,30 +28,4 @@ public final class BaseEpisode: Codable, Hashable { let lastWatchedAt = try container.decodeIfPresent(String.self, forKey: .lastWatchedAt) self.lastWatchedAt = TraktDateTransformer.dateTimeTransformer.transformFromJSON(lastWatchedAt) } - - public var hashValue: Int { - var hash = number.hashValue - - if let collectedAtHash = collectedAt?.hashValue { - hash = hash ^ collectedAtHash - } - - if let playsHash = plays?.hashValue { - hash = hash ^ playsHash - } - - if let lastWatchedAtHash = lastWatchedAt?.hashValue { - hash = hash ^ lastWatchedAtHash - } - - if let completedHash = completed?.hashValue { - hash = hash ^ completedHash - } - - return hash - } - - public static func == (lhs: BaseEpisode, rhs: BaseEpisode) -> Bool { - return lhs.hashValue == rhs.hashValue - } } diff --git a/TraktSwift/Models/Base/BaseIds.swift b/TraktSwift/Models/Base/BaseIds.swift index 84d98b08..d9586a51 100644 --- a/TraktSwift/Models/Base/BaseIds.swift +++ b/TraktSwift/Models/Base/BaseIds.swift @@ -29,22 +29,16 @@ public class BaseIds: Codable, Hashable, CustomStringConvertible { try container.encodeIfPresent(imdb, forKey: .imdb) } - public var hashValue: Int { - var hash = trakt.hashValue - - if let tmdbHash = tmdb?.hashValue { - hash = hash ^ tmdbHash - } - - if let imdbHash = imdb?.hashValue { - hash = hash ^ imdbHash - } - - return hash + public func hash(into hasher: inout Hasher) { + hasher.combine(trakt) + hasher.combine(tmdb) + hasher.combine(imdb) } public static func == (lhs: BaseIds, rhs: BaseIds) -> Bool { - return lhs.hashValue == rhs.hashValue + return lhs.trakt == rhs.trakt && + lhs.tmdb == rhs.tmdb && + lhs.imdb == rhs.imdb } public var description: String { diff --git a/TraktSwift/Models/Base/BaseMovie.swift b/TraktSwift/Models/Base/BaseMovie.swift index 94342e40..095d4d4c 100644 --- a/TraktSwift/Models/Base/BaseMovie.swift +++ b/TraktSwift/Models/Base/BaseMovie.swift @@ -1,6 +1,6 @@ import Foundation -public final class BaseMovie: Codable, Hashable { +public struct BaseMovie: Codable, Hashable { public let movie: Movie? public let collectedAt: Date? public let watchedAt: Date? @@ -14,7 +14,7 @@ public final class BaseMovie: Codable, Hashable { case listedAt = "listed_at" } - public required init(from decoder: Decoder) throws { + public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) movie = try container.decodeIfPresent(Movie.self, forKey: .movie) @@ -28,34 +28,4 @@ public final class BaseMovie: Codable, Hashable { self.listedAt = TraktDateTransformer.dateTimeTransformer.transformFromJSON(listedAt) self.watchedAt = TraktDateTransformer.dateTimeTransformer.transformFromJSON(watchedAt) } - - public var hashValue: Int { - var hash = 11 - - if let movieHash = movie?.hashValue { - hash ^= movieHash - } - - if let collectedAtHash = collectedAt?.hashValue { - hash ^= collectedAtHash - } - - if let watchedAtHash = watchedAt?.hashValue { - hash ^= watchedAtHash - } - - if let listedAtHash = listedAt?.hashValue { - hash ^= listedAtHash - } - - if let playsHash = plays?.hashValue { - hash ^= playsHash - } - - return hash - } - - public static func == (lhs: BaseMovie, rhs: BaseMovie) -> Bool { - return lhs.hashValue == rhs.hashValue - } } diff --git a/TraktSwift/Models/Base/BaseSeason.swift b/TraktSwift/Models/Base/BaseSeason.swift index b7ad42f9..775dc115 100755 --- a/TraktSwift/Models/Base/BaseSeason.swift +++ b/TraktSwift/Models/Base/BaseSeason.swift @@ -1,4 +1,4 @@ -public final class BaseSeason: Codable, Hashable { +public struct BaseSeason: Codable, Hashable { public let number: Int public let episodes: [BaseEpisode] public let aired: Int? @@ -8,7 +8,7 @@ public final class BaseSeason: Codable, Hashable { case number, episodes, aired, completed } - public required init(from decoder: Decoder) throws { + public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) number = try container.decode(Int.self, forKey: .number) @@ -16,24 +16,4 @@ public final class BaseSeason: Codable, Hashable { aired = try container.decodeIfPresent(Int.self, forKey: .aired) completed = try container.decodeIfPresent(Int.self, forKey: .completed) } - - public var hashValue: Int { - var hash = number.hashValue - - if let airedHash = aired?.hashValue { - hash = hash ^ airedHash - } - - if let completedHash = completed?.hashValue { - hash = hash ^ completedHash - } - - episodes.forEach { hash = hash ^ $0.hashValue } - - return hash - } - - public static func == (lhs: BaseSeason, rhs: BaseSeason) -> Bool { - return lhs.hashValue == rhs.hashValue - } } diff --git a/TraktSwift/Models/Base/BaseShow.swift b/TraktSwift/Models/Base/BaseShow.swift index 062157de..334077d7 100755 --- a/TraktSwift/Models/Base/BaseShow.swift +++ b/TraktSwift/Models/Base/BaseShow.swift @@ -1,6 +1,6 @@ import Foundation -public final class BaseShow: Codable, Hashable { +public struct BaseShow: Codable, Hashable { public let show: Show? public let seasons: [BaseSeason]? public let lastCollectedAt: Date? @@ -21,7 +21,7 @@ public final class BaseShow: Codable, Hashable { case nextEpisode = "next_episode" } - public required init(from decoder: Decoder) throws { + public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) show = try container.decodeIfPresent(Show.self, forKey: .show) @@ -40,50 +40,4 @@ public final class BaseShow: Codable, Hashable { self.listedAt = TraktDateTransformer.dateTimeTransformer.transformFromJSON(listedAt) self.lastWatchedAt = TraktDateTransformer.dateTimeTransformer.transformFromJSON(lastWatchedAt) } - - public var hashValue: Int { - var hash = 11 - - if let showHash = show?.hashValue { - hash = hash ^ showHash - } - - seasons?.forEach { hash = hash ^ $0.hashValue } - - if let lastCollectedAtHash = lastCollectedAt?.hashValue { - hash = hash ^ lastCollectedAtHash - } - - if let listedAtHash = listedAt?.hashValue { - hash = hash ^ listedAtHash - } - - if let playsHash = plays?.hashValue { - hash = hash ^ playsHash - } - - if let lastWatchedAtHash = lastWatchedAt?.hashValue { - hash = hash ^ lastWatchedAtHash - } - - if let airedHash = aired?.hashValue { - hash = hash ^ airedHash - } - - if let completedHash = completed?.hashValue { - hash = hash ^ completedHash - } - - hiddenSeasons?.forEach { hash = hash ^ $0.hashValue } - - if let nextEpisodeHash = nextEpisode?.hashValue { - hash = hash ^ nextEpisodeHash - } - - return hash - } - - public static func == (lhs: BaseShow, rhs: BaseShow) -> Bool { - return lhs.hashValue == rhs.hashValue - } } diff --git a/TraktSwift/Models/Base/BaseTrendingEntity.swift b/TraktSwift/Models/Base/BaseTrendingEntity.swift index 11d32224..0f0613f2 100644 --- a/TraktSwift/Models/Base/BaseTrendingEntity.swift +++ b/TraktSwift/Models/Base/BaseTrendingEntity.swift @@ -1,11 +1,11 @@ public class BaseTrendingEntity: Codable, Hashable { public let watchers: Int - public var hashValue: Int { - return watchers.hashValue + public func hash(into hasher: inout Hasher) { + hasher.combine(watchers) } public static func == (lhs: BaseTrendingEntity, rhs: BaseTrendingEntity) -> Bool { - return lhs.hashValue == rhs.hashValue + return lhs.watchers == rhs.watchers } } diff --git a/TraktSwift/Models/Base/StandardMediaEntity.swift b/TraktSwift/Models/Base/StandardMediaEntity.swift index 2ad466bf..f3b2ac63 100644 --- a/TraktSwift/Models/Base/StandardMediaEntity.swift +++ b/TraktSwift/Models/Base/StandardMediaEntity.swift @@ -1,12 +1,12 @@ import Foundation public class StandardMediaEntity: Codable, Hashable { - public var title: String? - public var overview: String? - public var rating: Double? - public var votes: Int? - public var updatedAt: Date? - public var translations: [String]? + public let title: String? + public let overview: String? + public let rating: Double? + public let votes: Int? + public let updatedAt: Date? + public let translations: [String]? private enum CodingKeys: String, CodingKey { case title @@ -40,35 +40,21 @@ public class StandardMediaEntity: Codable, Hashable { self.updatedAt = TraktDateTransformer.dateTimeTransformer.transformFromJSON(updatedAt) } - public var hashValue: Int { - var hash = 0 - - if let titleHash = title?.hashValue { - hash = hash ^ titleHash - } - - if let overviewHash = overview?.hashValue { - hash = hash ^ overviewHash - } - - if let ratingHash = rating?.hashValue { - hash = hash ^ ratingHash - } - - if let votesHash = votes?.hashValue { - hash = hash ^ votesHash - } - - if let updatedAtHash = updatedAt?.hashValue { - hash = hash ^ updatedAtHash - } - - translations?.forEach { hash = hash ^ $0.hashValue } - - return hash + public func hash(into hasher: inout Hasher) { + hasher.combine(title) + hasher.combine(overview) + hasher.combine(rating) + hasher.combine(votes) + hasher.combine(updatedAt) + hasher.combine(translations) } public static func == (lhs: StandardMediaEntity, rhs: StandardMediaEntity) -> Bool { - return lhs.hashValue == rhs.hashValue + return lhs.title == rhs.title && + lhs.overview == rhs.overview && + lhs.rating == rhs.rating && + lhs.votes == rhs.votes && + lhs.updatedAt == rhs.updatedAt && + lhs.translations == rhs.translations } } diff --git a/TraktSwift/Models/Movie/Movie.swift b/TraktSwift/Models/Movie/Movie.swift index bcbcd332..221e5894 100644 --- a/TraktSwift/Models/Movie/Movie.swift +++ b/TraktSwift/Models/Movie/Movie.swift @@ -35,43 +35,34 @@ public final class Movie: StandardMediaEntity { try super.init(from: decoder) } - public override var hashValue: Int { - var hash = super.hashValue ^ ids.hashValue - - if let yearHash = year?.hashValue { - hash = hash ^ yearHash - } - - if let certificationHash = certification?.hashValue { - hash = hash ^ certificationHash - } - - if let taglineHash = tagline?.hashValue { - hash = hash ^ taglineHash - } - - if let releasedHash = released?.hashValue { - hash = hash ^ releasedHash - } - - if let runtimeHash = runtime?.hashValue { - hash = hash ^ runtimeHash - } - - if let trailerHash = trailer?.hashValue { - hash = hash ^ trailerHash - } - - if let homepageHash = homepage?.hashValue { - hash = hash ^ homepageHash - } - - if let languageHash = language?.hashValue { - hash = hash ^ languageHash - } - - genres?.forEach { hash = hash ^ $0.hashValue } + public override func hash(into hasher: inout Hasher) { + super.hash(into: &hasher) + hasher.combine(year) + hasher.combine(ids) + hasher.combine(certification) + hasher.combine(tagline) + hasher.combine(released) + hasher.combine(runtime) + hasher.combine(trailer) + hasher.combine(homepage) + hasher.combine(language) + hasher.combine(genres) + } - return hash + public static func == (lhs: Movie, rhs: Movie) -> Bool { + let lhsMediaEntity = lhs as StandardMediaEntity + let rhsMediaEntity = rhs as StandardMediaEntity + + return lhsMediaEntity == rhsMediaEntity && + lhs.year == rhs.year && + lhs.ids == rhs.ids && + lhs.certification == rhs.certification && + lhs.tagline == rhs.tagline && + lhs.released == rhs.released && + lhs.runtime == rhs.runtime && + lhs.trailer == rhs.trailer && + lhs.homepage == rhs.homepage && + lhs.language == rhs.language && + lhs.genres == rhs.genres } } diff --git a/TraktSwift/Models/Movie/MovieIds.swift b/TraktSwift/Models/Movie/MovieIds.swift index b38cf071..bab378da 100644 --- a/TraktSwift/Models/Movie/MovieIds.swift +++ b/TraktSwift/Models/Movie/MovieIds.swift @@ -25,7 +25,15 @@ public final class MovieIds: BaseIds { try super.encode(to: encoder) } - public override var hashValue: Int { - return super.hashValue ^ slug.hashValue + public override func hash(into hasher: inout Hasher) { + super.hash(into: &hasher) + hasher.combine(slug) + } + + public static func == (lhs: MovieIds, rhs: MovieIds) -> Bool { + let lhsBaseIds = lhs as BaseIds + let rhsBaseIds = rhs as BaseIds + + return lhsBaseIds == rhsBaseIds && lhs.slug == rhs.slug } } diff --git a/TraktSwift/Models/SearchResult.swift b/TraktSwift/Models/SearchResult.swift index a1932ccd..61ba55ee 100644 --- a/TraktSwift/Models/SearchResult.swift +++ b/TraktSwift/Models/SearchResult.swift @@ -1,4 +1,4 @@ -public final class SearchResult: Codable { +public struct SearchResult: Codable, Hashable { public let type: SearchType public let score: Double? public let movie: Movie? @@ -17,27 +17,3 @@ public final class SearchResult: Codable { show = try container.decodeIfPresent(Show.self, forKey: .show) } } - -extension SearchResult: Hashable { - public var hashValue: Int { - var hash = type.rawValue.hashValue - - if let scoreHash = score?.hashValue { - hash ^= scoreHash - } - - if let movieHash = movie?.hashValue { - hash ^= movieHash - } - - if let showHash = show?.hashValue { - hash ^= showHash - } - - return hash - } - - public static func == (lhs: SearchResult, rhs: SearchResult) -> Bool { - return lhs.hashValue == rhs.hashValue - } -} diff --git a/TraktSwift/Models/Show/Episode.swift b/TraktSwift/Models/Show/Episode.swift index 2b46f141..f1653687 100755 --- a/TraktSwift/Models/Show/Episode.swift +++ b/TraktSwift/Models/Show/Episode.swift @@ -42,17 +42,26 @@ public final class Episode: StandardMediaEntity { try super.init(from: decoder) } - public override var hashValue: Int { - var hash = super.hashValue ^ season.hashValue ^ number.hashValue ^ ids.hashValue - - if let absoluteNumberHash = absoluteNumber?.hashValue { - hash = hash ^ absoluteNumberHash - } + public override func hash(into hasher: inout Hasher) { + super.hash(into: &hasher) + hasher.combine(season) + hasher.combine(number) + hasher.combine(ids) + hasher.combine(absoluteNumber) + hasher.combine(firstAired) + hasher.combine(runtime) + } - if let firstAiredHash = firstAired?.hashValue { - hash = hash ^ firstAiredHash - } + public static func == (lhs: Episode, rhs: Episode) -> Bool { + let lhsMediaEntity = lhs as StandardMediaEntity + let rhsMediaEntity = rhs as StandardMediaEntity - return hash + return lhsMediaEntity == rhsMediaEntity && + lhs.title == rhs.title && + lhs.overview == rhs.overview && + lhs.rating == rhs.rating && + lhs.votes == rhs.votes && + lhs.updatedAt == rhs.updatedAt && + lhs.translations == rhs.translations } } diff --git a/TraktSwift/Models/Show/EpisodeIds.swift b/TraktSwift/Models/Show/EpisodeIds.swift index 4973bb0b..a7fa274e 100755 --- a/TraktSwift/Models/Show/EpisodeIds.swift +++ b/TraktSwift/Models/Show/EpisodeIds.swift @@ -29,17 +29,17 @@ public final class EpisodeIds: BaseIds { try super.encode(to: encoder) } - public override var hashValue: Int { - var hash = super.hashValue + public override func hash(into hasher: inout Hasher) { + super.hash(into: &hasher) + hasher.combine(tvdb) + hasher.combine(tvrage) + } - if let tvdbHash = tvdb?.hashValue { - hash ^= tvdbHash - } + public static func == (lhs: EpisodeIds, rhs: EpisodeIds) -> Bool { + let lhsBaseIds = lhs as BaseIds + let rhsBaseIds = rhs as BaseIds - if let tvrageHash = tvrage?.hashValue { - hash ^= tvrageHash - } - return hash + return lhsBaseIds == rhsBaseIds && lhs.tvdb == rhs.tvdb && lhs.tvrage == rhs.tvrage } public override var description: String { diff --git a/TraktSwift/Models/Show/Season.swift b/TraktSwift/Models/Show/Season.swift index 75bdda9c..f8311865 100755 --- a/TraktSwift/Models/Show/Season.swift +++ b/TraktSwift/Models/Show/Season.swift @@ -1,4 +1,4 @@ -public final class Season: Codable, Hashable { +public struct Season: Codable, Hashable { public let number: Int public let ids: SeasonIds public let overview: String? @@ -15,7 +15,7 @@ public final class Season: Codable, Hashable { case airedEpisodes = "aired_episodes" } - public required init(from decoder: Decoder) throws { + public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) number = try container.decode(Int.self, forKey: .number) @@ -28,40 +28,4 @@ public final class Season: Codable, Hashable { episodes = try container.decodeIfPresent([Episode].self, forKey: .episodes) title = try container.decodeIfPresent(String.self, forKey: .title) } - - public var hashValue: Int { - var hash = number.hashValue ^ ids.hashValue - - if let overviewHash = overview?.hashValue { - hash = hash ^ overviewHash - } - - if let ratingHash = rating?.hashValue { - hash = hash ^ ratingHash - } - - if let votesHash = votes?.hashValue { - hash = hash ^ votesHash - } - - if let episodeCounthash = episodeCount?.hashValue { - hash = hash ^ episodeCounthash - } - - if let airedEpisodesHash = airedEpisodes?.hashValue { - hash = hash ^ airedEpisodesHash - } - - if let titleHash = title?.hashValue { - hash = hash ^ titleHash - } - - episodes?.forEach { hash = hash ^ $0.hashValue } - - return hash - } - - public static func == (lhs: Season, rhs: Season) -> Bool { - return lhs.hashValue == rhs.hashValue - } } diff --git a/TraktSwift/Models/Show/SeasonIds.swift b/TraktSwift/Models/Show/SeasonIds.swift index e0e1e386..bfc8fee0 100755 --- a/TraktSwift/Models/Show/SeasonIds.swift +++ b/TraktSwift/Models/Show/SeasonIds.swift @@ -1,4 +1,4 @@ -public final class SeasonIds: Codable, Hashable { +public struct SeasonIds: Codable, Hashable { public let tvdb: Int? public let tmdb: Int? public let trakt: Int @@ -15,7 +15,7 @@ public final class SeasonIds: Codable, Hashable { self.tvrage = tvrage } - public required init(from decoder: Decoder) throws { + public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) tvdb = try container.decodeIfPresent(Int.self, forKey: .tvdb) @@ -32,26 +32,4 @@ public final class SeasonIds: Codable, Hashable { try container.encode(trakt, forKey: .trakt) try container.encodeIfPresent(tvrage, forKey: .tvrage) } - - public var hashValue: Int { - var hash = trakt.hashValue - - if let tmdbHash = tmdb?.hashValue { - hash ^= tmdbHash - } - - if let tvdbHash = tvdb?.hashValue { - hash ^= tvdbHash - } - - if let tvrageHash = tvrage?.hashValue { - hash ^= tvrageHash - } - - return hash - } - - public static func == (lhs: SeasonIds, rhs: SeasonIds) -> Bool { - return lhs.hashValue == rhs.hashValue - } } diff --git a/TraktSwift/Models/Show/Show.swift b/TraktSwift/Models/Show/Show.swift index fbc2f213..f4a34689 100755 --- a/TraktSwift/Models/Show/Show.swift +++ b/TraktSwift/Models/Show/Show.swift @@ -42,55 +42,40 @@ public final class Show: StandardMediaEntity { try super.init(from: decoder) } - public override var hashValue: Int { - var hash = super.hashValue ^ ids.hashValue - - if let yearHash = year?.hashValue { - hash = hash ^ yearHash - } - - if let firstAiredHash = firstAired?.hashValue { - hash = hash ^ firstAiredHash - } - - if let airsHash = airs?.hashValue { - hash = hash ^ airsHash - } - - if let runtimeHash = runtime?.hashValue { - hash = hash ^ runtimeHash - } - - if let certificationHash = certification?.hashValue { - hash = hash ^ certificationHash - } - - if let networkHash = network?.hashValue { - hash = hash ^ networkHash - } - - if let countryHash = country?.hashValue { - hash = hash ^ countryHash - } - - if let trailerHash = trailer?.hashValue { - hash = hash ^ trailerHash - } - - if let homepageHash = homepage?.hashValue { - hash = hash ^ homepageHash - } - - if let statusHash = status?.hashValue { - hash = hash ^ statusHash - } - - if let languageHash = language?.hashValue { - hash = hash ^ languageHash - } - - genres?.forEach { hash = hash ^ $0.hashValue } + public override func hash(into hasher: inout Hasher) { + super.hash(into: &hasher) + hasher.combine(year) + hasher.combine(ids) + hasher.combine(firstAired) + hasher.combine(airs) + hasher.combine(runtime) + hasher.combine(certification) + hasher.combine(network) + hasher.combine(country) + hasher.combine(trailer) + hasher.combine(homepage) + hasher.combine(status) + hasher.combine(language) + hasher.combine(genres) + } - return hash + public static func == (lhs: Show, rhs: Show) -> Bool { + let lhsMediaEntity = lhs as StandardMediaEntity + let rhsMediaEntity = rhs as StandardMediaEntity + + return lhsMediaEntity == rhsMediaEntity && + lhs.year == rhs.year && + lhs.ids == rhs.ids && + lhs.firstAired == rhs.firstAired && + lhs.airs == rhs.airs && + lhs.runtime == rhs.runtime && + lhs.certification == rhs.certification && + lhs.network == rhs.network && + lhs.country == rhs.country && + lhs.trailer == rhs.trailer && + lhs.homepage == rhs.homepage && + lhs.status == rhs.status && + lhs.language == rhs.language && + lhs.genres == rhs.genres } } diff --git a/TraktSwift/Models/Show/ShowIds.swift b/TraktSwift/Models/Show/ShowIds.swift index d6ecd2cb..e19ed7f9 100755 --- a/TraktSwift/Models/Show/ShowIds.swift +++ b/TraktSwift/Models/Show/ShowIds.swift @@ -38,12 +38,21 @@ public final class ShowIds: BaseIds { try super.encode(to: encoder) } - public override var hashValue: Int { - var hash = super.hashValue ^ slug.hashValue ^ tvdb.hashValue - if let tvrageHash = tvrage?.hashValue { - hash = hash ^ tvrageHash - } - return hash + public override func hash(into hasher: inout Hasher) { + super.hash(into: &hasher) + hasher.combine(slug) + hasher.combine(tvdb) + hasher.combine(tvrage) + } + + public static func == (lhs: ShowIds, rhs: ShowIds) -> Bool { + let lhsBaseIds = lhs as BaseIds + let rhsBaseIds = rhs as BaseIds + + return lhsBaseIds == rhsBaseIds && + lhs.slug == rhs.slug && + lhs.tvdb == rhs.tvdb && + lhs.tvrage == rhs.tvrage } public override var description: String { diff --git a/TraktSwift/Models/Sync/HistoryParameters.swift b/TraktSwift/Models/Sync/HistoryParameters.swift index 9bb1483e..2ab336c9 100644 --- a/TraktSwift/Models/Sync/HistoryParameters.swift +++ b/TraktSwift/Models/Sync/HistoryParameters.swift @@ -1,6 +1,6 @@ import Foundation -public final class HistoryParameters: Hashable { +public struct HistoryParameters: Hashable { public let type: HistoryType? public let id: Int? public let startAt, endAt: Date? @@ -11,30 +11,4 @@ public final class HistoryParameters: Hashable { self.startAt = startAt self.endAt = endAt } - - public var hashValue: Int { - var hash = 11 - - if let typeHash = type?.hashValue { - hash ^= typeHash - } - - if let idHash = id?.hashValue { - hash ^= idHash - } - - if let startAtHash = startAt?.hashValue { - hash ^= startAtHash - } - - if let endAtHash = endAt?.hashValue { - hash ^= endAtHash - } - - return hash - } - - public static func == (lhs: HistoryParameters, rhs: HistoryParameters) -> Bool { - return lhs.hashValue == rhs.hashValue - } } diff --git a/TraktSwift/Models/Trending/TrendingMovie.swift b/TraktSwift/Models/Trending/TrendingMovie.swift index 6db69158..87215804 100644 --- a/TraktSwift/Models/Trending/TrendingMovie.swift +++ b/TraktSwift/Models/Trending/TrendingMovie.swift @@ -21,7 +21,15 @@ public final class TrendingMovie: BaseTrendingEntity { try super.encode(to: encoder) } - public override var hashValue: Int { - return super.hashValue ^ movie.hashValue + public override func hash(into hasher: inout Hasher) { + super.hash(into: &hasher) + hasher.combine(movie) + } + + public static func == (lhs: TrendingMovie, rhs: TrendingMovie) -> Bool { + let lhsTrendingEntity = lhs as BaseTrendingEntity + let rhsTrendingEntity = rhs as BaseTrendingEntity + + return lhsTrendingEntity == rhsTrendingEntity && lhs.movie == rhs.movie } } diff --git a/TraktSwift/Models/Trending/TrendingShow.swift b/TraktSwift/Models/Trending/TrendingShow.swift index 5466612c..0445a081 100755 --- a/TraktSwift/Models/Trending/TrendingShow.swift +++ b/TraktSwift/Models/Trending/TrendingShow.swift @@ -21,7 +21,15 @@ public final class TrendingShow: BaseTrendingEntity { try super.encode(to: encoder) } - public override var hashValue: Int { - return super.hashValue ^ show.hashValue + public override func hash(into hasher: inout Hasher) { + super.hash(into: &hasher) + hasher.combine(show) + } + + public static func == (lhs: TrendingShow, rhs: TrendingShow) -> Bool { + let lhsTrendingEntity = lhs as BaseTrendingEntity + let rhsTrendingEntity = rhs as BaseTrendingEntity + + return lhsTrendingEntity == rhsTrendingEntity && lhs.show == rhs.show } } diff --git a/TraktSwiftTests/SeasonsTests.swift b/TraktSwiftTests/SeasonsTests.swift index 4fbdb4d2..c6f64ddb 100644 --- a/TraktSwiftTests/SeasonsTests.swift +++ b/TraktSwiftTests/SeasonsTests.swift @@ -37,7 +37,7 @@ final class SeasonsTests: XCTestCase { Swift.fatalError("Unable to parse JSON: \(error)") } - let expectedEvents = [next(200, expectedSeasons), completed(200)] + let expectedEvents = [Recorded.next(200, expectedSeasons), Recorded.completed(200)] XCTAssertEqual(res.events, expectedEvents) } diff --git a/TraktSwiftTests/ShowsTest.swift b/TraktSwiftTests/ShowsTest.swift index ee1ade19..080b6993 100644 --- a/TraktSwiftTests/ShowsTest.swift +++ b/TraktSwiftTests/ShowsTest.swift @@ -38,7 +38,7 @@ final class ShowsTest: XCTestCase { Swift.fatalError("Unable to parse JSON") } - let expectedEvents: [Recorded>] = [next(0, expectedShow), completed(0)] + let expectedEvents: [Recorded>] = [Recorded.next(0, expectedShow), Recorded.completed(0)] XCTAssertEqual(showObserver.events, expectedEvents) } diff --git a/TraktSwiftTests/TraktTests.swift b/TraktSwiftTests/TraktTests.swift index 0b861764..fed7624d 100644 --- a/TraktSwiftTests/TraktTests.swift +++ b/TraktSwiftTests/TraktTests.swift @@ -148,7 +148,7 @@ final class TraktTests: XCTestCase { // Then let expectedError = TraktError.cantAuthenticate(message: "Trying to authenticate without a secret or redirect URL") - let expectedEvents: [Recorded>] = [error(0, expectedError)] + let expectedEvents: [Recorded>] = [Recorded.error(0, expectedError)] XCTAssertEqual(observer.events, expectedEvents) } @@ -170,7 +170,7 @@ final class TraktTests: XCTestCase { // Then let expectedError = TraktError.cantAuthenticate(message: "Trying to authenticate without a secret or redirect URL") - let expectedEvents: [Recorded>] = [error(0, expectedError)] + let expectedEvents: [Recorded>] = [Recorded.error(0, expectedError)] XCTAssertEqual(observer.events, expectedEvents) } @@ -185,7 +185,7 @@ final class TraktTests: XCTestCase { _ = trakt.finishesAuthentication(with: request).asObservable().subscribe(observer) // Then - let expectedEvents = [next(0, AuthenticationResult.undetermined), completed(0)] + let expectedEvents = [Recorded.next(0, AuthenticationResult.undetermined), Recorded.completed(0)] XCTAssertEqual(observer.events, expectedEvents) } @@ -200,7 +200,7 @@ final class TraktTests: XCTestCase { _ = trakt.finishesAuthentication(with: request).asObservable().subscribe(observer) // Then - let expectedEvents = [next(0, AuthenticationResult.undetermined), completed(0)] + let expectedEvents = [Recorded.next(0, AuthenticationResult.undetermined), Recorded.completed(0)] XCTAssertEqual(observer.events, expectedEvents) } @@ -215,7 +215,7 @@ final class TraktTests: XCTestCase { _ = trakt.finishesAuthentication(with: request).asObservable().subscribe(observer) // Then - let expectedEvents = [next(0, AuthenticationResult.authenticated), completed(0)] + let expectedEvents = [Recorded.next(0, AuthenticationResult.authenticated), Recorded.completed(0)] XCTAssertEqual(observer.events, expectedEvents) } @@ -259,7 +259,7 @@ final class TraktTests: XCTestCase { _ = trakt.finishesAuthentication(with: request).asObservable().subscribe(observer) // Then - let expectedEvents = [next(0, AuthenticationResult.authenticated), completed(0)] + let expectedEvents = [Recorded.next(0, AuthenticationResult.authenticated), Recorded.completed(0)] XCTAssertEqual(observer.events, expectedEvents) }