Skip to content

Commit

Permalink
added views
Browse files Browse the repository at this point in the history
  • Loading branch information
In-Ko committed Jun 18, 2023
1 parent 6e9a40d commit b6cad6c
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 24 deletions.
Binary file modified CookingFam/.DS_Store
Binary file not shown.
6 changes: 5 additions & 1 deletion CookingFam/CookingFam.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
5ED4E0072A1E8B4200DDC5D1 /* CookingFamTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5ED4E0062A1E8B4200DDC5D1 /* CookingFamTests.swift */; };
5ED4E0112A1E8B4200DDC5D1 /* CookingFamUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5ED4E0102A1E8B4200DDC5D1 /* CookingFamUITests.swift */; };
5ED4E0132A1E8B4200DDC5D1 /* CookingFamUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5ED4E0122A1E8B4200DDC5D1 /* CookingFamUITestsLaunchTests.swift */; };
5ED7CB432A3B3C6300B0DB7A /* AddCooksDialog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5ED7CB422A3B3C6300B0DB7A /* AddCooksDialog.swift */; };
5ED9343E2A30FF8800EE96CA /* RecipeCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5ED9343D2A30FF8800EE96CA /* RecipeCard.swift */; };
5EEFB3862A2F9DEF0000643B /* QrScanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EEFB3852A2F9DEF0000643B /* QrScanner.swift */; };
5EEFB3882A2F9E730000643B /* RecipeSearch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EEFB3872A2F9E730000643B /* RecipeSearch.swift */; };
Expand Down Expand Up @@ -75,6 +76,7 @@
5ED4E00C2A1E8B4200DDC5D1 /* CookingFamUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CookingFamUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5ED4E0102A1E8B4200DDC5D1 /* CookingFamUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CookingFamUITests.swift; sourceTree = "<group>"; };
5ED4E0122A1E8B4200DDC5D1 /* CookingFamUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CookingFamUITestsLaunchTests.swift; sourceTree = "<group>"; };
5ED7CB422A3B3C6300B0DB7A /* AddCooksDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddCooksDialog.swift; sourceTree = "<group>"; };
5ED9343D2A30FF8800EE96CA /* RecipeCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecipeCard.swift; sourceTree = "<group>"; };
5EEFB3852A2F9DEF0000643B /* QrScanner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QrScanner.swift; sourceTree = "<group>"; };
5EEFB3872A2F9E730000643B /* RecipeSearch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecipeSearch.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -217,10 +219,11 @@
children = (
5EEFB3872A2F9E730000643B /* RecipeSearch.swift */,
5E6246082A321D670058C759 /* RecipeDetails.swift */,
5ED7CB422A3B3C6300B0DB7A /* AddCooksDialog.swift */,
3B8FA6582A33B86A001A7843 /* SortDirections.swift */,
5EEFB3852A2F9DEF0000643B /* QrScanner.swift */,
5EEFB3892A2F9E8E0000643B /* Cooking.swift */,
5EEFB38B2A2F9EA10000643B /* Cookbook.swift */,
3B8FA6582A33B86A001A7843 /* SortDirections.swift */,
5ED9343F2A3106EA00EE96CA /* Components */,
);
path = Views;
Expand Down Expand Up @@ -387,6 +390,7 @@
5EFA887A2A2FB0B900C02F67 /* Ingredient+CoreDataClass.swift in Sources */,
5E6246092A321D670058C759 /* RecipeDetails.swift in Sources */,
5EFA887B2A2FB0B900C02F67 /* Ingredient+CoreDataProperties.swift in Sources */,
5ED7CB432A3B3C6300B0DB7A /* AddCooksDialog.swift in Sources */,
3B8FA6592A33B86A001A7843 /* SortDirections.swift in Sources */,
5ED4DFF12A1E8B4000DDC5D1 /* CookingFamApp.swift in Sources */,
5EEFB38C2A2F9EA10000643B /* Cookbook.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
filePath = "CookingFam/Data/DataController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "96"
endingLineNumber = "96"
startingLineNumber = "100"
endingLineNumber = "100"
landmarkName = "importCSV()"
landmarkType = "7">
<Locations>
Expand Down
41 changes: 36 additions & 5 deletions CookingFam/CookingFam/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct ContentView: View {
@Environment(\.managedObjectContext) var moc
@State var activeView : ActiveView = .search
@State var activeRecipe : Recipe = Recipe()
@State var cooks: [String] = []
var body: some View {
ZStack {
BackgroundColor()
Expand All @@ -28,24 +29,54 @@ struct ContentView: View {
Image(systemName: "book")
Text("Cookbook")
}



}
.edgesIgnoringSafeArea(.all)
} else if activeView == .qr{
QrScanner(activeView: $activeView)
} else if activeView == .details{
RecipeDetails(activeView: $activeView, activeRecipe: $activeRecipe)
} else if activeView == .addCooks{
AddCooksDialog(activeView: $activeView, cooks: $cooks)
} else if activeView == .sort_directions{
SortDirections(activeView: $activeView, activeRecipe: $activeRecipe)
SortDirections(activeView: $activeView, activeRecipe: $activeRecipe, cooks: $cooks)
}
}
}
}

enum ActiveView {
case search, details, qr, sort_directions
case search, details, qr, sort_directions, addCooks
}

extension String {
func capitalizingFirstLetter() -> String {
return prefix(1).uppercased() + self.lowercased().dropFirst()
}

mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}

extension UIViewController {
// TODO: add this somehow in every view where a Textfield could add a keyboard
// this code should dismiss keyboard every time you click outside of the keyboard
// Now in every UIViewController, all you have to do is call this function:
//
// override func viewDidLoad() {
// super.viewDidLoad()
// self.hideKeyboardWhenTappedAround()
// }

func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}

@objc func dismissKeyboard() {
view.endEditing(true)
}
}

struct ContentView_Previews: PreviewProvider {
Expand Down
8 changes: 6 additions & 2 deletions CookingFam/CookingFam/Data/DataController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ class DataController : ObservableObject{
let directions = data_columns[3].components(separatedBy: ";")
directions.forEach{direction in
if let newDirection = NSEntityDescription.insertNewObject(forEntityName: "Direction", into: container.viewContext) as? Direction {
// add number to keep right order for directions
newDirection.text = String(i) + direction
// add number to keep right order for directions
var dir_text = direction
if direction.first == " "{
dir_text = String(direction.dropFirst())
}
newDirection.text = (String(i) + ":") + dir_text
newDirection.recipe = newRecipe
newDirection.recipe?.name = data_columns[1]
i = i+1
Expand Down
80 changes: 80 additions & 0 deletions CookingFam/CookingFam/Views/AddCooksDialog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// AddCooksDialog.swift
// CookingFam
//
// Created by Alexis on 15.06.23.
//

import Foundation
import SwiftUI

struct AddCooksDialog: View {
@Binding var activeView: ActiveView
@Binding var cooks: [String]
@State private var newEntry = ""


var body: some View {
ZStack {
BackgroundColor()

VStack {
HStack {
BackButton(activeView: $activeView, prevView: .details)
.padding(.leading, 16)

Spacer()
}
Text("Add all awesome chefs")
.font(.title)
.padding()
HStack{
// adds randomness to default entry for inputfield
let awesome_words = ["Awesome","impressive","superior","topnotch","finest","unrivaled","unsurpassable",""]
let cook_words = ["cook","chef","knife-swinger","deep-fryer","food-creator","vegetable-smasher"]
TextField((awesome_words.randomElement() ?? "awesome") + " " + (cook_words.randomElement() ?? "chef"), text: $newEntry)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
// TODO: add "enter pressed" to add chefs as well
Button("Add") {
// adds uniqueness with id
var id = ""
let text = newEntry.capitalizingFirstLetter()
if cooks.contains(text){
id = String(returnUniqueID(entry: text, id: 1))
}
cooks.append(text + id)
newEntry = ""
}
.padding()
}

List {
ForEach(cooks, id: \.self) { entry in
Text(entry)
}
.onDelete(perform: deleteEntry)
}
.listRowBackground(Color.clear)

.padding()
Button("Continue") {
activeView = .sort_directions
}
.disabled(cooks.count == 0)
.padding()
}
}
}

private func deleteEntry(at offsets: IndexSet) {
cooks.remove(atOffsets: offsets)
}
private func returnUniqueID(entry: String, id: Int) -> Int{
if cooks.contains(entry + String(id)){
return returnUniqueID(entry: entry, id: id+1)
}
return id
}
}

6 changes: 3 additions & 3 deletions CookingFam/CookingFam/Views/RecipeDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ struct RecipeDetails: View {
.cornerRadius(10)
.shadow(color: .gray, radius: 2, x: 0, y: 2)
HStack{
Text("Let's cook!")
Text("Cook with others")
.padding(10)
}.onTapGesture {
activeView = .sort_directions
activeView = .addCooks
}
.background(Color(hex: 0xffd3b6))
.cornerRadius(10)
Expand All @@ -60,7 +60,7 @@ struct RecipeDetails: View {
y: 3)
HStack{
// Image(systemName: "Plus")
Text("Add chefs")
Text("Cook alone")
.padding(10)
}.onTapGesture {
activeView = .sort_directions
Expand Down
2 changes: 1 addition & 1 deletion CookingFam/CookingFam/Views/RecipeSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct RecipeSearch: View {
// dataController.importCSV()
// }

Form {
List {
// Cards:
ForEach(recipes, id: \.self){recipe in
RecipeCard(text: recipe.wrappedName, components: recipe.componentArray, selectedRecipe: recipe ,activeView: $activeView, activeRecipe: $activeRecipe)
Expand Down
82 changes: 72 additions & 10 deletions CookingFam/CookingFam/Views/SortDirections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,88 @@ import SwiftUI
struct SortDirections: View {
@Binding var activeView: ActiveView
@Binding var activeRecipe: Recipe
@Binding var cooks: [String]
@State var dividedDirections = [String:[String]]()
@State private var selectedCook: String = ""
@State private var selection: UUID?


var body: some View {
var directions: [Direction] = activeRecipe.directionArray
var texts : [String] = [""]
ForEach (directions){ d in
Text(texts[0])
//print(d.text ?? "error")
}

VStack(spacing: 16) {
HStack {
BackButton(activeView: $activeView, prevView: .details)
BackButton(activeView: $activeView, prevView: .addCooks)
.padding(.leading, 16)
Spacer()
}
HStack{
List {
ForEach(activeRecipe.directionArray) {direction in
VStack{
let text = direction.text?.dropFirst()
Text(text ?? "Error")
.font(.body)
}
HStack {
ForEach(cooks, id: \.self){cook in
Button(action: {
selectedCook = cook
}) {
Text(cook)
.padding()
.foregroundColor(selectedCook == cook ? .white : .blue)
.background(selectedCook == cook ? Color.blue : Color.clear)
.cornerRadius(10)
}
}
}
.padding()
NavigationStack {
List(directions, id: \.self, selection: $selection) { direction in
let text = removeDirectionID(direction: direction.text ?? "Error")
Text(text)
.font(.body)
}
.navigationTitle("List Selection")
.environment(\.editMode, .constant(.active))
}
// HStack{
// List(directions, id: \.self, selection: $selection){ direction in
// let text = removeDirectionID(direction: direction.text ?? "Error")
// Text(text)
// .font(.body)
// }
// .navigationTitle("Directions for " + selectedCook)
// .toolbar{
// EditButton()
// }
// List {
// ForEach(activeRecipe.directionArray) {direction in
// VStack{
// let text = direction.text?.dropFirst(2) ?? "Error"
// Text(text)
// .font(.body)
// }.onTapGesture {
// if selectedCook == ""{
// selectedCook = cooks[0]
// }
// var cook_directions : [String] = dividedDirections[selectedCook] ?? []
// if !cook_directions.contains(direction.text ?? "Error"){
// cook_directions.append(direction.text ?? "Error")
// }
// dividedDirections[selectedCook] = cook_directions.sorted()
// print(dividedDirections)
// print(directions)
// }
// }
// }
// }
}
}
func removeDirectionID(direction: String) -> String {
let ind :Int = direction.distance(from: direction.startIndex, to: direction.firstIndex(of: ":") ?? direction.startIndex)
var text = direction
for _ in 0...ind {
text.remove(at: direction.startIndex)
}
return text
}
}

0 comments on commit b6cad6c

Please sign in to comment.