Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sync framework #202

Merged
merged 32 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8413069
Create target CouchTrackerSync
Dec 30, 2019
5a58fe4
Bump build number
Dec 30, 2019
4f76bc8
Remove macOS targets and use only iOS targets
Dec 31, 2019
2ef74ca
Tests running for the Sync target
Dec 31, 2019
719bf02
Run SwiftLint
Dec 31, 2019
c9b7297
Refactoring CouchTrackerSyncTests
Dec 31, 2019
72f6600
Rename CouchTrackerSyncTests.swift to Helpers.swift
Jan 1, 2020
1de455e
Add Playground for Rx
Jan 1, 2020
607e26c
Small progress on syncing shows
Jan 1, 2020
df01a78
Improvements around the Extended info from Trakt
Jan 1, 2020
29bab70
Sync tests passing
Jan 1, 2020
9c47c89
Fix typos
Jan 1, 2020
d4b066d
Fix generate_secrets script
Jan 1, 2020
d734272
Fix warning
Jan 1, 2020
25933a2
Enable Build action on AllTests scheme
Jan 1, 2020
3ab1e81
Fastlane will run tests on AllTests scheme
Jan 1, 2020
611cbe8
Ignore Tests and Testable frameworks on codecov
Jan 1, 2020
a5227a4
Change an if-let to a guard-let
Jan 1, 2020
e309b68
Add firstAired and network properties into Trakt Season
Jan 1, 2020
f9feef0
Fix typo
Jan 1, 2020
ed6a099
CouchTrackerSyncTests green. We can sync and get back WatchedShows
Jan 7, 2020
22a0d09
Fix typo and compilation
Jan 7, 2020
2eda35f
Move TestableTrakt to TraktSwiftTestable framework
Jan 8, 2020
8e1ecc0
Fix TraktSwiftTests using TraktSwiftTestable
Jan 8, 2020
324aace
Add Moya to TraktSwiftTestable
Jan 8, 2020
955d050
Here we have a good version of the CouchTrackerSync framework
Jan 8, 2020
504e76c
We don't need to generate secrets for CouchTrackerSync anymore
Jan 8, 2020
5d94a42
Fix Sourcery templates
Jan 9, 2020
ac82ad8
Delete files used by code generation from CouchTrackerCore
Jan 9, 2020
ed7723a
Remove *.generated.swift from .gitignore
Jan 10, 2020
e042df7
Add generated files to git
Jan 10, 2020
f37bf4d
Update sourcery templates
Jan 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improvements around the Extended info from Trakt
  • Loading branch information
Pietro Caselani committed Jan 1, 2020
commit df01a784a64c1542e89e1b9826484a45d6a21cf3
5 changes: 3 additions & 2 deletions CouchTrackerCore/Core/Defaults.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import TraktSwift

