# HG changeset patch # User Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> # Date 1613589076 -3600 # Node ID 6488392684cc822ce11135267c4e81c69c1df5bd # Parent 26d69711ea667c190bc53921466dced40010ad37 Implementing CoreData diff -r 26d69711ea66 -r 6488392684cc LazyBear.xcodeproj/project.pbxproj --- a/LazyBear.xcodeproj/project.pbxproj Wed Feb 17 19:18:53 2021 +0100 +++ b/LazyBear.xcodeproj/project.pbxproj Wed Feb 17 20:11:16 2021 +0100 @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 95002580256D17D9008FFD28 /* StoreKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9500257F256D17D9008FFD28 /* StoreKit.framework */; }; + 950C0BB625DD9867006A49BC /* WatchlistManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950C0BB525DD9867006A49BC /* WatchlistManager.swift */; }; 951F589125DD86F200E051E0 /* TestAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 951F589025DD86F200E051E0 /* TestAPI.swift */; }; 951F589A25DD8A8100E051E0 /* Row.swift in Sources */ = {isa = PBXBuildFile; fileRef = 951F589925DD8A8100E051E0 /* Row.swift */; }; 951F589D25DD8A9C00E051E0 /* Watchlist.swift in Sources */ = {isa = PBXBuildFile; fileRef = 951F589C25DD8A9C00E051E0 /* Watchlist.swift */; }; @@ -22,6 +23,7 @@ /* Begin PBXFileReference section */ 95002578256D1564008FFD28 /* Configuration.storekit */ = {isa = PBXFileReference; lastKnownFileType = text; path = Configuration.storekit; sourceTree = ""; }; 9500257F256D17D9008FFD28 /* StoreKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = StoreKit.framework; path = System/Library/Frameworks/StoreKit.framework; sourceTree = SDKROOT; }; + 950C0BB525DD9867006A49BC /* WatchlistManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = WatchlistManager.swift; path = lazybear/Classes/WatchlistManager.swift; sourceTree = SOURCE_ROOT; }; 951F589025DD86F200E051E0 /* TestAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = TestAPI.swift; path = lazybear/Tests/TestAPI.swift; sourceTree = SOURCE_ROOT; }; 951F589925DD8A8100E051E0 /* Row.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Row.swift; path = lazybear/UI/Row.swift; sourceTree = SOURCE_ROOT; }; 951F589C25DD8A9C00E051E0 /* Watchlist.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Watchlist.swift; path = lazybear/UI/Watchlist.swift; sourceTree = SOURCE_ROOT; }; @@ -60,6 +62,14 @@ name = Frameworks; sourceTree = ""; }; + 950C0BB425DD9852006A49BC /* Classes */ = { + isa = PBXGroup; + children = ( + 950C0BB525DD9867006A49BC /* WatchlistManager.swift */, + ); + path = Classes; + sourceTree = ""; + }; 951F588F25DD86E600E051E0 /* Tests */ = { isa = PBXGroup; children = ( @@ -108,6 +118,7 @@ 95B04EB225212369000AD27F /* LazyBearApp.swift */, 95B04EB425212369000AD27F /* ContentView.swift */, 951F589825DD8A1B00E051E0 /* UI */, + 950C0BB425DD9852006A49BC /* Classes */, 951F588F25DD86E600E051E0 /* Tests */, ); path = LazyBear; @@ -185,6 +196,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 950C0BB625DD9867006A49BC /* WatchlistManager.swift in Sources */, 95B04EB525212369000AD27F /* ContentView.swift in Sources */, 95B04EB325212369000AD27F /* LazyBearApp.swift in Sources */, 951F589125DD86F200E051E0 /* TestAPI.swift in Sources */, diff -r 26d69711ea66 -r 6488392684cc LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed diff -r 26d69711ea66 -r 6488392684cc lazybear/Classes/WatchlistManager.swift --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lazybear/Classes/WatchlistManager.swift Wed Feb 17 20:11:16 2021 +0100 @@ -0,0 +1,29 @@ +// +// WatchlistManager.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 17/2/21. +// + +import SwiftUI +/* +class WatchlistManager { + @Environment(\.managedObjectContext) private var viewContext + @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \WatchlistCompanies.name, ascending: true)]) + var companies: FetchedResults + + // Add to watchlist + func addWatchlist(name: String, symbol: String) { + let watchlistCompanies = WatchlistCompanies(context: viewContext) + watchlistCompanies.name = name + watchlistCompanies.symbol = symbol + do { + try viewContext.save() + print("Company saved.") + } catch { + print(error.localizedDescription) + } + } + +} +*/ diff -r 26d69711ea66 -r 6488392684cc lazybear/LazyBear.entitlements --- a/lazybear/LazyBear.entitlements Wed Feb 17 19:18:53 2021 +0100 +++ b/lazybear/LazyBear.entitlements Wed Feb 17 20:11:16 2021 +0100 @@ -2,8 +2,6 @@ - aps-environment - development com.apple.developer.icloud-container-identifiers iCloud.dennis.LazyBear diff -r 26d69711ea66 -r 6488392684cc lazybear/LazyBearApp.swift --- a/lazybear/LazyBearApp.swift Wed Feb 17 19:18:53 2021 +0100 +++ b/lazybear/LazyBearApp.swift Wed Feb 17 20:11:16 2021 +0100 @@ -9,10 +9,12 @@ @main struct LazyBearApp: App { + let persistenceController = PersistenceController.shared var body: some Scene { WindowGroup { - TestAPI() + ContentView() + .environment(\.managedObjectContext, persistenceController.container.viewContext) } } } diff -r 26d69711ea66 -r 6488392684cc lazybear/UI/Row.swift --- a/lazybear/UI/Row.swift Wed Feb 17 19:18:53 2021 +0100 +++ b/lazybear/UI/Row.swift Wed Feb 17 20:11:16 2021 +0100 @@ -8,11 +8,11 @@ import SwiftUI struct Row: View { - var baseText: String - var underText: String? + @State var baseText: String + @State var underText: String? - var leftView: AnyView? - var rightView: AnyView? + @State var leftView: AnyView? + @State var rightView: AnyView? var body: some View { HStack { diff -r 26d69711ea66 -r 6488392684cc lazybear/UI/Watchlist.swift --- a/lazybear/UI/Watchlist.swift Wed Feb 17 19:18:53 2021 +0100 +++ b/lazybear/UI/Watchlist.swift Wed Feb 17 20:11:16 2021 +0100 @@ -8,8 +8,35 @@ import SwiftUI struct Watchlist: View { + @Environment(\.managedObjectContext) private var viewContext + @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \WatchlistCompanies.name, ascending: true)]) + var companies: FetchedResults + var body: some View { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + //let watchlistManager = WatchlistManager() + + + List(companies) { company in + let name = company.name ?? "Preview name" + let symbol = company.symbol ?? "Preview symbol" + Row(baseText: name, underText: symbol) + } + .onAppear { + //watchlistManager.addWatchlist(name: "Test", symbol: "Test") + } + } + + // Add to watchlist + private func addWatchlist(name: String, symbol: String) { + let watchlistCompanies = WatchlistCompanies(context: viewContext) + watchlistCompanies.name = name + watchlistCompanies.symbol = symbol + do { + try viewContext.save() + print("Company saved.") + } catch { + print(error.localizedDescription) + } } }