diff --git a/Shopping List.xcodeproj/project.pbxproj b/Shopping List.xcodeproj/project.pbxproj index 9970ba97..6c0fedcc 100644 --- a/Shopping List.xcodeproj/project.pbxproj +++ b/Shopping List.xcodeproj/project.pbxproj @@ -12,6 +12,11 @@ 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 */; }; + 4A1407AF249550380038F08C /* ShoppingItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A1407AE249550380038F08C /* ShoppingItem.swift */; }; + 4A1407B124959BFA0038F08C /* ShoppingListDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A1407B024959BFA0038F08C /* ShoppingListDetailViewController.swift */; }; + 4A1407B324959C2A0038F08C /* ItemsCollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A1407B224959C2A0038F08C /* ItemsCollectionViewController.swift */; }; + 4A1407B524959C5E0038F08C /* ItemsCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A1407B424959C5E0038F08C /* ItemsCollectionViewCell.swift */; }; + 4A1407B724959CC40038F08C /* shoppingItemModelController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A1407B624959CC40038F08C /* shoppingItemModelController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -22,6 +27,11 @@ 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 = ""; }; + 4A1407AE249550380038F08C /* ShoppingItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShoppingItem.swift; sourceTree = ""; }; + 4A1407B024959BFA0038F08C /* ShoppingListDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShoppingListDetailViewController.swift; sourceTree = ""; }; + 4A1407B224959C2A0038F08C /* ItemsCollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemsCollectionViewController.swift; sourceTree = ""; }; + 4A1407B424959C5E0038F08C /* ItemsCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemsCollectionViewCell.swift; sourceTree = ""; }; + 4A1407B624959CC40038F08C /* shoppingItemModelController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = shoppingItemModelController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -38,6 +48,9 @@ 4613E90521143BFC00E7FF73 = { isa = PBXGroup; children = ( + 4A1407AE249550380038F08C /* ShoppingItem.swift */, + 4A1407B624959CC40038F08C /* shoppingItemModelController.swift */, + 4A1407AD24954FDD0038F08C /* VIEWS */, 4613E91021143BFC00E7FF73 /* Shopping List */, 4613E90F21143BFC00E7FF73 /* Products */, ); @@ -64,6 +77,16 @@ path = "Shopping List"; sourceTree = ""; }; + 4A1407AD24954FDD0038F08C /* VIEWS */ = { + isa = PBXGroup; + children = ( + 4A1407B024959BFA0038F08C /* ShoppingListDetailViewController.swift */, + 4A1407B224959C2A0038F08C /* ItemsCollectionViewController.swift */, + 4A1407B424959C5E0038F08C /* ItemsCollectionViewCell.swift */, + ); + path = VIEWS; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -135,8 +158,13 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 4A1407AF249550380038F08C /* ShoppingItem.swift in Sources */, 4613E91421143BFC00E7FF73 /* ViewController.swift in Sources */, 4613E91221143BFC00E7FF73 /* AppDelegate.swift in Sources */, + 4A1407B724959CC40038F08C /* shoppingItemModelController.swift in Sources */, + 4A1407B124959BFA0038F08C /* ShoppingListDetailViewController.swift in Sources */, + 4A1407B324959C2A0038F08C /* ItemsCollectionViewController.swift in Sources */, + 4A1407B524959C5E0038F08C /* ItemsCollectionViewCell.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -280,15 +308,17 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = VXUQXR6S56; + DEVELOPMENT_TEAM = 2S8MH4C36K; INFOPLIST_FILE = "Shopping List/Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = "com.LambdaSchool.Shopping-List"; + PRODUCT_BUNDLE_IDENTIFIER = "com.LambdaSchool.Shopping-List2"; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; }; @@ -298,15 +328,17 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = VXUQXR6S56; + DEVELOPMENT_TEAM = 2S8MH4C36K; INFOPLIST_FILE = "Shopping List/Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = "com.LambdaSchool.Shopping-List"; + PRODUCT_BUNDLE_IDENTIFIER = "com.LambdaSchool.Shopping-List2"; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; }; diff --git a/Shopping List/Base.lproj/Main.storyboard b/Shopping List/Base.lproj/Main.storyboard index 03c13c22..c8c108b6 100644 --- a/Shopping List/Base.lproj/Main.storyboard +++ b/Shopping List/Base.lproj/Main.storyboard @@ -1,24 +1,183 @@ - + + - + + + - - + + - - - + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + diff --git a/ShoppingItem.swift b/ShoppingItem.swift new file mode 100644 index 00000000..d9fc4bd8 --- /dev/null +++ b/ShoppingItem.swift @@ -0,0 +1,17 @@ +// +// ShoppingItem.swift +// Shopping List +// +// Created by B$hady on 6/13/20. +// Copyright © 2020 Lambda School. All rights reserved. +// + +import Foundation +import UIKit + +struct ShoppingItem: Codable, Equatable { + + var name: String + var added: Bool + var imageName: String +} diff --git a/VIEWS/ItemsCollectionViewCell.swift b/VIEWS/ItemsCollectionViewCell.swift new file mode 100644 index 00000000..1822eb3b --- /dev/null +++ b/VIEWS/ItemsCollectionViewCell.swift @@ -0,0 +1,35 @@ +// +// ItemsCollectionViewCell.swift +// Shopping List +// +// Created by B$hady on 6/13/20. +// Copyright © 2020 Lambda School. All rights reserved. +// + +import UIKit + +class ItemsCollectionViewCell: UICollectionViewCell { + + @IBOutlet weak var itemNameLabel: UILabel! + @IBOutlet weak var itemImageView: UIImageView! + @IBOutlet weak var addedLabel: UILabel! + + var item: ShoppingItem? { + didSet { + updateViews() + } + } + + func updateViews() { + if let item = item { + itemNameLabel.text = item.name + itemImageView.image = UIImage(named: item.imageName) + if item.added == true { + addedLabel.text = "Added" + } else { + addedLabel.text = "Not Added" + } + } + + } +} diff --git a/VIEWS/ItemsCollectionViewController.swift b/VIEWS/ItemsCollectionViewController.swift new file mode 100644 index 00000000..0a4395b6 --- /dev/null +++ b/VIEWS/ItemsCollectionViewController.swift @@ -0,0 +1,64 @@ +// +// ItemsCollectionViewController.swift +// Shopping List +// +// Created by B$hady on 6/13/20. +// Copyright © 2020 Lambda School. All rights reserved. +// + +import UIKit + +class ItemsCollectionViewController: UICollectionViewController { + + + let shoppingItemControlla = shoppingItemController() + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + collectionView?.reloadData() + } + + + + override func viewDidLoad() { + super.viewDidLoad() + // Do any additional setup after loading the view. + } + + + // MARK: - Navigation + + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + if segue.identifier == "detailVCSegue" { + if let detailVC = segue.destination as? ShoppingListDetailViewController { + detailVC.shoppingItemController = shoppingItemControlla + } + } + } + + + // MARK: UICollectionViewDataSource + + override func numberOfSections(in collectionView: UICollectionView) -> Int { + return 1 + } + + override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + return shoppingItemControlla.itemNames.count + } + + override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell", for: indexPath) as? ItemsCollectionViewCell else { return UICollectionViewCell() } + cell.item = shoppingItemControlla.shoppingItems[indexPath.item] + return cell + } + + // MARK: UICollectionViewDelegate + + override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + shoppingItemControlla.shoppingItems[indexPath.item].added.toggle() + collectionView.reloadData() + shoppingItemControlla.saveToPersistentStore() + } +} diff --git a/VIEWS/ShoppingListDetailViewController.swift b/VIEWS/ShoppingListDetailViewController.swift new file mode 100644 index 00000000..78ac4036 --- /dev/null +++ b/VIEWS/ShoppingListDetailViewController.swift @@ -0,0 +1,45 @@ +// +// ShoppingListDetailViewController.swift +// Shopping List +// +// Created by B$hady on 6/13/20. +// Copyright © 2020 Lambda School. All rights reserved. +// + +import UIKit + +class ShoppingListDetailViewController: UIViewController { + + + @IBOutlet weak var enterName: UITextField! + @IBOutlet weak var enterAddress: UITextField! + + @IBOutlet weak var itemsCountLabel: UILabel! + + var shoppingItemController: shoppingItemController? + + + + // Do any additional setup after loading the view. +override func viewDidLoad() { + super.viewDidLoad() + updateViews() + } + + func updateViews() { + if let count = shoppingItemController?.addItemCount() { + itemsCountLabel.text = "You currently have \(count) in your shopping list" + } + + + } + + @IBAction func sendOrderTapped(_ sender: Any) { + + let addedItems = shoppingItemController?.shoppingItems.filter { $0.added == true } + let alert = UIAlertController(title: "Thanks, \(enterName.text ?? "User")", message: "Your order contains \(addedItems?.count ?? 0) items", preferredStyle: .alert) + let okButton = UIAlertAction(title: "OK", style: .cancel, handler: nil) + alert.addAction(okButton) + present(alert, animated: true, completion: nil) + } +} diff --git a/shoppingItemModelController.swift b/shoppingItemModelController.swift new file mode 100644 index 00000000..01099d74 --- /dev/null +++ b/shoppingItemModelController.swift @@ -0,0 +1,77 @@ +// +// shoppingItemModelController.swift +// Shopping List +// +// Created by B$hady on 6/13/20. +// Copyright © 2020 Lambda School. All rights reserved. +// + +import Foundation +import UIKit + +class shoppingItemController { + + init() { + if UserDefaults.standard.bool(forKey: "shoppingListLoaded") == true { + loadFromPersistantStore() + print("working") + } else { + for item in itemNames { + shoppingItems.append(ShoppingItem(name: item, added: false, imageName: item)) + } + saveToPersistentStore() + print("not working") + UserDefaults.standard.set(true, forKey: "shoppingListLoaded") + } + } + + func addItemCount() -> Int { + var count = 0 + for item in shoppingItems { + if item.added { + count += 1 + } + } + return count + } + + + var itemNames = ["Apple", "Grapes", "Milk", "Muffin", "Popcorn", "Soda", "Strawberries"] + var shoppingItems: [ShoppingItem] = [] + + func itemWasUpdated() { + saveToPersistentStore() + } + + //MARK: Persistence + + var persistentFileURL: URL? { + let fileMAnager = FileManager.default + guard let documentsDir = fileMAnager.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil } + let shoppingItemsURL = documentsDir.appendingPathComponent("shoppingList.plist") + return shoppingItemsURL + } + + private func loadFromPersistantStore() { + guard let fileURL = persistentFileURL else { return } + do { + let itemsData = try Data(contentsOf: fileURL) + let decoder = PropertyListDecoder() + let itemListArray = try decoder.decode([ShoppingItem].self, from: itemsData ) + self.shoppingItems = itemListArray + } catch { + print("error decoding stars: \(error)") + } + } + + func saveToPersistentStore() { + guard let fileURL = persistentFileURL else { return } + do { + let encoder = PropertyListEncoder() + let itemsData = try encoder.encode(shoppingItems) + try itemsData.write(to: fileURL) + } catch { + print("error encoding itemsArray: \(error)") + } + } + }