Skip to content

Commit

Permalink
CSV import with DataController working now
Browse files Browse the repository at this point in the history
  • Loading branch information
In-Ko committed Jun 9, 2023
1 parent 208aa82 commit d513670
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 42 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified CookingFam/.DS_Store
Binary file not shown.
14 changes: 13 additions & 1 deletion CookingFam/CookingFam.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
3B5278DB2A3379F2008CD8F3 /* BackButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B5278DA2A3379F2008CD8F3 /* BackButton.swift */; };
3BDDA13A2A33B53E0074CB1D /* Recipe.csv in Resources */ = {isa = PBXBuildFile; fileRef = 3BDDA1392A33B53E0074CB1D /* Recipe.csv */; };
5E50573B2A1E8CA40097339F /* ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E50573A2A1E8CA40097339F /* ViewModel.swift */; };
5E50573D2A1E8D6F0097339F /* ExampleStruct.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E50573C2A1E8D6F0097339F /* ExampleStruct.swift */; };
5E5057412A1E961E0097339F /* Model.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 5E50573F2A1E961E0097339F /* Model.xcdatamodeld */; };
Expand Down Expand Up @@ -55,6 +56,7 @@

/* Begin PBXFileReference section */
3B5278DA2A3379F2008CD8F3 /* BackButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackButton.swift; sourceTree = "<group>"; };
3BDDA1392A33B53E0074CB1D /* Recipe.csv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = Recipe.csv; path = ../../../../cookingFam/Recipe.csv; sourceTree = "<group>"; };
5E50573A2A1E8CA40097339F /* ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewModel.swift; sourceTree = "<group>"; };
5E50573C2A1E8D6F0097339F /* ExampleStruct.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleStruct.swift; sourceTree = "<group>"; };
5E5057402A1E961E0097339F /* Model.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Model.xcdatamodel; sourceTree = "<group>"; };
Expand Down Expand Up @@ -111,6 +113,15 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
3BDDA1382A33B4FF0074CB1D /* Data */ = {
isa = PBXGroup;
children = (
3BDDA1392A33B53E0074CB1D /* Recipe.csv */,
5E5057432A1E97C20097339F /* DataController.swift */,
);
path = Data;
sourceTree = "<group>";
};
5E50573E2A1E8D750097339F /* Structs */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -154,7 +165,7 @@
5ED4DFF02A1E8B4000DDC5D1 /* CookingFamApp.swift */,
5ED4DFF22A1E8B4000DDC5D1 /* ContentView.swift */,
5E50573A2A1E8CA40097339F /* ViewModel.swift */,
5E5057432A1E97C20097339F /* DataController.swift */,
3BDDA1382A33B4FF0074CB1D /* Data */,
5EEFB3842A2F9D890000643B /* Views */,
5E5057422A1E96270097339F /* Database */,
5E50573E2A1E8D750097339F /* Structs */,
Expand Down Expand Up @@ -332,6 +343,7 @@
buildActionMask = 2147483647;
files = (
5ED4DFF82A1E8B4100DDC5D1 /* Preview Assets.xcassets in Resources */,
3BDDA13A2A33B53E0074CB1D /* Recipe.csv in Resources */,
5ED4DFF52A1E8B4100DDC5D1 /* Assets.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file not shown.
116 changes: 116 additions & 0 deletions CookingFam/CookingFam/Data/DataController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// DataController.swift
// CookingFam
//
// Created by Alexis on 24.05.23.
//

import Foundation
import CoreData

class DataController : ObservableObject{
let container = NSPersistentContainer(name: "Model")

init(){
container.loadPersistentStores{ _, error in
if let error = error{
print("CoreData Error:\(error.localizedDescription)")
return
}
self.container.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyStoreTrump
}
}

func importCSV(){
//CSV structure: id,title,ingredients,directions,link,source,NER

guard let path = Bundle.main.path(forResource: "Recipe", ofType: "csv") else {
print("CSV not found")
return
}

do{
let url = URL(fileURLWithPath: path)
let data = try Data(contentsOf: url)
let csvString = String(data: data, encoding: .utf8)
let rows = csvString?.components(separatedBy: "\n")


if let rows = rows {
for row in rows {
// seperade data from nested data , e.g. A,[X,Y,Z],B,C
var nested = false
var field = ""
var data_columns: [String] = []
if row.contains(","){
for c in row {
if c == "[" || c == "]" {
nested.toggle()
}else if c == "," && !nested {
data_columns.append(field)
field = ""
}else{
field.append(c)
}
}
if !field.isEmpty {
data_columns.append(field)
}
}
if data_columns != []{
if let newRecipe = NSEntityDescription.insertNewObject(forEntityName: "Recipe", into: container.viewContext) as? Recipe {
newRecipe.name = data_columns[1]
let ingredients = data_columns[2].components(separatedBy: ",")
ingredients.forEach{ingredient in
if let newIngredient = NSEntityDescription.insertNewObject(forEntityName: "Ingredient", into: container.viewContext) as? Ingredient {
newIngredient.name = ingredient
newIngredient.recipe = newRecipe
newIngredient.recipe?.name = data_columns[1]
}
}
let directions = data_columns[3].components(separatedBy: ",")
directions.forEach{direction in
if let newDirection = NSEntityDescription.insertNewObject(forEntityName: "Direction", into: container.viewContext) as? Direction {
newDirection.text = direction
newDirection.recipe = newRecipe
newDirection.recipe?.name = data_columns[1]
}
}
let components = data_columns[6].components(separatedBy: ",")
components.forEach{component in
if let newComponent = NSEntityDescription.insertNewObject(forEntityName: "Component", into: container.viewContext) as? Component {
newComponent.name = component
newComponent.recipe = newRecipe
newComponent.recipe?.name = data_columns[1]
}
}
}
}

}
}
try container.viewContext.save()
} catch {
print("Error while importing CSV: \(error)")
}
}

func deleteAll() {
let fetchRequest1: NSFetchRequest<NSFetchRequestResult> = Recipe.fetchRequest()
let batchDeleteRequest1 = NSBatchDeleteRequest(fetchRequest: fetchRequest1)
_ = try? container.viewContext.execute(batchDeleteRequest1)

let fetchRequest2: NSFetchRequest<NSFetchRequestResult> = Ingredient.fetchRequest()
let batchDeleteRequest2 = NSBatchDeleteRequest(fetchRequest: fetchRequest2)
_ = try? container.viewContext.execute(batchDeleteRequest2)

let fetchRequest3: NSFetchRequest<NSFetchRequestResult> = Direction.fetchRequest()
let batchDeleteRequest3 = NSBatchDeleteRequest(fetchRequest: fetchRequest3)
_ = try? container.viewContext.execute(batchDeleteRequest3)

let fetchRequest4: NSFetchRequest<NSFetchRequestResult> = Component.fetchRequest()
let batchDeleteRequest4 = NSBatchDeleteRequest(fetchRequest: fetchRequest4)
_ = try? container.viewContext.execute(batchDeleteRequest4)
}

}
41 changes: 0 additions & 41 deletions CookingFam/CookingFam/DataController.swift

This file was deleted.

10 changes: 10 additions & 0 deletions CookingFam/CookingFam/Views/Cookbook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ struct Cookbook: View {
var body: some View {
ZStack{
BackgroundColor()

VStack{
//TODO: remove buttons
Button("delete"){
let dataController = DataController()
dataController.deleteAll()
}
Button("reload"){
let dataController = DataController()
dataController.importCSV()
}
Text("BOOK")
}
}
Expand Down

0 comments on commit d513670

Please sign in to comment.