-
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.
- Loading branch information
Pietro Caselani
committed
Jan 1, 2020
1 parent
1de455e
commit 607e26c
Showing
9 changed files
with
1,201 additions
and
2,287 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,23 +1,56 @@ | ||
import TraktSwift | ||
import RxSwift | ||
import Moya | ||
|
||
enum SyncState { | ||
case notSyncing | ||
case started | ||
enum SyncError: Error { | ||
case showIsNil | ||
} | ||
|
||
struct SyncData { | ||
var state: SyncState | ||
} | ||
struct WatchedProgressOptions { | ||
let hidden: Bool | ||
let specials: Bool | ||
let countSpecials: Bool | ||
|
||
enum SyncAction { | ||
case start | ||
init(hidden: Bool = false, specials: Bool = false, countSpecials: Bool = false) { | ||
self.hidden = hidden | ||
self.specials = specials | ||
self.countSpecials = countSpecials | ||
} | ||
} | ||
|
||
func syncReducer(syncData: inout SyncData, action: SyncAction) -> [Observable<SyncAction>] { | ||
switch action { | ||
case .start: | ||
syncData.state = .started | ||
struct SyncOptions { | ||
let watchedProgress: WatchedProgressOptions | ||
|
||
init(watchedProgress: WatchedProgressOptions = WatchedProgressOptions()) { | ||
self.watchedProgress = watchedProgress | ||
} | ||
} | ||
|
||
func startSync(options: SyncOptions) -> Observable<BaseShow> { | ||
return syncMain(options) | ||
} | ||
|
||
private func syncMain(_ options: SyncOptions) -> Observable<BaseShow> { | ||
return Current.syncWatchedShows(.noSeasons) | ||
.flatMap { Observable.from($0) } | ||
.flatMap { watchedProgress(options: options.watchedProgress, baseShow: $0) } | ||
} | ||
|
||
private func watchedProgress(options: WatchedProgressOptions, baseShow: BaseShow) -> Observable<BaseShow> { | ||
guard let show = baseShow.show else { return Observable.error(SyncError.showIsNil) } | ||
return Current.watchedProgress(options, show.ids).map { merge(syncBaseShow: baseShow, progressBaseShow: $0) } | ||
} | ||
|
||
return [] | ||
private func merge(syncBaseShow: BaseShow, progressBaseShow: BaseShow) -> BaseShow { | ||
BaseShow(show: syncBaseShow.show, | ||
seasons: progressBaseShow.seasons, | ||
lastCollectedAt: nil, | ||
listedAt: nil, | ||
plays: syncBaseShow.plays, | ||
lastWatchedAt: progressBaseShow.lastWatchedAt, | ||
aired: progressBaseShow.aired, | ||
completed: progressBaseShow.completed, | ||
hiddenSeasons: progressBaseShow.hiddenSeasons, | ||
nextEpisode: progressBaseShow.nextEpisode, | ||
lastEpisode: progressBaseShow.lastEpisode) | ||
} |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,41 @@ | ||
import TraktSwift | ||
import RxSwift | ||
|
||
struct SyncEnvironment { | ||
var networkScheduler: SchedulerType | ||
var trakt: TraktProvider | ||
let traktBuilder = TraktBuilder { | ||
$0.clientId = Secrets.Trakt.clientId | ||
$0.clientSecret = Secrets.Trakt.clientSecret | ||
$0.redirectURL = Secrets.Trakt.redirectURL | ||
$0.callbackQueue = DispatchQueue(label: "NetworkQueue", qos: .default) | ||
} | ||
|
||
extension SyncEnvironment { | ||
static let live: SyncEnvironment = { | ||
let networkQueue = DispatchQueue(label: "NetworkQueue", qos: .default) | ||
private let trakt = Trakt(builder: traktBuilder) | ||
|
||
let traktBuilder = TraktBuilder { | ||
$0.clientId = Secrets.Trakt.clientId | ||
$0.clientSecret = Secrets.Trakt.clientSecret | ||
$0.redirectURL = Secrets.Trakt.redirectURL | ||
$0.callbackQueue = networkQueue | ||
} | ||
struct SyncEnvironment { | ||
var syncWatchedShows: (Extended) -> Observable<[BaseShow]> | ||
var watchedProgress: (WatchedProgressOptions, ShowIds) -> Observable<BaseShow> | ||
} | ||
|
||
return SyncEnvironment( | ||
networkScheduler: ConcurrentDispatchQueueScheduler(queue: networkQueue), | ||
trakt: Trakt(builder: traktBuilder) | ||
) | ||
}() | ||
extension SyncEnvironment { | ||
static let live = SyncEnvironment(syncWatchedShows: syncWatchedShows(extended:), watchedProgress: watchedProgress(options:showIds:)) | ||
} | ||
|
||
#if DEBUG | ||
var Current = SyncEnvironment.live | ||
#else | ||
let Current = SyncEnvironment.live | ||
#endif | ||
|
||
private func syncWatchedShows(extended: Extended) -> Observable<[BaseShow]> { | ||
return trakt.sync.rx.request(.watched(type: .shows, extended: extended)).map([BaseShow].self).asObservable() | ||
} | ||
|
||
private func watchedProgress(options: WatchedProgressOptions, showIds: ShowIds) -> Observable<BaseShow> { | ||
return trakt.shows.rx.request( | ||
.watchedProgress( | ||
showId: showIds.realId, | ||
hidden: options.countSpecials, | ||
specials: options.countSpecials, | ||
countSpecials: options.countSpecials | ||
) | ||
).map(BaseShow.self).asObservable() | ||
} |
This file was deleted.
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
Oops, something went wrong.