From 4c88e5d39b132783ffbb64b16557c513af1c5810 Mon Sep 17 00:00:00 2001 From: Mike Nichols Date: Fri, 22 May 2020 12:39:53 -0500 Subject: [PATCH 1/4] sprint #3 --- Shopping List.xcodeproj/project.pbxproj | 66 +++++++++- Shopping List/Base.lproj/Main.storyboard | 121 ++++++++++++++++-- Shopping List/Models/DefaultsHelper.swift | 15 +++ Shopping List/Models/ShoppingItem.swift | 25 ++++ Shopping List/Models/ShoppingList.swift | 48 +++++++ .../{ => Resources}/AppDelegate.swift | 0 .../AppIcon.appiconset/Contents.json | 0 .../Apple.imageset/Contents.json | 0 .../Assets.xcassets/Apple.imageset/apple.png | Bin .../Assets.xcassets/Contents.json | 0 .../Grapes.imageset/Contents.json | 0 .../Grapes.imageset/grapes.png | Bin .../Milk.imageset/Contents.json | 0 .../Assets.xcassets/Milk.imageset/milk.png | Bin .../Muffin.imageset/Contents.json | 0 .../Muffin.imageset/muffin.png | Bin .../Popcorn.imageset/Contents.json | 0 .../Popcorn.imageset/popcorn.png | Bin .../Soda.imageset/Contents.json | 0 .../Assets.xcassets/Soda.imageset/soda.png | Bin .../Strawberries.imageset/Contents.json | 0 .../Strawberries.imageset/strawberries.png | Bin .../Base.lproj/LaunchScreen.storyboard | 0 .../OrderViewController.swift | 30 +++++ ...hoppingItemsCollectionViewController.swift | 95 ++++++++++++++ Shopping List/ViewController.swift | 25 ---- .../ShoppingItemsCollectionViewCell.swift | 19 +++ 27 files changed, 402 insertions(+), 42 deletions(-) create mode 100644 Shopping List/Models/DefaultsHelper.swift create mode 100644 Shopping List/Models/ShoppingItem.swift create mode 100644 Shopping List/Models/ShoppingList.swift rename Shopping List/{ => Resources}/AppDelegate.swift (100%) rename Shopping List/{ => Resources}/Assets.xcassets/AppIcon.appiconset/Contents.json (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Apple.imageset/Contents.json (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Apple.imageset/apple.png (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Contents.json (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Grapes.imageset/Contents.json (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Grapes.imageset/grapes.png (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Milk.imageset/Contents.json (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Milk.imageset/milk.png (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Muffin.imageset/Contents.json (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Muffin.imageset/muffin.png (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Popcorn.imageset/Contents.json (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Popcorn.imageset/popcorn.png (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Soda.imageset/Contents.json (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Soda.imageset/soda.png (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Strawberries.imageset/Contents.json (100%) rename Shopping List/{ => Resources}/Assets.xcassets/Strawberries.imageset/strawberries.png (100%) rename Shopping List/{ => Resources}/Base.lproj/LaunchScreen.storyboard (100%) create mode 100644 Shopping List/View Controllers/OrderViewController.swift create mode 100644 Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift delete mode 100644 Shopping List/ViewController.swift create mode 100644 Shopping List/Views/ShoppingItemsCollectionViewCell.swift diff --git a/Shopping List.xcodeproj/project.pbxproj b/Shopping List.xcodeproj/project.pbxproj index 9970ba97..c9d8f245 100644 --- a/Shopping List.xcodeproj/project.pbxproj +++ b/Shopping List.xcodeproj/project.pbxproj @@ -8,20 +8,30 @@ /* Begin PBXBuildFile section */ 4613E91221143BFC00E7FF73 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4613E91121143BFC00E7FF73 /* AppDelegate.swift */; }; - 4613E91421143BFC00E7FF73 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4613E91321143BFC00E7FF73 /* ViewController.swift */; }; 4613E91721143BFC00E7FF73 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4613E91521143BFC00E7FF73 /* Main.storyboard */; }; 4613E91921143BFD00E7FF73 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4613E91821143BFD00E7FF73 /* Assets.xcassets */; }; 4613E91C21143BFD00E7FF73 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4613E91A21143BFD00E7FF73 /* LaunchScreen.storyboard */; }; + B483F7002478236C004C8620 /* ShoppingItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B483F6FF2478236C004C8620 /* ShoppingItem.swift */; }; + B483F70224782578004C8620 /* ShoppingList.swift in Sources */ = {isa = PBXBuildFile; fileRef = B483F70124782578004C8620 /* ShoppingList.swift */; }; + B483F70524782DA3004C8620 /* DefaultsHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = B483F70424782DA3004C8620 /* DefaultsHelper.swift */; }; + B483F7072478321B004C8620 /* ShoppingItemsCollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B483F7062478321B004C8620 /* ShoppingItemsCollectionViewController.swift */; }; + B483F70924783248004C8620 /* OrderViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B483F70824783248004C8620 /* OrderViewController.swift */; }; + B483F70B24783262004C8620 /* ShoppingItemsCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B483F70A24783262004C8620 /* ShoppingItemsCollectionViewCell.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ 4613E90E21143BFC00E7FF73 /* Shopping List.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Shopping List.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 4613E91121143BFC00E7FF73 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 4613E91321143BFC00E7FF73 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 4613E91621143BFC00E7FF73 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 4613E91821143BFD00E7FF73 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 4613E91B21143BFD00E7FF73 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 4613E91D21143BFD00E7FF73 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + B483F6FF2478236C004C8620 /* ShoppingItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShoppingItem.swift; sourceTree = ""; }; + B483F70124782578004C8620 /* ShoppingList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShoppingList.swift; sourceTree = ""; }; + B483F70424782DA3004C8620 /* DefaultsHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultsHelper.swift; sourceTree = ""; }; + B483F7062478321B004C8620 /* ShoppingItemsCollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShoppingItemsCollectionViewController.swift; sourceTree = ""; }; + B483F70824783248004C8620 /* OrderViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderViewController.swift; sourceTree = ""; }; + B483F70A24783262004C8620 /* ShoppingItemsCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShoppingItemsCollectionViewCell.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -54,16 +64,53 @@ 4613E91021143BFC00E7FF73 /* Shopping List */ = { isa = PBXGroup; children = ( - 4613E91121143BFC00E7FF73 /* AppDelegate.swift */, - 4613E91321143BFC00E7FF73 /* ViewController.swift */, + B483F70E2478328D004C8620 /* Resources */, 4613E91521143BFC00E7FF73 /* Main.storyboard */, - 4613E91821143BFD00E7FF73 /* Assets.xcassets */, - 4613E91A21143BFD00E7FF73 /* LaunchScreen.storyboard */, + B483F70D2478326F004C8620 /* View Controllers */, + B483F70C24783266004C8620 /* Views */, 4613E91D21143BFD00E7FF73 /* Info.plist */, + B483F70324782D44004C8620 /* Models */, ); path = "Shopping List"; sourceTree = ""; }; + B483F70324782D44004C8620 /* Models */ = { + isa = PBXGroup; + children = ( + B483F6FF2478236C004C8620 /* ShoppingItem.swift */, + B483F70124782578004C8620 /* ShoppingList.swift */, + B483F70424782DA3004C8620 /* DefaultsHelper.swift */, + ); + path = Models; + sourceTree = ""; + }; + B483F70C24783266004C8620 /* Views */ = { + isa = PBXGroup; + children = ( + B483F70A24783262004C8620 /* ShoppingItemsCollectionViewCell.swift */, + ); + path = Views; + sourceTree = ""; + }; + B483F70D2478326F004C8620 /* View Controllers */ = { + isa = PBXGroup; + children = ( + B483F7062478321B004C8620 /* ShoppingItemsCollectionViewController.swift */, + B483F70824783248004C8620 /* OrderViewController.swift */, + ); + path = "View Controllers"; + sourceTree = ""; + }; + B483F70E2478328D004C8620 /* Resources */ = { + isa = PBXGroup; + children = ( + 4613E91121143BFC00E7FF73 /* AppDelegate.swift */, + 4613E91821143BFD00E7FF73 /* Assets.xcassets */, + 4613E91A21143BFD00E7FF73 /* LaunchScreen.storyboard */, + ); + path = Resources; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -135,8 +182,13 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 4613E91421143BFC00E7FF73 /* ViewController.swift in Sources */, + B483F7072478321B004C8620 /* ShoppingItemsCollectionViewController.swift in Sources */, + B483F70524782DA3004C8620 /* DefaultsHelper.swift in Sources */, + B483F7002478236C004C8620 /* ShoppingItem.swift in Sources */, + B483F70224782578004C8620 /* ShoppingList.swift in Sources */, 4613E91221143BFC00E7FF73 /* AppDelegate.swift in Sources */, + B483F70B24783262004C8620 /* ShoppingItemsCollectionViewCell.swift in Sources */, + B483F70924783248004C8620 /* OrderViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Shopping List/Base.lproj/Main.storyboard b/Shopping List/Base.lproj/Main.storyboard index 03c13c22..50fd049c 100644 --- a/Shopping List/Base.lproj/Main.storyboard +++ b/Shopping List/Base.lproj/Main.storyboard @@ -1,24 +1,125 @@ - + + - + + + - - + + - - - + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + diff --git a/Shopping List/Models/DefaultsHelper.swift b/Shopping List/Models/DefaultsHelper.swift new file mode 100644 index 00000000..55716919 --- /dev/null +++ b/Shopping List/Models/DefaultsHelper.swift @@ -0,0 +1,15 @@ +// +// DefaultsHelper.swift +// Shopping List +// +// Created by Mike Nichols on 5/22/20. +// Copyright © 2020 Lambda School. All rights reserved. +// + +import Foundation + +extension String { + static var isNewUser = true + + } +} diff --git a/Shopping List/Models/ShoppingItem.swift b/Shopping List/Models/ShoppingItem.swift new file mode 100644 index 00000000..2b269c0f --- /dev/null +++ b/Shopping List/Models/ShoppingItem.swift @@ -0,0 +1,25 @@ +// +// ShoppingItem.swift +// Shopping List +// +// Created by Mike Nichols on 5/22/20. +// Copyright © 2020 Lambda School. All rights reserved. +// + +import Foundation +import UIKit + +struct ShoppingItem { + var itemName: String + var isOnList: Bool + var itemImage: UIImage? { + let itemImageName = "\(itemName).png" + guard let itemImage = UIImage(named: itemImageName) else { + return nil + } + return itemImage + } +} + + + diff --git a/Shopping List/Models/ShoppingList.swift b/Shopping List/Models/ShoppingList.swift new file mode 100644 index 00000000..9da3bab1 --- /dev/null +++ b/Shopping List/Models/ShoppingList.swift @@ -0,0 +1,48 @@ +// +// ShoppingList.swift +// Shopping List +// +// Created by Mike Nichols on 5/22/20. +// Copyright © 2020 Lambda School. All rights reserved. +// + +import Foundation +import UIKit + +protocol ShoppingListDelegate: class { + func updateList() +} + +class ShoppingList: Codable { + var shoppingList: [ShoppingItem] = [] + + + private func updateList() { + let isNew = UserDefaults.standard.isNew + + if isNew = true { + + let itemNames = ["Apple", "Grapes", "Milk", "Muffin", "Popcorn", "Soda", "Strawberries"] + for item in itemNames { + let item = item + shoppingList.append(item) + } else { + + loadFromPersistentStore() + } + } + + func persistentFileURL() { + var persistentFileURL: URL? { + let fileManager = FileManager.default + let documentsDir = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first + let url = documentsDir?.appendingPathComponent("shoppingItems.plist") + + return url + } + } + + func loadFromPersistentStore() { + + } +} diff --git a/Shopping List/AppDelegate.swift b/Shopping List/Resources/AppDelegate.swift similarity index 100% rename from Shopping List/AppDelegate.swift rename to Shopping List/Resources/AppDelegate.swift diff --git a/Shopping List/Assets.xcassets/AppIcon.appiconset/Contents.json b/Shopping List/Resources/Assets.xcassets/AppIcon.appiconset/Contents.json similarity index 100% rename from Shopping List/Assets.xcassets/AppIcon.appiconset/Contents.json rename to Shopping List/Resources/Assets.xcassets/AppIcon.appiconset/Contents.json diff --git a/Shopping List/Assets.xcassets/Apple.imageset/Contents.json b/Shopping List/Resources/Assets.xcassets/Apple.imageset/Contents.json similarity index 100% rename from Shopping List/Assets.xcassets/Apple.imageset/Contents.json rename to Shopping List/Resources/Assets.xcassets/Apple.imageset/Contents.json diff --git a/Shopping List/Assets.xcassets/Apple.imageset/apple.png b/Shopping List/Resources/Assets.xcassets/Apple.imageset/apple.png similarity index 100% rename from Shopping List/Assets.xcassets/Apple.imageset/apple.png rename to Shopping List/Resources/Assets.xcassets/Apple.imageset/apple.png diff --git a/Shopping List/Assets.xcassets/Contents.json b/Shopping List/Resources/Assets.xcassets/Contents.json similarity index 100% rename from Shopping List/Assets.xcassets/Contents.json rename to Shopping List/Resources/Assets.xcassets/Contents.json diff --git a/Shopping List/Assets.xcassets/Grapes.imageset/Contents.json b/Shopping List/Resources/Assets.xcassets/Grapes.imageset/Contents.json similarity index 100% rename from Shopping List/Assets.xcassets/Grapes.imageset/Contents.json rename to Shopping List/Resources/Assets.xcassets/Grapes.imageset/Contents.json diff --git a/Shopping List/Assets.xcassets/Grapes.imageset/grapes.png b/Shopping List/Resources/Assets.xcassets/Grapes.imageset/grapes.png similarity index 100% rename from Shopping List/Assets.xcassets/Grapes.imageset/grapes.png rename to Shopping List/Resources/Assets.xcassets/Grapes.imageset/grapes.png diff --git a/Shopping List/Assets.xcassets/Milk.imageset/Contents.json b/Shopping List/Resources/Assets.xcassets/Milk.imageset/Contents.json similarity index 100% rename from Shopping List/Assets.xcassets/Milk.imageset/Contents.json rename to Shopping List/Resources/Assets.xcassets/Milk.imageset/Contents.json diff --git a/Shopping List/Assets.xcassets/Milk.imageset/milk.png b/Shopping List/Resources/Assets.xcassets/Milk.imageset/milk.png similarity index 100% rename from Shopping List/Assets.xcassets/Milk.imageset/milk.png rename to Shopping List/Resources/Assets.xcassets/Milk.imageset/milk.png diff --git a/Shopping List/Assets.xcassets/Muffin.imageset/Contents.json b/Shopping List/Resources/Assets.xcassets/Muffin.imageset/Contents.json similarity index 100% rename from Shopping List/Assets.xcassets/Muffin.imageset/Contents.json rename to Shopping List/Resources/Assets.xcassets/Muffin.imageset/Contents.json diff --git a/Shopping List/Assets.xcassets/Muffin.imageset/muffin.png b/Shopping List/Resources/Assets.xcassets/Muffin.imageset/muffin.png similarity index 100% rename from Shopping List/Assets.xcassets/Muffin.imageset/muffin.png rename to Shopping List/Resources/Assets.xcassets/Muffin.imageset/muffin.png diff --git a/Shopping List/Assets.xcassets/Popcorn.imageset/Contents.json b/Shopping List/Resources/Assets.xcassets/Popcorn.imageset/Contents.json similarity index 100% rename from Shopping List/Assets.xcassets/Popcorn.imageset/Contents.json rename to Shopping List/Resources/Assets.xcassets/Popcorn.imageset/Contents.json diff --git a/Shopping List/Assets.xcassets/Popcorn.imageset/popcorn.png b/Shopping List/Resources/Assets.xcassets/Popcorn.imageset/popcorn.png similarity index 100% rename from Shopping List/Assets.xcassets/Popcorn.imageset/popcorn.png rename to Shopping List/Resources/Assets.xcassets/Popcorn.imageset/popcorn.png diff --git a/Shopping List/Assets.xcassets/Soda.imageset/Contents.json b/Shopping List/Resources/Assets.xcassets/Soda.imageset/Contents.json similarity index 100% rename from Shopping List/Assets.xcassets/Soda.imageset/Contents.json rename to Shopping List/Resources/Assets.xcassets/Soda.imageset/Contents.json diff --git a/Shopping List/Assets.xcassets/Soda.imageset/soda.png b/Shopping List/Resources/Assets.xcassets/Soda.imageset/soda.png similarity index 100% rename from Shopping List/Assets.xcassets/Soda.imageset/soda.png rename to Shopping List/Resources/Assets.xcassets/Soda.imageset/soda.png diff --git a/Shopping List/Assets.xcassets/Strawberries.imageset/Contents.json b/Shopping List/Resources/Assets.xcassets/Strawberries.imageset/Contents.json similarity index 100% rename from Shopping List/Assets.xcassets/Strawberries.imageset/Contents.json rename to Shopping List/Resources/Assets.xcassets/Strawberries.imageset/Contents.json diff --git a/Shopping List/Assets.xcassets/Strawberries.imageset/strawberries.png b/Shopping List/Resources/Assets.xcassets/Strawberries.imageset/strawberries.png similarity index 100% rename from Shopping List/Assets.xcassets/Strawberries.imageset/strawberries.png rename to Shopping List/Resources/Assets.xcassets/Strawberries.imageset/strawberries.png diff --git a/Shopping List/Base.lproj/LaunchScreen.storyboard b/Shopping List/Resources/Base.lproj/LaunchScreen.storyboard similarity index 100% rename from Shopping List/Base.lproj/LaunchScreen.storyboard rename to Shopping List/Resources/Base.lproj/LaunchScreen.storyboard diff --git a/Shopping List/View Controllers/OrderViewController.swift b/Shopping List/View Controllers/OrderViewController.swift new file mode 100644 index 00000000..634104c1 --- /dev/null +++ b/Shopping List/View Controllers/OrderViewController.swift @@ -0,0 +1,30 @@ +// +// OrderViewController.swift +// Shopping List +// +// Created by Mike Nichols on 5/22/20. +// Copyright © 2020 Lambda School. All rights reserved. +// + +import UIKit + +class OrderViewController: UIViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + } + + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + // Get the new view controller using segue.destination. + // Pass the selected object to the new view controller. + } + */ + +} diff --git a/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift b/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift new file mode 100644 index 00000000..9f3ce26a --- /dev/null +++ b/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift @@ -0,0 +1,95 @@ +// +// ShoppingItemsCollectionViewController.swift +// Shopping List +// +// Created by Mike Nichols on 5/22/20. +// Copyright © 2020 Lambda School. All rights reserved. +// + +import UIKit + +class ShoppingItemsCollectionViewController: UICollectionViewController { + + var myList = ShoppingList() + + var delegate: ShoppingListDelegate? + + override func viewDidLoad() { + super.viewDidLoad() + + } + + + @IBAction func itemTapped(_ sender: UIButton) { + + + } + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + // Get the new view controller using [segue destinationViewController]. + // Pass the selected object to the new view controller. + } + + + // MARK: UICollectionViewDataSource + + override func numberOfSections(in collectionView: UICollectionView) -> Int { + // #warning Incomplete implementation, return the number of sections + return 0 + } + + + override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + // #warning Incomplete implementation, return the number of items + return myList.shoppingList.count + } + + override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell", for: indexPath) as? ShoppingItemsCollectionViewCell else { + fatalError("Collection view cell identifier is wrong or cell is not of type ShoppingItemsCollectionViewCell" ) + } + + let item = myList.shoppingList[indexPath.item] + cell.itemImageView.image = item.itemImage + cell.itemNameLabel.text = item.itemName + + + return cell + +} + // MARK: UICollectionViewDelegate + + /* + // Uncomment this method to specify if the specified item should be highlighted during tracking + override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool { + return true + } + */ + + /* + // Uncomment this method to specify if the specified item should be selected + override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { + return true + } + */ + + /* + // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item + override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool { + return false + } + + override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool { + return false + } + + override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) { + + } + */ + + + +} + +extension diff --git a/Shopping List/ViewController.swift b/Shopping List/ViewController.swift deleted file mode 100644 index eecb48b5..00000000 --- a/Shopping List/ViewController.swift +++ /dev/null @@ -1,25 +0,0 @@ -// -// ViewController.swift -// Shopping List -// -// Created by Spencer Curtis on 8/3/18. -// Copyright © 2018 Lambda School. All rights reserved. -// - -import UIKit - -class ViewController: UIViewController { - - override func viewDidLoad() { - super.viewDidLoad() - // Do any additional setup after loading the view, typically from a nib. - } - - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } - - -} - diff --git a/Shopping List/Views/ShoppingItemsCollectionViewCell.swift b/Shopping List/Views/ShoppingItemsCollectionViewCell.swift new file mode 100644 index 00000000..c812d0cf --- /dev/null +++ b/Shopping List/Views/ShoppingItemsCollectionViewCell.swift @@ -0,0 +1,19 @@ +// +// ShoppingItemsCollectionViewCell.swift +// Shopping List +// +// Created by Mike Nichols on 5/22/20. +// Copyright © 2020 Lambda School. All rights reserved. +// + +import UIKit + +class ShoppingItemsCollectionViewCell: UICollectionViewCell { + + @IBOutlet weak var itemImageView: UIImageView! + @IBOutlet weak var itemNameLabel: UILabel! + @IBOutlet weak var isOnListButton: UIButton! + + + +} From ec1f463b83925e9ac2b88e078adaa9ba03022b5f Mon Sep 17 00:00:00 2001 From: Mike Nichols Date: Fri, 22 May 2020 13:07:21 -0500 Subject: [PATCH 2/4] Too slow i guess --- Shopping List/Models/DefaultsHelper.swift | 2 -- .../ShoppingItemsCollectionViewController.swift | 1 - Shopping List/Views/ShoppingItemsCollectionViewCell.swift | 1 - 3 files changed, 4 deletions(-) diff --git a/Shopping List/Models/DefaultsHelper.swift b/Shopping List/Models/DefaultsHelper.swift index 55716919..b7acdf8c 100644 --- a/Shopping List/Models/DefaultsHelper.swift +++ b/Shopping List/Models/DefaultsHelper.swift @@ -10,6 +10,4 @@ import Foundation extension String { static var isNewUser = true - - } } diff --git a/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift b/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift index 9f3ce26a..6e506400 100644 --- a/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift +++ b/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift @@ -92,4 +92,3 @@ class ShoppingItemsCollectionViewController: UICollectionViewController { } -extension diff --git a/Shopping List/Views/ShoppingItemsCollectionViewCell.swift b/Shopping List/Views/ShoppingItemsCollectionViewCell.swift index c812d0cf..12bda248 100644 --- a/Shopping List/Views/ShoppingItemsCollectionViewCell.swift +++ b/Shopping List/Views/ShoppingItemsCollectionViewCell.swift @@ -14,6 +14,5 @@ class ShoppingItemsCollectionViewCell: UICollectionViewCell { @IBOutlet weak var itemNameLabel: UILabel! @IBOutlet weak var isOnListButton: UIButton! - } From e104a394e182ad4349318d7f6116bae5ecb2aac2 Mon Sep 17 00:00:00 2001 From: Mike Nichols Date: Fri, 22 May 2020 18:56:37 -0500 Subject: [PATCH 3/4] Thank you! --- Shopping List/Base.lproj/Main.storyboard | 36 ++++++++- Shopping List/Models/DefaultsHelper.swift | 3 +- Shopping List/Models/ShoppingItem.swift | 2 +- Shopping List/Models/ShoppingList.swift | 73 +++++++++++++------ .../OrderViewController.swift | 19 +++-- ...hoppingItemsCollectionViewController.swift | 54 +++++++------- .../ShoppingItemsCollectionViewCell.swift | 19 ++++- 7 files changed, 141 insertions(+), 65 deletions(-) diff --git a/Shopping List/Base.lproj/Main.storyboard b/Shopping List/Base.lproj/Main.storyboard index 50fd049c..ae6d5274 100644 --- a/Shopping List/Base.lproj/Main.storyboard +++ b/Shopping List/Base.lproj/Main.storyboard @@ -91,13 +91,47 @@ - + + + + + + + + + + + + + + + + + + + + + + diff --git a/Shopping List/Models/DefaultsHelper.swift b/Shopping List/Models/DefaultsHelper.swift index b7acdf8c..73df6898 100644 --- a/Shopping List/Models/DefaultsHelper.swift +++ b/Shopping List/Models/DefaultsHelper.swift @@ -9,5 +9,6 @@ import Foundation extension String { - static var isNewUser = true + static var isNewUser = "isNewUser" } + diff --git a/Shopping List/Models/ShoppingItem.swift b/Shopping List/Models/ShoppingItem.swift index 2b269c0f..ca24b364 100644 --- a/Shopping List/Models/ShoppingItem.swift +++ b/Shopping List/Models/ShoppingItem.swift @@ -9,7 +9,7 @@ import Foundation import UIKit -struct ShoppingItem { +struct ShoppingItem: Codable { var itemName: String var isOnList: Bool var itemImage: UIImage? { diff --git a/Shopping List/Models/ShoppingList.swift b/Shopping List/Models/ShoppingList.swift index 9da3bab1..59fb7385 100644 --- a/Shopping List/Models/ShoppingList.swift +++ b/Shopping List/Models/ShoppingList.swift @@ -7,42 +7,67 @@ // import Foundation -import UIKit protocol ShoppingListDelegate: class { - func updateList() + func saveToPersistentStore() + + func loadFromPersistentStore() } -class ShoppingList: Codable { - var shoppingList: [ShoppingItem] = [] - + +class ShoppingList: Codable { - private func updateList() { - let isNew = UserDefaults.standard.isNew + var shoppingList: [ShoppingItem] = [] + var persistentFileURL: URL? { + let fileManager = FileManager.default + let documentsDir = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first + let url = documentsDir?.appendingPathComponent("shoppingList.plist") - if isNew = true { + return url + } + + func loadFromPersistentStore() { + do { + guard let persistentFileURL = persistentFileURL else { return } + + let shoppingListPList = try Data(contentsOf: persistentFileURL) + let decoder = PropertyListDecoder() + let decodedShoppingList = try decoder.decode([ShoppingItem].self, from: shoppingListPList) + self.shoppingList = decodedShoppingList + } catch { + fatalError("Failed to load list") + } + } + + init() { + let isNewUser = UserDefaults.standard.bool(forKey: "isNewUser") + if isNewUser == true { let itemNames = ["Apple", "Grapes", "Milk", "Muffin", "Popcorn", "Soda", "Strawberries"] for item in itemNames { - let item = item + let item = ShoppingItem(itemName: item, isOnList: false) shoppingList.append(item) - } else { - - loadFromPersistentStore() } + let defaults = UserDefaults.standard + defaults.set(false, forKey: "isNewUser") + } else { + loadFromPersistentStore() } - - func persistentFileURL() { - var persistentFileURL: URL? { - let fileManager = FileManager.default - let documentsDir = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first - let url = documentsDir?.appendingPathComponent("shoppingItems.plist") - - return url - } - } - - func loadFromPersistentStore() { + } + + func saveToPersistentStore() { + do { + guard let persistentFileURL = persistentFileURL else { return } + + let encoder = PropertyListEncoder() + let encodedShoppingList = try encoder.encode(shoppingList) + try encodedShoppingList.write(to: persistentFileURL) + + } catch { + + fatalError("Failed to save list") } } +} + diff --git a/Shopping List/View Controllers/OrderViewController.swift b/Shopping List/View Controllers/OrderViewController.swift index 634104c1..90eb7b89 100644 --- a/Shopping List/View Controllers/OrderViewController.swift +++ b/Shopping List/View Controllers/OrderViewController.swift @@ -9,22 +9,21 @@ import UIKit class OrderViewController: UIViewController { + + @IBOutlet weak var nameTextField: UITextField! + @IBOutlet weak var addressTextField: UITextField! + @IBOutlet weak var itemCountLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view. } - - /* - // MARK: - Navigation - // In a storyboard-based application, you will often want to do a little preparation before navigation - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { - // Get the new view controller using segue.destination. - // Pass the selected object to the new view controller. + + @IBAction func submitWasTapped(_ sender: UIButton) { + let alert = UIAlertController(title: "Thank you, \(String(describing: nameTextField.text))!", message: "Your items will be delivered to \(String(describing: addressTextField.text)) in the next 15 minutes.", preferredStyle: .alert) + present(alert, animated: true, completion: nil) + } - */ - } diff --git a/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift b/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift index 6e506400..6d485697 100644 --- a/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift +++ b/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift @@ -12,33 +12,34 @@ class ShoppingItemsCollectionViewController: UICollectionViewController { var myList = ShoppingList() - var delegate: ShoppingListDelegate? + var delegate: ShoppingListCellDelegate? override func viewDidLoad() { super.viewDidLoad() - - } - - - @IBAction func itemTapped(_ sender: UIButton) { - - + collectionView?.dataSource = self + collectionView?.delegate = self } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { - // Get the new view controller using [segue destinationViewController]. - // Pass the selected object to the new view controller. + + guard let destinationVC = segue.destination as? OrderViewController else { return } + + var itemsOnList: Int = 0 + for item in myList.shoppingList { + if item.isOnList == true { + itemsOnList += 1 + } + } + destinationVC.itemCountLabel.text = String(itemsOnList) } - // MARK: UICollectionViewDataSource override func numberOfSections(in collectionView: UICollectionView) -> Int { // #warning Incomplete implementation, return the number of sections - return 0 + return 1 } - override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of items return myList.shoppingList.count @@ -57,6 +58,11 @@ class ShoppingItemsCollectionViewController: UICollectionViewController { return cell } + + override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + guard let cell = collectionView.cellForItem(at: indexPath) else { return } + delegate?.cellWasTapped(cell) + } // MARK: UICollectionViewDelegate /* @@ -73,22 +79,18 @@ class ShoppingItemsCollectionViewController: UICollectionViewController { } */ - /* - // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item - override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool { - return false - } - - override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool { - return false - } - override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) { + +} + +extension ShoppingItemsCollectionViewController: ShoppingListCellDelegate { + func updateViews() { + } - */ - - + func cellWasTapped() { + + } } diff --git a/Shopping List/Views/ShoppingItemsCollectionViewCell.swift b/Shopping List/Views/ShoppingItemsCollectionViewCell.swift index 12bda248..a72b3615 100644 --- a/Shopping List/Views/ShoppingItemsCollectionViewCell.swift +++ b/Shopping List/Views/ShoppingItemsCollectionViewCell.swift @@ -8,11 +8,26 @@ import UIKit -class ShoppingItemsCollectionViewCell: UICollectionViewCell { +protocol ShoppingListCellDelegate: class { + func cellWasTapped() + + func updateViews() +} + +class ShoppingItemsCollectionViewCell: UICollectionViewCell, ShoppingListCellDelegate { + + weak var delegate: ShoppingListCellDelegate? @IBOutlet weak var itemImageView: UIImageView! @IBOutlet weak var itemNameLabel: UILabel! @IBOutlet weak var isOnListButton: UIButton! - + func cellWasTapped(cell: ShoppingItemsCollectionViewCell) { + if cell.isOnListButton.isSelected == true { + cell.isOnListButton.isSelected = false + } else { + cell.isOnListButton.isSelected = true + } + saveToPersistentStore() + } } From 36b489db1a8f3d19f7aee40ebe04eb026e2ac2fc Mon Sep 17 00:00:00 2001 From: Mike Nichols Date: Fri, 22 May 2020 21:27:23 -0500 Subject: [PATCH 4/4] Just having trouble getting to conform to codable --- Shopping List/Models/ShoppingList.swift | 35 ++++++++++--------- ...hoppingItemsCollectionViewController.swift | 31 ++-------------- .../ShoppingItemsCollectionViewCell.swift | 12 +++---- 3 files changed, 27 insertions(+), 51 deletions(-) diff --git a/Shopping List/Models/ShoppingList.swift b/Shopping List/Models/ShoppingList.swift index 59fb7385..d4776418 100644 --- a/Shopping List/Models/ShoppingList.swift +++ b/Shopping List/Models/ShoppingList.swift @@ -8,7 +8,7 @@ import Foundation -protocol ShoppingListDelegate: class { +protocol ShoppingListDelegate { func saveToPersistentStore() func loadFromPersistentStore() @@ -17,6 +17,8 @@ protocol ShoppingListDelegate: class { class ShoppingList: Codable { + var delegate: ShoppingListDelegate? + var shoppingList: [ShoppingItem] = [] var persistentFileURL: URL? { let fileManager = FileManager.default @@ -25,21 +27,7 @@ class ShoppingList: Codable { return url } - - func loadFromPersistentStore() { - do { - guard let persistentFileURL = persistentFileURL else { return } - - let shoppingListPList = try Data(contentsOf: persistentFileURL) - let decoder = PropertyListDecoder() - let decodedShoppingList = try decoder.decode([ShoppingItem].self, from: shoppingListPList) - self.shoppingList = decodedShoppingList - - } catch { - fatalError("Failed to load list") - } - } - + init() { let isNewUser = UserDefaults.standard.bool(forKey: "isNewUser") if isNewUser == true { @@ -48,6 +36,7 @@ class ShoppingList: Codable { let item = ShoppingItem(itemName: item, isOnList: false) shoppingList.append(item) } + saveToPersistentStore() let defaults = UserDefaults.standard defaults.set(false, forKey: "isNewUser") } else { @@ -55,6 +44,20 @@ class ShoppingList: Codable { } } + func loadFromPersistentStore() { + do { + guard let persistentFileURL = persistentFileURL else { return } + + let shoppingListPList = try Data(contentsOf: persistentFileURL) + let decoder = PropertyListDecoder() + let decodedShoppingList = try decoder.decode([ShoppingItem].self, from: shoppingListPList) + self.shoppingList = decodedShoppingList + + } catch { + fatalError("Failed to load list") + } + } + func saveToPersistentStore() { do { guard let persistentFileURL = persistentFileURL else { return } diff --git a/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift b/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift index 6d485697..0b27185e 100644 --- a/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift +++ b/Shopping List/View Controllers/ShoppingItemsCollectionViewController.swift @@ -61,36 +61,9 @@ class ShoppingItemsCollectionViewController: UICollectionViewController { override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { guard let cell = collectionView.cellForItem(at: indexPath) else { return } - delegate?.cellWasTapped(cell) - } - // MARK: UICollectionViewDelegate - - /* - // Uncomment this method to specify if the specified item should be highlighted during tracking - override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool { - return true - } - */ - - /* - // Uncomment this method to specify if the specified item should be selected - override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { - return true - } - */ - - - - -} - -extension ShoppingItemsCollectionViewController: ShoppingListCellDelegate { - func updateViews() { - - } - - func cellWasTapped() { + delegate?.itemSelected(cell as! ShoppingItemsCollectionViewCell) } + } diff --git a/Shopping List/Views/ShoppingItemsCollectionViewCell.swift b/Shopping List/Views/ShoppingItemsCollectionViewCell.swift index a72b3615..22c599f7 100644 --- a/Shopping List/Views/ShoppingItemsCollectionViewCell.swift +++ b/Shopping List/Views/ShoppingItemsCollectionViewCell.swift @@ -9,25 +9,25 @@ import UIKit protocol ShoppingListCellDelegate: class { - func cellWasTapped() - - func updateViews() + func itemSelected(_ cell: ShoppingItemsCollectionViewCell) + } class ShoppingItemsCollectionViewCell: UICollectionViewCell, ShoppingListCellDelegate { - weak var delegate: ShoppingListCellDelegate? + var delegate: ShoppingListDelegate? @IBOutlet weak var itemImageView: UIImageView! @IBOutlet weak var itemNameLabel: UILabel! @IBOutlet weak var isOnListButton: UIButton! - func cellWasTapped(cell: ShoppingItemsCollectionViewCell) { + func itemSelected(_ cell: ShoppingItemsCollectionViewCell) { if cell.isOnListButton.isSelected == true { cell.isOnListButton.isSelected = false + } else { cell.isOnListButton.isSelected = true } - saveToPersistentStore() + delegate?.saveToPersistentStore() } }