public enum Defaults {
public static let showsSyncOptions = WatchedShowEntitiesSyncOptions(extended: Extended.fullEpisodes,
hiddingSpecials: false)
public static let showsSyncOptions = WatchedShowEntitiesSyncOptions(extended: .full,
hiddingSpecials: false,
seasonExtended: [.full, .episodes])
public static let appState = AppState(userSettings: nil, hideSpecials: false)

public static let itemsPerPage = 30
Expand Down
4 changes: 2 additions & 2 deletions CouchTrackerCore/Show/Episode/ShowEpisodeAPIRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public final class ShowEpisodeAPIRepository: ShowEpisodeRepository {
guard let strongSelf = self else { return Single.error(CouchTrackerError.selfDeinitialized) }

let options = WatchedShowEntitySyncOptions(showIds: showIds,
episodeExtended: .fullEpisodes,
seasonOptions: .yes(number: season, extended: .fullEpisodes),
episodeExtended: .full,
seasonOptions: .yes(number: season, extended: [.full, .episodes]),
hiddingSpecials: strongSelf.hideSpecials)

return strongSelf.synchronizer.syncWatchedShow(using: options)
Expand Down
2 changes: 1 addition & 1 deletion CouchTrackerCore/Synchronizer/CentralSynchronizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class CentralSynchronizer {

private static func syncOptionsFor(appState: AppState) -> WatchedShowEntitiesSyncOptions {
let defaultOptions = Defaults.showsSyncOptions
return WatchedShowEntitiesSyncOptions(extended: defaultOptions.extended, hiddingSpecials: appState.hideSpecials)
return WatchedShowEntitiesSyncOptions(extended: defaultOptions.extended, hiddingSpecials: appState.hideSpecials, seasonExtended: [.full, .episodes])
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class DefaultWatchedShowEntitiesDownloader: WatchedShowEntitiesDown
// MARK: - Public Interface

public func syncWatchedShowEntities(using options: WatchedShowEntitiesSyncOptions) -> Observable<WatchedShowEntity> {
return fetchWatchedShowsFromAPI(options: options).asObservable()
return fetchWatchedShowsFromAPI().asObservable()
.flatMap { [weak self] baseShows -> Observable<BaseShowWithGenres> in
guard let strongSelf = self else { return Observable.empty() }
return strongSelf.mapToBaseShowWithGenres(baseShows).asObservable()
Expand Down Expand Up @@ -77,16 +77,16 @@ public final class DefaultWatchedShowEntitiesDownloader: WatchedShowEntitiesDown

let showOptions = WatchedShowEntitySyncOptions(showIds: show.ids,
episodeExtended: options.extended,
seasonOptions: .yes(number: nil, extended: options.extended),
seasonOptions: .yes(number: nil, extended: options.seasonExtended),
hiddingSpecials: options.hiddingSpecials)

return showSynchronizer.syncWatchedShowEntitiy(using: showOptions).map { builder in
builder.set(detailShow: detailShow)
}
}

private func fetchWatchedShowsFromAPI(options: WatchedShowEntitiesSyncOptions) -> Single<[BaseShow]> {
let target = Sync.watched(type: .shows, extended: options.extended)
private func fetchWatchedShowsFromAPI() -> Single<[BaseShow]> {
let target = Sync.watched(type: .shows, extended: [.full, .noSeasons])
return trakt.sync.rx.request(target)
.filterSuccessfulStatusAndRedirectCodes()
.map([BaseShow].self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public final class DefaultWatchedShowEntityDownloader: WatchedShowEntityDownload

private func setSeasons(using options: WatchedShowEntitySyncOptions,
number: Int?,
seasonExtended: Extended,
seasonExtended: [Extended],
into builder: WatchedShowBuilder) -> Single<WatchedShowBuilder> {
return fetchSeasonsFromAPI(using: options, seasonExtended: seasonExtended, number: number)
.map { seasons -> [WatchedSeasonEntityBuilder] in
Expand Down Expand Up @@ -113,7 +113,7 @@ public final class DefaultWatchedShowEntityDownloader: WatchedShowEntityDownload
}

private func fetchSeasonsFromAPI(using options: WatchedShowEntitySyncOptions,
seasonExtended: Extended,
seasonExtended: [Extended],
number: Int?) -> Single<[Season]> {
let target = Seasons.summary(showId: options.showIds.realId, exteded: seasonExtended)
let seasonsSingle = trakt.seasons.rx.request(target)
Expand Down
6 changes: 4 additions & 2 deletions CouchTrackerCore/Synchronizer/SyncOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import TraktSwift

public enum SeasonSyncOptions {
case none
case yes(number: Int?, extended: Extended)
case yes(number: Int?, extended: [Extended])
}

public struct WatchedShowEntitySyncOptions {
Expand All @@ -22,9 +22,11 @@ public struct WatchedShowEntitySyncOptions {
public struct WatchedShowEntitiesSyncOptions {
public let extended: Extended
public let hiddingSpecials: Bool
public let seasonExtended: [Extended]

public init(extended: Extended, hiddingSpecials: Bool) {
public init(extended: Extended, hiddingSpecials: Bool, seasonExtended: [Extended]) {
self.extended = extended
self.hiddingSpecials = hiddingSpecials
self.seasonExtended = seasonExtended
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class ShowOverviewRepositoryTest: XCTestCase {

let expectedShow = TraktEntitiesMock.createTraktShowDetails()

_ = repository.fetchDetailsOfShow(with: "game-of-thrones", extended: .fullEpisodes)
_ = repository.fetchDetailsOfShow(with: "game-of-thrones", extended: .full)
.subscribe(onSuccess: { show in
testExpectation.fulfill()
XCTAssertEqual(show, expectedShow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension ShowsProgressMenuOptions {

enum ShowsProgressMocks {
static func createWatchedShowsMock() -> [BaseShow] {
return try! JSONDecoder().decode([BaseShow].self, from: Sync.watched(type: .shows, extended: .full).sampleData)
return try! JSONDecoder().decode([BaseShow].self, from: Sync.watched(type: .shows, extended: [.full]).sampleData)
}

static func createShowMock(_ showId: String) -> BaseShow? {
Expand Down
2 changes: 1 addition & 1 deletion CouchTrackerSync/CouchTrackerSync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func startSync(options: SyncOptions) -> Observable<BaseShow> {
}

private func syncMain(_ options: SyncOptions) -> Observable<BaseShow> {
return Current.syncWatchedShows(.noSeasons)
return Current.syncWatchedShows(.full)
.flatMap { Observable.from($0) }
.flatMap { watchedProgress(options: options.watchedProgress, baseShow: $0) }
}
Expand Down
6 changes: 6 additions & 0 deletions TraktSwift/Endpoints/Movies.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import Moya

public enum Movies {
/**
Only accepts .default or .full for Extended
*/
case trending(page: Int, limit: Int, extended: Extended)
/**
Only accepts .default or .full for Extended
*/
case summary(movieId: String, extended: Extended)
}

Expand Down
7 changes: 5 additions & 2 deletions TraktSwift/Endpoints/Seasons.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Moya

public enum Seasons {
case summary(showId: String, exteded: Extended)
/**
Accepts, .default, .full, and .episodes for Extended
*/
case summary(showId: String, exteded: [Extended])
}

extension Seasons: TraktType {
Expand All @@ -15,7 +18,7 @@ extension Seasons: TraktType {
public var task: Task {
switch self {
case let .summary(_, exteded):
return .requestParameters(parameters: ["extended": exteded.rawValue], encoding: URLEncoding.default)
return .requestParameters(parameters: ["extended": exteded.separatedByComma()], encoding: URLEncoding.default)
}
}

Expand Down
21 changes: 21 additions & 0 deletions TraktSwift/Endpoints/Shows.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import Moya

public enum Shows {
/**
Only accepts .default or .full for Extended
*/
case trending(page: Int, limit: Int, extended: Extended)
/**
Only accepts .default or .full for Extended
*/
case popular(page: Int, limit: Int, extended: Extended)
/**
Only accepts .default or .full for Extended
*/
case played(period: Period, page: Int, limit: Int, extended: Extended)
/**
Only accepts .default or .full for Extended
*/
case watched(period: Period, page: Int, limit: Int, extended: Extended)
/**
Only accepts .default or .full for Extended
*/
case collected(period: Period, page: Int, limit: Int, extended: Extended)
/**
Only accepts .default or .full for Extended
*/
case anticipated(page: Int, limit: Int, extended: Extended)
/**
Only accepts .default or .full for Extended
*/
case summary(showId: String, extended: Extended)
case watchedProgress(showId: String, hidden: Bool, specials: Bool, countSpecials: Bool)
}
Expand Down
7 changes: 5 additions & 2 deletions TraktSwift/Endpoints/Sync.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Moya

public enum Sync {
case watched(type: WatchedType, extended: Extended)
/**
Accepts .min, .default, .full, .noSeasons and .full for Extended
*/
case watched(type: WatchedType, extended: [Extended])
case addToHistory(items: SyncItems)
case removeFromHistory(items: SyncItems)
case history(params: HistoryParameters?)
Expand Down Expand Up @@ -47,7 +50,7 @@ extension Sync: TraktType {
public var task: Task {
switch self {
case let .watched(_, extended):
return .requestParameters(parameters: ["extended": extended.rawValue], encoding: URLEncoding.default)
return .requestParameters(parameters: ["extended": extended.separatedByComma()], encoding: URLEncoding.default)
case let .history(parameters):
guard let params = parameters else {
return .requestPlain
Expand Down
5 changes: 5 additions & 0 deletions TraktSwift/Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extension Sequence where Element == Extended {
public func separatedByComma() -> String {
return self.map { $0.rawValue }.joined(separator: ",")
}
}
1 change: 0 additions & 1 deletion TraktSwift/Models/Enums/Extended.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ public enum Extended: String {
case full
case noSeasons = "noseasons"
case episodes
case fullEpisodes = "full,episodes"
}
2 changes: 1 addition & 1 deletion TraktSwiftTests/JSONParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class JSONParsingTests: XCTestCase {
setupTraktForAuthentication(token)

// Then
_ = trakt.sync.rx.request(.watched(type: .shows, extended: .full)).map([BaseShow].self).asObservable().subscribe(observer)
_ = trakt.sync.rx.request(.watched(type: .shows, extended: [.full])).map([BaseShow].self).asObservable().subscribe(observer)

XCTAssertEqual(observer.events.count, 2)
let event = observer.events.first!
Expand Down
2 changes: 1 addition & 1 deletion TraktSwiftTests/SeasonsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class SeasonsTests: XCTestCase {
}

func testSeasons_requestSummaryforSeason_parseModels() {
let target = Seasons.summary(showId: "the-100", exteded: .fullEpisodes)
let target = Seasons.summary(showId: "the-100", exteded: [.full, .episodes])

let res = scheduler.start {
self.seasonsProvider.rx.request(target)
Expand Down
2 changes: 1 addition & 1 deletion TraktSwiftTests/ShowsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ShowsTest: XCTestCase {
}

func testShows_requestSummaryForAShow_parseToModels() {
let target = Shows.summary(showId: "game-of-thrones", extended: .fullEpisodes)
let target = Shows.summary(showId: "game-of-thrones", extended: .full)
let disposable = showsProvider.rx.request(target)
.map(Show.self)
.asObservable()
Expand Down
7 changes: 3 additions & 4 deletions TraktSwiftTests/TestableTrakt.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
@testable import CouchTrackerSync
import Moya
import TraktSwift
@testable import TraktSwift

public class TestableTrakt: Trakt, TraktProvider {
override func createProvider<T>(forTarget target: T.Type) -> MoyaProvider<T> where T: TraktType {
public class TestableTrakt: Trakt {
override public func createProvider<T>(forTarget target: T.Type) -> MoyaProvider<T> where T: TraktType {
let provider = super.createProvider(forTarget: target)

return MoyaProvider(endpointClosure: provider.endpointClosure,
Expand Down