Skip to content

Commit

Permalink
Revert "#18: Simplified some of the import logic."
Browse files Browse the repository at this point in the history
This reverts commit 1a7f552.
  • Loading branch information
jrtibbetts committed May 11, 2019
1 parent 1a7f552 commit 0e875a0
Showing 1 changed file with 50 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,28 @@ public class DiscogsCollectionImporter: NSManagedObjectContext {
self.discogsFields = discogsFieldsResult.fields ?? []

return self.createCoreDataFields(self.discogsFields)
}.then { (coreDataFields) in
}.then { (coreDataFields) /* -> Promise<CollectionFolders> */ in
self.discogs.collectionFolders(forUserName: userName)
}.then { (discogsFoldersResult) -> Promise<CoreDataFoldersByID> in
self.discogsFolders = discogsFoldersResult.folders

return self.createCoreDataFolders(forDiscogsFolders: discogsFoldersResult.folders)
}.then { (coreDataFoldersByID) -> Promise<CollectionFolderItems> in
let masterFolderID = 0

guard let masterFolder = coreDataFoldersByID[Int64(masterFolderID)] else {
}.then { (coreDataFoldersByID) -> Guarantee<[Result<CollectionFolderItems>]> in
guard let masterFolder = coreDataFoldersByID[Int64(0)] else {
throw ImportError.noAllFolderWasFound
}

return self.discogs.collectionItems(inFolderID: masterFolderID,
userName: userName,
pageNumber: 1,
resultsPerPage: Int(masterFolder.expectedItemCount))
}.then { (discogsItemsResult) -> Promise<CoreDataItemsByID> in
guard let discogsItems = discogsItemsResult.releases else {
return Promise<CoreDataItemsByID> { $0.fulfill(CoreDataItemsByID()) }
return self.downloadDiscogsItems(forUserName: userName,
inFolderWithID: 0,
expectedItemCount: Int(masterFolder.expectedItemCount))
}.then { (discogsItemsResultsGuarantee) -> Promise<CoreDataItemsByID> in
let discogsItems = discogsItemsResultsGuarantee.reduce([CollectionFolderItem]()) { (allItems, result) in
switch result {
case .fulfilled(let discogsCollectionItems):
return allItems + (discogsCollectionItems.releases ?? [])
default:
return allItems
}
}

print("Importing \(discogsItems.count) Discogs collection items.")
Expand Down Expand Up @@ -142,6 +144,22 @@ public class DiscogsCollectionImporter: NSManagedObjectContext {
}
}

public func downloadDiscogsItems(forUserName userName: String,
inFolderWithID folderID: Int,
expectedItemCount: Int) -> Guarantee<[Result<CollectionFolderItems>]> {
let pageSize = 100
let pageCount = (expectedItemCount / pageSize) + 1

let pagePromises: [Promise<CollectionFolderItems>] = pageCount.times.map { (pageNumber) -> Promise<CollectionFolderItems> in
return discogs.collectionItems(inFolderID: folderID,
userName: userName,
pageNumber: pageNumber + 1,
resultsPerPage: pageSize)
}

return when(resolved: pagePromises)
}

public func createCoreDataItems(forDiscogsItems discogsItems: [SwiftDiscogs.CollectionFolderItem]) -> Promise<CoreDataItemsByID> {
return Promise<CoreDataItemsByID> { (seal) in
coreDataItemsByID = [:]
Expand All @@ -162,30 +180,29 @@ public class DiscogsCollectionImporter: NSManagedObjectContext {

func addCoreDataItemsToOtherFolders(forUserName userName: String) {
discogsFolders.filter { $0.id != 0 }.forEach { (discogsFolder) in
addCoreDataItemsToFolder(discogsFolder.id,
folderItemCount: discogsFolder.count,
userName: userName)
}
}

func addCoreDataItemsToFolder(_ folderID: Int,
folderItemCount: Int,
userName: String) {
guard let coreDataFolder = self.coreDataFoldersByID[Int64(folderID)] else {
return
}
guard let coreDataFolder = self.coreDataFoldersByID[Int64(discogsFolder.id)] else {
return
}

discogs.collectionItems(inFolderID: folderID,
userName: userName,
pageNumber: 1,
resultsPerPage: folderItemCount).done { (discogsItemsResult) in
discogsItemsResult.releases?.forEach { (discogsItem) in
if let coreDataItem = self.coreDataItemsByID[Int64(folderID)] {
print("Adding item \(discogsItem.id) to folder \(folderID)")
coreDataItem.addToFolders(coreDataFolder)
downloadDiscogsItems(forUserName: userName,
inFolderWithID: discogsFolder.id,
expectedItemCount: discogsFolder.count).done { (downloadResults) in
print("Downloaded \(downloadResults.count) results in folder \(discogsFolder.id)")
downloadResults.forEach { (downloadResult) in
switch downloadResult {
case .fulfilled(let discogsItems):
discogsItems.releases?.forEach { (discogsItem) in
if let coreDataItem = self.coreDataItemsByID[Int64(discogsItem.id)] {
print("Adding item \(discogsItem.id) to folder \(discogsFolder.id)")
coreDataItem.addToFolders(coreDataFolder)
}
}
default:
break
}
}
}.cauterize()
}
}
}

}
Expand Down

0 comments on commit 0e875a0

Please sign in to comment.