Added functionality for previews. But previews are still crashing
This commit is contained in:
@@ -13,6 +13,12 @@ import CoreData
|
||||
// MARK: - Library Import
|
||||
|
||||
class LibraryImport {
|
||||
let CDContext : NSManagedObjectContext
|
||||
|
||||
init(context CDContext: NSManagedObjectContext = CDManager.shared.viewContext) {
|
||||
self.CDContext = CDContext
|
||||
}
|
||||
|
||||
static func backupName(from filename : String) -> String? {
|
||||
if filename.hasPrefix("libExp_") && filename.hasSuffix(".json") {
|
||||
var backupName = filename
|
||||
@@ -48,8 +54,8 @@ class LibraryImport {
|
||||
}
|
||||
|
||||
private func resetDatabase() {
|
||||
CDManager.shared.viewContext.reset()
|
||||
CDManager.shared.viewContext.performAndWait {
|
||||
self.CDContext.reset()
|
||||
self.CDContext.performAndWait {
|
||||
let deleteRequests =
|
||||
[NSBatchDeleteRequest(fetchRequest: Game.fetchRequest()),
|
||||
NSBatchDeleteRequest(fetchRequest: Accessory.fetchRequest()),
|
||||
@@ -59,7 +65,7 @@ class LibraryImport {
|
||||
let storeCoordinator = CDManager.shared.persistentContainer.persistentStoreCoordinator
|
||||
do {
|
||||
for deleteRequest in deleteRequests {
|
||||
try storeCoordinator.execute(deleteRequest, with: CDManager.shared.viewContext)
|
||||
try storeCoordinator.execute(deleteRequest, with: CDContext)
|
||||
}
|
||||
}catch let error {
|
||||
print(error)
|
||||
@@ -69,7 +75,7 @@ class LibraryImport {
|
||||
}
|
||||
|
||||
private func makeCDGame(from game: BHLGame, _ gameDict: inout [UUID : Game], _ cdConsole: Console) {
|
||||
let cdGame = Game(context: CDManager.shared.viewContext)
|
||||
let cdGame = Game(context: self.CDContext)
|
||||
gameDict[game.uuid] = cdGame
|
||||
cdGame.name = game.name ?? "n/a"
|
||||
cdGame.uuid = game.uuid
|
||||
@@ -93,7 +99,7 @@ class LibraryImport {
|
||||
}
|
||||
|
||||
private func makeCDAccessory(from accessory: BHLAccessory, _ accessoryDict: inout [UUID : Accessory], _ cdConsole: Console) {
|
||||
let cdAccessory = Accessory(context: CDManager.shared.viewContext)
|
||||
let cdAccessory = Accessory(context: self.CDContext)
|
||||
accessoryDict[accessory.uuid] = cdAccessory
|
||||
cdAccessory.name = accessory.name
|
||||
cdAccessory.uuid = accessory.uuid
|
||||
@@ -107,7 +113,7 @@ class LibraryImport {
|
||||
}
|
||||
|
||||
private func makeCDConsole(from console : BHLConsole) -> Console {
|
||||
let cdConsole = Console(context: CDManager.shared.viewContext)
|
||||
let cdConsole = Console(context: self.CDContext)
|
||||
cdConsole.name = console.name ?? "n/a"
|
||||
cdConsole.uuid = console.uuid
|
||||
cdConsole.manufacturer = console.manufacturer
|
||||
@@ -119,7 +125,7 @@ class LibraryImport {
|
||||
}
|
||||
|
||||
private func makeCDGameSeries(from gameSeries: BHLGameSeries, _ gameDict: inout [UUID : Game]) {
|
||||
let cdGameSeries = GameSeries(context: CDManager.shared.viewContext)
|
||||
let cdGameSeries = GameSeries(context: self.CDContext)
|
||||
cdGameSeries.name = gameSeries.name
|
||||
cdGameSeries.cover_icloud_path = gameSeries.cover_icloud_path
|
||||
|
||||
@@ -132,51 +138,59 @@ class LibraryImport {
|
||||
}
|
||||
|
||||
func importLIB(from filename: String) {
|
||||
do {
|
||||
guard let file = ICloudManager.backup_folder?.appendingPathComponent(filename) else {
|
||||
print("LibraryImport:importLIB Could not get icloud backup folder")
|
||||
return
|
||||
guard let file = ICloudManager.backup_folder?.appendingPathComponent(filename) else {
|
||||
print("LibraryImport:importLIB Could not get icloud backup folder")
|
||||
return
|
||||
}
|
||||
|
||||
if file.isFileURL {
|
||||
do {
|
||||
let data = try Data(contentsOf: file)
|
||||
importLIB(data: data)
|
||||
}catch let error {
|
||||
print("LibraryImport::importLIB::String - Cannot create data from file '\(error)'")
|
||||
}
|
||||
|
||||
if file.isFileURL {
|
||||
let library = try JSONDecoder().decode(BHLibrary.self, from: Data(contentsOf: file))
|
||||
resetDatabase()
|
||||
|
||||
var gameDict = [UUID:Game]()
|
||||
var accessoryDict = [UUID:Accessory]()
|
||||
|
||||
for console in library.consoles {
|
||||
let cdConsole = makeCDConsole(from: console)
|
||||
|
||||
print("CONSOLE: \(cdConsole.name ?? "n/a") with \(console.games.count) games")
|
||||
for uuid in console.games {
|
||||
|
||||
if let game = library.games.first(where: {$0.uuid == uuid}) {
|
||||
makeCDGame(from: game, &gameDict, cdConsole)
|
||||
}else{
|
||||
print("LibraryImport::importLIB - Game with UUID '\(uuid)' not found")
|
||||
}
|
||||
}
|
||||
|
||||
for uuid in console.accessories {
|
||||
if let accessory = library.accessories.first(where: {$0.uuid == uuid}) {
|
||||
makeCDAccessory(from: accessory, &accessoryDict, cdConsole)
|
||||
}else{
|
||||
print("LibraryImport::importLIB - Accessory with UUID '\(uuid)' not found")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for gameSeries in library.gameSeries {
|
||||
makeCDGameSeries(from: gameSeries, &gameDict)
|
||||
}
|
||||
}
|
||||
|
||||
}catch let error {
|
||||
print(error)
|
||||
print("LibraryImport::importLIB - Error while importing!")
|
||||
}
|
||||
}
|
||||
|
||||
func importLIB(data : Data) {
|
||||
do {
|
||||
let library = try JSONDecoder().decode(BHLibrary.self, from: data)
|
||||
resetDatabase()
|
||||
|
||||
var gameDict = [UUID:Game]()
|
||||
var accessoryDict = [UUID:Accessory]()
|
||||
|
||||
for console in library.consoles {
|
||||
let cdConsole = makeCDConsole(from: console)
|
||||
|
||||
print("CONSOLE: \(cdConsole.name ?? "n/a") with \(console.games.count) games")
|
||||
for uuid in console.games {
|
||||
|
||||
if let game = library.games.first(where: {$0.uuid == uuid}) {
|
||||
makeCDGame(from: game, &gameDict, cdConsole)
|
||||
}else{
|
||||
print("LibraryImport::importLIB - Game with UUID '\(uuid)' not found")
|
||||
}
|
||||
}
|
||||
|
||||
for uuid in console.accessories {
|
||||
if let accessory = library.accessories.first(where: {$0.uuid == uuid}) {
|
||||
makeCDAccessory(from: accessory, &accessoryDict, cdConsole)
|
||||
}else{
|
||||
print("LibraryImport::importLIB - Accessory with UUID '\(uuid)' not found")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for gameSeries in library.gameSeries {
|
||||
makeCDGameSeries(from: gameSeries, &gameDict)
|
||||
}
|
||||
}catch let error {
|
||||
print("LibraryImport::importLIB::DATA - Error while importing! '\(error)'")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - BHLLibrary Classes
|
||||
|
||||
Reference in New Issue
Block a user