Added functionality for previews. But previews are still crashing

This commit is contained in:
2021-05-25 14:43:42 +02:00
parent 6c12883cb3
commit e243727049
11 changed files with 233 additions and 87 deletions

View File

@@ -41,6 +41,7 @@
B9EC098523854C24004BC9AB /* QGrid in Frameworks */ = {isa = PBXBuildFile; productRef = B9EC098423854C24004BC9AB /* QGrid */; };
B9EC0987238555BF004BC9AB /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9EC0986238555BF004BC9AB /* SettingsView.swift */; };
B9ED3DD7265534C000FD2D46 /* test_date_utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9ED3DD6265534C000FD2D46 /* test_date_utils.swift */; };
B9ED3DD9265D1E5600FD2D46 /* CDPreview.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9ED3DD8265D1E5600FD2D46 /* CDPreview.swift */; };
B9F44ABA22F312E600FC6B29 /* ConsoleLibraryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9F44AB922F312E600FC6B29 /* ConsoleLibraryView.swift */; };
B9F44ABE22F31DEF00FC6B29 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9F44ABD22F31DEF00FC6B29 /* SceneDelegate.swift */; };
/* End PBXBuildFile section */
@@ -108,6 +109,7 @@
B9EC09812383F94B004BC9AB /* LibraryExport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LibraryExport.swift; sourceTree = "<group>"; };
B9EC0986238555BF004BC9AB /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
B9ED3DD6265534C000FD2D46 /* test_date_utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = test_date_utils.swift; sourceTree = "<group>"; };
B9ED3DD8265D1E5600FD2D46 /* CDPreview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CDPreview.swift; sourceTree = "<group>"; };
B9F44AB922F312E600FC6B29 /* ConsoleLibraryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConsoleLibraryView.swift; sourceTree = "<group>"; };
B9F44ABD22F31DEF00FC6B29 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -200,6 +202,7 @@
B93C1B9C21496BFD0014FD6E /* AppDelegate.swift */,
B9F44ABD22F31DEF00FC6B29 /* SceneDelegate.swift */,
B9BCF4C92168ACB600ECBAAC /* LaunchScreen.storyboard */,
B9ED3DD8265D1E5600FD2D46 /* CDPreview.swift */,
B98A735F22C1738800FB3410 /* CDManager.swift */,
B9BCCEB82653BDEA005F46D6 /* ICloudManager.swift */,
B93C1BA321496BFE0014FD6E /* Assets.xcassets */,
@@ -395,6 +398,7 @@
B9E2A07E233B6E4F00EAEB14 /* ConsoleAllView.swift in Sources */,
B9BCCEB92653BDEA005F46D6 /* ICloudManager.swift in Sources */,
B9EC0987238555BF004BC9AB /* SettingsView.swift in Sources */,
B9ED3DD9265D1E5600FD2D46 /* CDPreview.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -1,6 +1,9 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
},
"properties" : {
"compression-type" : "lossless"
}
}

View File

@@ -0,0 +1,13 @@
{
"data" : [
{
"filename" : "libExp_2021-05-20T16:53:25+0200.json",
"idiom" : "universal",
"universal-type-identifier" : "public.json"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

File diff suppressed because one or more lines are too long

View File

@@ -13,45 +13,100 @@ class CDManager {
static let shared : CDManager = CDManager();
private init() {
static var preview: CDManager = {
let controller = CDManager(inMemory: true)
//INIT Core Data
return controller
}()
let persistentContainer : NSPersistentCloudKitContainer
private init(inMemory: Bool = false) {
// If you didn't name your model Main you'll need
// to change this name below.
persistentContainer = NSPersistentCloudKitContainer(name: "Zockerhoehle")
if inMemory {
persistentContainer.persistentStoreDescriptions.first?.url = URL(fileURLWithPath: "/dev/null")
}
persistentContainer.loadPersistentStores { description, error in
if let error = error {
fatalError("Error: \(error.localizedDescription)")
}
}
}
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentCloudKitContainer(name: "Zockerhoehle")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
return container
}()
// lazy var persistentContainer: NSPersistentContainer = {
// /*
// The persistent container for the application. This implementation
// creates and returns a container, having loaded the store for the
// application to it. This property is optional since there are legitimate
// error conditions that could cause the creation of the store to fail.
// */
// let container = NSPersistentCloudKitContainer(name: "Zockerhoehle")
//
// container.loadPersistentStores(completionHandler: { (storeDescription, error) in
// if let error = error as NSError? {
// // Replace this implementation with code to handle the error appropriately.
// // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//
// /*
// Typical reasons for an error here include:
// * The parent directory does not exist, cannot be created, or disallows writing.
// * The persistent store is not accessible, due to permissions or data protection when the device is locked.
// * The device is out of space.
// * The store could not be migrated to the current model version.
// Check the error message to determine what the actual problem was.
// */
// fatalError("Unresolved error \(error), \(error.userInfo)")
// }
// })
// container.viewContext.automaticallyMergesChangesFromParent = true
// return container
// }()
//
// lazy var persistentContainerPreview: NSPersistentContainer = {
// /*
// The persistent container for the application. This implementation
// creates and returns a container, having loaded the store for the
// application to it. This property is optional since there are legitimate
// error conditions that could cause the creation of the store to fail.
// */
// let container = NSPersistentContainer(name: "Zockerhoehle")
//
// container.loadPersistentStores(completionHandler: { (storeDescription, error) in
// if let error = error as NSError? {
// // Replace this implementation with code to handle the error appropriately.
// // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//
// /*
// Typical reasons for an error here include:
// * The parent directory does not exist, cannot be created, or disallows writing.
// * The persistent store is not accessible, due to permissions or data protection when the device is locked.
// * The device is out of space.
// * The store could not be migrated to the current model version.
// Check the error message to determine what the actual problem was.
// */
// fatalError("Unresolved error \(error), \(error.userInfo)")
// }
// })
//
// container.viewContext.automaticallyMergesChangesFromParent = true
// return container
// }()
lazy var viewContext : NSManagedObjectContext = {
self.persistentContainer.viewContext
}()
lazy var viewContextPreview : NSManagedObjectContext = {
self.persistentContainer.viewContext
}()
// MARK: - Core Data Saving support
func saveContext () {

View File

@@ -0,0 +1,35 @@
//
// CDPreview.swift
// Zockerhoehle
//
// Created by Julian-Steffen Müller on 25.05.21.
// Copyright © 2021 Julian-Steffen Müller. All rights reserved.
//
import Foundation
import CoreData
import UIKit
struct CDPreview {
static let shared : CDPreview = CDPreview()
private init() {
let asset_core_data = NSDataAsset(name: "CoreDataSample", bundle: Bundle.main)
guard let data = asset_core_data?.data else { return }
LibraryImport(context: CDManager.preview.viewContextPreview).importLIB(data: data)
}
func consoles() -> [Console] {
let consolesFR = NSFetchRequest<Console>(entityName: "Console")
do {
let consoles : [Console] = try CDManager.preview.viewContextPreview.fetch(consolesFR)
return consoles
}catch {
return []
}
}
}

View File

@@ -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,14 +138,24 @@ 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
}
if file.isFileURL {
let library = try JSONDecoder().decode(BHLibrary.self, from: Data(contentsOf: file))
do {
let data = try Data(contentsOf: file)
importLIB(data: data)
}catch let error {
print("LibraryImport::importLIB::String - Cannot create data from file '\(error)'")
}
}
}
func importLIB(data : Data) {
do {
let library = try JSONDecoder().decode(BHLibrary.self, from: data)
resetDatabase()
var gameDict = [UUID:Game]()
@@ -170,13 +186,11 @@ class LibraryImport {
for gameSeries in library.gameSeries {
makeCDGameSeries(from: gameSeries, &gameDict)
}
}catch let error {
print("LibraryImport::importLIB::DATA - Error while importing! '\(error)'")
}
}
}catch let error {
print(error)
print("LibraryImport::importLIB - Error while importing!")
}
}
}
// MARK: - BHLLibrary Classes

