From 38f6bc953a436c4134afa3db0d77f7723be06586 Mon Sep 17 00:00:00 2001 From: Alex King Date: Sat, 28 Mar 2020 23:08:05 -0700 Subject: [PATCH 1/6] SwiftUI Ingredient List --- CookSmart/CookSmart.xcodeproj/project.pbxproj | 8 ++ CookSmart/CookSmart/CSConversionVC.m | 9 ++- CookSmart/CookSmart/CSConversionVCExt.swift | 17 +++++ CookSmart/CookSmart/Core/Colors.swift | 4 +- .../IngredientListViewController.swift | 74 +++++++++++++++++++ CookSmart/CookSmart/cake-Bridging-Header.h | 2 + 6 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 CookSmart/CookSmart/CSConversionVCExt.swift create mode 100644 CookSmart/CookSmart/IngredientListViewController.swift diff --git a/CookSmart/CookSmart.xcodeproj/project.pbxproj b/CookSmart/CookSmart.xcodeproj/project.pbxproj index f729367..9ac0d84 100644 --- a/CookSmart/CookSmart.xcodeproj/project.pbxproj +++ b/CookSmart/CookSmart.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXBuildFile section */ 392D8645242FD0FF002064D6 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392D8644242FD0FF002064D6 /* Colors.swift */; }; + 392D865424304F2D002064D6 /* IngredientListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392D865324304F2D002064D6 /* IngredientListViewController.swift */; }; + 392D865724306D0D002064D6 /* CSConversionVCExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392D865624306D0D002064D6 /* CSConversionVCExt.swift */; }; 77479AEA242FB9C8000CFB0E /* ScaleTile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77479AE9242FB9C8000CFB0E /* ScaleTile.swift */; }; 80433B2E1BB26C1A006B7A85 /* CSRecentsIngredientGroup.m in Sources */ = {isa = PBXBuildFile; fileRef = 80433B2D1BB26C1A006B7A85 /* CSRecentsIngredientGroup.m */; }; 80433B361BB2731B006B7A85 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 80433B351BB2731B006B7A85 /* Launch Screen.storyboard */; }; @@ -72,6 +74,8 @@ /* Begin PBXFileReference section */ 392D8644242FD0FF002064D6 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = ""; }; + 392D865324304F2D002064D6 /* IngredientListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IngredientListViewController.swift; sourceTree = ""; }; + 392D865624306D0D002064D6 /* CSConversionVCExt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CSConversionVCExt.swift; sourceTree = ""; }; 77479AE8242FB9C8000CFB0E /* cake-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "cake-Bridging-Header.h"; sourceTree = ""; }; 77479AE9242FB9C8000CFB0E /* ScaleTile.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = ScaleTile.swift; sourceTree = ""; }; 80433B2C1BB26C1A006B7A85 /* CSRecentsIngredientGroup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSRecentsIngredientGroup.h; sourceTree = ""; }; @@ -235,6 +239,7 @@ 9368096F1891059700655B89 /* CSIngredientListVC.m */, 807C0B6A1BB3DD4D007D2C03 /* CSIngredientListViewCell.h */, 807C0B6B1BB3DD4D007D2C03 /* CSIngredientListViewCell.m */, + 392D865324304F2D002064D6 /* IngredientListViewController.swift */, ); name = "Ingredient List UI"; sourceTree = ""; @@ -295,6 +300,7 @@ 9358BD6B1891CAAA00A99D51 /* CSConversionVC.h */, 9358BD6C1891CAAA00A99D51 /* CSConversionVC.m */, 9358BD6D1891CAAA00A99D51 /* CSConversionVC.xib */, + 392D865624306D0D002064D6 /* CSConversionVCExt.swift */, 8000355C18B3CBF50032BF91 /* Scales */, 80592C0218924AEA0082D4E1 /* Model */, B7BCA29F180524A800CF7588 /* CSAppDelegate.h */, @@ -520,9 +526,11 @@ 392D8645242FD0FF002064D6 /* Colors.swift in Sources */, 80592C121892CA110082D4E1 /* CSScaleTile.m in Sources */, 80A614DE18BF3D4100A205BB /* CSGlassView.m in Sources */, + 392D865424304F2D002064D6 /* IngredientListViewController.swift in Sources */, 93F6CCA818AED6050045400D /* CSScaleVC.m in Sources */, 931AF96D18C2BAE50018AA8B /* CSUnitCollection.m in Sources */, 935DB3AC18B46A28007989D7 /* CSEditIngredientVC.m in Sources */, + 392D865724306D0D002064D6 /* CSConversionVCExt.swift in Sources */, 80592C0818924B0F0082D4E1 /* CSIngredient.m in Sources */, 80592C0E189268D60082D4E1 /* CSScaleView.m in Sources */, 80592C0B1892541D0082D4E1 /* CSIngredientGroup.m in Sources */, diff --git a/CookSmart/CookSmart/CSConversionVC.m b/CookSmart/CookSmart/CSConversionVC.m index fc28dd8..239d6d5 100644 --- a/CookSmart/CookSmart/CSConversionVC.m +++ b/CookSmart/CookSmart/CSConversionVC.m @@ -15,6 +15,7 @@ #import "CSUnit.h" #import "CSUnitCollection.h" #import "CSScaleVC.h" +#import "cake-Swift.h" #define CHOOSE_UNITS_TEXT @"Choose Units" @@ -99,6 +100,7 @@ - (void)viewDidAppear:(BOOL)animated // When we first appear, always select the very first ingredient – the one most recently selected. [self selectIngredientAtIndex:self.ingredientIndex]; logViewChange(@"conversion", [self.scaleVC analyticsAttributes]); + self.view.backgroundColor = UIColor.systemGray6Color; } - (void)viewDidDisappear:(BOOL)animated @@ -153,9 +155,10 @@ - (void)refreshIngredientNameUI - (void)handleIngredientTap:(id)sender { - CSIngredientListVC* ingrListVC = [[CSIngredientListVC alloc] initWithDelegate:self]; - UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:ingrListVC]; - [self presentViewController:nav animated:YES completion:nil]; + [self showIngredientList]; +// CSIngredientListVC* ingrListVC = [[CSIngredientListVC alloc] initWithDelegate:self]; +// UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:ingrListVC]; +// [self presentViewController:nav animated:YES completion:nil]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView diff --git a/CookSmart/CookSmart/CSConversionVCExt.swift b/CookSmart/CookSmart/CSConversionVCExt.swift new file mode 100644 index 0000000..53ff50b --- /dev/null +++ b/CookSmart/CookSmart/CSConversionVCExt.swift @@ -0,0 +1,17 @@ +// +// CSConversionVCExt.swift +// cake +// +// Created by Alex King on 3/28/20. +// Copyright © 2020 Olga Galchenko. All rights reserved. +// + +import Foundation + +extension CSConversionVC { + @objc + func showIngredientList() { + let ingredientListViewController = IngredientListViewController() + present(ingredientListViewController, animated: true, completion: nil) + } +} diff --git a/CookSmart/CookSmart/Core/Colors.swift b/CookSmart/CookSmart/Core/Colors.swift index 3fd9f9b..d53b5f3 100644 --- a/CookSmart/CookSmart/Core/Colors.swift +++ b/CookSmart/CookSmart/Core/Colors.swift @@ -9,9 +9,9 @@ import UIKit enum Color { - static let redLineColor = Color.color( + static let cakeRed = Color.color( light: UIColor(red: 187.0 / 255.0, green: 1.0 / 255.0, blue: 3.0 / 255.0, alpha: 1.0), - dark: UIColor(red: 187.0 / 255.0, green: 1.0 / 255.0, blue: 3.0 / 255.0, alpha: 1.0) + dark: UIColor(red: 234.0 / 255.0, green: 110.0 / 255.0, blue: 104.0 / 255.0, alpha: 1.0) ) static let background = Color.color( diff --git a/CookSmart/CookSmart/IngredientListViewController.swift b/CookSmart/CookSmart/IngredientListViewController.swift new file mode 100644 index 0000000..797eb60 --- /dev/null +++ b/CookSmart/CookSmart/IngredientListViewController.swift @@ -0,0 +1,74 @@ +// +// IngredientListViewController.swift +// cake +// +// Created by Alex King on 3/28/20. +// Copyright © 2020 Olga Galchenko. All rights reserved. +// + +import Foundation +import SwiftUI + +class IngredientListViewController: UIHostingController { + init() { + super.init(rootView: IngredientListView()) + } + + @objc + dynamic required init?(coder _: NSCoder) { + assertionFailure("init(coder:) has not been implemented") + return nil + } +} + +struct IngredientListView: View { + @State private var searchText: String = "" + + var body: some View { + NavigationView { + VStack { + SearchBar(text: $searchText) + List { + Text("test") + } + }.navigationBarTitle("Ingredients") + } + } +} + +private struct SearchBar: UIViewRepresentable { + @Binding var text: String + + class Coordinator: NSObject, UISearchBarDelegate { + @Binding var text: String + + init(text: Binding) { + _text = text + } + + func searchBar(_: UISearchBar, textDidChange searchText: String) { + text = searchText + } + } + + func makeCoordinator() -> SearchBar.Coordinator { + Coordinator(text: $text) + } + + func makeUIView(context: UIViewRepresentableContext) -> UISearchBar { + let searchBar = UISearchBar(frame: .zero) + searchBar.delegate = context.coordinator + searchBar.searchBarStyle = .minimal + return searchBar + } + + func updateUIView(_ uiView: UISearchBar, context _: UIViewRepresentableContext) { + uiView.text = text + } +} + +private struct IngredientListViewController_Previews: PreviewProvider { + static var previews: some View { + IngredientListView() + } +} diff --git a/CookSmart/CookSmart/cake-Bridging-Header.h b/CookSmart/CookSmart/cake-Bridging-Header.h index 1b2cb5d..78d549e 100644 --- a/CookSmart/CookSmart/cake-Bridging-Header.h +++ b/CookSmart/CookSmart/cake-Bridging-Header.h @@ -2,3 +2,5 @@ // Use this file to import your target's public headers that you would like to expose to Swift. // +#import "CSIngredients.h" +#import "CSConversionVC.h" From 8af356fb37bfbd86cce6d741c5d74b57a411c9c1 Mon Sep 17 00:00:00 2001 From: Alex King Date: Sun, 29 Mar 2020 15:56:04 -0700 Subject: [PATCH 2/6] Adding nav bar item --- .../CookSmart/IngredientListViewController.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/CookSmart/CookSmart/IngredientListViewController.swift b/CookSmart/CookSmart/IngredientListViewController.swift index 797eb60..001d6ac 100644 --- a/CookSmart/CookSmart/IngredientListViewController.swift +++ b/CookSmart/CookSmart/IngredientListViewController.swift @@ -31,7 +31,16 @@ struct IngredientListView: View { List { Text("test") } - }.navigationBarTitle("Ingredients") + } + .navigationBarTitle("Ingredients") + .navigationBarItems(leading: + Button(action: { + print("Pressed") + }, label: { + Image(systemName: "xmark") + .font(Font.system(size: 20, weight: .medium, design: .default)) + }) + ) } } } From bc49463df918febfe3deb7a92201a08ea4057e8f Mon Sep 17 00:00:00 2001 From: Alex King Date: Sun, 29 Mar 2020 16:01:06 -0700 Subject: [PATCH 3/6] Fix preview --- CookSmart/CookSmart/IngredientListViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CookSmart/CookSmart/IngredientListViewController.swift b/CookSmart/CookSmart/IngredientListViewController.swift index 001d6ac..41c5691 100644 --- a/CookSmart/CookSmart/IngredientListViewController.swift +++ b/CookSmart/CookSmart/IngredientListViewController.swift @@ -76,7 +76,7 @@ private struct SearchBar: UIViewRepresentable { } } -private struct IngredientListViewController_Previews: PreviewProvider { +struct IngredientListViewController_Previews: PreviewProvider { static var previews: some View { IngredientListView() } From 15f6e328a75d479c2f3c9dade52b68e90b433ccf Mon Sep 17 00:00:00 2001 From: Alex King Date: Sun, 29 Mar 2020 23:29:21 -0700 Subject: [PATCH 4/6] Progress on Ingredient list --- CookSmart/CookSmart.xcodeproj/project.pbxproj | 8 +++ CookSmart/CookSmart/CSIngredientListVC.m | 46 --------------- CookSmart/CookSmart/CSIngredientsExt.swift | 33 +++++++++++ CookSmart/CookSmart/CSScaleView.m | 10 +++- .../IngredientListViewController.swift | 56 +++++++++++++++++-- .../ViewControllerRepresentables.swift | 20 +++++++ CookSmart/CookSmart/cake-Bridging-Header.h | 5 ++ 7 files changed, 125 insertions(+), 53 deletions(-) create mode 100644 CookSmart/CookSmart/CSIngredientsExt.swift create mode 100644 CookSmart/CookSmart/ViewControllerRepresentables.swift diff --git a/CookSmart/CookSmart.xcodeproj/project.pbxproj b/CookSmart/CookSmart.xcodeproj/project.pbxproj index c9cf661..0829eb4 100644 --- a/CookSmart/CookSmart.xcodeproj/project.pbxproj +++ b/CookSmart/CookSmart.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 39024F62243174BB0063725D /* CSIngredientsExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39024F61243174BB0063725D /* CSIngredientsExt.swift */; }; + 39024F6524317F720063725D /* ViewControllerRepresentables.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39024F6424317F720063725D /* ViewControllerRepresentables.swift */; }; 392D8645242FD0FF002064D6 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392D8644242FD0FF002064D6 /* Colors.swift */; }; 392D865424304F2D002064D6 /* IngredientListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392D865324304F2D002064D6 /* IngredientListViewController.swift */; }; 392D865724306D0D002064D6 /* CSConversionVCExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392D865624306D0D002064D6 /* CSConversionVCExt.swift */; }; @@ -73,6 +75,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 39024F61243174BB0063725D /* CSIngredientsExt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CSIngredientsExt.swift; sourceTree = ""; }; + 39024F6424317F720063725D /* ViewControllerRepresentables.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewControllerRepresentables.swift; sourceTree = ""; }; 392D8644242FD0FF002064D6 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = ""; }; 392D865324304F2D002064D6 /* IngredientListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IngredientListViewController.swift; sourceTree = ""; }; 392D865624306D0D002064D6 /* CSConversionVCExt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CSConversionVCExt.swift; sourceTree = ""; }; @@ -197,6 +201,7 @@ 93D0993B18977D320014DB3E /* Units */, 80592C0318924B010082D4E1 /* CSIngredients.h */, 80592C0418924B010082D4E1 /* CSIngredients.m */, + 39024F61243174BB0063725D /* CSIngredientsExt.swift */, 80592C0618924B0F0082D4E1 /* CSIngredient.h */, 80592C0718924B0F0082D4E1 /* CSIngredient.m */, 80592C091892541D0082D4E1 /* CSIngredientGroup.h */, @@ -306,6 +311,7 @@ 807C0B681BB3DD04007D2C03 /* Ingredient List UI */, B7BCA2A2180524A800CF7588 /* Images.xcassets */, B7BCA297180524A800CF7588 /* Supporting Files */, + 39024F6424317F720063725D /* ViewControllerRepresentables.swift */, ); path = CookSmart; sourceTree = ""; @@ -521,6 +527,7 @@ buildActionMask = 2147483647; files = ( 77479AEA242FB9C8000CFB0E /* ScaleTile.swift in Sources */, + 39024F6524317F720063725D /* ViewControllerRepresentables.swift in Sources */, 392D8645242FD0FF002064D6 /* Colors.swift in Sources */, 80A614DE18BF3D4100A205BB /* CSGlassView.m in Sources */, 392D865424304F2D002064D6 /* IngredientListViewController.swift in Sources */, @@ -544,6 +551,7 @@ B7BCA29D180524A800CF7588 /* main.m in Sources */, 8087758018C2615F00A94031 /* CSUnitPickerCenterLineView.m in Sources */, 936809701891059700655B89 /* CSIngredientListVC.m in Sources */, + 39024F62243174BB0063725D /* CSIngredientsExt.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/CookSmart/CookSmart/CSIngredientListVC.m b/CookSmart/CookSmart/CSIngredientListVC.m index a876a44..323c2dc 100644 --- a/CookSmart/CookSmart/CSIngredientListVC.m +++ b/CookSmart/CookSmart/CSIngredientListVC.m @@ -171,52 +171,6 @@ - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *) ((numGroups > 1) || (numGroups == 1 && [[[CSIngredients sharedInstance] ingredientGroupAtIndex:0] countOfIngredients] > 1)); } -- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath -{ - if (editingStyle == UITableViewCellEditingStyleDelete) - { - CSIngredients *ingredients = [self ingredientsToSupplyData]; - CSRecentsIngredientGroup *beforeChangeRecents = ingredients.recents; - CSIngredientGroup *ingredientGroup = [ingredients ingredientGroupAtIndex:indexPath.section]; - CSIngredient *ingredientToDelete = [ingredientGroup ingredientAtIndex:indexPath.row]; - BOOL needToDeleteFromRecents = beforeChangeRecents && [beforeChangeRecents indexOfIngredient:ingredientToDelete] != NSNotFound; - BOOL deleteSuccess = [ingredients deleteIngredientAtGroupIndex:indexPath.section ingredientIndex:indexPath.row]; - if (deleteSuccess) - { - logUserAction(@"ingredient_delete", [ingredientToDelete dictionaryForAnalytics]); - - [tableView beginUpdates]; - if (ingredientGroup.countOfIngredients == 0) - { - [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic]; - } - else - { - [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; - } - - if (needToDeleteFromRecents) - { - CSRecentsIngredientGroup *afterChangeRecents = ingredients.recents; - if (afterChangeRecents == nil) - { - [tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; - } - else - { - [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:[beforeChangeRecents indexOfIngredient:ingredientToDelete] inSection:0]] - withRowAnimation:UITableViewRowAnimationAutomatic]; - } - } - [tableView endUpdates]; - } - else - { - logIssue(@"ingredient_delete_fail", [ingredientToDelete dictionaryForAnalytics]); - } - } -} - - (void)editIngredient:(id)sender { UIViewController *editVC; diff --git a/CookSmart/CookSmart/CSIngredientsExt.swift b/CookSmart/CookSmart/CSIngredientsExt.swift new file mode 100644 index 0000000..66ce163 --- /dev/null +++ b/CookSmart/CookSmart/CSIngredientsExt.swift @@ -0,0 +1,33 @@ +// +// CSIngredientsExt.swift +// cake +// +// Created by Alex King on 3/29/20. +// Copyright © 2020 Olga Galchenko. All rights reserved. +// + +import Foundation + +extension CSIngredient: Identifiable { + public var id: String { name } +} + +extension CSIngredientGroup: Identifiable { + public var id: String { name } +} + +extension CSIngredientGroup { + var ingredients: [CSIngredient] { + (0 ..< countOfIngredients()).map { ingredient(at: $0) } + } +} + +extension CSIngredients { + var ingredientList: [CSIngredientGroup] { + var groups: [CSIngredientGroup] = (0 ..< countOfIngredientGroups()).map { ingredientGroup(at: $0) } + if let recents = recents(), recents.countOfIngredients() > 0 { + groups.insert(recents, at: 0) + } + return groups + } +} diff --git a/CookSmart/CookSmart/CSScaleView.m b/CookSmart/CookSmart/CSScaleView.m index 90703d7..6ba1831 100644 --- a/CookSmart/CookSmart/CSScaleView.m +++ b/CookSmart/CookSmart/CSScaleView.m @@ -196,7 +196,11 @@ static inline CGFloat unitsPerPoint(CSScaleView *scaleView) static inline CGFloat ptsPerUnit(CSScaleView *scaleView) { - return (SCALE_TILE_HEIGHT/scaleView.unitsPerTile); + CGFloat pts = (SCALE_TILE_HEIGHT/scaleView.unitsPerTile); + if (isnan(pts)) { + return 0; + } + return pts; } static inline CGFloat getTargetContentOffset(CSScaleView *scaleView) @@ -207,10 +211,14 @@ static inline CGFloat getTargetContentOffset(CSScaleView *scaleView) static inline void setScrollViewOffset(CSScaleView *scaleView, CGPoint newContentOffset, BOOL cancelDeceleration) { + if (isnan(newContentOffset.y)) { + return; + } id delegate = scaleView.delegate; scaleView.delegate = nil; if (cancelDeceleration) { + NSLog(@"%f", newContentOffset.y); [scaleView setContentOffset:newContentOffset animated:NO]; } else diff --git a/CookSmart/CookSmart/IngredientListViewController.swift b/CookSmart/CookSmart/IngredientListViewController.swift index 41c5691..ae2711c 100644 --- a/CookSmart/CookSmart/IngredientListViewController.swift +++ b/CookSmart/CookSmart/IngredientListViewController.swift @@ -19,6 +19,11 @@ class IngredientListViewController: UIHostingController { assertionFailure("init(coder:) has not been implemented") return nil } + + override func viewDidLoad() { + super.viewDidLoad() + CSIngredients.sharedInstance()?.refreshRecents() + } } struct IngredientListView: View { @@ -26,21 +31,59 @@ struct IngredientListView: View { var body: some View { NavigationView { - VStack { + List { SearchBar(text: $searchText) - List { - Text("test") + ForEach(CSIngredients.sharedInstance()!.ingredientList) { group in + Section(header: Text(group.name)) { + ForEach(group.ingredients) { ingredient in + IngredientListCell(ingredientName: ingredient.name) + } + } } } - .navigationBarTitle("Ingredients") + .listStyle(GroupedListStyle()) + .navigationBarTitle(searchText.isEmpty ? "Ingredients" : "Filtered") .navigationBarItems(leading: Button(action: { print("Pressed") }, label: { Image(systemName: "xmark") + .foregroundColor(SwiftUI.Color(Color.cakeRed)) .font(Font.system(size: 20, weight: .medium, design: .default)) - }) - ) + }), + trailing: + NavigationLink(destination: CSEditIngredientVCRepresentable(), + label: { Image(systemName: "plus") + .foregroundColor(SwiftUI.Color(Color.cakeRed)) + .font(Font.system(size: 20, weight: .medium, design: .default)) + })) + } + } +} + +struct IngredientListCell: View { + private let ingredientName: String + + init(ingredientName: String = "Sugar") { + self.ingredientName = ingredientName + } + + var body: some View { + ZStack { + NavigationLink(destination: CSEditIngredientVCRepresentable()) { + EmptyView() + }.buttonStyle(PlainButtonStyle()) + HStack { + Text(ingredientName) + Spacer() + Button(action: { + print("Edit Tapped") + }, label: { + Image(systemName: "pencil.circle") + .foregroundColor(SwiftUI.Color(Color.cakeRed)) + .font(Font.system(size: 22, weight: .regular, design: .default)) + }).buttonStyle(BorderlessButtonStyle()) + } } } } @@ -66,6 +109,7 @@ private struct SearchBar: UIViewRepresentable { func makeUIView(context: UIViewRepresentableContext) -> UISearchBar { let searchBar = UISearchBar(frame: .zero) + searchBar.placeholder = "Search" searchBar.delegate = context.coordinator searchBar.searchBarStyle = .minimal return searchBar diff --git a/CookSmart/CookSmart/ViewControllerRepresentables.swift b/CookSmart/CookSmart/ViewControllerRepresentables.swift new file mode 100644 index 0000000..2fae5cb --- /dev/null +++ b/CookSmart/CookSmart/ViewControllerRepresentables.swift @@ -0,0 +1,20 @@ +// +// ViewControllerRepresentables.swift +// cake +// +// Created by Alex King on 3/29/20. +// Copyright © 2020 Olga Galchenko. All rights reserved. +// + +import Foundation +import SwiftUI + +final class CSEditIngredientVCRepresentable: UIViewControllerRepresentable { + func updateUIViewController(_: CSEditIngredientVC, context _: Context) {} + + func makeUIViewController(context _: Context) -> CSEditIngredientVC { + CSEditIngredientVC(ingredient: nil, withDoneBlock: { _ in + + }) {} + } +} diff --git a/CookSmart/CookSmart/cake-Bridging-Header.h b/CookSmart/CookSmart/cake-Bridging-Header.h index 78d549e..ecc65d9 100644 --- a/CookSmart/CookSmart/cake-Bridging-Header.h +++ b/CookSmart/CookSmart/cake-Bridging-Header.h @@ -4,3 +4,8 @@ #import "CSIngredients.h" #import "CSConversionVC.h" +#import "CSIngredient.h" +#import "CSIngredientGroup.h" +#import "CSRecentsIngredientGroup.h" +#import "CSIngredientGroupInternals.h" +#import "CSEditIngredientVC.h" From ede39a8012b8cff7e3f6d2a091984906fc07a25d Mon Sep 17 00:00:00 2001 From: Alex King Date: Sat, 4 Apr 2020 17:46:32 -0700 Subject: [PATCH 5/6] Cleanup --- CookSmart/CookSmart/CSConversionVC.m | 1 - CookSmart/CookSmart/CSIngredientsExt.swift | 5 +---- CookSmart/CookSmart/IngredientListViewController.swift | 2 +- CookSmart/CookSmart/ViewControllerRepresentables.swift | 6 +++--- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/CookSmart/CookSmart/CSConversionVC.m b/CookSmart/CookSmart/CSConversionVC.m index 239d6d5..c5585ce 100644 --- a/CookSmart/CookSmart/CSConversionVC.m +++ b/CookSmart/CookSmart/CSConversionVC.m @@ -100,7 +100,6 @@ - (void)viewDidAppear:(BOOL)animated // When we first appear, always select the very first ingredient – the one most recently selected. [self selectIngredientAtIndex:self.ingredientIndex]; logViewChange(@"conversion", [self.scaleVC analyticsAttributes]); - self.view.backgroundColor = UIColor.systemGray6Color; } - (void)viewDidDisappear:(BOOL)animated diff --git a/CookSmart/CookSmart/CSIngredientsExt.swift b/CookSmart/CookSmart/CSIngredientsExt.swift index 66ce163..945746f 100644 --- a/CookSmart/CookSmart/CSIngredientsExt.swift +++ b/CookSmart/CookSmart/CSIngredientsExt.swift @@ -24,10 +24,7 @@ extension CSIngredientGroup { extension CSIngredients { var ingredientList: [CSIngredientGroup] { - var groups: [CSIngredientGroup] = (0 ..< countOfIngredientGroups()).map { ingredientGroup(at: $0) } - if let recents = recents(), recents.countOfIngredients() > 0 { - groups.insert(recents, at: 0) - } + let groups: [CSIngredientGroup] = (0 ..< countOfIngredientGroups()).map { ingredientGroup(at: $0) } return groups } } diff --git a/CookSmart/CookSmart/IngredientListViewController.swift b/CookSmart/CookSmart/IngredientListViewController.swift index ae2711c..20d2cde 100644 --- a/CookSmart/CookSmart/IngredientListViewController.swift +++ b/CookSmart/CookSmart/IngredientListViewController.swift @@ -42,7 +42,7 @@ struct IngredientListView: View { } } .listStyle(GroupedListStyle()) - .navigationBarTitle(searchText.isEmpty ? "Ingredients" : "Filtered") + .navigationBarTitle("Ingredients") .navigationBarItems(leading: Button(action: { print("Pressed") diff --git a/CookSmart/CookSmart/ViewControllerRepresentables.swift b/CookSmart/CookSmart/ViewControllerRepresentables.swift index 2fae5cb..67e78bf 100644 --- a/CookSmart/CookSmart/ViewControllerRepresentables.swift +++ b/CookSmart/CookSmart/ViewControllerRepresentables.swift @@ -13,8 +13,8 @@ final class CSEditIngredientVCRepresentable: UIViewControllerRepresentable { func updateUIViewController(_: CSEditIngredientVC, context _: Context) {} func makeUIViewController(context _: Context) -> CSEditIngredientVC { - CSEditIngredientVC(ingredient: nil, withDoneBlock: { _ in - - }) {} + CSEditIngredientVC(ingredient: nil, + withDoneBlock: { _ in }, + andCancel: {}) } } From 3766ae6fa1fa27aaab2ca99fe4414f41a7fdc7f6 Mon Sep 17 00:00:00 2001 From: Alex King Date: Sun, 12 Apr 2020 13:33:37 -0700 Subject: [PATCH 6/6] Fix build --- CookSmart/CookSmart.xcodeproj/project.pbxproj | 5 ----- CookSmart/CookSmart/Core/Colors.swift | 2 +- .../EditIngredientViewController.swift | 9 +++++++++ .../IngredientListViewController.swift | 10 +++++----- .../ViewControllerRepresentables.swift | 20 ------------------- CookSmart/CookSmart/cake-Bridging-Header.h | 1 - 6 files changed, 15 insertions(+), 32 deletions(-) delete mode 100644 CookSmart/CookSmart/ViewControllerRepresentables.swift diff --git a/CookSmart/CookSmart.xcodeproj/project.pbxproj b/CookSmart/CookSmart.xcodeproj/project.pbxproj index 1bafcc8..d16b3a6 100644 --- a/CookSmart/CookSmart.xcodeproj/project.pbxproj +++ b/CookSmart/CookSmart.xcodeproj/project.pbxproj @@ -8,7 +8,6 @@ /* Begin PBXBuildFile section */ 39024F62243174BB0063725D /* CSIngredientsExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39024F61243174BB0063725D /* CSIngredientsExt.swift */; }; - 39024F6524317F720063725D /* ViewControllerRepresentables.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39024F6424317F720063725D /* ViewControllerRepresentables.swift */; }; 392D8645242FD0FF002064D6 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392D8644242FD0FF002064D6 /* Colors.swift */; }; 392D865424304F2D002064D6 /* IngredientListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392D865324304F2D002064D6 /* IngredientListViewController.swift */; }; 392D865724306D0D002064D6 /* CSConversionVCExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 392D865624306D0D002064D6 /* CSConversionVCExt.swift */; }; @@ -88,7 +87,6 @@ /* Begin PBXFileReference section */ 39024F61243174BB0063725D /* CSIngredientsExt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CSIngredientsExt.swift; sourceTree = ""; }; - 39024F6424317F720063725D /* ViewControllerRepresentables.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewControllerRepresentables.swift; sourceTree = ""; }; 392D8644242FD0FF002064D6 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = ""; }; 392D865324304F2D002064D6 /* IngredientListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IngredientListViewController.swift; sourceTree = ""; }; 392D865624306D0D002064D6 /* CSConversionVCExt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CSConversionVCExt.swift; sourceTree = ""; }; @@ -348,7 +346,6 @@ 807C0B681BB3DD04007D2C03 /* Ingredient List UI */, B7BCA2A2180524A800CF7588 /* Images.xcassets */, B7BCA297180524A800CF7588 /* Supporting Files */, - 39024F6424317F720063725D /* ViewControllerRepresentables.swift */, ); path = CookSmart; sourceTree = ""; @@ -587,9 +584,7 @@ 774EBD6524426CC2002A59AC /* Double+StringUtils.swift in Sources */, 396E363F24396D5900C2815B /* UnitPickerView.swift in Sources */, 77479AEA242FB9C8000CFB0E /* ScaleTile.swift in Sources */, - 39024F6524317F720063725D /* ViewControllerRepresentables.swift in Sources */, 392D865424304F2D002064D6 /* IngredientListViewController.swift in Sources */, - 935DB3AC18B46A28007989D7 /* CSEditIngredientVC.m in Sources */, 392D865724306D0D002064D6 /* CSConversionVCExt.swift in Sources */, 774EBD802442EC26002A59AC /* UIView+Constraints.swift in Sources */, 392D8645242FD0FF002064D6 /* Colors.swift in Sources */, diff --git a/CookSmart/CookSmart/Core/Colors.swift b/CookSmart/CookSmart/Core/Colors.swift index 454affd..5304b96 100644 --- a/CookSmart/CookSmart/Core/Colors.swift +++ b/CookSmart/CookSmart/Core/Colors.swift @@ -9,7 +9,7 @@ import UIKit enum Color { - static let cakeRed = Color.color( + static let redLineColor = Color.color( light: UIColor(red: 187.0 / 255.0, green: 1.0 / 255.0, blue: 3.0 / 255.0, alpha: 1.0), dark: UIColor(red: 234.0 / 255.0, green: 110.0 / 255.0, blue: 104.0 / 255.0, alpha: 1.0) ) diff --git a/CookSmart/CookSmart/Edit Ingredient/EditIngredientViewController.swift b/CookSmart/CookSmart/Edit Ingredient/EditIngredientViewController.swift index 878a413..069907e 100644 --- a/CookSmart/CookSmart/Edit Ingredient/EditIngredientViewController.swift +++ b/CookSmart/CookSmart/Edit Ingredient/EditIngredientViewController.swift @@ -7,6 +7,15 @@ // import Foundation +import SwiftUI + +struct EditIngredientViewControllerRepresentable: UIViewControllerRepresentable { + func makeUIViewController(context: Context) -> EditIngredientViewController { + EditIngredientViewController() + } + + func updateUIViewController(_ uiViewController: EditIngredientViewController, context: Context) {} +} class EditIngredientViewController: UIViewController { diff --git a/CookSmart/CookSmart/IngredientListViewController.swift b/CookSmart/CookSmart/IngredientListViewController.swift index 20d2cde..f0d85a8 100644 --- a/CookSmart/CookSmart/IngredientListViewController.swift +++ b/CookSmart/CookSmart/IngredientListViewController.swift @@ -48,13 +48,13 @@ struct IngredientListView: View { print("Pressed") }, label: { Image(systemName: "xmark") - .foregroundColor(SwiftUI.Color(Color.cakeRed)) + .foregroundColor(SwiftUI.Color(Color.redLineColor)) .font(Font.system(size: 20, weight: .medium, design: .default)) }), trailing: - NavigationLink(destination: CSEditIngredientVCRepresentable(), + NavigationLink(destination: EditIngredientViewControllerRepresentable(), label: { Image(systemName: "plus") - .foregroundColor(SwiftUI.Color(Color.cakeRed)) + .foregroundColor(SwiftUI.Color(Color.redLineColor)) .font(Font.system(size: 20, weight: .medium, design: .default)) })) } @@ -70,7 +70,7 @@ struct IngredientListCell: View { var body: some View { ZStack { - NavigationLink(destination: CSEditIngredientVCRepresentable()) { + NavigationLink(destination: EditIngredientViewControllerRepresentable()) { EmptyView() }.buttonStyle(PlainButtonStyle()) HStack { @@ -80,7 +80,7 @@ struct IngredientListCell: View { print("Edit Tapped") }, label: { Image(systemName: "pencil.circle") - .foregroundColor(SwiftUI.Color(Color.cakeRed)) + .foregroundColor(SwiftUI.Color(Color.redLineColor)) .font(Font.system(size: 22, weight: .regular, design: .default)) }).buttonStyle(BorderlessButtonStyle()) } diff --git a/CookSmart/CookSmart/ViewControllerRepresentables.swift b/CookSmart/CookSmart/ViewControllerRepresentables.swift deleted file mode 100644 index 67e78bf..0000000 --- a/CookSmart/CookSmart/ViewControllerRepresentables.swift +++ /dev/null @@ -1,20 +0,0 @@ -// -// ViewControllerRepresentables.swift -// cake -// -// Created by Alex King on 3/29/20. -// Copyright © 2020 Olga Galchenko. All rights reserved. -// - -import Foundation -import SwiftUI - -final class CSEditIngredientVCRepresentable: UIViewControllerRepresentable { - func updateUIViewController(_: CSEditIngredientVC, context _: Context) {} - - func makeUIViewController(context _: Context) -> CSEditIngredientVC { - CSEditIngredientVC(ingredient: nil, - withDoneBlock: { _ in }, - andCancel: {}) - } -} diff --git a/CookSmart/CookSmart/cake-Bridging-Header.h b/CookSmart/CookSmart/cake-Bridging-Header.h index 9ca44f1..9922305 100644 --- a/CookSmart/CookSmart/cake-Bridging-Header.h +++ b/CookSmart/CookSmart/cake-Bridging-Header.h @@ -14,4 +14,3 @@ #import "CSIngredientGroup.h" #import "CSRecentsIngredientGroup.h" #import "CSIngredientGroupInternals.h" -#import "CSEditIngredientVC.h"