diff --git a/brain-marks.xcodeproj/project.pbxproj b/brain-marks.xcodeproj/project.pbxproj index 197d03b..0b27c02 100644 --- a/brain-marks.xcodeproj/project.pbxproj +++ b/brain-marks.xcodeproj/project.pbxproj @@ -21,10 +21,12 @@ C4392E80BFE640DAA2625F9D /* AWSTweet+Schema.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC43813A9098469FB57F5871 /* AWSTweet+Schema.swift */; }; CA80832580B84CB3A2D5F629 /* AmplifyModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14F5BDE61EAA4A2DB9030734 /* AmplifyModels.swift */; }; CF16AFB9BE3F401E98C85114 /* AWSTweet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AA54EDF3B34443CA9D530E5 /* AWSTweet.swift */; }; + FF36B661262357F9007A6D7F /* AWSCategory+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF36B660262357F9007A6D7F /* AWSCategory+Extension.swift */; }; FF59902B2622C1CE004DF328 /* Amplify in Frameworks */ = {isa = PBXBuildFile; productRef = FF59902A2622C1CE004DF328 /* Amplify */; }; FF59902D2622C1CE004DF328 /* AWSAPIPlugin in Frameworks */ = {isa = PBXBuildFile; productRef = FF59902C2622C1CE004DF328 /* AWSAPIPlugin */; }; FF59902F2622C1CE004DF328 /* AWSDataStorePlugin in Frameworks */ = {isa = PBXBuildFile; productRef = FF59902E2622C1CE004DF328 /* AWSDataStorePlugin */; }; FF5990692622DD61004DF328 /* DataStoreManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF5990682622DD61004DF328 /* DataStoreManager.swift */; }; + FF6DCBF22623530400EC366F /* CategoryListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF6DCBF12623530400EC366F /* CategoryListViewModel.swift */; }; FFEBBB3A26223F75000F475F /* brain_marksApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEBBB3926223F75000F475F /* brain_marksApp.swift */; }; FFEBBB3C26223F75000F475F /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEBBB3B26223F75000F475F /* ContentView.swift */; }; FFEBBB3E26223F7E000F475F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FFEBBB3D26223F7E000F475F /* Assets.xcassets */; }; @@ -68,7 +70,9 @@ CC43813A9098469FB57F5871 /* AWSTweet+Schema.swift */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 4; includeInIndex = 0; lastKnownFileType = sourcecode.swift; name = "AWSTweet+Schema.swift"; path = "amplify/generated/models/AWSTweet+Schema.swift"; sourceTree = ""; }; DA7D79B731BC4ECFAAE8E817 /* awsconfiguration.json */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = unknown; path = awsconfiguration.json; sourceTree = ""; }; ECA1CF04DB754255822691F2 /* amplifyconfiguration.json */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = unknown; path = amplifyconfiguration.json; sourceTree = ""; }; + FF36B660262357F9007A6D7F /* AWSCategory+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AWSCategory+Extension.swift"; sourceTree = ""; }; FF5990682622DD61004DF328 /* DataStoreManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStoreManager.swift; sourceTree = ""; }; + FF6DCBF12623530400EC366F /* CategoryListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryListViewModel.swift; sourceTree = ""; }; FFEBBB3626223F75000F475F /* brain-marks.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "brain-marks.app"; sourceTree = BUILT_PRODUCTS_DIR; }; FFEBBB3926223F75000F475F /* brain_marksApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = brain_marksApp.swift; sourceTree = ""; }; FFEBBB3B26223F75000F475F /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -184,6 +188,8 @@ FFEBBB7526226A39000F475F /* Secrets.swift */, 91739DEA2622D2A7000F982A /* AddURLView.swift */, FF5990682622DD61004DF328 /* DataStoreManager.swift */, + FF6DCBF12623530400EC366F /* CategoryListViewModel.swift */, + FF36B660262357F9007A6D7F /* AWSCategory+Extension.swift */, ); path = "brain-marks"; sourceTree = ""; @@ -387,8 +393,10 @@ C0DAFC52B24C456EAE5AEF21 /* AWSCategory.swift in Sources */, 684E2F2D3FCC4D36AC68E57D /* AWSCategory+Schema.swift in Sources */, CF16AFB9BE3F401E98C85114 /* AWSTweet.swift in Sources */, + FF36B661262357F9007A6D7F /* AWSCategory+Extension.swift in Sources */, C4392E80BFE640DAA2625F9D /* AWSTweet+Schema.swift in Sources */, A205CD422622A3EB00517DB5 /* TweetList.swift in Sources */, + FF6DCBF22623530400EC366F /* CategoryListViewModel.swift in Sources */, 91739DEB2622D2A7000F982A /* AddURLView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/brain-marks/AWSCategory+Extension.swift b/brain-marks/AWSCategory+Extension.swift new file mode 100644 index 0000000..d2aa744 --- /dev/null +++ b/brain-marks/AWSCategory+Extension.swift @@ -0,0 +1,10 @@ +// +// AWSCategory+Extension.swift +// brain-marks +// +// Created by Mikaela Caron on 4/11/21. +// + +import Foundation + +extension AWSCategory: Identifiable {} diff --git a/brain-marks/CategoryList.swift b/brain-marks/CategoryList.swift index ab91b35..c89e262 100644 --- a/brain-marks/CategoryList.swift +++ b/brain-marks/CategoryList.swift @@ -8,17 +8,18 @@ import SwiftUI struct CategoryList: View { - - var categories: [Category] + + @StateObject var viewModel = CategoryListViewModel() + @State private var showingSheet = false - + var body: some View { NavigationView { - List(categories) { category in - NavigationLink(destination: TweetList(category: category)) { + List(viewModel.categories) { category in + NavigationLink(destination: TweetList(category: category)) { CategoryRow(category: category) } - + }.listStyle(InsetGroupedListStyle()) .environment(\.horizontalSizeClass, .regular) .navigationTitle("Categories") @@ -39,6 +40,9 @@ struct CategoryList: View { } } } + .onAppear { + viewModel.getCategories() + } } } @@ -68,6 +72,7 @@ struct NewCategorySheetView: View { Button { presentationMode.wrappedValue.dismiss() + DataStoreManger.shared.createCategory(category: AWSCategory(name: newCategory, imageName: "swift")) } label: { Text("Create") .frame(width: 150, height: 50) @@ -86,16 +91,7 @@ struct NewCategorySheetView: View { struct CategoryList_Previews: PreviewProvider { static var previews: some View { - CategoryList(categories: [ - Category(id: 0, - name: "SwiftUI", - numberOfTweets: 3, - imageName: "swift"), - Category(id: 1, - name: "BigBrainHacks", - numberOfTweets: 5, - imageName: "laptopcomputer") - ]) + CategoryList() NewCategorySheetView() } diff --git a/brain-marks/CategoryListViewModel.swift b/brain-marks/CategoryListViewModel.swift new file mode 100644 index 0000000..8d19507 --- /dev/null +++ b/brain-marks/CategoryListViewModel.swift @@ -0,0 +1,27 @@ +// +// CategoryListViewModel.swift +// brain-marks +// +// Created by Mikaela Caron on 4/11/21. +// + +import SwiftUI + +final class CategoryListViewModel: ObservableObject { + + @Published var categories = [AWSCategory]() + + func getCategories() { + DataStoreManger.shared.fetchCategories { result in + switch result { + case .success(let categories): + DispatchQueue.main.async { + self.categories = categories + } + case .failure(let error): + print("Error fetching categories: \(error)") + } + } + } + +} diff --git a/brain-marks/CategoryRow.swift b/brain-marks/CategoryRow.swift index 109763d..1c856d8 100644 --- a/brain-marks/CategoryRow.swift +++ b/brain-marks/CategoryRow.swift @@ -8,11 +8,11 @@ import SwiftUI struct CategoryRow: View { - var category: Category + var category: AWSCategory var body: some View { HStack { - Image(systemName: category.imageName) + Image(systemName: category.imageName ?? "swift") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 40, height: 40) @@ -22,18 +22,18 @@ struct CategoryRow: View { } } -struct CategoryRow_Previews: PreviewProvider { - static var previews: some View { - Group { - CategoryRow(category: Category(id: 0, - name: "SwiftUI", - numberOfTweets: 3, - imageName: "swift")) - CategoryRow(category: Category(id: 1, - name: "BigBrainHacks", - numberOfTweets: 5, - imageName: "laptopcomputer")) - } - .previewLayout(.fixed(width: 300, height: 70)) - } -} +//struct CategoryRow_Previews: PreviewProvider { +// static var previews: some View { +// Group { +// CategoryRow(category: Category(id: 0, +// name: "SwiftUI", +// numberOfTweets: 3, +// imageName: "swift")) +// CategoryRow(category: Category(id: 1, +// name: "BigBrainHacks", +// numberOfTweets: 5, +// imageName: "laptopcomputer")) +// } +// .previewLayout(.fixed(width: 300, height: 70)) +// } +//} diff --git a/brain-marks/ContentView.swift b/brain-marks/ContentView.swift index 0e5a42c..5a5b361 100644 --- a/brain-marks/ContentView.swift +++ b/brain-marks/ContentView.swift @@ -12,10 +12,7 @@ struct ContentView: View { var body: some View { VStack { - CategoryList(categories: [ - Category(id: 0, name: "SwiftUI", numberOfTweets: 3, imageName: "swift"), - Category(id: 1, name: "BigBrainHacks", numberOfTweets: 5, imageName: "laptopcomputer") - ]) + CategoryList() } } } diff --git a/brain-marks/TweetList.swift b/brain-marks/TweetList.swift index aea31f7..99141a6 100644 --- a/brain-marks/TweetList.swift +++ b/brain-marks/TweetList.swift @@ -8,7 +8,7 @@ import SwiftUI struct TweetList: View { - var category: Category + var category: AWSCategory var body: some View { @@ -17,7 +17,7 @@ struct TweetList: View { } .toolbar { ToolbarItem(placement: .principal) { HStack { - Image(systemName: category.imageName) + Image(systemName: category.imageName ?? "swift") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 25, height: 25)