View File

@@ -7,6 +7,7 @@
//
import SwiftUI
import CoreData
struct ModalAddToConsoleLibrary : View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

View File

@@ -203,5 +203,4 @@ struct GameDetailView : View {
}), secondaryButton: Alert.Button.cancel(Text("Lieber doch nicht")))
})
}
}

View File

@@ -65,6 +65,8 @@ struct ModalAddGameSeries : View {
}
struct GameSeriesAllView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(entity: GameSeries.entity(), sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)])
var gameSeries: FetchedResults<GameSeries>
@@ -104,3 +106,23 @@ struct GameSeriesAllView: View {
}
}
}
struct GameSeriesAllView_Preview : PreviewProvider {
static var previews: some View {
//let consoles : [Console] = CDPreview.shared.consoles()
//if consoles.count > 0 {
//let console : Console = consoles[0]
//let v = GameSeriesAllView()//.environment(\.managedObjectContext, CDManager.preview.viewContext)
return Text("muzh")
//}else{
// return Text("kuH")
//}
//return Text("bla")
//let preview_viww = ConsoleLibraryView(console: console).environment(\.managedObjectContext, CDManager.shared.viewContextPreview)
}
}

View File

@@ -104,7 +104,6 @@ struct Overview: View {
var GamesOverview : some View {
VStack(alignment: .leading) {
//TODO
OverviewHeader(title: "Zuletzt gehortete Spiele", destination: GamePickupsView())
if games.count == 0 {
@@ -205,9 +204,9 @@ struct Overview: View {
NavigationView {
ScrollView(.vertical, showsIndicators: true) {
VStack {
//featured
Text("TODO FEATURED")
Divider()
////featured
//Text("TODO FEATURED")
//Divider()
ConsolesOverview
Divider()
GamesOverview